Chapter 2 (Two)
Chapter 2 (Two)
thread
Chapter Two
New Terminate
Interrupt
Admitted
Exit
Ready Running
Scheduler
Dispatch
I/O or event Input output
completion (Event Wait)
Blocked
Compiled by Abel H. belamine320@gmail.com 6
Two State Process Model
Twostate process model refers to running and non-running states
which are described below.
1.Running: When new process is created by Operating System that
process enters into the system as in the running state.
2.Non-Running Processes that are not running are kept in
queue, waiting for their turn to execute. Each entry in the
queue is a pointer to a particular process. Queue is
implemented by using linked list. Use of dispatcher is as follows.
When a process is interrupted, that process is transferred in the
waiting queue. If the process has completed or aborted, the
process is discarded. In either case, the dispatcher then
selects a process from the queue to execute.
Compiled by Abel H. belamine320@gmail.com 7
Process hierarchy
✓ In some systems, when a process creates another process, the parent
process and child process continue to be associated in certain ways.
✓ The child process can it self create more process. Forming process
hierarchy.
✓ Note that unlike plants and animals that use sexual reproduction, a
process has only one parent (but zero, one, two, or more children).
✓ In contrast, windows does not have any concept of a process hierarchy,
All process are equal. The only place where there is something like a
process hierarchy is that when a process is created the parent is given a
special token called handle, that it can use to handle the child. How
ever its free to pass this token to some other process, thus invalidating
the hierarchy.
Compiled by Abel H. belamine320@gmail.com 8
1 Process is heavy weight or resource Thread is light weight taking lesser resources
intensive. than a process
2 Process switching needs interaction with Thread switching does not need to interact
operating system. with operating system.
3 In multiple processing environments each All threads can share same set of open files,
process executes the same code but has child processes.
its own memory and file resources.
4 If one process is blocked then no other While one thread is blocked and waiting,
process can execute until the first second thread in the same task can run
process is unblocked.
5 Multiple processes without using threads Multiple threaded processes use fewer
use more resources. resources.
6 In multiple processes each process One thread can read, write or change another
Compiled by Abel H. belamine320@gmail.com 11
operates independently of the others thread's data.
Concurrency
Imagine a web server, which might like to handle multiple
requests concurrently
While waiting for the credit card server to approve a purchase for one
client, it could be retrieving the data requested by another client from
disk, and assembling the response for a third client from cached
information
Imagine a web browser, which might like to initiate multiple
requests concurrently
While browser displays images or text, it retrieves data from the
network.
A word processor
For example, displaying graphics, responding to keystrokes from the
user, and performing spelling and grammar checking in the
background.
What’s needed?
In each of these examples of concurrency (web server, web
browser, word processor):
Everybody wants to run the same code
Everybody wants to access the same data
Everybody has the same privileges (most of the time)
Everybody uses the same resources (open files, network
connections, etc.)
But you’d like to have multiple hardware execution states:
an execution stack and stack pointer (SP)
traces state of procedure calls made
the program counter (PC), indicating the next instruction
a set of general-purpose processor registers and their values
How could we achieve this?
main() {
CreateThread(ComputePI(“pi.txt”));
CreateThread(PrintClassList(“clist.text”));
}
What does “CreateThread” do?
Start independent thread running for a given procedure
A thread
is a basic unit of CPU utilization; it comprises a thread ID, PC, a
register set, and a stack.
Shares with other threads belonging to the same process its code
and data sections, and other OS resources (ex: open files and
signals)
Threads of the same process are not protected from each other.
Single and Multithreaded Processes
Process address space
thread 1 stack
SP (T1)
Economy
• Creating and destroying threads is cheaper than processes.
• Context switching between threads is also cheaper.
• It’s much easier to communicate between threads.
Benefits of multithreaded
Scalability
• Multithreading can be greatly increased in a multiprocessor systems
• Threads may be running in parallel on different processors.
Multicore Programming
On a single-core system, concurrency means that the execution of
threads will be interleaved over time – executing only one thread at a
time.
Kernel threads:
Most OS kernels are multi-threaded.
Several threads operate in the kernel, each performing a specific task.
Ex: managing devices, interrupt handling.
Supported and managed directly by the Kernel.
Examples: Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X.
User-level threads are faster to create and manage than are kernel threads.
Why?
Because no intervention from the kernel is required.
Multithreading Models
▪ Many-to-One
▪ One-to-One
▪ Many-to-Many
Many-to-One
Many user-level threads mapped to single kernel thread:
Thread management is done by the thread library in user space →
efficient.
The entire process will block if a thread makes a blocking system call.
Because only one thread can access the kernel at a time, multiple
threads are unable to run in parallel on multiprocessors.
Examples:
Solaris Green Threads
GNU Portable Threads
One-to-One
Each user-level thread maps to kernel thread
Adv : allows another thread to run when a thread makes a blocking system
call → more concurrency.
Adv : allows multiple threads to run in parallel on multiprocessors.
Dis : creating a user thread requires creating corresponding kernel thread →
can burden the applications performance.
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
Many-to-Many Model
Allows many user level threads to be mapped to many kernel threads
Does not suffer from the shortcomings of the previous two models. How? read
P159
Solaris prior to version 9
Two-level Model
Similar to M:M, except that it allows a user thread to be bound to kernel
thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
Thread Libraries
Thread library provides programmer with API for creating and managing threads
Process schedulers
✓ thescheduler is the concept of an operating system that
determines which process should be run and when.
✓ CPU scheduling is the process of selecting a process and
allocating the processor to the selected process for
execution.
✓ There are 3 distinct types of schedulers: a long-term
scheduler (also know as an admission scheduler or high-level
scheduler), a mid-term or medium-term scheduler and a
short-term scheduler (also known as a dispatcher).
Compiled by Abel H. belamine320@gmail.com 32
Characteristics of perfect CPU
scheduler
bound.
Types of schedulers
2. Short Term Scheduler (CPU scheduler)
✓ Main objective is increasing system performance in accordance with
the chosen set of criteria.
✓ It is the change of ready state to running state of the
process.
✓ CPU scheduler selects process among the processes that are
ready to execute and allocates CPU to one of them.
✓ Short term scheduler also known as dispatcher, execute most
frequently and makes the fine grained decision of which process
Compiled by Abel H. belamine320@gmail.com 35
to execute next
Types of schedulers
3. Medium term scheduler
✓ Medium term scheduling is part of the swapping.
✓ It removes the processes from the memory.
✓ The medium term scheduler is in-charge of handling the
swapped out-processes.
✓ Suspended processes cannot make any progress towards
completion. In this condition, to remove the process from
memory and make space for other process, the suspended process
is moved to the secondary storage. This process is called
Compiled by Abel H. belamine320@gmail.com 36
swapping,
Comparison between Schedulers
S.N Long Term scheduler Short term scheduler Medium term scheduler
2 Speed is lesser than short Speed is fastest among Speed is in between both
term scheduler other two short and long term scheduler.
5 It selects processes from It selects those processes It can re-introduce the process
pool
Compiled by Abeland
H. loads them into
belamine320@gmail.com which are ready to into memory
37 and execution
memory for execution execute can be continued.
Process Scheduling Algorithms
Many algorithms are provided there to schedule the CPU. We should
have some criteria to select an algorithm, which suits the
circumstances. Some of the major criteria are
1. CPU Utilization = keep the CPU as busy as possible.
2. Throughput = number of processes that complete their execution per
time unit.
3. Turnaround time = amount of time to execute a particular process.
4. Waiting time = amount of time a process has been waiting in the
ready queue.
5. Response time = amount of time it takes, from when a request was
submitted until the first response is produced, not output.
Compiled by Abel H. belamine320@gmail.com 38
Process Scheduling Algorithms
Scheduling is the method by which a thread, process or data flows
are given access to system resources. Scheduling can be divided
into the following categories.
1.Non –Preemptive: a non preemptive scheduling algorithm picks
a process to run and then just lets it run until it blocks (either on
I/O or waiting for another process) or until it voluntarily releases
the CPU. Even if it runs for hours it will not be forcefully
suspended.
2. Preemptive: this picks a process and lets it run for maximum of
some fixed time. If it is still running at the end of the time interval,
it is suspended and the scheduler pics another process (if one is
Compiled by Abel H. belamine320@gmail.com 39
available).
Various scheduling algorithms
➢ First Come First Serve (FCFS) Scheduling
➢ Shortest-Job-First (SJF) Scheduling
➢ Round Robin(RR) Scheduling
➢ Priority Scheduling
➢ Multilevel Queue Scheduling
➢ Realtime scheduling
➢ Dynamic Scheduling algorithm (RMA, EDF,…)
➢ …..
Compiled by Abel H. belamine320@gmail.com 40
Some acronyms
➢ C.T= Completion Time = the time a process completes executing.
➢ A.T=> Arriving Time = the time a process requests for a CPU.
➢ T.A.T=> Turnaround Time= total time, from requesting CPU until
it finishes executing.
➢ W.T=> Waiting Time= amount of time a process has been waiting
in the ready queue.
➢ B.T=> Burst Time= The Execution time of a process.
➢ R.T=> Response Time= the first response time for the process.
➢ A.W.T=> Average Waiting Time
Compiled by Abel H. belamine320@gmail.com 41
system.
Cont.
Failed System
A failed system is a system that cannot satisfy one or more of the
requirements stipulated in the system requirements specification.
Means something catastrophic occurs if the deadline is not met.
Types of Realtime system
Consider a system consisting of a set of tasks, T = {τ1, τ2, ..., τn},
where the finishing time of each task τi∈T is Fi. The system is said to
be real-time if there exists at least one task τi∈T , which falls into
one of the following categories