Chapter 2 - Process and Process Management
Chapter 2 - Process and Process Management
Operating Systems
2
Chapter Objectives
To introduce the notion of a process- a program in
execution, which forms the basis of all computation.
To describe the various features of processes,
including scheduling, creation and termination, and
communication.
To introduce the notion of a thread- a fundamental
unit of CPU utilization that forms the basis of
multithreaded computer systems.
3
Topics Included
The Process Concept
The Threads Concept
Inter-process Communication
Process Scheduling
4
The Process Concept
5
Objectives
After completing this topic you should be able to:
Define process
Differentiate program and process
Explain different types of processes
Explain different states of process
Understand how OS manages processes
6
1 Process Concept
Process vs. Program
Program
It is sequence of instructions defined to perform some task
It is a passive entity, a file containing a list of instructions stored on
disk(often called an executable file).
Process
It is a program in execution. A process needs certain resources,
including CPU time, memory, files, and I/O devices, to accomplish
its task.
It is an instance of a program running on a computer.
It is an active entity, with a program counter specifying the next
instruction to execute and a set of associated resources.
A processor performs the actions defined by a process.
A program becomes a process when an executable file is loaded into
memory.
A process is more than the program code.. I
7
Process concept(cont’d)
A process includes:
program counter: contains the address of the next instruction to
be fetched
Stack: which contains temporary data (such as function
parameters, return addresses, and local variables).
data section: which contains global variables.
Real life Example: Consider a computer scientist who is baking
a birthday cake for her daughter and who is interrupted by her
daughter’s bleeding accident
Analysis
Processes Baking Cake First Aid
Processor Computer Scientist Computer Scientist
Program Recipe First Aid Book
Input Ingredients First Aid Kit
Output Cake First Aid Service
Priority Lower Higher
States Running, Idle Running, Idle 8
Process concept(cont’d)
Sequence of actions
Bringing ingredients i.e. flour, sugar, eggs, etc
Placing the mixture into the oven
Following the baking processes
Hearing a cry and analyzing it to be because of bleeding
Recording baking processes state and switching to provide
first aid service
Providing first aid service
Coming back and resuming the baking process
9
2. Types of Processes
There are two types of processes
Sequential Processes
Concurrent Processes
Sequential Processes
Execution progresses in a sequential fashion.
At any point in time, at most one process is being executed
10
Types of Processes(cont’d)
Apparent Concurrency (Multiprogramming)
Two or more processes are executed in parallel in a uniprocessor
environment by switching from one process to another
Supports pseudo parallelism, i.e. the fast switching among
processes gives illusion of parallelism
11
3. Process Creation
Principal events that cause process creation:
System initialization.
When an operating system is booted, often several processes are
created. Some of them are foreground and others are background.
Foreground processes: processes that interact with (human) users
and perform work for them.
Background processes, which are not associated with particular
users, but instead have some specific function. For example, a
background process may be designed to accept incoming requests
for web pages hosted on that machine.
Execution of a process creation system call by a running
process.
a running process will issue system calls to create one or more
new processes to help it do its job.
A user request to create a new process. E.g. open a document
Initiation of a batch job(on large mainframes).
12
4. Process State
During its lifetime, a process passes through a number of states.
The most important states are: Ready, Running, Blocked
(waiting)
New
A process that has just been created but has not yet been
admitted to the pool of executable processes by the operating
system
Information concerning the process is already maintained in
memory but the code is not loaded and no space has been
allocated for the process
Ready
A process that is not currently executing but that is ready to be
executed as soon as the operating system dispatches it
13
Process State(cont’d)
Running
A process that is currently being executed
Blocked (Waiting)
A process that is waiting for the completion of some event,
such as completion of an I/O operation
Exit (Terminated)
A process that has been released from the pool of executable
processes by the operating system, either because it halted or
because it aborted for some reason.
Conditions:
Normal exit: processes terminate because they have done their
work
14
Process State(cont’d)
Error exit: due to a program bug. Examples include executing an
illegal instruction, referencing nonexistent memory, or dividing by
zero.
Fatal error: For example, if a user types the command cc foo.c
to compile the program foo.c and no such file exists, the compiler
simply exits.
Killed by another process
15
4. Process Description
We can think of the OS as that entity that manages the use of
system resources by processes.
16
Process Description(cont’d)
What information does the OS need to control
processes and manage resources for them?
Control Tables
If the operating system is to manage processes and
resources, it must have information about the current status
of each process and resources
The operating system constructs and maintains tables of
information, called control tables.
There are four major control tables
Memory tables
I/O tables
File tables
Process tables
17
Process Description(cont’d)
Memory tables
Are used to keep track of both main and secondary (virtual)
memory
Some of main memory is reserved for use by the OS; the
remainder is available for use by processes.
The memory tables must include the following information:
Allocation of main memory to processes
Allocation of secondary memory to processes
Any information needed to manage virtual memory
I/O tables
Used by the OS to manage the I/O devices.
If an I/O operation is in progress, the OS needs to know the
status of the I/O operation and the location in main memory
being used as the source or destination of the I/O transfer.
18
Process Description(cont’d)
File tables
These tables provide information about the existence of files,
their location on secondary memory, their current status, and
other attributes.
Process tables
Are used to manage processes
OS must know the location of each process and the attributes
of each process(process ID and process state)
21
Process Control Block(cont’d)
Process state. The state may be new, ready running,
waiting, halted, and so on.
Program counter
The counter indicates the address of the next instruction to be
executed for this process.
CPU registers
The registers vary in number and type, depending on the
computer architecture. They include accumulators, index
registers, stack pointers, and general-purpose registers, plus
any condition-code information. Along with the program
counter, this state information must be saved when an
interrupt occurs, to allow the process to be continued correctly
afterward
22
Process Control Block(cont’d)
CPU-scheduling information
This information includes a process priority, pointers to
scheduling queues, and any other scheduling parameters.
Memory-management information
This information may include such information as the value of
the base and limit registers, the page tables, or the segment
tables, depending on the memory system used by the
operating system.
Accounting information
This information includes the amount of CPU and real time
used, time limits, account numbers, job or process numbers,
and so on.
24
Process Switching…
25
The Thread Concept
26
Objectives
After completing this topic you should be able to:
Define thread
Differentiate process and thread
Understand thread management
27
Processes and Threads
The discussion so far has presented the concept of a
process as embodying two characteristics:
A. Resource ownership: A process includes a virtual address
space to hold the process image; collection of program, data,
stack, and attributes defined in the process control block.
The OS performs a protection function to prevent unwanted
interference between processes with respect to resources.
B. Scheduling/execution: a process has an execution state
(Running, Ready, etc.) and a dispatching priority and is the
entity that is scheduled and dispatched by the OS.
These two characteristics are independent and could be
treated independently by the OS.
28
Processes and Threads(cont’d)
The unit of dispatching is usually referred to as a
thread or lightweight process, while the unit of
resource ownership is usually still referred to as a
process or task.
A process is a collection of one or more threads and
associated system resources.
Processes are used to group resources together;
threads are the entities scheduled for execution on
the CPU.
Traditional operating systems are single-threaded
systems
Modern operating systems are multithreaded systems
29
Multithreading
Multithreading is a technique in which a process, executing an
application, is divided into threads that can run concurrently.
Handling several independent tasks of an application
Having great control over the modularity of the application and the
timing of application related events.
Multithreading can make your program more responsive and
interactive, as well as enhance performance.
For example,
A good word processor lets you print or save a file while you are typing.
When downloading a large file, we can put multiple threads to work—one
to download the clip, and another to play it.
31
Shared and Private Items
A B
33
User-Level and Kernel-Level Threads
Threads can be managed in the kernel or user space
Kernel Space
The operating system is aware of the existence of multiple threads
When a thread blocks, the operating system chooses the next one
to run, either from the same process or a different one
The kernel has a thread table
Thread management is slow
User Space
The operating system is not aware of the existence of multiple
threads
The user space has a thread table
Thread management is much faster (specially switching)
When one thread blocks, the kernel blocks the entire process
34
User-Level and Kernel-Level Threads(cont’d)
Advantage of ULT
Allow each process to have its own customized scheduling
algorithm.
Can be implemented on an operating system that does not
support threads.
Disadvantages of ULTs
when a thread is blocked, all of the threads within that process
are blocked
cannot take advantage of multiprocessing
kernel assigns one process to only one processor at a time
35
User-Level and Kernel-Level Threads(cont’d)
36
Inter-process Communication
(IPC)
37
Objectives
After completing this topic you should be able to:
Define concurrency
Understand problems associated with concurrency.
Understand different techniques used for solving issues
related with concurrency.
38
Central theme of OS design
Multiprogramming
management of multiple processes within a uniprocessor
system.
Multiprocessing
management of multiple processes within a multiprocessor
system.
Distributed processing
management of multiple processes executing on multiple,
distributed computer systems.
40
Inter-process Communication
Concurrency provides major benefits in processing
efficiency and in program structuring.
There is frequent interaction among concurrently
running processes.
There are three issues here:
The first is how one process can pass information to another.
The second has to do with making sure two or more processes
do not get into each other's way when engaging in critical
activities (suppose two processes each try to grab the last 1
MB of memory).
The third concerns proper sequencing when dependencies are
present: if process A produces data and process B prints it, B
has to wait until A has produced some data before starting to
print.
41
Inter-process Communication(cont’d)
There are three ways in which concurrent processes
interact with each other:
Competition for resources
Cooperation by Sharing Resources
Cooperation by Communication
Cooperation by Communication
It occurs when various processes communicate with each
other, for instance with message passing in order to provide a
way to synchronize or coordinate their various activities.
Processes are aware of each other directly. 43
Concurrency Problems
Race Conditions
A situation that occurs when two or more processes are
reading or writing into some shared data and the final result
depends on who runs precisely when.
E.g. Printer Spooler: when a process wants to print a file, it
enters the file name in a special spooler directory. Assume
that the spooler directory has a large number of slots,
numbered 0,1,2,…
There are two globally shared variables
Outfile: points to the next file to be printed
44
Concurrency Problems(cont’d)
Assume that simultaneously process A and process B decide
they want to queue a file for printing
Process A reads infile and stores the value 7 in its local variable
(x=infile)
An interrupt occurs and the CPU decides that process A has run
long enough so it switches to process B
Process B reads infile and stores the value 7 in its local variable
(y=infile)
Process B stores the name of its file in slot 7 and adjusts infile to
be 8
Eventually process A runs again, it checks x and finds 7 there, and
writes its file name in slot 7, erasing the name process B just put
there, it updates infile to be 8
The printer will now print the file of process A, process B file will
never get any output
45
Concurrency Problems(cont’d)
46
Concurrency Problems(cont’d)
Deadlock
It is the permanent blocking of a set of processes compete for
system resources or communicate with each other.
It involves conflicting needs for resources by two or more
processes.
It refers to a situation in which a set of two or more processes
are waiting for other members of the set to complete an
operation in order to proceed, but none of the members is able
to proceed.
E.g. Traffic deadlock
It is a difficult phenomenon to anticipate and there are no easy
general solutions to this problem
47
Concurrency Problems(cont’d)
Example:
Consider two processes, P1 and P2, and two resources, R1
and R2.
Suppose that each process needs access to both resources to
perform part of its function.
Then it is possible to have the following situation: the OS
assigns R1 to P2, and R2 to P1.
Each process is waiting for one of the two resources.
Neither will release the resource that it already owns until it
has acquired the other resource and performed the function
requiring both resources.
The two processes are deadlocked.
48
Concurrency Problems(cont’d)
49
Concurrency Problems(cont’d)
Starvation
It referees to the situation in which a process is ready to
execute but is continuously denied access to a processor in
deference to other processes.
E.g. suppose that there are three processes P1, P2, and P3
and each require periodic access to resource R. If the
operating system grants the resource to P1 and P2 alternately,
P3 may indefinitely be denied access to the resource, thus
starvation may occur.
In large part, it is a scheduling issue
50
Mutual Execution
The key to preventing race condition is to enforce
mutual exclusion.
It is the ability to exclude (prohibit) all other processes
from using a shared variable or file while one process
is using it.
Part of a program where shared resource (critical
resource) is accessed is called critical region or
critical section.
51
Mutual Execution(cont’d)
52
Mutual Execution(cont’d)
53
Mutual Execution(cont’d)
Four conditions to provide mutual exclusion
No two processes simultaneously in critical region
No assumptions made about speeds or numbers of CPUs
No process running outside its critical region may block another
process
No process must wait forever to enter its critical region.
Implementing Mutual Execution with Busy Waiting
while one process is busy updating shared memory in its critical
region, no other process will enter its critical region and cause
trouble.
Types:
Disabling Interrupts
Lock Variables
Strict Alternation
Peterson’s Solution
TSL Instruction
54
Disabling Interrupts
An interrupt is an event that alters the sequence in
which a process executes instruction
In this technique, each process disables all interrupts
just after entering its critical section and re-enables
them just before leaving it.
With interrupts turned off the processor will not be
switched to another process
55
Disabling Interrupts(cont’d)
This approach is generally unattractive because it is
unwise to give user processes the power to turn off
interrupts.
For instance, a process can turn off interrupts but never turn
them on again in which the whole system freezes.
If the system is multiprocessor, disabling interrupts affects only
the processor that executed the disable instruction. The other
ones will continue running and can access the shared
memory.
59
Strict Alternation(cont’d)
While (TRUE) {
While(turn!=1) /*wait*/;
Critical_region();
turn=0;
Noncritical_region();
}
(b) Process 1
60
Strict Alternation(cont’d)
Continuously testing a variable waiting for some value
to appear is called busy waiting. This technique
wastes processor time
Disadvantages
Taking turns is not a good idea when one of the processes is
much slower than the other. This situation violates condition 3
of implementing mutual exclusions: process 0 is being blocked
by a process not in its critical region.
61
Peterson’s Solution
It combines the idea of taking turns with the idea of
lock variables.
It does not require strict alternation.
Initially, neither process is in its critical section.
Now process 0 calls enter_region.
It indicates its interest by setting its array element & sets turn
to 0.
Since process 1 is not interested, enter_region returns
immediately.
If process 1 now calls enter_region, it will hang there until
interested[0] goes to false, an event that only happens when
process 0 calls leave_region to exit the critical region.
62
Peterson’s Solution(cont’d)
Now consider the case that both processes call
enter_region almost simultaneously.
Both will store their process number in turn.
Whichever store is done last is the one that counts; the first one is
lost.
Suppose that process 1 stores last, so turn is 1.
When both processes come to the while statement, process 0
executes it zero times and enters its critical section. Process 1 loops
and does not enter its critical section.
63
Peterson’s Solution(cont’d)
Now consider the case that both processes call
enter_region almost simultaneously
Both will store their process number in turn.
Whichever store is done last is the one that counts; the first
one is lost.
Suppose that process 1 stores last, so turn is 1.
When both processes come to the while statement, process 0
executes it zero times and enters its critical section. Process 1
loops and does not enter its critical section.
64
Peterson’s Solution(cont’d)
66
TSL Instruction(cont’d)
To implement mutual exclusion with TSL instruction, we
use a shared variable, lock, to coordinate access to
shared memory. When lock is 0 the process may set it to 1
using TSL instruction and executes its critical section.
When it is done, the process sets lock into 0.
Before entering its critical region, a process calls
enter_region, which does busy waiting until the lock is
free; then it acquires the lock and returns. After the critical
region the process calls leave_region, which stores a 0 in
lock.
67
TSL Instruction(cont’d)
70
Producer-Consumer Problem
Two processes share a common fixed-size buffer.
One of, the producers, puts information in the buffer,
and the other one, the consumer, takes it out.
When the producer wants to put a new item in the
buffer, it checks the buffer, if it is full, it goes to sleep,
to be awakened when the consumer has removed
one or more items.
When the consumer wants to remove an item from
the buffer and sees that the buffer is empty, it goes to
sleep until the producer puts something in the buffer
and wakes it up.
Let us see the producer-consumer problem using C
programming. 71
Producer-Consumer Problem(cont’d)
73
Producer-Consumer Problem(cont’d)
When the consumer next runs, it will test the value of count it
previously read, find it to be 0, and go to sleep.
Sooner or later the producer will fill up the buffer and also go to
sleep. Both will sleep forever.
The problem arises because the wakeup signal is lost.
A quick fix is to add to the rules by adding wakeup-waiting bit.
When a wakeup is sent to a process that is still awake, this bit is
set.
Later, when the process tries to go to sleep, if the wakeup-waiting
bit is on, it will be turned off, but the process will stay awake.
The wakeup waiting bit cannot be a general solution,
especially for any random number of processes.
74
Semaphores
Semaphores solve the lost-wakeup problem
A semaphore is a new integer variable type that
counts the number of wakeups saved for future use.
A semaphore could have the value 0, indicating that
no wakeups were saved or some positive value if one
or more wakeups were pending.
Two operations were proposed to implement
semaphores: up and down (which are generalizations
of sleep and wakeup, respectively).
DOWN operation
It checks the value of the semaphore to see if the value is greater
than 0. If so it decrements the value and just continues. If the
value is 0, the process is put to sleep without completing the
DOWN operation for the moment. 75
Semaphores(cont’d)
Checking the value, changing it and going to sleep is all done
as a single, indivisible atomic operation. Once a semaphore
operation has started, no other process can access the
semaphore until the operation has completed or blocked.
This atomicity is absolutely essential to solving
synchronization problems and avoiding race conditions.
UP operation
It increments the value of the semaphore
The process of incrementing the semaphore and waking up one
process is also indivisible.
76
Semaphores(cont’d)
The solution uses three semaphores:
full, to count the number of full slots,
empty, to count the number of empty slots and
mutex, to make sure the producer and the consumer do not
access the buffer at the same time
77
Semaphores(cont’d)
79
Semaphores(cont’d)
Suppose that the two downs in the producer’s code were
interchanged or reversed in order and suppose also the buffer
was full and mutex is 1
down (&mutex);
down (&empty);
The producer does a down on mutex and mutex becomes 0
and then the producer does a down on empty.
The producer would block since the buffer is full.
Next time the consumer does a down on mutex and it blocks
since mutex is 0.
Therefore both processes would block forever and hence deadlock
would occur.
80
Monitors
A monitor is a higher level of synchronization primitive
proposed by Hoare and Branch Hansen to make
writing a correct program easier.
A monitor is a collection of procedures, variables and
data structures that are all grouped together in a
special kind of module or package.
Rules associated with monitors
Processes may call the procedures in a monitor whenever
they want to, but they can not directly access the monitor’s
internal data structures
Only one procedure can be active in a monitor at any instant.
Monitors are programming language construct, so the compiler
knows that they are special and can handle calls to a monitor
procedures differently from other procedure calls
81
Monitors(cont’d)
It is the compiler that implements mutual exclusion. The
person writing the monitor does not have to be aware of how
the compiler arranges for mutual exclusion. It is sufficient to
know that by turning all critical regions into monitor procedure,
no two processes will ever execute their critical regions at the
same time.
83
The producer-consumer problem using Monitor
84
Classical IPC Problems
Three better-known problems:
Dinning-philosopher problem
The Readers and Writers problem
The sleeping barber problem
Dinning-philosopher problem
Five philosophers are seated around a circular table.
Each philosopher has a plate of spaghetti.
Between each pair of plates is one fork.
The life of a philosopher consists of alternate periods of eating and
thinking.
85
Dinning-philosopher problem
86
Dinning-philosopher problem(cont’d)
The procedure: take_fork waits until the specified fork
is available and then seizes it.
Problem:
Suppose that all five philosophers take their left forks
simultaneously. None will be able to take their right forks, and
their will be deadlock.
87
Dinning-philosopher problem(cont’d)
88
Dinning-philosopher problem(cont’d)
Solution to dining philosophers problem
After taking the left fork, the program checks to see if the right
fork is available.
If it is not, the philosopher puts down the left one, waits for
some time, and then repeats the whole process.
Still some problem. If all the philosophers could start the
algorithm simultaneously
Picking up their right forks were not available, putting down their
left forks, waiting, picking up their left forks again simultaneously,
and so on, forever.
This situation is called starvation.
Just wait a random time instead of the same time. But, this
does not always works.
89
Dinning-philosopher problem(cont’d)
92
The Readers and Writers Problems(cont’d)
94
The Sleeping Barber Problem(cont’d)
97
Objectives
To introduce CPU scheduling, which is the basis for
multiprogrammed operating systems.
To describe various CPU-scheduling algorithms.
To discuss evaluation criteria for selecting a CPU-
scheduling algorithm for a particular system.
98
Scheduling
Scheduling refers to a set of policies and mechanisms to
control the order of work to be performed by a computer
system.
Of all the resources of a computer system that are
scheduled before use, the CPU/processor is the far most
important.
Processor Scheduling is the means by which operating
systems allocate processor time for processes.
It can be implemented on uniprocessor systems,
multiprocessor systems, and real-time systems.
Scheduler: part of CPU which choice a process to be
executed when two process are simultaneously in ready
state.
The algorithm it uses is called scheduling algorithm.
99
Scheduling(cont’d)
Process Behavior
100
Scheduling(cont’d)
The operating system makes three types of
scheduling decisions with respect to execution of
processes:
Long-term Scheduling
The decision to add to the pool of processes to be executed
It determines when new processes are admitted to the system
It is performed when a new process is created and admitted
New Ready
Blocked/Suspend Ready/Suspend
Medium-term Scheduling
The decision to add to the number of processes that are
partially/fully in main memory
It determines when a program is brought partially or fully into
main memory so that it may be executed 101
Scheduling(cont’d)
It is performed when swapping is done
Ready/Suspend Ready
Blocked/Suspend Blocked(Read the situation from William
Stalling 6th ed., PP119)
Short-term Scheduling
The decision of which ready process to execute next
It determines which ready process will get processor time next
It is performed when a ready process is allocated the
processor (when it is dispatched)
Ready Running
102
Scheduling(cont’d)
Scheduling criteria:
Efficiency/CPU Utilization: The percentage of time that the
CPU is busy should be maximized.
Throughput: The number of processes completed per unit
time should be maximized.
Fairness: Each process should get its fair share of the CPU.
No process should suffer starvation
105
Scheduling(cont’d)
Turn Around Time: The interval from the time of submission
of a process to the time of completion.
The sum of the periods spent waiting to get into memory,
waiting in the ready queue, executing on the CPU, and
doing I/0.
Waiting time: is the sum of the periods spent waiting in the
ready queue.
Response Time: is the time from the submission of a request
until the first response is produced.
For interactive users, the time from submission of request until
the response begins to be received should be minimized
It is desirable to maximize CPU utilization and
throughput and to minimize turnaround time, waiting
time, and response time.
106
Scheduling(cont’d)
There are two main characteristics of short-term
scheduling algorithms
Selection function
It determines which process, among ready processes, is selected
for execution.
It may be based on
Priority
Resource requirement
Execution behavior: time spent in system so far (waiting and executing),
time spent in execution so far, total service time required by the process
107
Scheduling(cont’d)
Decision mode
It specifies the instants in time at which the selection function is
exercised
108
Scheduling(cont’d)
Preemptive
The strategy of allowing processes that are logically runnable to
be temporarily suspended and be moved to the ready state.
Events that may result pre-emption are arrival of new processes,
occurrence of an interrupt that moves blocked process to ready
state and clock interrupt
Suitable for general purpose systems with multiple users
Guarantees acceptable response time and fairness
Context switching is an overhead
109
First-Come-First-Served Scheduling
First-Come-First-Served Scheduling (FCFSS)
Basic Concept
The process that requested the CPU first is allocated the CPU
and keeps it until it released it.
The process that has been in the ready queue the longest is
selected for running.
Its selection function is waiting time and it uses non
preemptive scheduling/decision mode.
110
FCFSS(cont’d)
Illustration
Consider the following processes arrive at time 0
Process P1 P2 P3
CPU Burst/Service Time (in ms) 24 3 3
Case i. If they arrive in the order of P1, P2, P3
Process P1 P2 P3
Service Time (Ts) 24 3 3
Turn around time (Tr) 24 27 30
Response time 0 24 27
Tr/Ts 1 9 10
Average response time = (0+24+27)/3 = 17
Average turn around time = (24+27+30)/3=27
Throughput = 3/30= 1/10
111
FCFSS(cont’d)
Case ii. If they arrive in the order of P3, P2, P1
Process P3 P2 P1
Service Time (Ts) 3 3 24
Turn around time (Tr) 3 6 30
Response time 0 3 6
Tr/Ts 1 2 1.25
Average response time = (0+3+6)/3 = 3
Average turn around time = (3+6+30)/3=13
Throughput = 3/30= 1/10
112
FCFSS(cont’d)
Consider the following processes arrive at time 0, 1,
2, 3 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 1 2 3
Service Time (Ts) 1 100 1 100
Turn around time (Tr) 1 100 100 199
Response time 0 0 99 99
Tr/Ts 1 1 100 1.99
Average response time = (0+0+99+99)/4 = 49.5
Average turn around time = (1+100+100+199)/4=100
Throughput = 4/202
113
FCFSS(cont’d)
Exercise
1. Consider the following processes arrive at time 0, 3,
8, 11 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 3 8 11
Service Time (Ts) 3 5 3 100
Turn around time (Tr) ? ? ? ?
Response time ? ? ? ?
Tr/Ts ? ? ? ?
Average response time = ?
Average turn around time = ?
Throughput =?
114
FCFSS(cont’d)
Process P1 P2 P3 P4
Arrival Time (Ta) 0 3 8 11
Service Time (Ts) 3 5 3 100
Turn around time (Tr) 3 5 3 100
Response time 0 0 0 0
Tr/Ts 1 1 1 1
Average response time = (0+0+0+0)/4 = 0
Average turn around time = (3+ 5+3+100)/4=27.75
Throughput = 4/111
115
FCFSS(cont’d)
2. Consider the following processes arrive at time 0, 2,
3, 4 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 2 3 4
Service Time (Ts) 4 16 50 1
Turn around time (Tr) ? ? ? ?
Response time ? ? ? ?
Tr/Ts ? ? ? ?
Average response time = ?
Average turn around time = ?
Throughput = ?
116
FCFSS(cont’d)
Consider the following processes arrive at time 0, 2,
3, 4 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 2 3 4
Service Time (Ts) 4 16 50 1
Turn around time (Tr) 0 18 67 67
Response time 0 2 17 66
Tr/Ts 0 1.33 1.34 67
Average response time = (0+2+17+66)/4 = 20.5
Average turn around time = (0+ 18+67+67)/4=38
Throughput = 4/71
117
FCFSS(cont’d)
Advantages
It is the simplest of all non-preemptive scheduling algorithms:
process selection & maintenance of the queue is simple.
There is a minimum overhead and no starvation.
It is often combined with priority scheduling to provide
efficiency.
Drawbacks
Poor CPU and I/O utilization: CPU will be idle when a process
is blocked for some I/O operation.
Poor and unpredictable performance: it depends on the arrival
of processes.
Unfair CPU allocation: If a big process is executing, all other
processes will be forced to wait for a long time until the
process releases the CPU. It performs much better for long
processes than short ones.
118
Shortest Job First Scheduling
Shortest Job First Scheduling (SJFS)
Basic Concept
Process with the shortest expected processing time (CPU
burst) is selected next.
Its selection function is execution time and it uses non
preemptive scheduling/decision mode.
119
SJFS
Illustration
Consider the following processes arrive at time 0
Process P1 P2 P3
CPU Burst (in ms) 24 3 3
Case i. FCFSS
Process P1 P2 P3
Turn around time 24 27 30
Response time 0 24 27
Average response time = (0+24+27)/3 = 17
Average turn around time = (24+27+30)/3=27
Throughput = 3/30
120
SJFS(cont’d)
Case ii. SJFS
Process P3 P2 P1
Response time 0 3 6
Throughput = 3/30
121
SJFS(cont’d)
Consider the following processes arrive at time 0,
2, 4, 6, 8 respectively
Process P1 P2 P3 P4 P5
Arrival Time (Ta) 0 2 4 6 8
Service Time (Ts) 3 6 4 5 2
Turn around time (Tr) 3 7 11 14 3
Response time 0 1 7 9 1
Tr/Ts 1 1.17 2.75 2.8 1.5
Average response time = (0+1+7+9+1)/5 = 3.6
Average turn around time = (3+7+11+14+3)/5=7.6
Throughput = 5/20
122
SJFS(cont’d)
Exercise
Consider the following processes arrive at time 0,
2, 3, 7, 10 respectively
Process P1 P2 P3 P4 P5
Arrival Time (Ta) 0 2 3 8 10
Service Time (Ts) 7 10 5 1 2
Turn around time (Tr) ? ? ? ? ?
Response time ? ? ? ? ?
Tr/Ts ? ? ? ? ?
Average response time = ?
Average turn around time = ?
Throughput = ?
123
SJFS(cont’d)
Consider the following processes arrive at time 0,
2, 3, 7, 10 respectively
Process P1 P2 P3 P4 P5
Arrival Time (Ta) 0 2 3 8 10
Service Time (Ts) 7 10 5 1 2
Turn around time (Tr) 7 23 9 5 5
Response time 0 13 4 4 3
Tr/Ts 1 1.17 2.75 2.8 1.5
Average response time = (0+13+4+4+3)/5 = 4.8
Average turn around time = (7+23+9+5+5)/5=9.8
Throughput = 5/25
124
SJFS(cont’d)
Advantages
It produces optimal average turn around time and average
response time
There is a minimum overhead
Drawbacks
Starvation: some processes may not get the CPU at all as long
as there is a steady supply of shorter processes.
It is not desirable for a time-sharing or transaction processing
environment because of its lack of processing.
Variability of response time is increased, especially for longer
processes.
125
Shortest Remaining Time Scheduling
Shortest Remaining Time Scheduling (SRTS)
Basic Concept
The process that has the shortest expected remaining process
time.
If a new process arrives with a shorter next CPU burst than
what is left of the currently executing process, the new process
gets the CPU.
Its selection function is remaining execution time and uses
preemptive decision mode
126
SRTS(cont’d)
Illustration
Consider the following processes arrive at time 0, 2, 4
Process P1 P2 P3
Arrival Time (Ta) 0 2 4
Service Time (Ts) 3 8 4
Turn around time (Tr) 3 12 4
Response time 0 1 0
Tr/Ts 1 1.5 1
Average response time = (0+1+0)/3= 0.33
Average turn around time = (3+12+4)/3= 19/3
Throughput = 3/14
127
SRTS(cont’d)
Illustration
Consider the following processes arrive at time 0, 2, 4, 6, 8
respectively
Process P1 P2 P3 P4 P5
Arrival Time (Ta) 0 2 4 6 8
Service Time (Ts) 3 6 4 5 2
Turn around time (Tr) 3 13 4 14 2
Response time 0 1 0 9 0
Tr/Ts 1 2.17 1 2.8 1
Average response time = (0+1+0+9+0)/5 = 2
Average turn around time = (3+13+4+14+2)/5=7.2
Throughput = 5/20
128
SRTS(cont’d)
Advantages
It gives superior turnaround time performance to SJFS,
because a short job is given immediate preference to a
running longer process
Drawbacks
There is a risk of starvation of longer processes
High overhead due to frequent process switch
129
Round Robin Scheduling
Round Robin Scheduling (RRS)
Basic Concept
A small amount of time called a quantum or time slice is
defined. According to the quantum, a clock interrupt is
generated at periodic intervals. When the interrupt occurs, the
currently running process is placed in the ready queue, and
the next ready process is selected on a FCFS basis.
The CPU is allocated to each process for a time interval of
upto one quantum. When a process finishes its quantum it is
added to the ready queue, when it is requesting I/O it is added
to the waiting queue.
The ready queue is treated as a circular queue
Its selection function is based on quantum and it uses
preemptive decision mode
130
RRS(cont’d)
Illustration
Consider the following processes arrive at time 0, 2, 4
respectively; quantum=4
Process P1 P2 P3
Arrival Time (Ta) 0 2 4
Service Time (Ts) 3 7 4
Turn around time (Tr) 3 12 7
Response time 0 1 3
Average response time = (0+1+3)/3
Average turn around time = (3+12+7)/3
Throughput = 3/14
131
RRS(cont’d)
Illustration
Consider the following processes arrive at time 0, 3, 4, 7
respectively; quantum=4
Process P1 P2 P3 p4
Arrival Time (Ta) 0 3 4 7
Service Time (Ts) 7 5 9 10
Turn around time (Tr) 19 17 25 24
Response time 0 1 4 5
Average response time = (0+1+4+5)/4
Average turn around time =(19+17+25+24)/4
Throughput = 4/31
132
RRS(cont’d)
Features
The oldest, simplest, fairest and most widely used preemptive
scheduling
Reduces the penalty that short processes suffer with FCFS
Drawbacks
CPU-bound processes tend to receive unfair portion of CPU
time, which results in poor performance for I/O bound
processes.
It makes implicit assumption that all processes are equally
important. It does not take external factors into account
Maximum overhead due to frequent process switch
133
RRS(cont’d)
Difficulty with RRS
The length of the quantum should be decided carefully
E.g.1) quantum =20ms, context switch =5ms, % of context
switch = 5/25 *100=20%
Poor CPU utilization
Good interactivity
E.g.2) quantum =500ms, context switch =5ms, % of context
switch = 5/505 *100<1%
Improved CPU utilization
Poor interactivity
Setting the quantum too short causes
Poor CPU utilization
Good interactivity
134
RRS(cont’d)
Setting the quantum too long causes
Improved CPU utilization
Poor interactivity
A quantum around 100ms is often reasonable compromise
To enable interactivity, a maximum response time (MR) can be
specified, and the quantum (m) can be computed dynamically
as follows
135
Priority Scheduling
Priority Scheduling (PS)
Basic Concept
Each process is assigned a priority and the runnable process
with the highest priority is allowed to run i.e. a ready process
with highest priority is given the CPU.
It is often convenient to group processes into priority classes
and use priority scheduling among the classes but round robin
scheduling within each class
136
PS(cont’d)
Ilustration
Consider the following processes arrive at time 0
Process P1 P2 P3 P4 P5
Priority 2 4 5 3 1
Service Time (Ts) 3 6 4 5 2
Turn around time (Tr) 18 10 4 15 20
Response time 15 4 0 10 18
Tr/Ts 6 1.67 1 3 10
Average response time = (0+15+4+10+18)/5 = 9.4
Average turn around time = (18+10+4+15+20)/5=13.4
Throughput = 5/20= 0.25
137
PS(cont’d)
Consider the following priority classes
Processes type:
Deans Heads Instructors Secretaries Students
Priority: 5 4 3 2 1
138
PS(cont’d)
Advantages
It considers the fact that some processes are more important
than others, i.e. it takes external factors into account
Drawbacks
A high priority process may run indefinitely and it can prevent
all other processes from running. This creates starvation on
other processes. There are two possible solutions for this
problem:
139
Multilevel Queues Scheduling
Multilevel Queues Scheduling (MLQS)
It is used for situations for which processes can be classified
into different groups
System processes
Interactive processes
Batch processes
The ready queue is partitioned into several separate queues
Each processes is assigned to one queue permanently, based
on some property of the process such as memory size,
process priority, process type, etc
140
MLQS(cont’d)
Each queue has its own scheduling algorithm: RRS, FCFSS,
SJFS, PS etc
System processes…………… PS
Interactive processes…………RRS
Batch processes………………FCFSS
There must be some kind of scheduling between the queues
Commonly implemented as fixed-priority preemptive scheduling,
i.e. system processes have absolute priority over the interactive
processes and interactive processes have absolute priority over the
batch processes. Lowest priority processes may be starved.
141
Quiz
Define the following terminologies (5 pts)
A. Throughput
B. Turnaround time
C. Response time
142
Quiz
Q1. (5 pts) Consider the following processes arrive at
time 0, 4, 5, 6 respectively
Process P1 P2 P3 P4
Arrival Time (Ta) 0 4 5 6
Service Time (Ts) 9 3 1 8
Turn around time (Tr) ? ? ? ?
Response time ? ? ? ?
Throughput = ?
143
Quiz…
Q2. (5 pts) Consider the following processes arrive at
time 0 and quantum value is 5:
Process P1 P2 P3 P4
Service Time (Ts) 13 3 15 7
Turn around time (Tr) ? ? ? ?
Response time ? ? ? ?
Throughput = ?
144