07/10/2020 1
Lecture 9
Chapter 2Processes and Threads
discover yourself
Scheduling
The problem of determining when processors should be assigned and to which processes is called processor scheduling or CPU scheduling.
When more than one process is runable, the operating system must decide which one first. The part of the operating system concerned with this decision is called the scheduler, and algorithm it uses is called the
scheduling algorithm.
discover yourself
07/10/2020 3
Long-term Scheduling
Medium-term SchedulingShort-term Scheduling
Types of Scheduling
discover yourself
Long-Term Scheduling:
1. The decision to add to the pool of processes to be executed.
2. Selects process and loads it into memory for execution.
discover yourself
07/10/2020 5
Medium-Term Scheduling:
1. The decision to add to the number of processes that are partially or fully in main memory.
2. The mid-term scheduler temporarily removes processes from main memory and places them on secondary memory (swapping)
3. Suspend processes for which adequate resources are not currently available.
discover yourself
Short-Term Scheduling
If a process requires a resource (or input) that it does not
have, it is removed from the ready list (and enters the WAITING state)
The decision as to which available process will be executed by the processor.
discover yourself
07/10/2020 7
Scheduling and process state transitions
discover yourself
Queuing Diagram for Scheduling
discover yourself
07/10/2020 9 discover yourself
discover yourself
07/10/2020 11
Class Work
discover yourself
Summary
Long-term: which process to admit?
Medium-term: which process to swap in or out?
Short-term: which ready process to execute next?
discover yourself
07/10/2020 13
When to Schedule?
There are variety of situations in which scheduling is needed.
1. When a new process is created, a decision needs to be made whether to run the parent process or the child process.
2. When a process exits, so some other processes must be chosen from the set of ready processes.
3. When I/O interrupt occur and now be ready to run when I/ O interrupt finishes. The decision has to be made whether to run newly ready process or the process that was running at the time of interrupt.
discover yourself
CPU Scheduling GOALS
Fairness- makes sure each process gets its fair share of the CPU.
Efficiency–keep the CPU busy 100 percent of the time.
Waiting time- minimize waiting time for job.
Turnaround – minimize the time, how long batch users must wait for output.
Throughput – maximize the number of jobs processed per hour.
discover yourself
07/10/2020 15
Summary
CPU utilization – keep the CPU as busy as possible
keep the CPU busy 100 percent of the time.
Throughput – # of processes that complete their execution per time unit.
Maximize the number of jobs processed per hour.
Turnaround time (TAT) – amount of time to execute a particular process. Time for each process to complete. Minimize the time, how long batch users must
wait for output. Time from submission to 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. Time used by the system to respond to the user job
discover yourself
Non-preemptive Scheduling
A scheduling discipline is non-preemptive if, once a process has been given the CPU, the CPU cannot be taken away from that process.
Preemptive Scheduling
A scheduling discipline is preemptive if, once a process has been given the CPU can taken away.
discover yourself
07/10/2020 17
Types of Scheduler Types of Scheduler
Primary Scheduler Secondary Scheduler
discover yourself
Primary Scheduler
1. Runs threads with the highest priority.
2. It then compares their priority and assigns resources to them, depending on their priority.
3. Threads with the highest priority will be executed for the current time slice.
4. With two or more threads with the same priority, they are put on a stack to allow each run on a given time slice.
discover yourself
07/10/2020 19
Secondary Scheduler
1. It is responsible for increasing the priority of non-executing threads.
2. It is important that those low-priority threads are given a chance to run on the operating system and this is the function of this type of scheduler.
discover yourself
1. FCFS Scheduling
2. Round Robin (RR) Scheduling 3. SJF Scheduling
4. SRTN Scheduling 5. Priority Scheduling
6. Multilevel Queue Scheduling
CPU Scheduling Algorithm
discover yourself
07/10/2020 21 discover yourself
1. First-In-First-Out (FIFO) 2. Run-to-Completion
3. Run-Until-Finish
Processes are dispatched according to their arrival time on the ready queue. Being a non-preemptive discipline, once a process has a CPU, it runs to completion.
FCFS Scheduling
discover yourself
10/07/2020 23
1. Runs the processes in the order they arrive at the short-term scheduler
2. Removes a process from the processor only if it blocks (i.e., goes into the Wait state) or terminates
3. Wonderful for long processes when they finally get on.
4. Terrible for short processes if they are behind a long process.
discover yourself
discover yourself
10/07/2020 25
discover yourself
discover yourself
10/07/2020 27
discover yourself
•
Suppose we change the order of arriving
job P2, P3, P1
discover yourself
10/07/2020 29
The problem with FCFS is that the average waiting time can be long. Consider the following processes
This gives us an average waiting time of 21ms (i.e. (0 + 27 + 36) /3 ).
Now consider if the processes had arrived in the order P2, P3, P1. The average waiting time would now be 6.6ms (i.e. (0 + 9 + 11) /3).
Process Burst Time
P1 27
P2 9
P3 2
discover yourself
Burst Waiting Turnaround Process Time Time Time
P1 24 0 24
P2 3 24 27
P3 3 27 30
Average - 17 27
If the processes arrive in the order P1, P2, P3 and are served in FCFS order, we get the result in the following chart
discover yourself
10/07/2020 31
discover yourself
discover yourself
inspire posterity
Process Wait Time : Service Time - Arrival Time
P0 0 - 0 = 0
P1 5 - 1 = 4
P2 8 - 2 = 6
P3 16 - 3 = 13
10/07/2020 33 Process Burst P1 24 P2 3 P3 3
CLASS WORK-1
FCFS SchedulingAssume processes arrive in this order P1, P2, P3
Calculate the average waiting time and turnaround time?
discover yourself
Process Burst
P2 3
P3 3
P1 24
CLASS WORK-2
FCFS Scheduling
Assume processes arrive in this order P2, P3, P1
Calculate the average waiting time & turnaround time?
discover yourself
10/07/2020 35
discover yourself
inspire posterity
Process P1 P2 P3 P4 P5 Arrival Time 12 8 10 9 7
Burst Time 5 6 11 11 9
Problem
Practice Sheet — CPU Scheduling
Consider the following set of processes with the arrival time and the length of the CPU burst time given in milliseconds:
(a) FCFS (First-Come-First-Served)
Advantages and Disadvantages
Advantages:
1. simple
2. easy to understand & implement 3. first come first served
Disadvantages:
4. This scheduling method is non-preemptive, the process will run until it finishes.
5. Because of this non-preemptive scheduling, short processes which are at the back of the queue have to wait for the long process at the front to finish.
6. Poor in performance as average wait time is high.
discover yourself
10/07/2020 37
discover yourself
discover yourself