You are on page 1of 30

Introduction to Operating Systems

Lecture 2

Lecture 2 contents

Process in the hardware. Process States and control block Process Scheduling Scheduling: Behavior and Algorithms

Running process in the hardware components.


Operating System:
Slow mechanic al device
Manages the allocation of processes in the memory Controls which process to run now in the processor Interrupt

Hard Disk: programs are saved


Running program

Terminating running process

Faster electronic device

Main Memory: Processes are Cash Memory: loaded in the selected Instructions are memory and process saved to increase the processing ready/waiting for execution
Temporary memory , nearer to the processor and faster than the main memory.

Processor: Run instruction by instruction Registry: Load a single instruction for a single running process

Fastest Memory in the processor

Process states

Process control block


PROCESS CONTROL BLOCK: is a block of data, loaded with the process in the memory, that contains information associated with each process. It's a data structure holding:

PC, CPU registers,


memory management information, accounting ( time used, ID, ... ) I/O status ( such as file resources ), scheduling data ( relative priority, etc. )

Process State (so running, ready, etc. is simply a field in the PCB ).

Process Scheduling: Switching


The act of Scheduling a process means changing the active PCB pointed to by the CPU. When CPU switches to another process, the system must save the state of the old process into the memory and load the saved state for the new process from the memory via a context switch.

The CPU switching from one process to another.

3: Processes

Process Scheduling: Queues


(Process is driven by events that are triggered by needs and availability ) Ready queue = contains those processes that are ready to run. I/O queue (waiting state ) = holds those processes waiting for I/O service.

Ready Q And IO Qs

Process Scheduling : (short/long term scheduling)

In many multitasking systems the processor scheduling subsystem operates on three levels Long term scheduling: which determines which programs are admitted to the system for execution and when, and which ones should be exited. selects which processes should be brought into the ready queue Short term scheduling (or dispatching): which determines which of the ready processes can have CPU resources, and for how long. selects which process should be executed next

3: Processes

Process Scheduling : (short/long term scheduling)


Long Term Scheduler
Short Term Scheduler Long Term Scheduler Interrupt Handler

Short Term Scheduler

Scheduling : definition

Interleave the execution of processes to maximize CPU utilization while providing reasonable response time The scheduler determines: o Who will run o When it will run o For how long

Process Scheduling
Interleave the execution of processes to maximize CPU utilization while providing reasonable response time.

Fairness: Comparable processes should get comparable service Efficiency: keep CPU and I/O devices busy Response time: minimize, for interactive users

Turnaround time: average time for a job to complete


Waiting time: minimize the average over processes Throughput: number of completed jobs per time unit

Process Scheduling
Types of Scheduling
a context switch is the process of storing and restoring the state (context) of a process so that execution can be resumed from the same point at a later time.

Non-preemptive scheduling, a process runs until it terminates. Preemptive scheduling, even if the current process is still running, a context switch will occur when its time slice is used up.

Burst time is an assumption of how long a process requires the CPU between I/O waits. It can not be predicted exactly, before a process starts.
It means the amount of time a process uses the CPU for a single time. (A process can use the CPU several times before complete the job in the Preemptive scheduling)

Context Switch

A context switch is the switching of the CPU (central processing unit) from one process or thread to another. The steps of switching process A to process B are as follows:

Suspend As execution Transfer the control to the CPU scheduler. A CPU mode switch may be needed. Save As context to its PCB and other tables. Load Bs context to register, etc. Resume Bs execution of the instruction at Bs program counter. A CPU mode switch may be needed.

13

Scheduling : behavior types

Bursts of CPU usage alternate with periods of I/O wait


o o

CPU-bound processes I/O bound processes


14

Scheduling Criteria

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)
15

Scheduling: Algorithms
1. 2. 3. 4. 5. FCFS : First-come-first-served (Non-Preempt) SJF: Shortest job first (Non-Preempt) SRTF: Shortest Remaining Time First (Preempt) RR : Round robin (Preempt) Priority, Multilevel feedback queues (Preempt)

16

Scheduling : 1. FCFS Algorithm


