Anda di halaman 1dari 66

Chapter 3 Processor Management

Operating Systems

How Does Processor Manager Allocate CPU(s) to Jobs?


Process Manager performs job scheduling, process scheduling and interrupt management. In single-user systems, processor is busy only when user is executing a job at all other times it is idle.
Processor management is simple.

In multiprogramming environment, processor must be allocated to each job in a fair and efficient manner.
Requires scheduling policy and a scheduling algorithm.

Active multiprogramming is a timesharing system which allows each program to use a preset slice of each CPU time. When the time expires, the job is interrupted and another job is allowed to begin. Passive multiprogramming allows each program to be serviced in turn, one after another by an interrupt. It ties up CPU time while all other jobs have to wait.

Job Scheduling vs. Process Scheduling Processor Manager has 2 sub-managers: Job Scheduler
in charge of job scheduling.

Process Scheduler
in charge of process scheduling.

Job Scheduler High-level scheduler. Selects jobs from a queue of incoming jobs. Places them in process queue (batch or interactive), based on each jobs characteristics. Goal is to put jobs in a sequence that uses all systems resources as fully as possible. Strives for balanced mix of jobs with large I/O interaction and jobs with lots of computation. Tries to keep most system components busy most of time.

Process Scheduler Low-level scheduler assigns the CPU to execute processes of those jobs placed on ready queue by Job Scheduler. After a job has been placed on the READY queue by Job Scheduler, Process Scheduler that takes over. Determines which jobs will get CPU, when, and for how long. Decides when processing should be interrupted. Determines queues job should be moved to during execution. Recognizes when a job has concluded and should be terminated.

Schedulers Swapped out processes

Disk

Ready Queue Job Scheduler I/O Process Scheduler

CPU End

I/O waiting Queue

CPU Cycles and I/O Cycles To schedule CPU, Process Scheduler uses common trait among most computer programs: they alternate between CPU cycles and I/O cycles.

CPU-bound process spends more time doing computations; few very long CPU bursts. I/O-bound process spends more time doing I/O than computations, many short CPU bursts.

I/O-bound process
I/O Module I/O Module I/O Module I/O Module I/O Module I/O Module Computation I/O Module I/O Module I/O Module I/O Module I/O Module Computation

CPU-bound process
Computation Computation Computation Computation Computation Computation I/O Module Computation Computation Computation Computation Computation Computation I/O Module

READ A,B C = A+B D = (A*B)C E = AB F = D/E WRITE A,B,C,D,E,F STOP END

I/O cycle CPU cycle

I/O cycle terminate execution

Middle-Level Scheduler In a highly interactive environment there s a third layer


called middle-level scheduler.

Removes active jobs from memory to reduce degree of multiprogramming and allows jobs to be completed faster

Middle-Level Scheduler

Swapped out processes

Disk

Ready Queue Job Scheduler Process Scheduler

CPU End

I/O

I/O waiting Queue

The Process
A process is basically a program in execution.
1. the executable program 2. current activity which include the value of the program counter & registers 3. such as subroutine parameters, return address, and temporary variables) 4. data section containing global variables.

Two or more processes may be associated with the same program.

Process State
Hol /N w Finish

R a y

Running

Waiting

Process State Hold/New: The process is being created. Running: Instructions are being executed. Waiting: The process is waiting for some event to occur. (wait for I/O) Ready: The process is waiting to be assigned to a process. (waiting for CPU) Finish : The process has finished execution.

Process State
N w/Hol

Sch ul r ispatch
R a y Running

Finish

A mitt Int rrupt I/O Compl tion


Waiting

Exit

I/O or v nt wait

Transition among Process States HOLD to READY


Job Scheduler using a predefined policy.

READY to RUNNING
Process Scheduler using some predefined algorithm

RUNNING back to READY


Process Scheduler according to some predefined time limit or other criterion.(quantum has expired)

RUNNING to WAITING Process Scheduler and is initiated by an instruction in the job. WAITING to READY Process Scheduler and is initiated by signal from I/O device manager that I/O request has been satisfied and job can continue. RUNNING to FINISHED Process Scheduler or Job Scheduler.

Process Control Block (PCB)

Each process in the O/S is represented by a PCB repository for information that vary from process to other process

Process Control Block (PCB)


Process State Program Counter CPU Registers CPU Scheduling Information Memory Management Information Accounting Information I/O Status Information
Ne /Hold/Running/ Wait/Ready/Finish Address for next instruction AC, IR and Stack pointer Process Priority

Base and Limit Register CPU Time Used I/O devices allocated

Process Control Block (PCB)


