Unit 2 Part 2 - Process Synchronization and CPU Scheduling
Unit 2 Part 2 - Process Synchronization and CPU Scheduling
Two general approaches are used to handle critical sections in operating systems:
preemptive kernels and nonpreemptive kernels.
• Suppose that all five philosophers become hungry at the same time and each grabs
her left chopstick. All the elements of chopstick will now be equal to 0.
• When each philosopher tries to grab her right chopstick, she will be delayed
forever.
• Reader Process
Dispatcher
• The dispatcher is the module that gives control of the CPUto the process
selected by the short-term scheduler.
• This function involves the following:
• Switching context
• Switching to user mode
• Jumping to the proper location in the user programt o restart that
program state.
• The time it takes for the dispatcher to stop one process and start another
running is known as the dispatch latency.
G ARUL SELVAN AP CSE EGSPEC 22
Scheduling Criteria
• CPU utilization. keep the CPU as busy as possible. Conceptually,
CPU utilization can range from 0 to 100 percent. In a real system, it
should range from 40 percent (for a lightly loaded system) to 90
percent (for a heavily loaded system).
• Throughput. If the CPU is busy executing processes, then work is
being done. One measure of work is the number of processes that
are completed per time unit, called throughput.
• Turnaround time. The important criterion is how long it takes to
execute that process. The interval from the time of submission of a
process to the time of completion is the turnaround time.
• Turnaround time is the sum of the periods spent waiting to get into
memory, waiting in the ready queue, executing on the CPU, and
doing I/O.
• Waiting time. Waiting time is the sum of the periods spent waiting
in the ready queue.
G ARUL SELVAN AP CSE EGSPEC 23
CPU Scheduling Algorithms
• CPU scheduling deals with the problem of deciding which of the
processes in the ready queue is to be allocated the CPU.
• First-Come, First-Served Scheduling (FCFS)
• Shortest Job First Scheduling (SJF).
• Priority Scheduling.
• Round-Robin Scheduling
• Multilevel Queue Scheduling
• Gantt chart, which is a bar chart that illustrates a particular schedule, including
the start and finish times of each of the participating processes:
• If the CPU burst of the currently running process is longer than 1 time quantum, the timer will
go off and will cause an interrupt to the operating system.
• A context switch will be executed, and the process will be put at the tail of the ready queue.
• The CPU scheduler will then select the next process in the ready queue.
• The average waiting time for this schedule. P1 waits for 6 milliseconds (10 - 4),
P2 waits for 4 milliseconds, and P3 waits for 7 milliseconds.
• Thus, the average waiting time is 17/3 = 5.66 milliseconds.
G ARUL SELVAN AP CSE EGSPEC 27
Shortest Job First Scheduling
• This algorithm associates with each process the length of the
process’s next CPU burst.
• When the CPU is available, it is assigned to the process that
has the smallest next CPU burst.
• If the next CPU bursts of two processes are the same, FCFS
scheduling is used to break the tie.
• It is also called shortest-next-CPU-burst algorithm, because
scheduling depends on the length of the next CPU burst of a
process, rather than its total length.
• 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.
G ARUL SELVAN AP CSE EGSPEC 28
Consider the following set of processes, with the length of the CPU burst given in
milliseconds
Gantt chart:
Gantt chart
• Nonpreemptive SJF scheduling would result in an average waiting time of 7.75 milliseconds.