An in-depth exploration of threads, their implementation, usage, and synchronization in advanced operating systems. Context switching, thread sharing of resources, thread preemption, and multithreaded servers. Additionally, it discusses thread implementation in windows, thread scheduling, and processor allocation strategies. Essential for students studying advanced operating systems, particularly those focusing on threads and concurrency.
Download Advanced Operating Systems: Lecture 5 - Threads and Processor Allocation and more Slides Advanced Operating Systems in PDF only on Docsity!
CS 620 Advanced Operating
Systems
Lecture 5 – Processes
Threads
• Motivation
Keep programs interactive.
Programs may block waiting for I/O.
This is a bad thing if the program is interactive.
Background save.
Consider saving a large file to a slow disk system.
The user has to wait for the save operation to finish before proceeding.
Multiple clients.
Consider also a server program with multiple clients.
We must fork off a new process for each client.
Thread Usage in Nondistributed Systems
Context switching as the result of IPC
Threads
Threads also share:
open files
child processes
timers
etc.
– Synchronization primitives available.
Necessary since threads share a common memory.
Semaphores, mutexes, etc.
Threads
• Is it managed from user or kernel level?
– User threads have a very cheap task context
switch
– Kernel threads handle blocking I/O cleanly
– In order for user threads not block, we need
extended models for I/O
E.g. select() indicates which files are ready to
transfer data so we don’t block
– Hybrid is also possible
Thread Implementation
Combining kernel-level lightweight processes and user-level
threads.
Threads
• So how do you use threads?
– User interface/computation/I/O handled
separately (think of a browser)
– Pop-up server threads
– On multiprocessor systems, we can have threads
working in parallel on the multiple processors as
an alternative to shared memory IPC
Multithreaded Servers (1)
A multithreaded server organized in a dispatcher/worker
model.
Threads in Windows
– The Windows Kernel schedules threads and
handles interrupts and exceptions.
The Kernel schedules or dispatches threads for the
processor in order of priority.
It also preempts threads of lower priority in favor of
threads of higher priority.
It can force context switches , directing the processor to
drop one task ands pick up another.
Therefore code operating in this system must be
reentrant. (Able to be interrupted and resumed
unharmed and shared by different threads executing
the code on different processors.)
Threads in Windows
The Kernel’s own code does not, technically, run in
threads.
Hence it is the only part of the OS that is not preemptible or pageable.
The rest of the threads in Windows are preemptible and fully reentrant.
Code which is non-reentrant can cause serialization, damaging the performance of the OS on SMP machines.
The Kernel schedules ready threads for processor time
based upon their dynamic priority, a number from 1 to 31.
Threads in Windows
When a user interacts with a process (the process window is at the top of the window stack), Windows boosts the priority of the process to maximize its response.
The base priority of a thread is a function of the base priority of the process in which it runs. It varies within +/- 2 from the base priority of the process.
The dynamic priority of a thread is a function of its base priority. Windows continually adjusts the dynamic priority of threads within the range established by its base priority.
The base priority class of a running process can be changed by using Task Manager.
Threads in Windows
The Windows Kernel takes maximum advantage of
multiprocessor configurations by implementing symmetric
multiprocessing (SMP) and soft affinity.
SMP allows the threads of any process, including the OS, to run on any processor.
The threads of a single process can run on different processors at the same time.
With soft affinity, the Kernel attempts to run the thread on the last processor it ran on.
Applications can restrict threads to run only on certain processors ( hard affinity ).
Threads in Windows
A thread may wait for the I/O to complete or we may
use an application procedure call (APC) that the I/O
manager calls when the I/O completes or we may use a
synchronization object (e.g. an event) that the I/O
system sets to the signaled state when I/O completes.