Anda di halaman 1dari 17

CSC308 Operating Systems

Lab Exercise The CPU algorithms 16 January 2013 FCFS ALGORITHM


Exercise: The durations of the CPU bursts of P1, P2, and P3 are 19, 10, and 7 respectively. A. If Processes arrival time: P1 (0), P2 (2), P3 (4) find the waiting times and the average waiting time. B. If Processes arrival time: P3 (0), P2 (2), P1 (4) find the waiting times and the average waiting time.

Exercise: find the waiting times and the average waiting time.

Process P1 P2 P3 P4

Arrival time 0 1 2 3

Burst time 8 4 9 5

Average waiting time : [0 + (8-1) + (12- 2 ) + (21-3 )] / 4 = 8.75

Exercise: Assume you have the following jobs to execute with one processor, with the jobs arriving in the order listed here: Process 0 1 2 3 4 Burst time 80 20 10 20 50

A. Suppose a system uses FCFS scheduling. Create a Gantt chart illustrating the execution of these processes? B. What is the turnaround time for process p3? C. What is the average wait time for the processes?

a. The Gantt chart is as follows:

80

100

110 130

180

b. The turnaround time for process p3 is T.A. (p3) = T (p3) + T.A. (p2) = T (p3) + (T (p2) + T.A. (p1)) = T (p3) + (T (p2) + (T (p1) + T.A. (p0))) = T (p3) + (T (p2) + (T (p1) + T (p0))) = 20 + 10 + 20 + 80 = 130. c. Average waiting time calculation: Waiting Time for process p0 = 0 sec. p1 = 80 sec. p2 = 100 sec. p3 = 110 sec. p4 = 130 sec.

Hence the average waiting time = (0+80+100+110+130)/5 = 84 sec.

SJF Scheduling (Shortest Job First)


CPU is allocated to the process that has the smallest next CPU burst. Non-preemptive: once the CPU has been given to a process, it cannot be preempted until the process completes its CPU burst. Preemptive: when a new process Pi whose next CPU burst is less than the remaining CPU burst of the current process is inserted into the ready queue, the current process relinquishes the CPU involuntarily and the CPU is allocated to Pi. (Also known as Shortest-Remaining-Time First (SRTF) algorithm)

SJF (non-preemptive) examples


Exercise: Process Burst Time Arrival Start Wait Finish TA 1 2 3 4 6 8 7 3 0 0 0 0 3 16 9 0 3 16 9 0 9 24 16 3 9 24 16 3

Gantt chart:

average waiting time: (3+16+9+0)/4 = 7 average turnaround time: (9+24+16+3)/4 = 13


Exercise:
Process Burst Time Arrival Start Wait Finish TA 1 2 3 4

7 4 1 4

0 2 4 5

0 8 7 12

0 6 3 7

7 12 8 16

7 10 4 11

Gantt chart:

average waiting time: (0+(8-2)+(7-4)+(12-5))/4 = 4 average turnaround time: (7+(12-2)+(8-4)+(16-5))/4 = 8


Process Burst Time Arrival Start Wait Finish TA 1 2 3 4 5 10 29 3 7 12 0 0 0 0 0 10 32 0 3 20 10 32 0 3 20 20 61 3 10 32 20 61 3 10 32

Exercise:

Gantt chart:

average waiting time: (10+32+0+3+20)/5 = 13 average turnaround time: (10+39+42+49+61)/5 = 25.2

SRTF (preemptive) examples


Exercise:
Process Burst Time Arrival Start Wait Finish TA 1 2 3 4 8 4 9 5 0 1 2 3 0 1 17 5 9 0 15 2 17 5 26 10 17 4 24 7

Gantt chart:

Shortest job is runned first. So, P1 which is the first one arrives at 0ms, then P2 is executed fully, then P4, then P3. average waiting time: ((10-1)+0+(17-2)+(5-3))/4 = 6.5 average turnaround time: ((17-0)+(5-1)+(26-2)+(10-3))/4 = 13
Exercise:
Process Burst Time Arrival Start Wait Finish TA 1 2 3 7 4 1 0 2 4 0 2 4 9 1 0 16 7 5 16 5 1

11

Gantt chart:

average waiting time: ((11-2)+(7-4)+0+(7-5))/4 = 3 average turnaround time: (16+5+1+6)/4 = 7

