week 5 - L2 - Threads Flashcards

1
Q
  • what is a process?
  • what is address space?
  • what can the kernel do to multiple processes?
A

1 - What is a process?
- A program that its running with its own address space(unit of isolation)

2 - what is address space?
o Basically where instructions and data for the process are stored

  • Kernel scheduler can execute many processes simultaneously
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

thread

  • what is a thread?
  • what does each thread have?
  • how can a thread be scheduled?
  • what does each process have? what space?
  • what is a process, knowing about threads?
  • what can the process use threads for?
A

What is a thread?
- An independent stream of instructions (flow of work) in a process which shares resources in that process

  • Each thread has its own stack and CPU context and so can be scheduled independently by the kernel (light weight process)
  • Consequently, a process is an address space with one or more threads executing within that address space, and the required system resources for those threads
  • A process can offload time consuming tasks (like complex calculations) to background threads keeping the main user interface responsive
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what are the 2 concepts embodied by a process?

  • what are they called?
  • what does the OS prevent the processes from doing with each other? what’s the technique called
  • what are each of them?
A

A process embodies two independent concepts

  1. Resource ownership
  2. Execution & scheduling

Resource ownership
- A process is allocated address space to hold the image and is granted control of the I/O devices and file

  • Also the O/S prevents interference of the processes while they make use of resources (multiplexing)

Execution and scheduling

  • A process follows an execution path through a program
  • It has an execution state and is scheduled for dispatching
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Multithreading

  • what is it?
  • what does it require?
  • what does each thread receive?
  • what does the process keep? and what does it list?
A
  • Refers to the ability of the OS to support multiple threads of execution in a SINGLE PROCESS

Multithreading requires changes in the process

  • each thread receives each own control block and stack (TCB 1, TCB 2) thread control block
    o Own execution state (running, blocked etc)
    o Own copy of CPU registers
    o Own execution history(stack)
  • The process keeps a global control block listing resources currently used
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Threads - Separation of resource ownership and execution

  • how are the resources and execution separated?
  • what does each thread have?
  • numeric identifier for what?
A

Threads - Separation of resource ownership and execution

We have Per-process items and Per-thread items In the control block structures

-	Process identification data + thread identifiers
o	Each thread has an identifier 
o	Numeric identifiers of :
	the process 
	the parent process
	the user, etc
  • CPU state information
    o User-visible, control & status registers
    o Stack pointers
  • Process control information
    o Scheduling: state, priority, awaited event
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Multithreading

  • what is it similar to?
  • where is it?
  • picture in your mind how it looks.
  • what illusion is achieved through multithreading?
  • how many CPUs are in a single-processor system, and what does it do?
A

The same multitasking idea applies in multithreading

  • Multithreading is the same as multitasking at a finer level of temporal resolution (and within the same address space)
  • The same illusion of parallelism is achieved at a fine grain
  • In a single-processor system, there is still only one CPU going through all the threads of all the processes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Benefits of multithreading compared to multitasking

  • benefits in terms of time?
  • how do threads communicate differently from processes?
  • what are threads called sometimes?
  • which one is more efficient? which one do we use if we have a set of related executions?
A

1
Benefits of multithreading compared to multitasking
- It takes less time to create a thread than a process
- It also takes less time to terminate a thread than a process
- It takes less time to switch between two threads than between two processes

2
- Threads within the same process share memory and files and therefore they can communicate with each other without having to involve the kernel

3
- For this reason threads are sometimes called lightweight processes

4
- If an application should be implemented as a set of related executions, it is way more efficient to use threads than processes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how can threads be implemented? (at what levels)

A

Implementation of threads
Threads are implemented in two broad categories
- User-Level Threads (ULTs)
- Kernel-Level Threads(KLTs)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

practical use of multithreading

  • examples of real-world multithread applications?
  • details about each of them
A

Threads – Practical use of multithreading

Examples of real-world multithread applications
- Web client (browser)
o Must download the page content simultaneously(image, styles, etc)
o Cannot wait for each image in series

  • Webserver
    o Must serve pages to hundreds of web clients simultaneously, can not serve them one by one
  • Word processor, spreadsheet
    o Provides uninterrupted GUI service to the user while reformatting and saving the document in the background (basically you write something and it doesn’t pause you from using it to save it)
  • Same principle as time-sharing (the illusion of interactivity while performing other tasks), this time inside the same process (because we work with threads)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

multithreading - web server

- what is spawned by who

