Unit 3-1 Operating System
Unit 3-1 Operating System
milliseconds
If the processes arrive in the order P1, P2, P3, and are served in FCFS
order,
Gantt chart
The waiting time is
• 0 milliseconds for process P1,
• 24 milliseconds for process P2, and
• 27 milliseconds for process P3.
• The average waiting time is (0 + 24 + 27)/3 = 17 milliseconds
Example
If the processes arrive in the order P2, P3, P1, and are served in FCFS
order,
Gantt chart
The waiting time is
• 0 milliseconds for process P2,
• 3 milliseconds for process P3, and
• 6 milliseconds for process P1.
• The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds.
• The average waiting time under an FCFS policy is generally not
minimal and may vary substantially.
Convoy effect
All the other processes wait for the one big process to get off the CPU is
called Convoy effect.
• FCFS scheduling algorithm is nonpreemptive.
• Once the CPU has been allocated to a process, that process keeps the
CPU until it releases the CPU, either by terminating or by requesting
I/O.
• The FCFS algorithm is thus particularly troublesome for time-sharing
systems, where it is important that each user get a share of the CPU
at regular intervals. It would be disastrous to allow one process to
keep the CPU for an extended period.
Shortest-Job-First Scheduling
• Shortest Job First (SJF) is a type of disk scheduling algorithm
in the operating system in which the processor executes the
job first that has the smallest execution time.
• In the shortest Job First algorithm, the processes are
scheduled according to the burst time of these processes.
Example
Example
The waiting time is
• 3 milliseconds for process P1,
• 16 milliseconds for process P2,
• 9 milliseconds for process P3, and
• 0 milliseconds for process P4.
Thus, the average waiting time is (3 + 16 + 9 + 0)/4 = 7 milliseconds.
By using FCFS scheduling scheme, the average waiting time would be
10.25 milliseconds
• The SJF scheduling algorithm is provably optimal, in that it gives the
minimum average waiting time for a given set of processes.
• Moving a short process before a long one decreases the waiting time
of the short process more than it increases the waiting time of the
long process. Consequently, the average waiting time decreases.
• SJF algorithm can be either preemptive or nonpreemptive.
• The choice arises when a new process arrives at the ready queue
while a previous process is still executing.
• The next CPU burst of the newly arrived process may be shorter than
what is left of the currently executing process.
• A preemptive SJF algorithm will preempt the currently executing
process, whereas a nonpreemptive SJF algorithm will allow the
currently running process to finish its CPU burst.
• Preemptive SJF scheduling is sometimes called shortest-remaining-
time-first scheduling.
Non-Preemptive SJF
Step 3) At time = 3, process P4 will finish its execution. The burst time
of P3 and P1 is compared. Process P1 is executed because its burst
time is less compared to P3.
• Step 4) At time = 4, process P5 arrives and is added to the waiting
queue. P1 will continue execution.
2. Deadlock avoidance
• Consider a system with twelve magnetic tape drives and three processes:
P0, P1, and P2.
• Process P0 requires ten tape drives,
• process P1 may need as many as four tape drives, and
• process P2 may need up to nine tape drives.
• Suppose that, at time t0, process P0 is holding five tape drives, process P1
is holding two tape drives, and process P2 is holding two tape drives.
(Thus, there are three free tape drives.)
• At time t0, the system is in a safe state. The sequence <P1, P0, P2>
satisfies the safety condition.
• Process P1 can immediately be allocated all its tape drives and then
return them (the system will then have five available tape drives);
then process P0 can get all its tape drives and return them (the
system will then have ten available tape drives); and finally process P2
can get all its tape drives and return them (the system will then have
all twelve tape drives available).
Resource allocation graph algorithm
If no cycle exists, then the allocation of the resource will leave the
system in a safe state.
If a cycle is found, then the allocation will put the system in an
unsafe state.
• Suppose that P2 requests R2. Although R2 is currently free, we cannot
allocate it to P2, since this action will create a cycle in the graph.
• A cycle, as mentioned, indicates that the system is in an unsafe state.
If P1 requests R2, and P2 requests R1, then a deadlock will occur
n is the number of processes in the system and m is the number of resource types
Banker’s Algorithm(con..)
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize Work = Available and Finish[i] = false for i = 0, 1, ..., n − 1.
2. Find an index i such that both
• Finish[i] == false
• Needi ≤ Work
If no such i exists, go to step 4.
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.
Banker’s Algorithm(con..)
Resource-Request Algorithm
1. If Requesti ≤ Needi , go to step 2. Otherwise, raise an error condition,
since the process has exceeded its maximum claim.
2. If Requesti ≤ Available, go to step 3. Otherwise, Pi must wait, since
the resources are not available.
3. Have the system pretend to have allocated the requested resources
to process Pi by modifying the state as follows:
• Available = Available–Requesti ;
• Allocationi = Allocationi + Requesti ;
• Needi = Needi –Requesti ;
Step 1: Need = Max – Allocation
Safe sequence:
1.For process P0, Need = (3, 2, 1) and
Available = (2, 1, 0)
Need <=Available = False
• So, the system will move to the next process.
10. i=4 is selected as both Finish[4] = false and [0, 0, 2]<=[7, 2, 4].
Work =[7, 2, 4]+[0, 0, 2] =>[7, 2, 6] &
Finish = [true, true, true, true, true].
the system is not in a deadlocked state
Safe sequence <P0, P2, P3, P1, P4>
• Suppose now that process P2 makes one additional request for an
instance of type C. The Request matrix is modified as follows: