L4ch5scheduling

Page 1

objectives To introduce CPU scheduling, which is the basis for multi-programmed operating systems. To describe various CPU-scheduling algorithms To discuss evaluation criteria for selecting a CPUscheduling algorithm for a particular system.

Ref: Operating System Concepts – 7 th Edition

5.1

Prof. Dr. Hanafy M.


Chapter 5: CPU Scheduling


Basic Concepts The objective of multiprogramming is to have some process running at all times to maximize CPU utilization;

With multiprogramming, several processes are kept in memory at one time. When one process has to wait ( typically for the completion of some I/O request), the operating system take the CPU away from that process and gives to another process

Ref: Operating System Concepts – 7 th Edition

5.3

Prof. Dr. Hanafy M.


Basic Concepts (cont.) CPU–I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait. Process execution begins with a CPU burst, followed by I/O burst, followed by another CPU burst, followed by another I/O burst, and so. The final CPU burst ends with a system request to terminate execution. CPU burst distribution Histogram of CPU-burst durations is generally characterized as exponential; with a large number of short CPU bursts and a small number of long CPU bursts. An I/O-bound program has many short CPU bursts. A CPU-bound program might have a few long CPU bursts.

Ref: Operating System Concepts – 7 th Edition

5.4

Prof. Dr. Hanafy M.


Alternating Sequence of CPU And I/O Bursts

Ref: Operating System Concepts – 7 th Edition

5.5

Prof. Dr. Hanafy M.


Histogram of CPU-burst Times

Ref: Operating System Concepts – 7 th Edition

5.6

Prof. Dr. Hanafy M.


CPU Scheduler Whenever the CPU becomes idle, the OS selects from among the processes in memory (ready queue) that are ready to execute, and allocates the CPU to one of them. The selection is carried out by the short-term scheduler ( CPU scheduler). The ready queue can be implemented as a FIFO queue, a priority queue, a tree, or unordered linked list. CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state (I/O request) 2. Switches from running to ready state (interrupt occurs) 3. Switches from waiting to ready (completion of I/O) 4. Terminates

Ref: Operating System Concepts – 7 th Edition

5.7

Prof. Dr. Hanafy M.


Preemptive scheduling Scheduling under 1 and 4 is nonpreemptive ( or cooperative); Once the CPU has been allocated to a process , the process keeps the CPU until it release the CPU either by terminating or by switching to the waiting state. A new process (if any in the ready queue) must be selected for execution. It is used in Windows 95 and all subsequent versions of Windows OS All other scheduling is preemptive.

Ref: Operating System Concepts – 7 th Edition

5.8

Prof. Dr. Hanafy M.


Dispatcher Another component involved in the CPU-scheduling function is the dispatcher. Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context switching to user mode jumping to the proper location in the user program to restart that program The dispatcher should be as fast as possible, since it is invoked during every process switch. Dispatch latency – time it takes for the dispatcher to stop one process and start another running.

Ref: Operating System Concepts – 7 th Edition

5.9

Prof. Dr. Hanafy M.


Scheduling Criteria CPU utilization – keep the CPU as busy as possible; in a real system, CPU utilization should range from 40% to 90%. Throughput – # of processes that complete their execution per time unit. For long processes, the rate may be one process/hr; for short processes, it may be 10 processes/sec. Turnaround time – amount of time to execute a particular process. The interval from the time of submission of a process to the time of completion. 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)

Ref: Operating System Concepts – 7 th Edition

5.10

Prof. Dr. Hanafy M.


Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

Ref: Operating System Concepts – 7 th Edition

5.11

Prof. Dr. Hanafy M.


First-Come, First-Served (FCFS) Scheduling With this scheme, the process that requests the CPU first is allocated the CPU first. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to process at the head of the queue. The running process is then removed from the queue. The average waiting time under the FCFS policy is often quite long.

Ref: Operating System Concepts – 7 th Edition

5.12

Prof. Dr. Hanafy M.


FCFS Scheduling (Cont.) Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds: Process Burst Time P1 24 P2

3

P3

3

Suppose that the processes arrive in the order: P 1 , P 2 , P 3 The Gantt Chart for the schedule is: P1

P2

P3

0 24 27 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27ms

30

