Os Unit-2-1
Os Unit-2-1
CPU SCHEDULING
Basic Concepts
The objective of multiprogramming is to have some process running at all times, in order to
maximize CPU utilization. The idea of multiprogramming is relatively simple. Several processes
are kept in memory at one time. When one process has to wait, the operating system takes the
CPU away from that process and gives the CPU to another process. This pattern continues.
CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait.
CPU Scheduler
Selects from among the processes in memory that are ready to execute, and allocates the CPU to
one of them
4) Terminates
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler;
this involves: switching context, switching to user mode, jumping to the proper location in the
user program to restart that program.
Dispatch latency – time it takes for the dispatcher to stop one process and start another running
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Waiting time – amount of time a process has been waiting in the ready queue
Response time – amount of time it takes from when a request was submitted until the first
response is produced, not output (for time-sharing environment)
Optimization Criteria
Max throughput
Scheduling Algorithms
Priority scheduling
Round-robin Scheduling
Average waiting time under FCFS policy is generally not minimal and may vary
substantially if the process CPU burst times vary greatly.
Non-preemptive scheduling
Would allow one process to keep the CPU fro an extended period.
P1 24
P2 3
P3 3
P
30
0
3
623
1
Algorithm:
2. Get the arrival time and service time of each and every job
5. Compute each job and find the average waiting by dividing total compute each job and find
number of jobs.
Associate with each process the length of its next CPU burst. Use these lengths to schedule the
process with the shortest time. Two schemes:
1) Non-Preemptive – once CPU given to the process it cannot be preempted until completes its
CPU burst.
2) Preemptive – if a new process arrives with CPU burst length less than remaining time of
current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First
(SRTF).
SJF is optimal – gives minimum average waiting time for a given set of processes.
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
P1 P3 P2 P4
0 3 7 8 12 16
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
P1 P2 P3 P2 P4 P1
0 11 16
2 4 5 7
Algorithm:
6) Compute each job and find the average waiting by dividing total compute each job and find
number of jobs.
Priority Scheduling
A priority number (integer) is associated with each process.
The CPU is allocated to the process with the highest priority (smallest integer º highest
priority).
– Preemptive
– nonpreemptive
SJF is a priority scheduling where priority is the predicted next CPU burst time.
Algorithm:
2) Get the arrival time, service time and priority of every jobs
4) While processing the jobs according to the started time and the service time of the jobs being
processed currently to the waiting time for each job
5) Compute the total waiting time by adding individual waiting time of the each job and find the
arrange time of job.
• Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds.
After this time has elapsed, the OS preempts the process and moves it to the end of the
ready queue, timer set to interrupt in 1 quantum and new process is dispatched.
• 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 a time. No process waits
more than (n-1)q time units.
• Performance
P1 53
P2 17
P3 68
P4 24
Algorithm
2) Get the arrival time, service time and time slice of each job.
4) Repeat the above steps in till all the jobs are completely serviced.
Multilevel Queue
• Ready queue is partitioned into separate queues: foreground (interactive) and background
(batch).
• Each has different response-time requirements.
• Each queue has its own scheduling algorithm, foreground – RR and background – FCFS.
– Fixed priority scheduling; (i.e., serve all from foreground then from background).
Possibility of starvation.
– Time slice – each queue gets a certain amount of CPU time which it can schedule
amongst its processes; i.e., 80% to foreground in RR and 20% to background in
FCFS.
Example: Multilevel Queue Scheduling with 5 queues - scheduling within each queue and
– aging can be implemented this way – if a process has been waiting too long it can
be moved to a higher priority queue.
– If a process uses too much CPU time it can be moved into a lower priority queue.
– number of queues
– scheduling algorithms for each queue
– method used to determine which queue a process will enter when that process
needs service.
Three queues:
o Q2 – FCFS
Scheduling
o A new job enters queue Q0 which is served FCFS. When it gains CPU, job
receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to
queue Q1.
Multiple-Processor Scheduling
Load sharing
Asymmetric multiprocessing – only one processor accesses the system data structures,
alleviating the need for data sharing.
Real-Time Scheduling
Hard real-time systems – required to complete a critical task within a guaranteed amount of
time.
Soft real-time computing – requires that critical processes receive priority over less fortunate
ones.
Algorithm Evaluation
Maximum CPU utilization under the constraint that the maximum response time is 1
second.
Critical Section is the part of a program which tries to access shared resources
All the other processes have to wait to execute in their critical sections.
The critical section cannot be executed by more than one process at the same time; operating
system faces the difficulties in allowing and disallowing the processes from entering the critical
section.
Mutual Exclusion
Mutual exclusion implies that only one process can be inside the critical section at any
time. If any other processes require the critical section, they must wait until it is free.
Progress
Progress means that if a process is not using the critical section, then it should not stop
any other process from accessing it. In other words, any process can enter a critical
section if it is free.
Bounded Waiting
Bounded waiting means that each process must have a limited waiting time. Itt should not
wait endlessly to access the critical section.
do {
flag[i] = true;
turn = j;
/* critical section */
flag[i] = false;
int turn;
boolean flag[2];
The algorithm uses two variables, flag and turn . A flag[n] value of true indicates that the
process n wants to enter the critical section. Entrance to the critical section is granted for process
P0 if P1 does not want to enter its critical section or if P1 has given priority to P0 by
setting turn to 0
The algorithm satisfies the three essential criteria to solve the critical section problem, provided
that changes to the variables turn , flag[0] , and flag[1] propagate immediately and atomically.
The while condition works even with preemption. The three criteria are mutual exclusion,
progress, and bounded waiting.
Synchronization hardware:
In Synchronization hardware, we explore several more solutions to the critical-section problem
using techniques ranging from hardware to software based APIs available to application
programmers. These solutions are based on the premise of locking; however, the design of such
locks can be quite sophisticated.
There are three algorithms in the hardware approach of solving Process Synchronization
problem:
boolean rv = target;
target = true;
return rv;
while(1){
while (TestAndSet(lock));
critical section
lock = false;
remainder section
}
2. Swap:
Swap algorithm is a lot like the TestAndSet algorithm. Instead of directly setting lock to true
in the swap function, key is set to true and then swapped with lock. So, again, when a process
is in the critical section, no other process gets to enter it as the value of lock is true
Swap Pseudocode –
// Shared variable lock initialized to false
boolean lock;
Individual key;
boolean temp = a;
a = b;
b = temp;
}
while (1){
key = true;
while(key)
swap(lock,key);
critical section
lock = false;
remainder section
}
3. Unlock and Lock :
Unlock and Lock Algorithm uses TestAndSet to regulate the value of lock but it adds another
value, waiting[i], for each process which checks whether or not a process has been waiting. A
ready queue is maintained with respect to the process in the critical section. All the processes
coming in next are added to the ready queue with respect to their process number, not
necessarily sequentially.
Unlock and Lock Pseudocode –
// Shared variable lock initialized to false
boolean lock;
Individual key;
Individual waiting[i];
while(1){
waiting[i] = true;
key = true;
key = TestAndSet(lock);
critical section
j = (i+1) % n;
while(j != i && !waiting[j])
j = (j+1) % n;
if(j == i)
lock = false;
else
waiting[j] = false;
remainder section
}
Semaphores:
Semaphores are integer variables that are used to solve the critical section problem by using two
atomic operations, wait and signal that are used for process synchronization.
The definitions of wait and signal are as follows −
Wait
The wait operation decrements the value of its argument S, if it is positive. If S is
negative or zero, then no operation is performed.
wait(S)
while (S<=0);
S--;
Signal
The signal operation increments the value of its argument S.
signal(S)
S++;
Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary semaphores.
Details about these are given as follows −
Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate the resource access, where the semaphore count is the
number of available resources. If the resources are added, semaphore count automatically
incremented and if the resources are removed, the count is decremented.
Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to 0 and
1. The wait operation only works when the semaphore is 1 and the signal operation
succeeds when semaphore is 0. It is sometimes easier to implement binary semaphores
than counting semaphores.
Advantages of Semaphores
Some of the advantages of semaphores are as follows −
Semaphores allow only one process into the critical section. They follow the mutual
exclusion principle strictly and are much more efficient than some other methods of
synchronization.
There is no resource wastage because of busy waiting in semaphores as processor time is
not wasted unnecessarily to check if a condition is fulfilled to allow a process to access
the critical section.
Semaphores are implemented in the machine independent code of the microkernel. So
they are machine independent.
Disadvantages of Semaphores
Some of the disadvantages of semaphores are as follows −
Semaphores are complicated so the wait and signal operations must be implemented in
the correct order to prevent deadlocks.
Semaphores are impractical for last scale use as their use leads to loss of modularity. This
happens because the wait and signal operations prevent the creation of a structured layout
for the system.
Semaphores may lead to a priority inversion where low priority processes may access the
critical section first and high priority processes later.
Properties of Semaphores
Example of Use
Process i
begin
P(mutex);
execute CS;
V(mutex);
End;
These problems are used for testing nearly every new proposed synchronization scheme.
• The Bounded Buffer Problem (also called the The Producer-Consumer Problem)
These problems are used to test nearly every newly proposed synchronization scheme or
primitive.
process producer
while (true)
{
empty.acquire();
mutex.acquire();
// produce
mutex.release();
full.release();
process consumer
while (true) {
full.acquire();
mutex.acquire();
// consume
mutex.release();
empty.release();
The semaphore mutex provides mutual exclusion for access to the buffer
Each process is classified as either a reader or writer.Multiple readers may access the file
simultaneously.
A writer must have exclusive access (i.e., cannot share with either a reader or another writer).A
solution gives priority to either readers or writers.
• readers' priority: no reader is kept waiting unless a writer has already obtained permission to
access the database
• writers' priority: if a writer is waiting to access the database, no new readers can start reading.
A solution to either version may cause starvation in the readers' priority version, writers may
starving the writers' priority version, readers may starve
Semaphore mutex = 1;
Semaphore db = 1;
int readerCount = 0;
process writer {
db.acquire();
// write
db.release();
process reader {
// protecting readerCount
mutex.acquire();
++readerCount;
if (readerCount == 1)
db.acquire();
mutex.release();
// read
// protecting readerCount
mutex.acquire();
--readerCount;
if (readerCount == 0)
db.release;
mutex.release();
readerCount is a <cs> over which we must maintain control and we use mutex to do so.
n philosophers sit around a table thinking and eating. When a philosopher thinks she does not
interact with her colleagues. Periodically, a philosopher gets hungry and tries to pick up the
chopstick on his left and on his right. A philosopher may only pick up one chopstick at a time
and, obviously, cannot pick up a chopstick already in the hand of neighbor philosopher.
A semaphore solution:
process philosopher_i
while (true)
{
chopstick[i].acquire();
chopstick[(i+1) % 5].acquire();
// eat
chopstick[i].release();
chopstick[(i+1) % 5].release();
// think
This solution guarantees no two neighboring philosophers eat simultaneously, but has the
possibility of creating a deadlock.
Atomic Transactions:
System Model
Log-based Recovery
Checkpoints
Assures that operations happen as a single logical unit of work, in its entirety, or not at all
Volatile storage – information stored here does not survive system crashes
Log-Based Recovery:
Log on stable storage, each log record describes single transaction write
operation, including
Transaction name
Old value
New value
Log entry must reach stable storage before operation on data occurs
Using the log, system can handle any volatile memory errors
Checkpoints:
Checkpoint scheme:
1. Output all log records currently in volatile storage to stable storage
Serializability:
Nonserial Schedule:
S is conflict serializable
Locking Protocol:
Locks
Shared – Ti has shared-mode lock (S) on item Q, Ti can read Q but not write Q
Timestamp-based Protocols:
DEAD LOCKS:
In a multiprogramming environment, several processes may compete for a finite number of
resources. A process requests resources, if the resources are not available at the time, the process
enters a wait state. It may happen that waiting processes will never again change state, because
other waiting processes holds the resources they have requested. This is known as a deadlock
SYSTEM MODEL:
A system consists of a finite number or resources to be distributed among a number of
competing processes. The resources are partitioned into several types, each of which consists of
some number of identical instances.
Under the normal mode of operation, a process may utilize a resource in only the following
sequence:
Request: If the request cannot be granted immediately (for example, if the resource is a printer,
the process can acquire the resource.
Use: The process can operate on the resource (for, example, if the resource is a printer, the
process can print on the printer).
A deadlock situation can arise if the following four conditions hold simultaneously in
a system:
Mutual exclusion: At least one resource must be held in a non-sharable mode; that is, only one
process at a time can use the resource. If another process requests that resource, the requesting
process must be delayed until the resource has been released.
Hold and wait: There must exist a process that is holding at least one resource and is waiting to
acquire additional resources that are currently being held by other processes.
No preemption resources: Cannot be preempted; that is, the process holding it after that process
has completed its task can release a resource only voluntarily by the process holding it, after that
process has completed its task.
Circular wait: There must exist a set {Po,P1,…,Pn} of waiting processes such that P0 is
waiting for a resource that is held by P1,P1 is waiting for a resource that is held by P2,…,Pn-1 is
waiting for a resource that held by Pn and Pn is waiting for a resource that is held by Po.
A set of vertices V and a set of edges E.
P = {P1, P2, …, Pn}, the set consisting of all the processes in the system.
R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.
Process
Pi requests instance of Rj
Pi
Pi is holding an instance of Rj
Pi
Resource Allocation Graph With A Deadlock
We can allow the system to enter a deadlock state and then recover
We can ignore the problem all together, and pretend that deadlocks never occur in the
system. This solution is the one used by most operating systems, including UNIX.
Deadlock prevention is a set of methods for ensuring that at least one of the necessary
conditions cannot hold.
Deadlock avoidance, on the other hand, requires that the operating system be given in
advance additional information concerning which resources a process will request and
use during its lifetime.
Deadlock Prevention
Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources.
Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any
other resources.
Require process to request and be allocated all its resources before it begins
execution, or allow process to request resources only when the process has none.
No Preemption –
If a process that is holding some resources requests another resource that cannot
be immediately allocated to it, then all resources currently being held are released.
Preempted resources are added to the list of resources for which the process is
waiting.
Process will be restarted only when it can regain its old resources, as well as the
new ones that it is requesting.
Circular Wait –impose a total ordering of all resource types, and require that each process
requests resources in an increasing order of enumeration.
Deadlock Avoidance
The simplest and most useful model requires that each process declare the maximum number of
resources of each type that it may need.
SAFE STATE
A state is safe if the system can allocate resources to each process (up to its maximum) in some
order and still avoid deadlock.
More formally, a system is in a safe state only if there exist a safe sequence.
A sequence of processes <P1,P2,…,Pn> is a safe sequence for the current allocation state if, for
each Pi, the resources that Pi can still request can be satisfied by the currently available resources
plus the resources held by all the Pj, with j<i.
If a system is in safe state Þ no deadlocks.
Claim edge indicates the process Pi may request resource Rj at some time in the future. The
edge resembles a request edge in direction, but is represented by a dashed line. The resource
allocation graph for deadlock avoidance is shown below.
BANKER’S ALGORITHM
Allocation: An nXm matrix defines the number of resources of each type currently
allocated to each process.
Need: An nXm matrix indicates the remaining resource need of each process.
Safety Algorithm
Work = Available
3.Work=Work+Allocationi
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.
Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of
resource type Rj.
1.If Requesti £ Needi go to step 2. Otherwise, raise error condition, since process has exceeded
its maximum claim.
2.If Requesti £ Available, go to step 3. Otherwise Pi must wait, since resources are not
available.
3.Pretend to allocate requested resources to Pi by modifying the state as follows:
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.
Check that Request £ Available (that is, (1,0,2) £ (3,3,2) Þ true.
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 1 600
P3 2 1 1 011
P4 0 0 2 431
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety
requirement.
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
Available: A vector of length m indicates the number of available resources of each type.
Allocation: An n x m matrix defines the number of resources of each type currently allocated to
each process.
Request: An n x m matrix indicates the current request of each process. If Request [ij] = k, then
process Pi is requesting k more instances of resource type. Rj.
Detection Algorithm
(a)Finish[i] == false
(b)Requesti £ Work
4.If Finish[i] == false, for some i, 1 £ i £ n, then the system is in deadlock state. Moreover, if
Finish[i] == false, then Pi is deadlocked.
Algorithm requires an order of O(m x n2) operations to detect whether the system is in
deadlocked state.
P1 200 202
P2 303 000
P3 211 100
P4 002 002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all
Request
ABC
P0 000
P1 201
P2 001
P3 100
P4 002
State of system
Can reclaim resources held by process P0, but insufficient resources to fulfill other
processes; requests.
Detection-Algorithm Usage
When, and how often, to invoke depends on: How often a deadlock is likely to occur?
How many processes will need to be rolled back? one for each disjoint cycle
If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and
so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.
Deadlock Recovery
Process Termination
How long process has computed, and how much longer to completion.
Resource Preemption
Rollback – return to some safe state, restart process for that state.
Starvation – same process may always be picked as victim, include number of rollback
in cost factor.
Combine the three basic approaches prevention, avoidance and detection allowing the use of the
Use most appropriate technique for handling deadlocks within each class.