week 5 - L2 - Threads Flashcards
- what is a process?
- what is address space?
- what can the kernel do to multiple processes?
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
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?
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
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 process embodies two independent concepts
- Resource ownership
- 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
Multithreading
- what is it?
- what does it require?
- what does each thread receive?
- what does the process keep? and what does it list?
- 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
Threads - Separation of resource ownership and execution
- how are the resources and execution separated?
- what does each thread have?
- numeric identifier for what?
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
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?
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
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?
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 can threads be implemented? (at what levels)
Implementation of threads
Threads are implemented in two broad categories
- User-Level Threads (ULTs)
- Kernel-Level Threads(KLTs)
practical use of multithreading
- examples of real-world multithread applications?
- details about each of them
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)
multithreading - web server
- what is spawned by who
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“
multithreading - word processor
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
thread model
- what are the different types of thread models?
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
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?
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
Semaphores
- what is it?
- what can it be thought as?
- what does it restrict access to?
- what are the 2 types of semaphores?
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
binary semaphore
- what states does it have?
- what happens during each of them?
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