Average waiting time: (0 + 24 + 27)/3 = 17ms

Ref: Operating System Concepts – 7 th Edition

5.13

Prof. Dr. Hanafy M.


FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P2 , P3 , P1 The Gantt chart for the schedule is:

P2 0

P3 3

P1 6

Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 ms Average waiting time:

30

(6 + 0 + 3)/3 = 3 ms

Much better than previous case There is a convey effect as all short processes behind one big long process wait for it to get off the CPU. FCFS scheduling algorithm is a nonpreemptive. Once the CPU has been allocated to a process, that process keeps the CPU until it releases the CPU, either by terminating or by requesting I/O. Thus, the FCFS algorithm is trouble-some for time-sharing systems.

Ref: Operating System Concepts – 7 th Edition

5.14

Prof. Dr. Hanafy M.


Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU burst. When the CPU is available, it is assigned to the process with the shortest next CPU burst. 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

Ref: Operating System Concepts – 7 th Edition

5.15

Prof. Dr. Hanafy M.


Example of Non-Preemptive SJF Process

Arrival Time

Burst Time

P1

0.0

7

P2

2.0

4

P3

4.0

1

P4

5.0

4

SJF (non-preemptive) P1 0

3

P3 7

P2 8

P4 12

16

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

Ref: Operating System Concepts – 7 th Edition

5.16

Prof. Dr. Hanafy M.


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) P1 0

P2 2

P3 4

P2 5

P4 7

P1 11

16

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

Ref: Operating System Concepts – 7 th Edition

5.17

Prof. Dr. Hanafy M.


Priority Scheduling A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (we assume that smallest integer ≥ highest priority). Equal-priority processes are scheduled in FCFS order. Priority scheduling can be Preemptive or Nonpreemptive. When a process arrives at the ready queue, its priority is compared with the priority of the currently running process. if the priority of the newly arrived process is higher than the priority of the currently running process: Preemptive: The algorithm will preempt CPU Nonpreemptive. The algorithm will simply put the new process at the head of the ready queue SJF is a priority scheduling where priority is the predicted next CPU burst time. The larger the CPU burst, the lower the priority.

Ref: Operating System Concepts – 7 th Edition

5.18

Prof. Dr. Hanafy M.


Example of priority scheduling Process

Burst Time

Priority

P1

10

3

P2

1

1

P3

2

4

P4

1

5

P5

5

2

Ref: Operating System Concepts – 7 th Edition

5.19

Prof. Dr. Hanafy M.


Example of priority scheduling Using priority scheduling, we could schedule these processes according to the following Gantt chart P2

0

P5

1

P1

6

P3

16

P 4 18 19

Average waiting time is (6 + 0 + 16 + 18 + 1)/5 = 8.2 ms

Ref: Operating System Concepts – 7 th Edition

5.20

Prof. Dr. Hanafy M.


Priority Scheduling (cont.) A major problem is indefinite blocking ( or starvation) – The algorithm can leave some low-priority processes waiting indefinitely; low priority processes may never execute Solution ≡ Aging – it is a technique of gradually increasing the priority of that wait in the system for a long time; as time progresses increase the priority of the process. For example, if the priorities ranges from 127( low) to 0 (high), we could increase the priority of a waiting process by 1 every 15 minutes. A process of initial priority 127 would take no more than 32 hours to be executed.

Ref: Operating System Concepts – 7 th Edition

5.21

Prof. Dr. Hanafy M.


Round Robin Scheduling(RR) It is designed especially for time-sharing systems. It is similar to FCFS scheduling, but preemption is added to switch between processes. Each process gets a small unit of CPU time (time quantum or time slice ), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. New processes are added to the tail of the ready queue. The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1 time quantum. If the process CPU burst < 1 time quantum, the scheduler will proceed to the next process in the ready queue > 1 time quantum, an interrupt will be occurred, a context switch will be executed, and the process will be put at the tail of the ready queue. The scheduler then selects the next process in the ready queue.

Ref: Operating System Concepts – 7 th Edition

5.22

Prof. Dr. Hanafy M.


Example of RR with Time Quantum = 20 Process P1

Burst Time 53

P2

17

P3

68

P4

24

The Gantt chart is: P1 0