Process P0 executing Save State into PCB0 idle Reload State from PCB1 idle Process P1

Interrupts or I/O
Save State into PCB1 Reload State from PCB0

executing

idle executing

PCBs and Queuing PCB of job created when Job Scheduler accepts it Updated as job goes from beginning to termination. Queues use PCBs to track jobs. PCBs, not jobs, are linked to form queues. E.g., PCBs for every ready job are linked on READY queue; all PCBs for jobs just entering system are linked on HOLD queue. Queues must be managed by process scheduling policies and algorithms.

Ready Queue Queue Header Head Tail

P1
PCB1

P2
PCB2

Pn
PCBn

Process Scheduling Policies Before operating system can schedule all jobs in a multiprogramming environment, it must resolve three limitations of system: finite number of resources (such as disk drives, printers, and tape drives) some resources can t be shared once they re allocated (such as printers) some resources require operator intervention (such as tape drives).

A Good Scheduling Policy Maximize throughput by running as many jobs as possible in a given amount of time. Maximize CPU efficiency by keeping CPU busy 100 % of time. Ensure fairness for all jobs by giving everyone an equal amount of CPU and I/O time. Minimize response time by quickly turning around interactive requests. Minimize turnaround time by moving entire jobs in/out of system quickly. Minimize waiting time by moving jobs out of READY queue as quickly as possible

Waiting Time ( ) = Finish Time Arrival Time CPU Cycle Turnaround Time (t) = Finish Time Arrival Time

Preemptive scheduling policy interrupts processing of a job and transfers the CPU to another job. (the Arrival process will interrupt the current process) Non-preemptive scheduling policy functions without external interrupts. Once a job captures processor and begins execution, it remains in RUNNING state uninterrupted until it issues an I/O request (natural wait) or until it is finished (exception for infinite loops). (current process cannot be preempted until completes its CPU cycle). (current process cannot be preempted until completes its CPU cycle).

Process Scheduling Algorithms First Come First Served (FCFS) non preemptive Shortest Job Next (SJN)- non preemptive Shortest Remaining Time First (SRT)
preemptive

Round Robin- preemptive Priority Scheduling- preemptive Priority Scheduling- non-preemptive

First Come First Served (FCFS) Non-preemptive. Handles jobs according to their arrival time -- the earlier they arrive, the sooner they re served. Simple algorithm to implement -- uses a FIFO queue. Good for batch systems; not so good for interactive ones. Turnaround time is unpredictable.

To draw timeline and to do calculations for process scheduling algorithms

Job A B C D E

Arrival Time 0 1 2 3 4

CPU Cycle 8 5 3 1 7

a. First Come First Serve (FCFS) Scheduling The Timeline / Gantt Chart for the schedule is:
Job A B C D E Arrival Time 0 1 2 3 4 CPU Cycle 8 5 3 1 7

A
0 8

B
13

D
16 17

E
24

a. First Come First Serve (FCFS) Scheduling The Timeline / Gantt Chart for the schedule is:

A
0 8

B
13

D
16 17

24 Waiting Time ( ) = Finish Time Arrival Time CPU Cycle (A) = 8 0 8 = 0 (B) = 13 1 5 = 7 (C) = 16 2 3 = 11

(D) = 17 3 1 = 13 (E) = 24 4 7 = 13
Average Waiting Time = Total Waiting Time / Total number of processes Average Waiting Time = 44 / 5 Average Waiting Time = 8.8

a. First Come First Serve (FCFS) Scheduling The Timeline / Gantt Chart for the schedule is:

A
0

8 13 16 17 24 Turnaround Time (t) = Finish Time Arrival Time

(A) = 8 0 = 8 (B) = 13 1 = 12 (C) = 16 2 = 14 (D) = 17 3 = 14 (E) = 24 4 = 20


Average Turnaround Time = Total Turnaround Time / Total number of processes Average Turnaround Time = 62 / 5 Average Turnaround Time = 13.6

Shortest Job Next (SJN)


Non-preemptive. Handles jobs based on length of their CPU cycle time. Use lengths to schedule process with shortest time. Optimal gives minimum average waiting time for a given set of processes. Optimal only when all of jobs are available at same time and the CPU estimates are available and accurate. Doesn t work in interactive systems because users don t estimate in advance CPU time required to run their jobs.

b. Shortest Job Next (SJN) Scheduling (Non Preemption) The Timeline / Gantt Chart for the schedule is:
Job A B C D E Arrival Time 0 1 2 3 4 CPU Cycle 8 5 3 1 7

A
0 8

D
9

C
12

B
17

E
24

A
0 8

D
9

C
12

B
17

E
24