A

Web server
- As each new request comes in a “dispatcher thread” spawns a new “worker thread” to read the requested file
o Worker threads may be discharged or recycled in a “thread pool“

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

multithreading - word processor

A

Word processor

  • One thread listens continuously to mouse and keyboard events
  • Second thread reformats the document( to prepare page 600 for example)
  • A third thread writes to disk periodically
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

thread model

- what are the different types of thread models?

A

Thread model
We have different types of thread models, that use threads differently
- Pipeline model – threads run one after the other
- Master-slave model – master (main) thread doesn’t do any work, it just waits for the slave threads to finish working
- Equal worker model – all threads work the same way

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Thread synchronization (mutex)

  • MUTual Exclusive
  • it’s like a lock
  • what is a critical section?
  • what happens if more than 1 threads tries to execute it at a time?
  • what is a MUTex and what does it do?
  • what does the MUTex protect the data from?
  • what mechanism does it have?
  • what do threads need to do?
  • how many mutex can you use per critical section?
  • what possible states does a mutex have?
  • what happens to a thread when it attempts to lock a mutex that is already locked by another thread?
  • what can happen when 2 resources try to access the same lock?
A

Thread synchronisation (mutex)

A critical section is a section of code that can only be executed by one thread at a time

  • If two or more threads were to execute this thread at the same time, incorrect behaviour might result

A mutex (MUTual Exclusion) is a thread synchronization mechanism for protecting a critical code section shared by many threads

  • A mutex is used to guard against multiple threads modifying the same shared data simultaneously
  • It provides a locking/unlocking mechanism for critical code sections where shared data is modified
  • Each thread waits for the mutex to be unlocked by the thread who locked it, before performing the code section
  • You can use two or more mutex locks with one lock per critical section
Pthread_mutex_lock
-	A mutex has two possible states:
o	Unlocked – not owned by any thread
o	Locked – owned by one thread
-	A mutex can never be owned by two different threads simultaneously 
  • When a thread attempts to lock a mutex that is already locked by another thread is suspended until the owning thread unlocks the mutex first
  • Care has to be taken when using locking mutexes that two threads do not hold each others lock as this can lead to a deadlock
    o For example one thread need the resource of another thread that locks part of resource, and both thread lock
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Semaphores

  • what is it?
  • what can it be thought as?
  • what does it restrict access to?
  • what are the 2 types of semaphores?
A

A semaphore can be thought as a protected integer variable
that can restrict access to a shared resource in a multi-threading (or processing) environment

There are two types of semaphores

binary semaphore
counting semaphore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

binary semaphore

  • what states does it have?
  • what happens during each of them?
A
Binary semaphore (it’s similar to mutexes)
-	Has two states 1 and 0
  • Value is 1 – the critical code will be executed
  • Value is 0 – the thread will go into a wait state and wait until the value changes to 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

counting semaphore

  • what does it use?
  • how does it work?
  • what can be used to keep track of?
  • a semaphore contains a value that supports two operations. What are they?
  • explain each of them
  • what does the code need to include for it to work?
A

Counting semaphore
- Permits a limited number of threads to execute a section of the code by using a counter

  • When the count value reaches 0, no further access to critical code is possible (wait state) until the counter value increases to 1 again
  • A counting semaphore can be used to keep track of the number of threads using a data structure such as a stack
  • A semaphore contains a value that supports two operations:
    o Post
    o Wait
  • Post – increments the semaphore and immediately returns
  • Wait – will wait if the count is 0
  • If the count is non-zero the semaphore decrements the count and immediately returns
  • Code needs to include the semaphore header file
17
Q
  • what’s the difference in the overhead of semaphore compared to mutex?
  • a mutex can be thought as a semaphore that …
A
  • The overhead of using a semaphore is greater than a mutex

- A mutex can be thought of as a semaphore that ALWAYS waits before it posts

18
Q

possix threads
- threads of processes, which require less overhead?
- why?
-

A
  • POSSIX threads usually referred to as threads are defined by IEEE standard POSIX standard
  • Threads require less overhead than a process

o This is because the system doesn’t need to initialise a new system virtual memory space and environment for the process

  • A Mutex is basically a mutually exclusive flag which acts as a gate keeper to a section of code allowing only one thread at a time to run the code
    o The thread must release the mutex when it’s done
  • Semaphore is a variable used to control access to a common resource by a fixed number of simultaneous callers i.e. binary or n-binary access to a resource