OS_Unit-2_23-24 (1)
OS_Unit-2_23-24 (1)
1/87
Operating Systems - Unit-II February 23, 2024 1 / 87
Unit-II Syllabus
CPU Scheduling
Scheduling Criteria
Scheduling Algorithms
Multiple -Processor Scheduling
System call interface for process management-fork, exit, wait, waitpid,
exec.
Process Management and Synchronization
The Critical Section Problem
Synchronization Hardware
Semaphores
Classical Problems of Synchronization
Critical Regions
Monitors
2/87
Operating Systems - Unit-II February 23, 2024 2 / 87
Scheduling Algorithms
Basic Concepts
3/87
Operating Systems - Unit-II February 23, 2024 3 / 87
Scheduling Algorithms CPU Scheduler
CPU Scheduler
4/87
Operating Systems - Unit-II February 23, 2024 4 / 87
Scheduling Algorithms CPU Scheduler
Dispatcher
5/87
Operating Systems - Unit-II February 23, 2024 5 / 87
Scheduling Algorithms CPU Scheduler
Scheduling Criteria
6/87
Operating Systems - Unit-II February 23, 2024 6 / 87
Scheduling Algorithms CPU Scheduler
7/87
Operating Systems - Unit-II February 23, 2024 7 / 87
Scheduling Algorithms Scheduling Algorithms
Scheduling Algorithms
8/87
Operating Systems - Unit-II February 23, 2024 8 / 87
Scheduling Algorithms Scheduling Algorithms
FCFS Scheduling
9/87
Operating Systems - Unit-II February 23, 2024 9 / 87
Scheduling Algorithms Scheduling Algorithms
FCFS Scheduling
10/87
Operating Systems - Unit-II February 23, 2024 10 / 87
Scheduling Algorithms Scheduling Algorithms
SJF Scheduling
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest time
SJF is optimal – gives minimum average waiting time for a given set
of processes
The difficulty is knowing the length of the next CPU request
Could ask the user
Preemptive version called shortest-remaining-time-first
11/87
Operating Systems - Unit-II February 23, 2024 11 / 87
Scheduling Algorithms Scheduling Algorithms
SJF Scheduling
12/87
Operating Systems - Unit-II February 23, 2024 12 / 87
Scheduling Algorithms Scheduling Algorithms
13/87
Operating Systems - Unit-II February 23, 2024 13 / 87
Scheduling Algorithms Scheduling Algorithms
Priority Scheduling
14/87
Operating Systems - Unit-II February 23, 2024 14 / 87
Scheduling Algorithms Scheduling Algorithms
Priority Scheduling
15/87
Operating Systems - Unit-II February 23, 2024 15 / 87
Scheduling Algorithms Scheduling Algorithms
RoundRobin Scheduling
Each process gets a small unit of CPU time (time quantum q),
usually 10-100 milliseconds. After
this time has elapsed, the process is preempted and added to the end
of the ready queue.
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.
Timer interrupts every quantum to schedule next process
Performance
q large - FIFO
q small - q must be large with respect to context switch, otherwise
overhead is too high
16/87
Operating Systems - Unit-II February 23, 2024 16 / 87
Scheduling Algorithms Scheduling Algorithms
RR Scheduling
17/87
Operating Systems - Unit-II February 23, 2024 17 / 87
Scheduling Algorithms Scheduling Algorithms
18/87
Operating Systems - Unit-II February 23, 2024 18 / 87
Scheduling Algorithms Scheduling Algorithms
19/87
Operating Systems - Unit-II February 23, 2024 19 / 87
Scheduling Algorithms Scheduling Algorithms
20/87
Operating Systems - Unit-II February 23, 2024 20 / 87
Scheduling Algorithms Scheduling Algorithms
21/87
Operating Systems - Unit-II February 23, 2024 21 / 87
Scheduling Algorithms Thread Scheduling
Thread Scheduling
22/87
Operating Systems - Unit-II February 23, 2024 22 / 87
Scheduling Algorithms Thread Scheduling
PThread Scheduling
23/87
Operating Systems - Unit-II February 23, 2024 23 / 87
Scheduling Algorithms Multiple Processor Scheduling
24/87
Operating Systems - Unit-II February 23, 2024 24 / 87
Scheduling Algorithms Multiple Processor Scheduling
25/87
Operating Systems - Unit-II February 23, 2024 25 / 87
Scheduling Algorithms Multiple Processor Scheduling
Multicore Processors
26/87
Operating Systems - Unit-II February 23, 2024 26 / 87
Scheduling Algorithms Multiple Processor Scheduling
27/87
Operating Systems - Unit-II February 23, 2024 27 / 87
Scheduling Algorithms Real-Time CPU Scheduling
28/87
Operating Systems - Unit-II February 23, 2024 28 / 87
Scheduling Algorithms Real-Time CPU Scheduling
29/87
Operating Systems - Unit-II February 23, 2024 29 / 87
Scheduling Algorithms Real-Time CPU Scheduling
30/87
Operating Systems - Unit-II February 23, 2024 30 / 87
Scheduling Algorithms Real-Time CPU Scheduling
31/87
Operating Systems - Unit-II February 23, 2024 31 / 87
Scheduling Algorithms Real-Time CPU Scheduling
LINUX Scheduling
LINUX Scheduling
33/87
Operating Systems - Unit-II February 23, 2024 33 / 87
Scheduling Algorithms Real-Time CPU Scheduling
LINUX Scheduling
34/87
Operating Systems - Unit-II February 23, 2024 34 / 87
Scheduling Algorithms Real-Time CPU Scheduling
Windows Scheduling
35/87
Operating Systems - Unit-II February 23, 2024 35 / 87
Systemcalls
Systemcalls
Fork()
Exec()
Wait()
Exit()
36/87
Operating Systems - Unit-II February 23, 2024 36 / 87
Systemcalls
Fork()
A system call that creates a new process identical to the calling one
Makes a copy of text, data, stack, and heap – Starts executing on that
new copy
Uses of fork()
To create a parallel program with multiple processes
(E.g. Web server forks a process on each HTTP request)
To launch a new program using exec() family of functions (E.g. Linux
shell forks an ‘ls’ process)
37/87
Operating Systems - Unit-II February 23, 2024 37 / 87
Systemcalls
38/87
Operating Systems - Unit-II February 23, 2024 38 / 87
Systemcalls
Exec()
39/87
Operating Systems - Unit-II February 23, 2024 39 / 87
Systemcalls
Wait()
40/87
Operating Systems - Unit-II February 23, 2024 40 / 87
Systemcalls
Exit()
41/87
Operating Systems - Unit-II February 23, 2024 41 / 87
Process Synchrinization
Process Synchrinization
42/87
Operating Systems - Unit-II February 23, 2024 42 / 87
Process Synchrinization
43/87
Operating Systems - Unit-II February 23, 2024 43 / 87
Process Synchrinization
Race Condition
44/87
Operating Systems - Unit-II February 23, 2024 44 / 87
Process Synchrinization Critical Section Problem
45/87
Operating Systems - Unit-II February 23, 2024 45 / 87
Process Synchrinization Critical Section Problem
Critical Section
46/87
Operating Systems - Unit-II February 23, 2024 46 / 87
Process Synchrinization Critical Section Problem
47/87
Operating Systems - Unit-II February 23, 2024 47 / 87
Process Synchrinization Critical Section Problem
48/87
Operating Systems - Unit-II February 23, 2024 48 / 87
Process Synchrinization Critical Section Problem
Critical-Section Handling in OS
49/87
Operating Systems - Unit-II February 23, 2024 49 / 87
Process Synchrinization Critical Section Problem
Peterson’s Solution
50/87
Operating Systems - Unit-II February 23, 2024 50 / 87
Process Synchrinization Critical Section Problem
Algorithm for Pi
51/87
Operating Systems - Unit-II February 23, 2024 51 / 87
Process Synchrinization Critical Section Problem
Petersons Solution
52/87
Operating Systems - Unit-II February 23, 2024 52 / 87
Process Synchrinization Synchronization Hardware
Synchronization Hardware
53/87
Operating Systems - Unit-II February 23, 2024 53 / 87
Process Synchrinization Synchronization Hardware
54/87
Operating Systems - Unit-II February 23, 2024 54 / 87
Process Synchrinization Synchronization Hardware
1 Executed atomically
2 Returns the original value of passed parameter
3 Set the new value of passed parameter to “TRUE”.
55/87
Operating Systems - Unit-II February 23, 2024 55 / 87
Process Synchrinization Synchronization Hardware
56/87
Operating Systems - Unit-II February 23, 2024 56 / 87
Process Synchrinization Synchronization Hardware
1 Executed atomically
2 Returns the original value of passed parameter “value”
3 Set the variable “value” the value of the passed parameter
“new value” but only if “value” ==“expected”. That is, the swap
takes place only under this condition.
57/87
Operating Systems - Unit-II February 23, 2024 57 / 87
Process Synchrinization Synchronization Hardware
58/87
Operating Systems - Unit-II February 23, 2024 58 / 87
Process Synchrinization Synchronization Hardware
59/87
Operating Systems - Unit-II February 23, 2024 59 / 87
Process Synchrinization Synchronization Hardware
60/87
Operating Systems - Unit-II February 23, 2024 60 / 87
Process Synchrinization Synchronization Hardware
Mutex Locks
61/87
Operating Systems - Unit-II February 23, 2024 61 / 87
Process Synchrinization Synchronization Hardware
62/87
Operating Systems - Unit-II February 23, 2024 62 / 87
Process Synchrinization Semaphore
Semaphore
Synchronization tool that provides more sophisticated ways (than
Mutex locks) for process to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible (atomic) operations
wait() and signal()
Originally called P() and V()
63/87
Operating Systems - Unit-II February 23, 2024 63 / 87
Process Synchrinization Semaphore
Semaphore Usage
Semaphore Implementation
Must guarantee that no two processes can execute the wait() and
signal() on the same semaphore at the same time
Thus, the implementation becomes the critical section problem where
the wait and signal code are placed in the critical section
Could now have busy waiting in critical section implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections and
therefore this is not a good solution
65/87
Operating Systems - Unit-II February 23, 2024 65 / 87
Process Synchrinization Semaphore
66/87
Operating Systems - Unit-II February 23, 2024 66 / 87
Process Synchrinization Semaphore
67/87
Operating Systems - Unit-II February 23, 2024 67 / 87
Process Synchrinization Semaphore
69/87
Operating Systems - Unit-II February 23, 2024 69 / 87
Process Synchrinization Classical Problems of Synchronization
Bounded-Buffer Problem
70/87
Operating Systems - Unit-II February 23, 2024 70 / 87
Process Synchrinization Classical Problems of Synchronization
71/87
Operating Systems - Unit-II February 23, 2024 71 / 87
Process Synchrinization Classical Problems of Synchronization
Readers-Writers Problem
72/87
Operating Systems - Unit-II February 23, 2024 72 / 87
Process Synchrinization Classical Problems of Synchronization
Readers-Writers Problem
73/87
Operating Systems - Unit-II February 23, 2024 73 / 87
Process Synchrinization Classical Problems of Synchronization
74/87
Operating Systems - Unit-II February 23, 2024 74 / 87
Process Synchrinization Classical Problems of Synchronization
Dining-Philosophers Problem
76/87
Operating Systems - Unit-II February 23, 2024 76 / 87
Process Synchrinization Classical Problems of Synchronization
Deadlock handling
Allow at most 4 philosophers to be sitting simultaneously at the table.
Allow a philosopher to pick up the forks only if both are available
(picking must be done in a critical section.
Use an asymmetric solution – an odd-numbered philosopher picks up
first the left chopstick and then the right chopstick. Even-numbered
philosopher picks up first the right chopstick and then the left
chopstick.
77/87
Operating Systems - Unit-II February 23, 2024 77 / 87
Process Synchrinization Classical Problems of Synchronization
78/87
Operating Systems - Unit-II February 23, 2024 78 / 87
Process Synchrinization Monitors
Monitors
79/87
Operating Systems - Unit-II February 23, 2024 79 / 87
Process Synchrinization Monitors
Condition Variables
condition x, y;
Two operations are allowed on a condition variable:
x.wait() – a process that invokes the operation is suspended until
x.signal()
x.signal() – resumes one of processes (if any) that invoked x.wait()
If no x.wait() on the variable, then it has no effect on the variable
80/87
Operating Systems - Unit-II February 23, 2024 80 / 87
Process Synchrinization Monitors
81/87
Operating Systems - Unit-II February 23, 2024 81 / 87
Process Synchrinization Monitors
82/87
Operating Systems - Unit-II February 23, 2024 82 / 87
Process Synchrinization Monitors
83/87
Operating Systems - Unit-II February 23, 2024 83 / 87
Process Synchrinization Monitors
84/87
Operating Systems - Unit-II February 23, 2024 84 / 87
Process Synchrinization Monitors
85/87
Operating Systems - Unit-II February 23, 2024 85 / 87
Process Synchrinization Examples
Linux Synchronization
Linux:
Prior to kernel Version 2.6, disables interrupts to implement short
critical sections
Version 2.6 and later, fully preemptive
Linux provides:
Semaphores
atomic integers
spinlocks
reader-writer versions of both
On single-cpu system, spinlocks replaced by enabling and disabling
kernel preemption
86/87
Operating Systems - Unit-II February 23, 2024 86 / 87
Process Synchrinization Examples
Windows Synchronization
87/87
Operating Systems - Unit-II February 23, 2024 87 / 87