Waiting Time ( ) = Finish Time Arrival Time CPU Cycle (A) = 8 0 8 = 0 (B) = 17 1 5 = 11 (C) = 12 2 3 = 7

(D) = 9 3 1 = 5 w(E) = 24 4 7 = 13
Average Waiting Time = Total Waiting Time / Total number of processes Average Waiting Time = 36 / 5 Average Waiting Time = 7.2

A
0 8

D
9

C
12

B
17

E
24

Turnaround Time (t) = Finish Time Arrival Time

w(A) = 8 0 = 8 w(B) = 17 1 = 16 w(C) = 12 2 = 10 w(D) = 9 3 = 6 w(E) = 24 4 = 20


Average Turnaround Time = Total Turnaround Time / Total number of processes Average Turnaround Time = 60 / 5 Average Turnaround Time = 12

Shortest Remaining Time (SRT)


Preemptive version of the SJN algorithm. Processor allocated to job closest to completion. This job can be preempted if a newer job in READY queue has a time to completion that's shorter. Can t be implemented in interactive system -requires advance knowledge of CPU time required to finish each job. SRT involves more overhead than SJN. OS monitors CPU time for all jobs in READY queue and performs context switching .

c. Shortest Remaining Time First (preemptive)

Process Arrival Time CPU Cycle A 0 8 70 B 1 5 40 C 2 3 20 D 3 10 E 4 70 A B C D C


0 1 2 3 4 6

B
10

A
17

E
24

Shortest Remaining Time First (preemptive)

A B C D C
0

10 17 6 24 1 2 3 4 Waiting Time (w) = Finish Time Arrival Time CPU Cycle w(A) = 17 0 8 = 9 w(B) = 10 1 5 = 4 w(C) = 6 2 3 = 1

w(D) = 4 3 1 = 0 w(E) = 24 4 7 = 13
Average Waiting Time = Total Waiting Time / Total number of processes Average Waiting Time = 27 / 5 Average Waiting Time = 5.4

Shortest Remaining Time First (preemptive)

A B C D C

10 17 0 1 2 3 4 6 24 Turnaround Time (t) = Finish Time Arrival Time

w(A) = 17 0 = 17 w(B) = 10 1 = 9 w(C) = 6 2 = 4 w(D) = 4 3 = 1 w(E) = 24 4 = 20


Average Turnaround Time = 51 / 5 Average Turnaround Time = 10.2

Shortest Remaining Time (preemptive)

Process Arrival Time CPU Cycle A 0 3 210 B 1 50 C 2 3 20 D 3 10 5 10 E AA A D C E C


0 1 2 3 4 5 6 8

B
13

Shortest Remaining Time (preemptive)

Process Arrival Time CPU Cycle A 0 3 B 1 5 C 2 3 D 3 1 5 1 E A


0 1 2

D C E C
3 4 5 6 8

B
13

Round Robin
Preemptive. Used extensively in interactive systems because its easy to implement. Isnt based on job characteristics but on a predetermined slice of time thats given to each job. yEnsures CPU is equally shared among all active processes and isnt monopolized by any one job. Time slice is called a time quantum ysize crucial to system performance (100 ms to 1-2 secs)

d. Round Robin

(Preemption)

The Timeline / Gantt Chart for the schedule is:


Job A B C D E Arrival Time 0 1 2 3 4 CPU Cycle 8 5 3 1 7

(Time Quantum = 3)

Process Arrival Time CPU Cycle A 0 8520 B 1 5 20 C 2 30 D 3 10 E 4 7 4 10 A


0 3

B
6

C
9

D
10

A
13

B A
16 18 20

E E
23 24

B
1

Ready Queue C D A E B A
2 3 4 6 13

E
16

E
23

A
0 3

B
6

C
9

D
10

A
13

B A
16 18 20

E E
23 24

Waiting Time (w) = Finish Time Arrival Time CPU Cycle w(A) = 20 0 8 = 12 w(B) = 18 1 5 = 12 w(C) = 9 2 3 = 4

w(D) = 10 3 1 = 6 w(E) = 24 4 7 = 13
Average Waiting Time = Total Waiting Time / Total number of processes Average Waiting Time = 47/ 5 Average Waiting Time = 9.4

A
0 3

B
6

C
9

D
10

A
13

B A
16 18 20

E E
23 24

Turnaround Time (t) = Finish Time Arrival Time

w(A) = 20 0 = 20 w(B) = 18 1 = 17 w(C) = 9 2 = 7 w(D) = 10 3 = 7 w(E) = 24 4 = 20


Average Turnaround Time = 71 / 5 Average Turnaround Time = 14.2

