OS Unit2
OS Unit2
Process Management
Process vs Program
An operating system executes a variety of programs that run as a process.
Process is a program in execution.
Process is considered as an instance of a program, replete with registers, variables, and a
program counter
A process is an activity of some kind. It has a program, input, output and a state.
A program is a set of instructions, written to complete some task
Program is passive entity stored on disk (executable file); process is active
o Program becomes process when an executable file is loaded into memory
Execution of program started via GUI mouse clicks, command line entry of its name, etc are
examples of processes
If a program is running twice, it counts as two processes.
The difference between a process and a program is subtle, but absolutely crucial. An analogy may
help you here. Consider a culinary-minded computer scientist who is baking a birthday cake
for his young daughter. He has a birthday cake recipe and a kitchen well stocked with all the
input: flour, eggs, sugar, extract of vanilla, and so on. In this analogy, the recipe is the program,
that is, an algorithm expressed in some suitable notation, the computer scientist is the processor
(CPU), and the cake ingredients are the input data. The process is the activity consisting of
our baker reading the recipe, fetching the ingredients, and baking the cake. Now imagine
that the computer scientist’s son comes running in screaming his head off, saying that he has been
stung by a bee. The computer scientist records where he was in the recipe (the state of the
current process is saved), gets out a first aid book, and begins following the directions in it. Here
we see the processor being switched from one process (baking) to a higher-priority process
(administering medical care), each having a different program (recipe versus first aid book).
When the bee sting has been taken care of, the computer scientist goes back to his cake,
continuing at the point where he left off.
1
Collected by Bipin Timalsina
Unit-2/OS
NOTE: A single processor may be shared among several processes, with some scheduling
algorithm being accustomed to determine when to stop work on one process and service a different
one. In contrast, a program is something that may be stored on disk, not doing anything.
A program and a process are related terms but are not same. The major difference between
program and process is that program is a group of instructions to carry out a specified task
whereas the process is a program in execution. While a process is an active entity, a
program is considered to be a passive one.
2
Collected by Bipin Timalsina
Unit-2/OS
Multiprogramming
In Multiprogramming, to execute the processes, only one CPU is used.
In a multiprogramming system there are one or more programs loaded in main memory
which are ready to execute. Only one program at a time is able to get the CPU for executing
its instructions (i.e., there is at most one process running on the system) while all the others
are waiting their turn.
Multiprogramming is based on context switching which doesn’t allow CPU to sit idle
thereby maximizing CPU utilization.
easier to think about a collection of processes running in (pseudo) parallel than to try to
keep track of how the CPU switches from program to program. This rapid switching back
and forth is called multiprogramming.
In any multiprogramming system, the CPU switches from process to process quickly,
running each for tens or hundreds of milliseconds. While, strictly speaking, at any one
instant the CPU is running only one process, in the course of 1 second it may work on
several of them, giving the illusion of parallelism. Sometimes people speak of
pseudoparallelism in this context, to contrast it with the true hardware parallelism of
multiprocessor systems (which have two or more CPUs sharing the same physical
memory).
Figure : (a) Multiprogramming of four programs. (b) Conceptual model of four independent, sequential processes. (c) Only one
program is active at once.
Process Creation
Operating systems need some way to create processes. Events which can cause process creation
4
Collected by Bipin Timalsina
Unit-2/OS
• System initialization.
Process Termination
After a process has been created, it starts running and does whatever its job is. However, nothing
lasts forever, not even processes.
Process States
As a process executes, it changes state. The state of a process is defined in part by the current
activity of that process. A process may be in one of the following states:
• Waiting: The process is waiting for some event to occur (such as an I/O
completion or reception of a signal).
5
Collected by Bipin Timalsina
Unit-2/OS
6
Collected by Bipin Timalsina
Unit-2/OS
Following figure shows some of the key fields in a typical system. The fields in the first column
relate to process management. The other two relate to memory management and file management,
respectively. It should be noted that precisely which fields the process table has is highly system
dependent, but this figure gives a general idea of the kinds of information needed.
7
Collected by Bipin Timalsina
Unit-2/OS
Threads
A thread is a light-weight smallest part of a process that can run concurrently with the
other parts (other threads) of the same process. It has a separate path of execution
A process can have multiple threads.
Threads are independent. If a thread gets an exception or an error at the time of its
execution, it doesn't affect the execution of the other threads.
All the threads share a common memory and have their own stack, local variables and
program counter.
Because threads have some of the properties of processes, they are sometimes called
lightweight processes.
When multiple threads are executed in parallel at the same time, this process is known
as multithreading.
Each thread belongs to exactly one process and no thre.ad can exist outside a process. Each
thread represents a separate flow of control.
A thread has :
A program counter that keeps track of which instruction to execute next.
Registers, which hold its current working variables.
A stack, which contains the execution history, with one frame for each procedure
called but not yet returned from
Thread vs Process
Although a thread must execute in some process, the thread and its process are different
concepts and can be treated separately.
Processes are used to group resources together; threads are the entities scheduled for
execution on the CPU.
What threads add to the process model is to allow multiple executions to take place in the
same process environment, to a large degree independent of one another
Having multiple threads running in parallel in one process is analogous to having multiple
processes running in parallel in one computer.
In the former case, the threads share an address space and other resources.
In the latter case, processes share physical memory, disks, printers, and other
resources.
8
Collected by Bipin Timalsina
Unit-2/OS
Because threads have some of the properties of processes, they are sometimes called
lightweight processes.
The term multithreading is also used to describe the situation of allowing multiple threads
in the same process
In following figure (a) we see three traditional processes. Each process has its own address
space and a single thread of control. In contrast, in (b) we see a single process with three
threads of control. Although in both cases we have three threads, in (a) each of them
operates in a different address space, whereas in (b) all three of them share the same address
space
Figure: (a) Three processes each with one thread. (b) One process with
9
Collected by Bipin Timalsina
Unit-2/OS
Benefits of threads
Responsiveness – may allow continued execution if part of process is blocked, especially
important for user interfaces
Resource Sharing – threads share resources of process, easier than shared memory or
message passing
Economy – cheaper than process creation, thread switching lower overhead than context
switching
10
Collected by Bipin Timalsina
Unit-2/OS
11
Collected by Bipin Timalsina
Unit-2/OS
Advantages:
o Thread table contains info about threads (program counter, stack pointer...) so that
run time system can manage them
o If thread blocks, run time system stores thread info in table and finds new thread to
run.
o They allow each process to have its own customized scheduling algorithm.
o The procedure that saves the thread’s state and the scheduler are just local
procedures, so invoking them is much more efficient than making a kernel call.
User-level threads are easier and faster to create than kernel-level threads. They can
also be more easily managed.
User-level threads can be run on any operating system.
There are no kernel mode privileges required for thread switching in user-level
threads.
Limitations:
The entire process is blocked if one user-level thread performs blocking operation.
There is a lack of coordination between threads and operating system kernel.
12
Collected by Bipin Timalsina
Unit-2/OS
Advantages:
Limitations:
13
Collected by Bipin Timalsina
Unit-2/OS
When this approach is used, the programmer can determine how many kernel
threads to use and how many user-level threads to multiplex on each one. This
model gives the ultimate in flexibility
With this approach, the kernel is aware of only the kernel-level threads and
schedules those. Some of those threads may have multiple user-level threads
multiplexed on top of them. These user-level threads are created, destroyed, and
scheduled just like user-level threads in a process that runs on an operating system
without multithreading capability. In this model, each kernel-level thread has some
set of user-level threads that take turns using it.
14
Collected by Bipin Timalsina
Unit-2/OS
Hybrid Approach
A process is independent if it does not share data with any other processes executing in
the system.
A process is cooperating if it can affect or be affected by the other processes executing in
the system.
An independent process is not affected by the execution of other processes while a co-
operating process can be affected by other executing processes.
Cooperating process can affect or be affected by other processes, including sharing data
Cooperating processes need interprocess communication (IPC)
There are several reasons for providing an environment that allows process cooperation:
Information sharing: Since several applications may be interested in the same piece
of information (for instance, copying and pasting), we must provide an environment to
allow concurrent access to such information.
Computation speedup: If we want a particular task to run faster, we must break it into
subtasks, each of which will be executing in parallel with the others. Notice that such
a speedup can be achieved only if the computer has multiple processing cores.
Modularity: We may want to construct the system in a modular fashion, dividing the
system functions into separate processes or threads.
15
Collected by Bipin Timalsina
Unit-2/OS
NOTE:
• Shared memory
• Message passing
• In the shared-memory model, a region of memory that is shared by the cooperating processes
is established. Processes can then exchange information by reading and writing data to the
shared region.
• In the message-passing model, communication takes place by means of messages exchanged
between the cooperating processes
16
Collected by Bipin Timalsina
Unit-2/OS
Race Conditions
The Situations, where two or more processes are reading or writing some shared data and
the final result depends on who runs precisely when, are called race conditions.
A race condition is a condition when there are many processes and every process shares
the data (resource) with each other and accessing the data (resource) concurrently, and the
output of execution depends on a particular sequence in which they share the data
(resource) and access
The inconsistent output produced by race conditions may cause bugs that are difficult to
detect.
Unfortunately, with increasing parallelism due to increasing numbers of cores, race
condition are becoming more common.
17
Collected by Bipin Timalsina
Unit-2/OS
.
18
Collected by Bipin Timalsina
Unit-2/OS
19
Collected by Bipin Timalsina
Unit-2/OS
20
Collected by Bipin Timalsina
Unit-2/OS
Disabling interrupts
Lock variables
Strict alternation
Peterson's solution
21
Collected by Bipin Timalsina
Unit-2/OS
Disabling interrupts
On a single-processor system, the simplest solution is to have each process disable all
interrupts just after entering its critical region and re-enable them just before leaving it.
Idea: process disables interrupts, enters critical region, enables interrupts when it leaves
critical region.
Problems:
It is unwise to give user processes the power to turn off interrupts. Process might never
enable interrupts, crashing system
Won’t work on multi-core chips (or multiprocessor system) as disabling interrupts only
effects one CPU at a time.
Disabling interrupts affects only the CPU that executed the disable instruction.
The other ones will continue running and can access the shared memory.
Lock variables
It is a software solution.
Consider having a single, shared (lock) variable, initially 0.
When a process wants to enter its critical region, it first tests the lock.
o If the lock is 0, the process sets it to 1 and enters the critical region.
o If the lock is already 1, the process just waits until it becomes 0.
Thus,
lock = 0 => no process is in its critical section
lock = 1 => some process is in its critical section.
Problem:
Race condition
If process P1 sees the value of lock variable 0 and before it can set it to 1 context
switch occurs.
Now process P2 runs and finds value of lock variable 0, so it sets value to 1,
enters critical region.
At some point of time P1 resumes, sets the value of lock variable to 1, enters
critical region.
22
Collected by Bipin Timalsina
Unit-2/OS
Now two processes are in their critical regions accessing the same shared
memory, which violates the mutual exclusion condition.
Strict alternation
This approach is the software mechanism implemented at user mode.
It is a busy waiting solution which can be implemented only for two processes.
In this approach, the integer variable turn, initially 0, keeps track of whose turn it is to enter
the critical region and examine or update the shared memory
Initially, turn value is set to 0.
turn = 0 means it is the turn of process P0 to enter the critical section.
turn = 1 means it is the turn of process P1 to enter the critical section.
IDEA: First me, then you!
Figure: A proposed solution (Strict alternation) to the critical-region problem. (a) Process 0. (b) Process 1.
23
Collected by Bipin Timalsina
Unit-2/OS
Process 0 (P0) exits the critical region and sets turn to 1. Process 1 (P1) is also executing in
non-critical region At this point turn is 1 and both processes are executing in their
noncritical regions.
Suddenly, P0 finishes its noncritical region and goes back to the top of its loop.
Unfortunately, it is not permitted to enter its critical region now, because turn is 1 and P1
is busy with its noncritical region. It hangs in its while loop until process P1 sets turn to 0.
This situation violates condition 3 set out above ("No process running outside its critical region
may block any process."): process 0 is being blocked by a process not in its critical region.
Taking turns is not a good idea when one of the processes is much slower than the other.
Problems:
Employs busy waiting-while waiting for the cr, a process spins
If one process is outside the cr and it is its turn, then other process has to wait until
outside guy finishes both outside AND inside (cr) work
24
Collected by Bipin Timalsina
Unit-2/OS
NOTES:
Continuously testing a variable until some value appears is called busy waiting. It
should usually be avoided, since it wastes CPU time. Only when there is a reasonable
expectation that the wait will be short is busy waiting used. A lock that uses busy
waiting is called a spin lock.
In fact, this solution (strict alternation) requires that the two processes strictly
alternate in entering their critical regions, for example, in spooling files. Neither one
would be permitted to spool two in a row. While this algorithm does avoid all races, it
is not really a serious candidate as a solution because it violates condition 3.
Peterson’s Solution
It is a classical software-based solution to the critical section
This solution is restricted to two processes that alternate execution between their critical
sections and remainder sections.
25
Collected by Bipin Timalsina
Unit-2/OS
Before using the shared variables (i.e., before entering its critical region), each process calls
enter_region with its own process number, 0 or 1, as parameter.
o This call will cause it to wait, if need be, until it is safe to enter.
After it has finished with the shared variables, the process calls leave_region to indicate that
it is done and to allow the other process to enter, if it so desires.
Let us see how this solution works. Initially neither process is in its critical region.
o Now process 0 calls enter_region.
It indicates its interest by setting its array element and sets turn to 0.
Since process 1 is not interested, enter_region returns immediately. (no busy
waiting )
If process 1 now makes a call to enter_region, it will hang there until interested[0]
goes to FALSE, an event that happens only when process 0 calls leave_region to
exit the critical region.
o 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 overwritten and 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
region.
Process 1 loops and does no enter its critical region until process 0 exits its
critical region.
Disadvantages:
26
Collected by Bipin Timalsina
Unit-2/OS
TSL instruction is an instruction that returns the old value of a memory location and sets
the memory location value to nonzero as a single atomic operation.
If one process is currently executing a test-and-set, no other process is allowed to begin
another test-and-set until the first process test-and-set is finished.
TSL is atomic. Memory bus is locked until it is finished executing.
To use the TSL instruction, a shared variable, lock, is used to coordinate access to shared
memory.
When lock is 0, any process may set it to 1 using the TSL instruction and then read or
write the shared memory.
When it is done, the process sets lock back to 0 using an ordinary move instruction.
lock = 0 means the critical section is currently vacant and no process is present
inside it.
lock = 1 means the critical section is currently occupied and a process is present
inside it.
27
Collected by Bipin Timalsina
Unit-2/OS
The TSL instruction can be used to prevent two processes from simultaneously entering their
critical regions. The solution is given in above figure (algorithm).
There a four-instruction subroutine (enter_region) in a fictitious (but typical) assembly
language is shown.
The first instruction (TSL REGISTER,LOCK) copies the old value of lock to the register
and then sets lock to 1.
Then the old value is compared with 0 (CMP REGISTER,#0) .
If it is nonzero, the lock was already set, so the program just goes back to the beginning
and tests it again.(REGISTER,#0)
Sooner or later it will become 0 (when the process currently in its critical region is done
with its critical region), and the subroutine returns, with the lock set. (RET)
Another subroutine, leave_region is used for clearing the lock which is very simple. The
program just stores a 0 in lock. No special synchronization instructions are needed.
Disadvantages:
It is a busy waiting solution which keeps the CPU busy when the process is actually
waiting.
TSL doesn't provide Architectural Neutrality. It depends on the hardware platform. The
TSL instruction is provided by the operating system. Some platforms might not provide
that. Hence it is not Architectural natural.
28
Collected by Bipin Timalsina
Unit-2/OS
Problems:
Solutions:
There are some interposes communication primitives that block instead of wasting CPU time when
they are not allowed to enter their critical regions.
29
Collected by Bipin Timalsina
Unit-2/OS
Example to show how the sleep and wakeup primitives are used
Producer Consumer Problem
Also known as the bounded-buffer problem
Two processes share a common, fixed-size buffer. One of them, the producer, puts
information into the buffer, and the other one, the consumer, takes it out.
30
Collected by Bipin Timalsina
Unit-2/OS
(It is also possible to generalize the problem to have m producers and n consumers, but we
will consider only the case of one producer and one consumer because this assumption
simplifies the solutions.)
Trouble arises when the producer wants to put a new item in the buffer, but it is already
full. The solution is for the producer to go to sleep, to be awakened when the consumer has
removed one or more items. Similarly, if 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.
To keep track of the number of items in the buffer, we will need a variable, count. If the
maximum number of items the buffer can hold is N,
o The producer’s code will first test to see if count is N. If it is, the producer will go
to sleep; if it is not, the producer will add an item and increment count.
o The consumer’s code is similar: first test count to see if it is 0. If it is, go to sleep;
if it is nonzero, remove an item and decrement the counter. Each of the processes
also tests to see if the other should be awakened, and if so, wakes it up. The code
for both producer and consumer is shown in following figure.
31
Collected by Bipin Timalsina
Unit-2/OS
Problem:
This approach sounds simple enough, but it leads to the same kinds of race conditions we saw
earlier with the spooler directory. It can occur because access to count is unconstrained. As a
consequence, the following situation could possibly occur.
The buffer is empty and the consumer has just read count to see if it is 0.
At that instant, the scheduler decides to stop running the consumer temporarily and start running
the producer.
The producer inserts an item in the buffer, increments count, and notices that it is now 1.
Reasoning that count was just 0, and thus the consumer must be sleeping, the producer calls wakeup
to wake the consumer up.
Unfortunately, the consumer is not yet logically asleep, so the wakeup signal is lost.
32
Collected by Bipin Timalsina
Unit-2/OS
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.
NOTE: The essence of the problem here is that a wakeup sent to a process that is not (yet) sleeping is
lost. If it were not lost, everything would work.
Semaphore
A semaphore is a variable that provides an abstraction for controlling access of a shared
resource by multiple processes in a parallel programming environment.
There are 2 types of semaphores:
1. Binary semaphores
Binary semaphores can take only 2 values (0/1).
Binary semaphores have 2 methods associated with it (up, down / lock, unlock).
They are used to acquire locks.
2. Counting semaphores
Counting semaphore can have possible values more man two.
NOTE: Dijkstra (1965) suggested using an integer variable to count the number of wakeups saved
for future use. In his proposal, a new variable type, which he called a semaphore, was introduced.
A semaphore could have the value 0, indicating that no wakeups were saved, or some positive
value if one or more wakeups were pending.
Down checks semaphore. If not zero, decrements semaphore. If zero, process goes to
sleep
Up increments semaphore. If more than one process asleep, one is chosen randomly
and enters critical region (first does a down)
Checking the value, changing it, and possibly going to sleep, are all done as a single, indivisible
atomic action. It is guaranteed that 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.
33
Collected by Bipin Timalsina
Unit-2/OS
The operation of incrementing the semaphore and waking up one process is also indivisible.
No process ever blocks doing an up, just as no process ever blocks doing a wakeup in the
earlier model.
34
Collected by Bipin Timalsina
Unit-2/OS
• Three semaphores: full, empty and mutex are used in this solution.
35
Collected by Bipin Timalsina
Unit-2/OS
Limitations
Monitors
A higher-level synchronization primitive
A monitor is a collection of procedures, variables, and data structures that are all grouped
together in a special kind of module or package.
Processes may call the procedures in a monitor whenever they want to, but they cannot
directly access the monitor’s internal data structures from procedures declared outside the
monitor
In a monitor it is the job of the compiler, not the programmer to enforce mutual exclusion.
Monitors have an important property that makes them useful to achieve mutual exclusion:
only one process at a time can be in the monitor.
Monitor is a language construct which enforces mutual exclusion and blocking
mechanism
C does not have monitor
36
Collected by Bipin Timalsina
Unit-2/OS
Another process is allowed to enter the monitor (e.g. consumer).This process can issue a
signal, causing blocked process (producer) to wake up
37
Collected by Bipin Timalsina
Unit-2/OS
Figure: An outline of the producer-consumer problem with monitors. Only one monitor procedure at a time is active. The buffer
has N slots
Limitations
38
Collected by Bipin Timalsina
Unit-2/OS
NOTE:
Monitors and semaphores only work for shared memory
They don’t work for multiple CPU’s which have their own private memory (Eg: A
distributed system consisting of multiple CPUs, each with its own private memory and
connected by a local area network.)
Conclusion: The conclusion is that semaphores are too low level and monitors are not usable
except in a few programming languages. Also, none of the primitives allow information
exchange between machines. Something else (Message Passing) is needed.
Message Passing
Message Passing is information exchange between machines
This method of interprocess communication uses two primitives: send and receive, which,
like semaphores and unlike monitors, are system calls rather than language constructs
Two primitives in Message Passing
1. Send: It is used to send the message.
send (destination, &message)
Here, destination is the process to which sender want to send message and message
is what the sender wants to send.
receive (source,&message)
Here, source is the process that has sent message and message is what the sender
has sent.
39
Collected by Bipin Timalsina
Unit-2/OS
40
Collected by Bipin Timalsina
Unit-2/OS
Five philosophers are seated around a circular table. Each philosopher has a plate of
spaghetti. The spaghetti is so slippery that a philosopher needs two forks to eat it.
Between each pair of plates is one fork. The life of a philosopher consists of alternating
periods of eating and thinking. When a philosopher gets sufficiently hungry, she tries
to acquire her left and right forks, one at a time, in either order. If successful in
acquiring two forks, she eats for a while, then puts down the forks, and continues to
think. The key question is: Can you write a program for each philosopher that does
what it is supposed to do and never gets stuck?
Figure: Lunch time in the Philosophy Department (Layout of Table in Dining Philosophers Problem).
41
Collected by Bipin Timalsina
Unit-2/OS
An obvious solution is illustrated in following figure. The procedure take_fork waits until
the specified fork is available and then seizes it.
42
Collected by Bipin Timalsina
Unit-2/OS
---------------------------------------------------------------------------------------------------------------------
We could easily modify the program so that 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.
Suppose all the philosophers could start the algorithm simultaneously, picking up
their left forks, seeing that their right forks were not available, putting down their
left forks, waiting, and picking up their left forks again simultaneously, and so on,
forever.
A situation like this, in which all the programs continue to run indefinitely but fail
to make any progress, is called starvation.
---------------------------------------------------------------------------------------------------------------------
A solution presented below is deadlock-free and allows the maximum parallelism for an
arbitrary number of philosophers.
The program uses an array of semaphores, one per philosopher, so hungry philosophers
can block if the needed forks are busy.
Note that each process runs the procedure philosopher as its main code, but the other
procedures, take_forks, put_forks, and test, are ordinary procedures and not separate
processes.
A philosopher can only move to eating state if neither neighbor is eating.
43
Collected by Bipin Timalsina
Unit-2/OS
44
Collected by Bipin Timalsina
Unit-2/OS
45
Collected by Bipin Timalsina
Unit-2/OS
In practice, there are a number of problems that can occur that are illustrative of general
scheduling problems.
Some cases: (problems)
A customer may arrive and observe that the barber is cutting hair, so he goes to the
waiting room. While he is on his way, the barber finishes the haircut he is doing and
goes to check the waiting room. Since there is no one there (the customer not having
arrived yet), he goes back to his chair and sleeps. The barber is now waiting for a
customer and the customer is waiting for the barber.
Two customers may arrive at the same time when there happens to be a single seat in
the waiting room. They observe that the barber is cutting hair, go to the waiting room,
and both attempt to occupy the single chair.
The solution of this problem is to include three Semaphores.
The first one, customers,to count the number of customers present in the waiting
room.
The second one, barbers, for the barber. 0 and 1 are used to signify if the barber
is idle or not.
The third, mutex , is for mutual exclusion. It is needed for the program to run.
We also need a variable, waiting, which also counts the waiting customers. The reason for
having waiting is that there is no way to read the current value of a semaphore
When the barber shows up for work in the morning, he executes the procedure barber, causing
him to block on the semaphore customers because it is initially 0. The barber then goes to
sleep. He stays asleep until the first customer shows up.
When a customer arrives, he executes customer, starting by acquiring mutex to enter a critical
region. If another customer enters shortly thereafter, the second one will not be able to do
anything until the first one has released mutex. The customer then checks to see if the number
of waiting customers is less than the number of chairs. If not, he releases mutex and leaves
without a haircut.
If there is an available chair, the customer increments the integer variable, waiting. Then he
does an up on the semaphore customers, thus waking up the barber. At this point, the customer
and the barber are both awake. When the customer releases mutex, the barber grabs it, does
some housekeeping, and begins the haircut.
When the haircut is over, the customer exits the procedure and leaves the shop.
46
Collected by Bipin Timalsina
Unit-2/OS
47
Collected by Bipin Timalsina
Unit-2/OS
}
}
Process Scheduling
When a computer is multiprogrammed, it frequently has multiple processes or threads
competing for the CPU at the same time. This situation occurs whenever two or more of
them are simultaneously in the ready state.
If only one CPU is available, a choice has to be made which process to run next. The part
of the operating system that makes the choice is called the scheduler, and the algorithm it
uses is called the scheduling algorithm.
The process scheduling is the activity of the process scheduler that handles the removal of the
running process from the CPU and the selection of another process on the basis of a particular
strategy.
Process scheduler selects among available processes for next execution on CPU core
Batch servers
Time sharing machines
Networked servers
You care if you have a bunch of users and/or if the demands of the jobs differ
Who doesn’t care about scheduling algorithms?
PC’s
One user who only competes with himself for the CPU
Process Behavior
Nearly all processes alternate bursts of computing with (disk or network) I/O requests. (Process
execution consists of cycle of CPU execution and I/O wait).
Some processes, such as the one in following figure (a), spend most of their time computing, while
other processes, such as the one shown in (b), spend most of their time waiting for I/O.
48
Collected by Bipin Timalsina
Unit-2/OS
Figure: Bursts of CPU usage alternate with periods of waiting for I/O. (a) A CPU-bound process. (b) An I/O-bound process
The processes which spends most of their time computing are called compute-bound
or CPU bound process
The processes which spends most of their time waiting for I/O are called I/O bound
process
CPU Burst is the time that a process spends on the CPU executing some code. CPU
Burst deals with the running state of the process.
I/O Burst is the time that a process spends in waiting for the completion of the I/O
request. I/O Burst deals with the waiting state of the process.
Compute-bound processes typically have long CPU bursts and thus infrequent I/O
waits, whereas I/O-bound processes have short CPU bursts and thus frequent I/O
waits.
Note that the key factor is the length of the CPU burst, not the length of the I/O
burst.
o I/O-bound processes are I/O bound because they do not compute much between
I/O requests, not because they hav e especially long I/O requests.
o It takes the same time to issue the hardware request to read a disk block no
matter how much or how little time it takes to process the data after they arrive.
The basic idea: If an I/O-bound process wants to run, it should get a chance quickly so that it can
issue its disk request and keep the disk busy
49
Collected by Bipin Timalsina
Unit-2/OS
When to Schedule?
A key issue related to scheduling is when to make scheduling decisions. It turns out that there are
a variety of situations in which scheduling is neede d.
• When a new process is created, a decision needs to be made whether to run the parent
process or the child process.
• When a process exits. The process can no longer run (since it no longer exists), so some
other process must be chosen from the set of ready processes.
• When a process blocks on I/O, on a semaphore, or for some other reason, another process
has to be selected to run.
• When an I/O interrupt occur. If the interrupt came from an I/O device that has now completed its
work, some process that was blocked waiting for the I/O may now be ready to run. It is up to the
scheduler to decide whether to run the newly ready process, the process that was running
at the time of the interrupt, or some third process.
Nonpreemptive Scheduling
• A nonpreemptive 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 voluntarily releases the CPU
• Even if it runs for many hours, it will not be forcibly suspended. In effect, no scheduling
decisions are made during clock interrupts.
50
Collected by Bipin Timalsina
Unit-2/OS
Preemptive Scheduling
• A preemptive scheduling algorithm picks a process and lets it run for a maximum of some
fixed time.
• If it is still running at the end of the time interval, it is suspended and the scheduler picks
another process to run (if one is available).
• Doing preemptive scheduling requires having a clock interrupt occur at the end of the time
interval to give control of the CPU back to the scheduler.
In preemptive scheduling, the CPU is allocated to the processes for a limited time whereas, in Non-
preemptive scheduling, the CPU is allocated to the process till it terminates or switches to the waiting
state.
Scheduling Criteria
Different CPU-scheduling algorithms have different properties, and the choice of a particular
algorithm may favor one class of processes over another. In choosing which algorithm to use in a
particular situation, we must consider the properties of the various algorithms. Many criteria have
been suggested for comparing CPU-scheduling algorithms. Some of them are as follows:
51
Collected by Bipin Timalsina
Unit-2/OS
1. Batch systems.
2. Interactive systems.
52
Collected by Bipin Timalsina
Unit-2/OS
53
Collected by Bipin Timalsina
Unit-2/OS
We know,
Advantage:
Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is FCFS, calculate the average waiting time and average turn-around time.
54
Collected by Bipin Timalsina
Unit-2/OS
Gantt Chart-
Now, we know-
P1 7 7–3=4 4–4=0
P2 13 13 – 5 = 8 8–3=5
P3 2 2–0=2 2–2=0
P4 14 14 – 5 = 9 9–1=8
P5 10 10 – 4 = 6 6–3=3
55
Collected by Bipin Timalsina
Unit-2/OS
Problem-02:
Consider the set of 3 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is FCFS, calculate the average waiting time and average turn -around time.
Gantt Chart-
Now, we know-
Completion
Process Turnaround time Waiting time
Time
P1 2 2–0=2 2–2=0
P2 4 4–3=1 1–1=0
P3 11 11- 5 = 6 6–6=0
Now,
56
Collected by Bipin Timalsina
Unit-2/OS
P1 P4 P3 P2
0 6 9 16 24
P2 24 8 24 - 2 = 22 22 - 8 = 14
P3 16 7 16 - 4= 12 12 - 7 = 5
P4 9 3 9- 5=4 4- 3=1
57
Collected by Bipin Timalsina
Unit-2/OS
Advantages:
Disadvantages:
Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is SJF non-preemptive, calculate the average waiting time and average turn -
around time.
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
58
Collected by Bipin Timalsina
Unit-2/OS
Solution-
Gantt Chart-
Now, we know-
P1 7 7–3=4 4–1=3
P2 16 16 – 1 = 15 15 – 4 = 11
P3 9 9–4=5 5–2=3
P4 6 6–0=6 6–6=0
P5 12 12 – 2 = 10 10 – 3 = 7
Now,
59
Collected by Bipin Timalsina
Unit-2/OS
Exercise: Schedule the given processes using SJF preemptive and find average turnaround time
and average waiting time
P1 P2 P4 P1 P3
0 1 5 10 17 26
60
Collected by Bipin Timalsina
Unit-2/OS
P2 5 4 5–1=4 4–4=0
P3 26 9 26 – 2 = 24 24 – 9 = 15
P4 10 5 10 – 3= 7 7– 5 = 2
Advantages:
Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below:
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
If the CPU scheduling policy is SJF preemptive, calculate the average waiting time and average
turn-around time.
61
Collected by Bipin Timalsina
Unit-2/OS
Solution:
Gantt Chart:
Now, we know-
Completion
Process Turnaround time Waiting time
Time
P1 4 4–3=1 1–1=0
P2 6 6–1=5 5–4=1
P3 8 8–4=4 4–2=2
P4 16 16 – 0 = 16 16 – 6 = 10
P5 11 11 – 2 = 9 9–3=6
Now,
62
Collected by Bipin Timalsina
Unit-2/OS
Problem-02:
Consider the set of 6 processes whose arrival time and burst time are given below. If the
CPU scheduling policy is shortest remaining time first, calculate the average waiting time
and average turn -around time.
P1 0 7
P2 1 5
P3 2 3
P4 3 1
P5 4 2
P6 5 1
Solution:
Gantt chart:
P1 19 19 – 0 = 19 19 – 7 = 12
P2 13 13 – 1 = 12 12 – 5 = 7
P3 6 6–2=4 4–3=1
P4 4 4–3=1 1–1=0
P5 9 9–4=5 5–2=3
P6 7 7–5=2 2–1=1
63
Collected by Bipin Timalsina
Unit-2/OS
Exercises:
1. Consider the set of 3 processes whose arrival time and burst time are given below. If
the CPU scheduling policy is SRTF, calculate the average waiting time and average
turn-around time. (Answer : 5, 12.33)
P1 0 9
P2 1 4
P3 2 9
2. Consider the set of following four processes whose arrival time and burst time are given
in milliseconds. Now schedule the processes using SJF and SRTF and also compare
the average waiting time. (Answer: SJF = 4, SRTF = 3)
3. Consider the set of 4 processes whose arrival time and burst time are given below. If
the CPU scheduling policy is SRTF, calculate the average waiting time and average
turn-around time.
64
Collected by Bipin Timalsina
Unit-2/OS
When a quantum time is over or process completes execution (whichever is earlier), it starts
new process. Selection of new process is as per FCFS scheduling algorithm.
This strategy can be implemented using FIFO queue.
If any process comes, or process releases CPU, or process is preempted. It is moved
to the end of the queue.
When CPU becomes free, a process from the first position in a queue is selected to run.
If there are n processes in the ready queue and the time quantum is q, then each process
gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more
than (n-1)q time units.
65
Collected by Bipin Timalsina
Unit-2/OS
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Advantages:
Disadvantages:
NOTES:
Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time
and average turn-around time.
Gantt Chart-
P1 13 13 – 0 = 13 13 – 5 = 8
P2 12 12 – 1 = 11 11 – 3 = 8
P3 5 5–2=3 3–1=2
P4 9 9–3=6 6–2=4
P5 14 14 – 4 = 10 10 – 3 = 7
67
Collected by Bipin Timalsina
Unit-2/OS
Problem-02:
Consider the set of 6 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is Round Robin with time quantum = 2, calculate the average waiting time and
average turn-around time.
Gantt chart-
P1 8 8–0=8 8–4=4
P2 18 18 – 1 = 17 17 – 5 = 12
P3 6 6–2=4 4–2=2
P4 9 9–3=6 6–1=5
P5 21 21 – 4 = 17 17 – 6 = 11
P6 19 19 – 6 = 13 13 – 3 = 10
68
Collected by Bipin Timalsina
Unit-2/OS
Problem-03:
Consider the set of 4 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is Round Robin with time quantum = 4, calculate the average waiting time and
average turn-around time.
Exercise:
Consider the set of 6 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is Round Robin with time quantum = 3, calculate the average waiting time and
average turn-around time. (Answer: 16, 21.33)
P1 5 5
P2 4 6
P3 3 7
P4 1 9
P5 2 2
P6 6 3
Priority Scheduling
Even on a PC with a single owner, there may be multiple processes, some of them more important
than others. For example, a daemon process sending electronic mail in the background should be
assigned a lower priority than a process displaying a video film on the screen in real time.
IDEA: Each process is assigned a priority, and the runnable process with the highest priority is
allowed to run.
69
Collected by Bipin Timalsina
Unit-2/OS
A priority is associated with each process, and the CPU is allocated to the process with the
highest priority
Priorities can be assigned to processes statically or dynamically
Priorities are generally indicated by some fixed range of numbers, such as 0 to 10 or 0 to
4,095. However, there is no general agreement on whether 0 is the highest or lowest
priority. Some systems use low numbers to represent low priority; others use low
numbers for high priority.
Equal-priority processes are scheduled in FCFS order.
Round-robin scheduling makes the implicit assumption that all processes are equally
important
An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of the
(predicted) next CPU burst. The larger the CPU burst, the lower the priority, and vice versa.
Two types:
Preemptive
A preemptive priority scheduling algorithm will preempt the CPU if the
priority of the newly arrived process is higher than the priority of the
currently running process.
Nonpreemptive
A nonpreemptive priority scheduling algorithm will simply put the new
process at the head of the ready queue.
70
Collected by Bipin Timalsina
Unit-2/OS
Example: (lower integer represents higher priority and, arrival time of all process is 0 and order
of arrival is P1 , P2 , P3 , P4 , P5 )
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Advantages:
Disadvantages:
Aging– gradually increase the priority of processes that wait in the system for a
long time.
For example, if priorities range from 127 (low) to 0 (high), we could periodically
(say, every second) increase the priority of a waiting process by 1. Eventually, even
a process with an initial priority of 127 would have the highest priority in the system
and would be executed. In fact, it would take a little over 2 minutes for a priority-
127 process to age to a priority-0 process.
71
Collected by Bipin Timalsina
Unit-2/OS
We know,
72
Collected by Bipin Timalsina
Unit-2/OS
Advantages:
Disadvantages:
Problem 01:
Consider the following 7 processes P1, P2, P3, P4, P5, P6 and P7. Schedule the process using
nonpreemptive priority scheduling algorithm and compute the average turn-around time and
average waiting time. (Lower integer represents higher priority)
P1 2 0 3
P2 6 2 5
P3 3 1 4
P4 5 4 2
P5 7 6 9
P6 4 5 4
P7 10 7 10
Solution:
Gantt chart:
73
Collected by Bipin Timalsina
Unit-2/OS
P1 2 0 3 3 3–0 =3 3–3 =0
P2 6 2 5 18 18 – 2 = 16 16 – 5 = 11
P3 3 1 4 7 7–1 = 6 6–4 =2
P4 5 4 2 13 13 – 4 = 9 9–2 =7
P5 7 6 9 27 27– 6 = 21 21 – 9 = 12
P6 4 5 4 11 11 – 5 = 6 6– 4 = 2
P7 10 7 10 37 37 – 7 = 30 30 – 10 =20
Problem-02:
Consider the set of 5 processes whose arrival time and burst time are given below. If the CPU
scheduling policy is priority preemptive, calculate the average waiting time and average turn-
around time. (Higher number represents higher priority)
74
Collected by Bipin Timalsina
Unit-2/OS
Solution:
Gantt Chart-
P1 15 15 – 0 = 15 15 – 4 = 11
P2 12 12 – 1 = 11 11 – 3 = 8
P3 3 3–2=1 1–1=0
P4 8 8–3=5 5–5=0
P5 10 10 – 4 = 6 6–2=4
Now,
75
Collected by Bipin Timalsina
Unit-2/OS
Another Example:
(Smaller value of priority represents higher priority arrival time of all process is 0)
P1 4 3
P2 5 2
P3 8 2
P4 7 1
P5 3 3
Run the process with the highest priority. Processes with the same priority run
round-robin
Gantt Chart with time quantum = 2
76
Collected by Bipin Timalsina
Unit-2/OS
Example: Separate queues might be used for foreground and background processes
77
Collected by Bipin Timalsina