View
214
Download
1
Tags:
Embed Size (px)
Citation preview
Operating System Concepts with Java – 7th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007
Processes and Their Scheduling
5.2 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Topics for the Day: Processes, Scheduling and Synchronization
We will cover: Process Concept Process Scheduling Process Synchronization (with focus on mutual
exclusion and semaphores) Some Classic Problems in Concurrency
5.3 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Process Concept
An operating system executes a variety of programs: Batch system – jobs Time-shared systems – user programs or tasks
Process – a program in execution; process execution must progress in sequential fashion
Note: process (active entity) is different from program (passive entity). Several processes may be instances of the same program.
A process includes, e.g.: program counter Stack (what do you think this is used for?) data section
5.4 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Process State
A process state describes the current status of its execution. As a process executes, it changes state
new: The process is being created running: Instructions are being executed waiting: The process is waiting for some event to occur ready: The process is waiting to be assigned to a process terminated: The process has finished execution
How do processes change state and why?
5.5 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Diagram of Process State
5.6 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Process Control Block (PCB)
Information associated with each process by the OS Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information
A snapshot of the process in execution.
To be saved at CPU context switches.
5.7 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Process Control Block (PCB): Pictorially
5.8 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
CPU Switch From Process to Process
5.9 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Where do PCBs of Idle Processes Go?
PCBs come in and out of process scheduling queues Remind me:
What is a queue? What is a priority queue?
Which types of queues are common in an OS?
5.10 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Process Scheduling Queues
Job queue – set of all processes in the system
Ready queue – set of all processes residing in main memory, ready and waiting to execute
Device queues – set of processes waiting for an I/O device
Processes migrate among the various queues
5.11 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Flow of Processes in the OS
5.12 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Overview: CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms
5.13 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Basic Concepts and Assumptions Fact of Life: Maximum CPU
utilization obtained with multiprogramming scheduling!
CPU–I/O Burst Cycle Assumption: Process execution consists of a cycle of CPU execution and I/O wait
CPU burst distribution
5.14 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
CPU Scheduler
Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them
CPU scheduling decisions may take place when a process:1.Switches from running to waiting state2.Switches from running to ready state3.Switches from waiting to ready4.Terminates
Scheduling under 1 and 4 is nonpreemptive All other scheduling is preemptive. (Pretty much the
rule today, especially for real-time systems.)
5.15 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Typical Scheduling Criteria
CPU utilization – keep the CPU as busy as possible Throughput – # of processes that complete their execution
per time unit Turnaround time – amount of time to execute a particular
process 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 (important for time-sharing environment)
Question: Which one do we want to optimize? How? General framework: Combinatorial optimization.
5.16 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Optimization Criteria
Max CPU utilization Max throughput Min turnaround time Min waiting time (our sample criterion) Min response time
Fact of life 1: Selecting an appropriate scheduling algorithm is a major design decision. Often one needs to consider several criteria.
Fact of life 2: Proper mathematical analysis and
experimental evaluation are needed.
5.17 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
First-Come, First-Served (FCFS) Scheduling
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
Average waiting time? Can we do better? Pros and cons?
P1 P2 P3
24 27 300
5.18 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Shortest-Job-First (SJF) Scheduling
Idea: Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time
Two schemes: nonpreemptive – once CPU given to the process it
cannot be preempted until completes its CPU burst 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
5.19 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4 SJF (non-preemptive)
Average waiting time? What if we used preemptive scheduling?
Example of Non-Preemptive SJF
P1 P3 P2
73 160
P4
8 12
5.20 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)
Average waiting time? Recall that SJF is optimal. Should we look any further?
P1 P3P2
42 110
P4
5 7
P2 P1
16
5.21 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Priority Scheduling
A priority number (integer) is associated with each process
The CPU is allocated to the ready process with the highest priority (typically, smallest integer highest priority) Preemptive nonpreemptive
SJF is a priority scheduling where priority is the predicted next CPU burst time
Problem Starvation – low priority processes may never execute (example: LegOS scheduler)
Solution Aging – as time progresses increase the priority of the process in the ready queue
5.22 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Round Robin (RR)
Idea: Each process gets a small unit of CPU time (time quantum), 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.
Performance q large FIFO q small q must be large with respect to context
switch, otherwise overhead is too high
5.23 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24 The Gantt chart is:
Is avg waiting time good for RR? Typically, higher average turnaround than SJF, but better
response
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
5.24 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Algorithm Evaluation
Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload
Queueing models Implementation
5.25 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Algorithm Evaluation
Evaluation of CPU schedulers by simulation