Round Robin (Preemption)


The Timeline / Gantt Chart for the schedule is:
Job A B C D E Arrival Time 0 0 2 3 4 CPU Cycle 8 5 3 1 7

(Time Quantum = 3)

Process Arrival Time CPU Cycle A 0 8520 B 0 5 20 C 2 30 D 3 10 E 4 7 4 10 A


0 3

B
6

C
9

D
10

A
13

B A
16 18 20

E E
23 24

B
0

Ready Queue C D A E B A
2 3 4 6 13

E
16

E
23

If Jobs CPU Cycle < Time Quantum If jobs last CPU cycle & job is finished, then all resources allocated to it are released & completed job is returned to user. If CPU cycle was interrupted by I/O request, then info about the job is saved in its PCB & it is linked at end of the appropriate I/O queue. Later, when I/O request has been satisfied, it is returned to end of READY queue to await allocation of CPU

Round Robin (Preemption)


The Timeline / Gantt Chart for the schedule is:
Job A B C D E Arrival Time 0 0 0 0 0 CPU Cycle 8 5 3 1 7

(Time Quantum = 3)

Process Arrival Time CPU Cycle A 0 8 B 0 5 C 0 3 D 0 1 E 0 7 A


0 3

B
6

C
9

D
10

E
13

A B E A E
16 18 21 23 24

B C D E
0

Ready Queue A B
3 6

E
13

A
16

E
21

Time Slices Should Be ... Long enough to allow 80 % of CPU cycles to run to completion. At least 100 times longer than time required to perform one context switch. Flexible - depends on the system.

Process

Arrival Time

Burst Time

P1 P2
Quantum = 12 P1
0

0 0

10 4
P2
10 14

Quantum = 6 P1
0 6

P2
10

P1
14

Quantum = 2 P1
0 2

P2
4

P1
6

P2
8

P1
10

P1
12

P1
14

e. Non-Preemptive Priority Process Arrival Time Burst Time Priority

P1 P2 P3 P4

0.0 2.0 2.0 5.0

7 4 1 4

9 10 9 11

Assume a larger no implies a higher priority

P1
0 7

P4
11

P2
15

P3
16

Priority Scheduling
Non-preemptive algorithm which is commonly used in batch systems. Gives preferential treatment to important jobs. Allows the program with the highest priority to be processed first and these high priority jobs are not interrupted until their CPU cycles (run times) are completed or a natural wait occurs. If two or more jobs with equal priority, then uses FCFS policy within the same priority group

Process

f. Preemptive Priority Arrival Time Burst Time Priority

P1 P2 P3 P4

0.0 2.0 2.0 5.0

750 410 10 40

9 10 9 11

Assume a larger no implies a higher priority

P1
0 2

P2
5

P4
9

P2
10

P1 P3
15 16

Context Switching Is Required by All Preemptive Algorithms When Job A is preempted All of its processing information must be saved in its PCB for later (when Job A s execution is continued). Contents of Job B s PCB are loaded into appropriate registers so it can start running again (context switch).

Later when Job A is once again assigned to processor, another context switch is performed. Info from preempted job is stored in its PCB. Contents of Job A s PCB are loaded into appropriate registers. Note: Natural wait is a common term used to identify an I/O request from a program in a multiprogramming environment that would cause a process to wait naturally before resuming execution.

Round Robin Process P1 P2 P3 P4

Burst Time 53 20 68 24

Eg. Time Quantum = 20ms and Context Switch =2ms


P1
0 20 22

P2
42 44

P3
64 66

P4
86

d. Shortest Remaining Time First (Preemption) The Timeline / Gantt Chart for the schedule is:
Job A B C D E Arrival Time 0 1 2 3 4 CPU Cycle 8 5 3 1 7

Draw a timeline for each of the following algorithms. Also calculate the average waiting time and average turnaround time for each of them using the table format below, if the context switching time takes 1 ms.

d. Round Robin

(Preemption)

The Timeline / Gantt Chart for the schedule is:


Job A B C D E Arrival Time 0 1 2 3 4 CPU Cycle 8 5 3 1 7

(Time Quantum = 3)

Progression Exercises Process Arrival Time CPU Cycle A 0 75 B 10 40 C 10 25 D 80 20 E 85 45 Shortest Job Next (SJN) A
0 75

C
100

D
120

B
160

E
205

Shortest Remaining Time First (preemptive)

Process Arrival Time CPU Cycle A 0 75 65 60 0 B 10 40 0 C 10 25 0 D 80 2015 0 E 85 45 0 A


0 10

C
35

A D D
75 80 85 100

E
145

A
205

Anda mungkin juga menyukai