P2 20

37

P3

P4 57

P1 77

P3

P4

P1

P3

P3

97 117 121 134 154 162

Average waiting time = (81 + 20 + 94 + 53) /4 = 62 ms Typically, higher average turnaround than SJF, but better response

Ref: Operating System Concepts – 7 th Edition

5.23

Prof. Dr. Hanafy M.


Round Robin (cont.) 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 until its next time quantum. For example, with five processes and a time quantum of 20 ms, each process will get up to 20 ms every 100 ms. The performance of the algorithm depends mainly on the size of the time quantum . If q is extremely large ⇒ the RR is the same as FIFO If q is extremely small ⇒ the RR approach is called processor sharing and creates the appearance that each of n processes has its own processor running at 1/n the speed of the real processor. q must be large with respect to context switch, otherwise overhead is too high

Ref: Operating System Concepts – 7 th Edition

5.24

Prof. Dr. Hanafy M.


Time Quantum and Context Switch Time

Ref: Operating System Concepts – 7 th Edition

5.25

Prof. Dr. Hanafy M.


Multilevel Queue This algorithm is used for situations in which processes are easily classified into different groups. A common division is made between: foreground (interactive) processes background (batch) processes These two types have different response time requirements and different scheduling needs. foreground (interactive) processes may have priority over background (batch) processes.

Ref: Operating System Concepts – 7 th Edition

5.26

Prof. Dr. Hanafy M.


Multilevel Queue (cont.) Ready queue is partitioned into separate queues (see next slide) Each queue has its own scheduling algorithm. For example: foreground – RR background – FCFS Scheduling must be done between the queues, which is commonly implemented as: Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation. Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 

80% to foreground in RR

 20%

to background in FCFS

Ref: Operating System Concepts – 7 th Edition

5.27

Prof. Dr. Hanafy M.


Multilevel Queue Scheduling

Ref: Operating System Concepts – 7 th Edition

5.28

Prof. Dr. Hanafy M.


Multilevel Feedback Queue A process can move between the various queues; aging can be implemented this way to prevent starvation. The idea is to separate processes according to the characteristics of their CPU bursts: If a process uses too much CPU time, it will be moved to a lower-priority queue I/O-bound and interactive processes will be in higher-priority queues A process that waits too long in lower-priority queue may be moved to higher-priority queue

Ref: Operating System Concepts – 7 th Edition

5.29

Prof. Dr. Hanafy M.


Multilevel Feedback Queue Multilevel-feedback-queue scheduler defined by the following parameters: number of queues scheduling algorithms for each queue method used to determine when to upgrade a process to a higher priority queue method used to determine when to demote a process to a lower priority queue method used to determine which queue a process will enter when that process needs service

Ref: Operating System Concepts – 7 th Edition

5.30

Prof. Dr. Hanafy M.


Example of Multilevel Feedback Queue Three queues: Q0 – RR with time quantum 8 milliseconds Q1 – RR time quantum 16 milliseconds Q2 – FCFS Scheduling A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1. At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

Ref: Operating System Concepts – 7 th Edition

5.31

Prof. Dr. Hanafy M.


Multilevel Feedback Queues

Ref: Operating System Concepts – 7 th Edition

5.32

Prof. Dr. Hanafy M.


Algorithm Evaluation: Deterministic Modeling This method takes a particular predetermined workload and defines the performance of each algorithm for that workload. For example, assume we have the workload shown below. All five processes arrive at time 0, in the order given, with the length of the CPU burst given in milliseconds. Process Burst Time P1

10

P2

29

P3

3

P4

7

P5

12

Consider the FCFS, SJF, and RR (quantum = 10 ms) scheduling algorithms for this set of processes. Which algorithm would give the minimum average time?

Ref: Operating System Concepts – 7 th Edition

5.33

Prof. Dr. Hanafy M.


Algorithm Evaluation: Deterministic Modeling For the FCFS algorithm, would execute the processes as:

The waiting time is: Process

Waiting-Time

P1

0

P2

10

P3

39

P4

42

P5

49

The average waiting time is (0 + 10 + 39 + 42 + 49) /5 = 28 ms

Ref: Operating System Concepts – 7 th Edition

5.34

Prof. Dr. Hanafy M.