FCFS may cause long waiting times
Process
P1 P2 P3

Burst time (milli)


24 3 3

If they arrive in the order P1, P2, P3, we get: P1 0 Average waiting time: (0+24+27) / 3 = 17 P2 P3

24

27

30

Scheduling : 1. FCFS Algorithm


FCFS may cause long waiting times (contd)
Process
P1 P2 P3

Burst time (milli)


24 3 3

If they arrive in the order P1, P2, P3, average waiting time 17

What if they arrive in the order P2, P3, P1?

P2 0 3

P3 6

P1 30 (0+3+6) / 3 = 3

Average waiting time:

Scheduling : 2. SJF Algorithm


The CPU is assigned to the process that has the smallest next CPU burst In some cases, this quantity is known or can be approximated
Process
A B C D

Burst time (milli)


5 2 4 1 D

FCFS schedule
A B C 7 A 7 12 11

Average waiting time: 5.75

12

SJF schedule
D 0 1

B
3

Average waiting time: 2.75


19

2. SJF

Scheduling : 2. SJF Algorithm


SJF: example (varying arrival times)
Process
P1 P2 P3 P4

Arrival time
0 2 4 5

Burst time
7 4 1 5

Non-preemptive SJF schedule


P1 0 2 P2 4 P3 5 P4 (0+6+3+7) / 4 = 4
20

P3
7 8

P2 12

P4 16

Average waiting time:

Scheduling : 3. SRTF Algorithm


Process
P1 P2 P3 P4

Arrival time
0 2 4 5

Burst time
7 4 1 5

Preemptive SJF schedule


P1 0 2 P1(5) P2 4 P3 5 P2 7 P4 11 P1 16

P2(2)
(9+1+0+2) / 4 = 3 21

Average waiting time: P4(4)

Scheduling : 4. Round Robin

Each process gets a small unit of CPU time (time-quantum), usually 10-100 milliseconds

Context switch overhead


Time-quantum ~ switching time (or smaller) Fast response : relatively large waste of CPU time Time-quantum >> switching time long response (waiting) times, FCFS

Scheduling : 4. Round Robin


Context switch overhead

Context Switching is time consuming

Choosing a long time quantum to avoid switching:


o o

Deteriorates response time. Increases CPU utilization

Choosing a short time quantum:


o o

Faster response

CPU wasted on switches

23

Scheduling : 4. Round Robin

24

Scheduling : 5. Priority Alg.

Select runnable process with highest priority When there are classes of processes with the same priority: o group priorities o each priority group may uses round robin scheduling o A process from a lower priority group runs only if there is no higher priority process waiting

Scheduling : 5. Priority Alg.


Multi-Level Queue Scheduling
Highest priority

system processes interactive processes

Interactive editing processes

Runnable processes

batch processes Low-priority batch processes

Lowest priority

Disadvantage?

26

5. M

Scheduling : 5. Priority Alg.


Dynamic multi level scheduling
The main drawback of priority scheduling: starvation! To avoid it: Prevent high priority processes from running indefinitely by changing priorities dynamically:
o
o

Each process has a base priority increase priorities of waiting processes at each clock tick
increase priorities of I/O bound processes (decrease those of CPU bound processes) priority = 1/f (f is the fraction of time-quantum used)

Approximation SJF scheduling


o

27

Sample Question

In SJF (Shortest Job First) Scheduling method. How to calculate Average Waiting Time and average Turn-around time?

Is the following Gannt Chart correct ?

28

Sample Answer

Average waiting time=(0+2+2+3+3)/5=2 Average Turnaround time=(3+3+4+7+6)/5=4.6 Gantt chart is wrong... First process P3 has arrived so it will execute first. Since the burst time of P3 is 3sec after the completion of P3, processes P2,P4, and P5 has been arrived. Among P2,P4, and P5 the shortest burst time is 1sec for P2, so P2 will execute next. Then P4 and P5. At last P1 will be executed. Gantt chart for this ques will be: | P3 | P2 | P4 | P5 | P1 |

29

Operating Systems

Lecture 2

You might also like