Chapter 6: CPU Scheduling Preemptive vs. Non-Preemptive Scheduling
Chapter 6: CPU Scheduling Preemptive vs. Non-Preemptive Scheduling
Non-preemptive
Chapter 6: CPU Scheduling
Scheduling
• Always want to have CPU working • Non-preemptive scheduling
• Usually many processes in ready queue – A new process is selected to run either
– Ready to run on CPU • when a process terminates or
– Consider a single CPU here • when an explicit system request causes a wait state
• Need strategies (e.g., I/O or wait for child)
– Selecting next process to run • Preemptive scheduling
– For allocating CPU time – New process selected to run also when
– What happens after a process does a system call? • An interrupt occurs
• Short-term scheduling • When new processes become ready
– Must not take much CPU time
1
SJF: Shortest Job First SJF Solution
P5 P3 P2 P4 P1
• The job with the shortest next CPU burst
time is selected 4 12 24 40 60
2
RR (Round Robin) Scheduling Solution
completes
completes completes completes completes
• Waiting times:
• Continue until all processes completed
• P1: 16 + 12 + 8 + 4 = 40
• Example
• P2: 4 + 16 + 12 = 32
– CPU job burst times & order in queue
• P1: 20, P2: 12, P3: 8, P4: 16, P5: 4 • P3: 8 + 16 = 24
– Draw Gantt chart, and compute average wait time • P4: 12 + 16 + 8 = 36
• P5: 16
• Average wait time: 29.6
3
Context Switching Issues Example
• These calculations have not taken context switch • Calculate average wait time for RR (round
duration into account
robin) scheduling, for
– In general, the context switch will take time
– Just like the CPU burst of a process takes time – Processes: P1: 24, P2: 4, P3: 4
– Response time, wait time etc. will be affected by – Assume this arrival order
context switch time
– Quantum = 4; context switch time = 1
• RR (Round Robin) & quantum duration
– The smaller the time quantum, the better the average
response time, but the more system overhead
– Want the quantum large compared to context switch
time
Synchronization Issues
Multiprocessor Scheduling
• Symmetric multiprocessing
• When a computer has more than one processor, • Involves synchronization of access to global ready
need a method of dispatching processes queue
• Types of ready queues – E.g., only one processor must execute a job at one time
– Local: dispatch to a specific processor
• Processors: CPU1, CPU2, CPU3, …
– Global: dispatch to any processor (“load sharing”)
• When a processor (e.g., CPU1) accesses the ready
• Processor/process relationship queue
– Run on only a specific processor (e.g., if it must use a – All other processors (CPU2, CPU3, …) must wait, and
device on that processor’s private bus) be denied access to the ready queue
– Run on any processor – The accessing processor (e.g., CPU1) will remove a
• Symmetric: Each processor does own scheduling process from the ready, and dispatch it on itself
• Master/slave: – Then that processor will make the ready queue
– Master processor dispatches processes to slaves available for use by the other CPU’s (CPU2, CPU3, …)
4
Pre-emptive Scheduling &
Operating System Design
• With pre-emptive CPU scheduling, a new process
can run when interrupt occurs
• What if thread A was in the middle of updating
data structures, and was put back in ready queue
– Either on disk or in shared memory
• If thread B also accesses same data structures
– May access data structures in an inconsistent state
• Need mechanisms for cooperative data access
– Both in Kernel
• Kernel, in general, needs to handle interrupts
• Don’t want to loose interrupts
• Real-time & multi-processor issues
• May need preemption in the kernel itself
– And by multiple processes/threads