Exercise: The durations of the CPU bursts of P1, P2, and P3 are 19, 10, and 7 respectively. P1, P2, and P3 have only one CPU burst. Processes arrival time: P1 (0), P2 (2), P3 (4). Find the waiting times and the average waiting time when it is non-preemptive, and when it is preemptive.

Analysis: P1 waiting time equals to the first execution waiting time which is 0 as P1 does not wait for execution (i.e. executed immediately on arrival) plus the second time of execution where we subtract the actual arrival time (19) minus the execution time earlier (2). P2 waiting time equals to the first execution waiting time which is 0 as P2 does not wait for execution (i.e. executed immediately on arrival on 2ms) plus the second time of execution where we subtract the actual arrival time (11) minus the execution time earlier (4). P3 waiting time equals to the first execution waiting time which is 0 as P3 does not wait for execution (i.e. executed immediately on arrival on 4ms).

Exercise: Assume you have the following jobs to execute with one processor, with the jobs arriving in the order listed here: Process 0 1 2 3 4 Burst time 80 20 10 20 50

A. Suppose a system uses SJF (as the book) scheduling. Create a Gantt chart illustrating the execution of these processes? B. What is the turnaround time for process p3? C. What is the average wait time for the processes?

a. The Gantt chart is:

10

30

50 100

180

b. The turnaround time for process P3 is = 100. c. Average waiting time calculation: Waiting time for process p0 = 0 sec. p1 = 10 sec. p2 = 30 sec. p3 = 50 sec. p4 = 100 sec.

Therefore, the average waiting time is = (0+10+30+50+100)/5 =38

Exercise: find the waiting times and the average waiting time.

Process P1 P2 P3 P4

Arrival time 0 1 2 3

Burst time 8 4 9 5

Priority Scheduling: Preemptive or Non-Preemptive


Exercise: The durations of the CPU bursts of P1, P2, and P3 are 19, 10, and 7 respectively. P1, P2, and P3 have only one CPU burst. Processes arrival time: P1 (0), P2 (2), P3 (4), and their priorities are 3 (lowest), 2, and 1 (highest), respectively. Find the waiting times and the average waiting time when it is non-preemptive, and when it is preemptive.

P1 arrives first because it has no other process to compare with. P1 executes for 2ms, then P2 arrives, then P3 arrives with higher priority. Then P3 completes, P2 is then running because of the priority, P1 then because of the lowest priority. Exercise: [because no arrival time is given, then preemptive and non preemptive are the same] Process 1 2 3 4 5 Burst Time 10 1 2 1 5 Priority 3 1 4 5 2 Arrival 0 0 0 0 0 Start 6 0 16 18 1 Wait 6 0 16 18 1 Finish 16 1 18 19 6 TA 16 1 18 19 6

Gantt chart:

average waiting time: (6+0+16+18+1)/5 = 8.2 average turnaround time: (1+6+16+18+19)/5 = 12

Round Robin (RR)


Exercise: if Quantum = 4, then find the waiting times and the average waiting time:

Process P1 P2 P3 P4

Arrival time 0 1 2 3

Burst time 8 4 9 5

WAITING TIME = FINAL TIME - ARRIVAL TIME

18.25

Analysis: P1 runs after p4 because P1 is runned first and is unfinished so it goes after the last process. Then P2 runned and is unfinished, so it goes after P1, then P3 goes after P2. Exercise: Assume you have the following jobs to execute with one processor, with the jobs arriving in the order listed here: Process P0 P1 P2 P3 P4 Arrival time 0 10 10 80 85 Burst time 80 20 10 20 50

a. Suppose a system uses RR scheduling with a quantum of 15 .Create a Gantt chart illustrating the execution of these processes? b. What is the turnaround time for process p3? c. What is the average wait time for the processes? a. As the Round-Robin Scheduling follows a circular queue implementation, the Gantt chart is as follows:

10

b. The turnaround time for process P3 is =160-80 = 80 sec. c. Average waiting time : Waiting time for process p0 = 0 sec. p1 = 5 sec. p2 = 20 sec. p3 = 30 sec. p4 = 40 sec. Therefore, the average waiting time is (0+5+20+30+40)/5 = 22 sec.

Exercise: (q = 20)
Process Burst Time Arrival Start Wait Finish TA 1 2 3 4 53 17 68 24 0 0 0 0 0 20 37 57 ? ? ? ? 134 37 162 121 134 37 162 121

Create the Gantt chart and give the waiting times for all processes, the average turnaround time and the average waiting time.

Gantt chart:

In this example we dont have an arrival time, therefore we use this formula to calculate the waiting times. waiting times: p1: (77-20) + (121-97) = 81 Note: P1 was waiting from 20 until 77 and from 97 to 121 p2: (20-0) = 20 Note: P2 was waiting from 0 until 20 p3: (37-0) + (97-57) + (134-117) = 94 Note: P3 was waiting from 0 until 37 and from 57 to 97 and 117 to 134 p4: (57-0) + (117-77) = 97 Note: P4 was waiting from 0 until 57 and from 77 to 117 average waiting time: (81+20+94+97)/4 = 73 average turnaround time: (134+37+162+121)/4= 113.5

11

Exercise: (q = 4):
Process 1 2 3 Burst Time 24 3 3 Arrival 0 0 0 Start 0 4 7 Wait 6 4 7 Finish 30 7 10 TA 30 7 10

Create the Gantt chart and give the waiting times for all processes, the average turnaround time and the average waiting time.

Gantt chart:

average waiting time: (6+4+7)/3 = 5.67 average turnaround time: (30+7+10)/3 = 15.67 Exercise: (q = 10):
Process 1 2 3 4 5 Burst Time 10 29 3 7 12 Arrival 0 0 0 0 0 Start 0 10 20 23 30 Wait 0 32 20 23 40 Finish 10 61 23 30 52 TA 10 61 23 30 52

Create the Gantt chart and give the waiting times for all processes, the average turnaround time and the average waiting time.

Gantt chart:

average waiting time: (0+32+20+23+40)/5 = 23 average turnaround time: (10+39+42+49+61)/5 = 35.2

12

REVISION QUESTIONS
Exercise 1: Consider the following set of processes, with the length of the CPU burst time given in milliseconds:
Process P1 P2 P3 P4 P5 Burst Time 4 2 5 1 3 Priority 3 1 = Highest 2 5 = Lowest 4

The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0. a) Draw four Gantt charts illustrating the execution of these processes using FCFS, SJF, a nonpreemptive priority (a smaller priority number implies a higher priority), and RR (quantum = 1) scheduling. b) What is the turnaround time of each process for each of the scheduling algorithms in part a? c) What is the waiting time of each process for each of the scheduling algorithm s in part a? d) Which of the schedules in part a results in the minimal average waiting time (over all processes)?

13

Ans: (a) The four Gantt charts are 0 4 6 11 12 15 +--------+---+---------+--+------+ | P1 | P2| P3 |P4| P5 | FCFS +--------+---+---------+--+------+ 0 1 3 6 10 15 +--+---+-----+--------+----------+ |P4| P2| P5 | P1 | P3 | SJF +--+---+-----+--------+----------+ 0 2 7 11 14 15 +---+---------+--------+------+--+ | P2| P3 | P1 | P5 |P4| Priority +---+---------+--------+------+--+ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |P1|P2|P3|P4|P5|P1|P2|P3|P5|P1|P3|P5|P1|P3|P3| RR +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ (b) Turnaround time: | FCFS | SJF | Priority | RR --------+------+-----+----------+-----P1 | 4 | 10 | 11 | 13 P2 | 6 | 3 | 2 | 7 P3 | 11 | 15 | 7 | 15 P4 | 12 | 1 | 15 | 4 P5 | 15 | 6 | 14 | 12 --------+------+-----+----------+-----Average | 9.6 | 7 | 9.8 | 10.2 (c) Waiting time: | FCFS | SJF | Priority | RR --------+------+-----+----------+-----P1 | 0 | 6 | 7 | 9 P2 | 4 | 1 | 0 | 5 P3 | 6 | 10 | 2 | 10 P4 | 11 | 0 | 14 | 3 P5 | 12 | 3 | 11 | 9 --------+------+-----+----------+-----Average | 6.6 | 4 | 6.8 | 7.2 (d) Shortest Job First (SJF) has the minimal average waiting time.

14

Exercise 2: Suppose that the following processes arrive for execution at time 0 in the order A, B, C:

(a) Draw four Gantt charts illustrating the execution of these processes using FCFS, SJF, a nonpreemptive priority (a smaller priority number implies a higher priority), and RR (quantum = 2) scheduling. (b) What is the waiting time of each process for each of the scheduling algorithms? (c) What is the turnaround time of each process for each of the scheduling algorithms?

15

Exercise 3:
Suppose that the following processes arrive for execution at time 0 in the order A, B, C:

16

Exercise 4:

17

Anda mungkin juga menyukai