Algorithm Evaluation: Deterministic Modeling For the nonpreemptive SJF algorithm, would execute the processes as:

The waiting time is:

Process

Waiting-Time

P1

0

P2

32

P3

20

P4

23

P5

40

The average waiting time is (0 + 32 + 20 + 23 + 40) /5 = 23 ms

Ref: Operating System Concepts – 7 th Edition

5.35

Prof. Dr. Hanafy M.


Algorithm Evaluation: Deterministic Modeling For the RR algorithm, would execute the processes as:

The waiting time is: Process

Waiting-Time

P1

0

P2

10

P3

39

P4

42

P5

49

The average waiting time is (0 + 10 + 39 + 42 + 49) /5 = 28 ms

Ref: Operating System Concepts – 7 th Edition

5.36

Prof. Dr. Hanafy M.


Exercises EX1:Consider the following set of processes, with the length of the CPU burst given in milliseconds : Process

Burst Time

Priority

P1

10

3

P2

1

1

P3

2

3

P4

1

4

P5

5

2

The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0 a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive priority (a smaller priority number implies a higher priority), and RR (quantum = 1) 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 algorithms in part a? d. Which of the algorithms in part a results in the minimum average waiting time (overall processes)

Ref: Operating System Concepts – 7 th Edition

5.37

Prof. Dr. Hanafy M.


Exercises EX2:Consider the following set of processes, with the length of the CPU burst given in milliseconds : Process

Burst Time

Priority

P1

12

3

P2

3

1

P3

4

4

P4

3

5

P5

7

2

The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0 a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive priority (a smaller priority number implies a higher priority), and RR (quantum = 1) 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 algorithms in part a? d. Which of the algorithms in part a results in the minimum average waiting time (overall processes)

Ref: Operating System Concepts – 7 th Edition

5.38

Prof. Dr. Hanafy M.


Exercises EX3:Consider the following set of processes, with the length of the CPU burst given in milliseconds : Process

Arrival time

Burst time

P1

0

8

P2

1

4

P3

2

9

P4

3

5

if processes arrive at the ready queue at the times shown and need the indicated burst times: a. Draw the Gantt charts that illustrate the execution of these processes using the preemptive SJF schedule. b. What is the turnaround time of each process. c. What is the waiting time of each process d. Calculate the average waiting time (overall processes)

Ref: Operating System Concepts – 7 th Edition

5.39

Prof. Dr. Hanafy M.


QUESTIONS Question: Define the difference between preemptive and nonpreemptive scheduling. Answer: Preemptive scheduling allows a process to be interrupted in the midst of its execution, taking the CPU away and allocating it to another process. Nonpreemptive scheduling ensures that a process relinquishes control of the CPU only when it finishes with its current CPU burst.

Ref: Operating System Concepts – 7 th Edition

5.40

Prof. Dr. Hanafy M.


QUESTIONS Question: Suppose that the following processes arrive for execution at the times indicated. Each process will run the listed amount of time. In answering the questions, use nonpreemptive scheduling and base all decisions on the information you have at the time the decision must be made. Process

Arrival Time

Burst Time

P1

0.0

8

P2

0.4

4

P3

1.0

1

a. What is the average turnaround time for these processes with the FCFS scheduling algorithm? b. What is the average turnaround time for these processes with the SJF scheduling algorithm?

Ref: Operating System Concepts – 7 th Edition

5.41

Prof. Dr. Hanafy M.


QUESTIONS c. The SJF algorithm is supposed to improve performance, but notice that we chose to run process P1 at time 0 because we did not know that two shorter processes would arrive soon. Compute what the average turnaround time will be if the CPU is left idle for the first 1 unit and then SJF scheduling is used. Remember that processes P1 and P2 are waiting during this idle time, so their waiting time may Increase. This algorithm could be known as future-knowledge scheduling. Answer: a. 10.53 b. 9.53 c. 6.86 Remember that turnaround time is finishing time minus arrival time, so you have to subtract the arrival times to compute the turnaround times. FCFS is 11 if you forget to subtract arrival time.

Ref: Operating System Concepts – 7 th Edition

5.42

Prof. Dr. Hanafy M.


End of Chapter 5


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.