25
ating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

Operating System Concepts with Java – 7th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007

Processes and Their Scheduling

Page 2: Operating System Concepts with Java – 7 th 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

Page 3: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 4: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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?

Page 5: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

5.5 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006

Diagram of Process State

Page 6: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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.

Page 7: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

5.7 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006

Process Control Block (PCB): Pictorially

Page 8: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

5.8 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006

CPU Switch From Process to Process

Page 9: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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?

Page 10: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 11: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

5.11 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006

Flow of Processes in the OS

Page 12: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 13: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 14: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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.)

Page 15: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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.

Page 16: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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.

Page 17: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 18: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 19: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 20: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 21: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 22: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 23: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 24: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

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

Page 25: Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Processes and Their Scheduling

5.25 Silberschatz, Galvin and Gagne ©2007Operating System Concepts with Java – 7th Edition, Nov 15, 2006

Algorithm Evaluation

Evaluation of CPU schedulers by simulation