23
Module 3 – Process Scheduling Hemang Kothari Assistant Professor Computer Engineering Department MEFGI, Rajkot. Email: [email protected] Slides: http://www.slideshare.net/hemangkothari Computer Engineering Department - MEFGI 1 06/18/2022

Module 3 part 2 process scheduling

Embed Size (px)

Citation preview

Page 1: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 1

Module 3 – Process Scheduling

Hemang KothariAssistant Professor

Computer Engineering Department MEFGI, Rajkot.

Email: [email protected]: http://www.slideshare.net/hemangkothari

Page 2: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 2

Page 3: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 3

Objective

• Max CPU utilization• Max throughput• Min turnaround time • Min waiting time • Min response time

Page 4: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 4

Process Scheduling Goals• 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, not output (for time-sharing environment)

Page 5: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 5

Background Work• Maximum CPU utilization obtained with Multiprogramming• CPU–I/O Burst Cycle.

Page 6: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 6

CPU Schedular• 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 state.2. Switches from running to ready state.3. Switches from waiting to ready.4. Terminates.

• Scheduling under 1 and 4 is nonpreemptive.• All other scheduling is preemptive.

Page 7: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 7

First-Come, First-Served (FCFS) Scheduling

Process Burst TimeP1 24

P2 3

P3 3

• Suppose that the processes arrive in the order: P1 , P2 , P3

The Gantt Chart for the schedule is:

• Waiting time for P1 = 0; P2 = 24; P3 = 27• Average waiting time: (0 + 24 + 27)/3 = 17• Average turn around time: (24 + 27 + 30) / 3 = 81/3 = 27

P1 P2 P3

24 27 300

Page 8: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 8

First-Come, First-Served (FCFS) SchedulingSuppose that the processes arrive in the order

P2 , P3 , P1 .• The Gantt chart for the schedule is:

• Waiting time for P1 = 6; P2 = 0; P3 = 3• Average waiting time: (6 + 0 + 3)/3 = 3• Much better than previous case.• Convoy effect short process behind long process

P1P3P2

63 300

Page 9: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 9

Review - FIFO• Suppose the following jobs arrive for processing at the times

indicated, each job will run the listed amount of time.

• Give Gantt charts illustrating the execution of these jobs using FCFS scheduling algorithms. Compute the average turn around time and average waiting time.

Remember that turnaround time is finishing time minus arrival time, so you have to subtract the arrival times to compute the turnaround times.

Page 10: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 10

Shortest Job First (SJF)• 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 11: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 11

Example of Non Preemptive SJFProcess Arrival Time Burst TimeP1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4• SJF (non-preemptive)

• Average waiting time = (0 + 6 + 3 + 7)/4 ≈ 4

P1 P3 P2

73 160

P4

8 12

Page 12: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 12

Review - Non Preemptive SJF • Suppose the following jobs arrive for processing at the times

indicated, each job will run the listed amount of time.

• Give Gantt charts illustrating the execution of these jobs using SJF scheduling algorithms. Compute the average turn around time and average waiting time.

Page 13: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 13

Example of Preemptive SJFProcess Arrival Time Burst TimeP1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4• SJF (preemptive)

• Average waiting time = (9 + 1 + 0 +2)/4 = 3

P1 P3P2

42 110

P4

5 7

P2 P1

16

Page 14: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 14

Review - Preemptive SJF • Suppose the following jobs arrive for processing at the times

indicated, each job will run the listed amount of time.

• Give Gantt charts illustrating the execution of these jobs using SJF scheduling algorithms. Compute the average turn around time and average waiting time.

Page 15: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 15

Priority Scheduling • A priority number (integer) is associated with each process• The CPU is allocated to the process with the highest priority (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.• Solution Aging – as time progresses increase the priority of the

process.

Page 16: Module 3 part 2 process scheduling

16

Priority Scheduling: Example

Process Duration Priority Arrival Time

P1 6 4 0

P2 8 1 0

P3 7 3 0

P4 3 2 0

0 8 P4 (3) P1 (6)11 P3 (7) 18

P2 waiting time: 0P4 waiting time: 8P3 waiting time: 11P1 waiting time: 18

The average waiting time (AWT): (0+8+11+18)/4 = 9.25(worse than SJF)

P2 (8)24

Page 17: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 17

Round Robin • 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 18: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 18

Round Robin with Quantum = 20Process Burst TimeP1 53

P2 17

P3 68

P4 24• The Gantt chart is:

• 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 19: Module 3 part 2 process scheduling

Example of RR with Time Quantum = 20

• Waiting Time:– P1: (68-20)+(112-88) = 72– P2: (20-0) = 20– P3: (28-0)+(88-48)+(125-108) = 85– P4: (48-0)+(108-68) = 88

• Completion Time:– P1: 125– P2: 28– P3: 153– P4: 112

• Average Waiting Time: (72+20+85+88)/4 = 66.25• Average Completion Time: (125+28+153+112)/4 = 104.5

A process can finish before the time quantum expires, and release the CPU.

Lec 3 Operating Systems 19

Page 20: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 20

Review • Five batch jobs A through E , arrive at a computer center at almost the same

time. They have estimated running times of 10, 6, 2, 4, and 8 minutes. Their (externally determined) priorities are 3, 5, 2, 1, and 4, respectively, with 5 being the highest priority. For each of the following scheduling algorithms, determine the mean process turnaround time. Ignore process switching overhead.(a) Round robin.(b) Priority scheduling.(c) First-come, first-served (run in order 10, 6, 2, 4, 8).(d) Shortest job first.

• For (a), assume that the system is multiprogrammed, and that each job gets its fair share of the CPU. For (b) through (d) assume that only one job at a time runs, until it finishes. All jobs are completely CPU bound.

Page 21: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 21

Solution • For round robin, during the first 10 minutes each job gets 1/5 of the CPU. At the

end of 10 minutes, C finishes. During the next 8 minutes, each job gets 1/4 of the CPU, after which time D finishes. Then each of the three remaining jobs gets 1/3 of the CPU for 6 minutes, until B finishes, and so on. The finishing times for the five jobs are 10, 18, 24, 28, and 30, for an average of 22 minutes.

• For priority scheduling, B is run first. After 6 minutes it is finished. The other jobs finish at 14, 24, 26, and 30, for an average of 18.8 minutes.

• If the jobs run in the order A through E, they finish at 10, 16, 18, 22, and 30, for an average of 19.2 minutes.

• Finally, shortest job first yields finishing times of 2, 6, 12, 20, and 30, for an average of 14 minutes.

Page 22: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 22

Review Job Time Arrival Priority

1 8 0 32 4 0 23 6 0 14 1 0 4

• All jobs arrive at time 0(but in the order 1, 2, 3&4).Draw charts and calculate the average time to complete (turn-around time) using the following scheduling algorithms: FCFS, SJF, Priority scheduling and round Robin (t=2)

Page 23: Module 3 part 2 process scheduling

04/11/2023 Computer Engineering Department - MEFGI 23

Out of Subject Context - Technical

• Chaos Theory “ Chaos: When the present determines the future, but the

approximate present does not approximately determine the future “

• Watch “ Butterfly Effect “ Movie - Thriller