CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and...

108
CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1 / 33

Transcript of CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and...

Page 1: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

CO2101 — Processes and Multi-tasking

Tom Ridge (tr61)

7th October 2019

tr61 Multi-tasking 7th October 2019 1 / 33

Page 2: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Why multi-tasking?

Simple Fetch-Execute Cycle

CPU operates in a Fetch-Execute cycle — it just does one thing at atime

1 Fetch next instruction from memory2 Execute the instruction

If an I/O operation is required, CPU will wait until I/O is complete

Execute

Start

process

Fetch

instruction

instruction

Process

terminated

tr61 Multi-tasking 7th October 2019 2 / 33

Page 3: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Why multi-tasking?

Simple Fetch-Execute Cycle

CPU operates in a Fetch-Execute cycle — it just does one thing at atime

1 Fetch next instruction from memory2 Execute the instruction

If an I/O operation is required, CPU will wait until I/O is complete

Execute

Start

process

Fetch

instruction

instruction

Process

terminated

tr61 Multi-tasking 7th October 2019 2 / 33

Page 4: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Why multi-tasking?

Why do we want more than one process at a time?

For convenience of the user.

A modern OS typically needs to monitor and control all kinds ofbackground tasks.

For better CPU utilisation.

tr61 Multi-tasking 7th October 2019 3 / 33

Page 5: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Why multi-tasking?

Why do we want more than one process at a time?

For convenience of the user.

A modern OS typically needs to monitor and control all kinds ofbackground tasks.

For better CPU utilisation.

tr61 Multi-tasking 7th October 2019 3 / 33

Page 6: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Why multi-tasking?

Why do we want more than one process at a time?

For convenience of the user.

A modern OS typically needs to monitor and control all kinds ofbackground tasks.

For better CPU utilisation.

tr61 Multi-tasking 7th October 2019 3 / 33

Page 7: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking How to appear to multi-task

How to implement multi-tasking:

There is only a single CPU (or perhaps a handful).

Apparent simultaneous execution of several processes isachieved by the CPU performing a little bit of each process thensuspending it and moving to the next.

CPU needs a mechanism to determine that a particular processcan be suspended to allow the next one to take over.

Mechanism is interrupts

tr61 Multi-tasking 7th October 2019 4 / 33

Page 8: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking How to appear to multi-task

How to implement multi-tasking:

There is only a single CPU (or perhaps a handful).

Apparent simultaneous execution of several processes isachieved by the CPU performing a little bit of each process thensuspending it and moving to the next.

CPU needs a mechanism to determine that a particular processcan be suspended to allow the next one to take over.

Mechanism is interrupts

tr61 Multi-tasking 7th October 2019 4 / 33

Page 9: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking How to appear to multi-task

How to implement multi-tasking:

There is only a single CPU (or perhaps a handful).

Apparent simultaneous execution of several processes isachieved by the CPU performing a little bit of each process thensuspending it and moving to the next.

CPU needs a mechanism to determine that a particular processcan be suspended to allow the next one to take over.

Mechanism is interrupts

tr61 Multi-tasking 7th October 2019 4 / 33

Page 10: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking How to appear to multi-task

How to implement multi-tasking:

There is only a single CPU (or perhaps a handful).

Apparent simultaneous execution of several processes isachieved by the CPU performing a little bit of each process thensuspending it and moving to the next.

CPU needs a mechanism to determine that a particular processcan be suspended to allow the next one to take over.

Mechanism is interrupts

tr61 Multi-tasking 7th October 2019 4 / 33

Page 11: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Interrupts and the CPU cycle

The (interrupted) CPU cycle

N

Handle

interrupt

Process

terminated

Start

process

Fetch

instruction

instruction

Interrupt?

Execute

Y

tr61 Multi-tasking 7th October 2019 5 / 33

Page 12: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Interrupts and the CPU cycle

Definition and role of interrupts

Interrupts: events that cause the CPU to stop the current processand switch to the special process for handling the interrupt.

The Dispatcher performs the operation required by the interrupt(e.g. sending data to a printer) and then decides which processis the next to run.

tr61 Multi-tasking 7th October 2019 6 / 33

Page 13: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Interrupts and the CPU cycle

Definition and role of interrupts

Interrupts: events that cause the CPU to stop the current processand switch to the special process for handling the interrupt.

The Dispatcher performs the operation required by the interrupt(e.g. sending data to a printer) and then decides which processis the next to run.

tr61 Multi-tasking 7th October 2019 6 / 33

Page 14: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Interrupts and the CPU cycle

Types of Interrupts

Input/Output (I/O): by I/O devices to signal completion, error or otherstate change.

Timer: by a clock within the CPU; used to alert the OS at pre-setintervals for time critical activities.

Hardware Error: by hardware faults.

Program: by error conditions within user programs, e.g, division by zero

tr61 Multi-tasking 7th October 2019 7 / 33

Page 15: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Interrupts and the CPU cycle

Types of Interrupts

Input/Output (I/O): by I/O devices to signal completion, error or otherstate change.

Timer: by a clock within the CPU; used to alert the OS at pre-setintervals for time critical activities.

Hardware Error: by hardware faults.

Program: by error conditions within user programs, e.g, division by zero

tr61 Multi-tasking 7th October 2019 7 / 33

Page 16: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Interrupts and the CPU cycle

Types of Interrupts

Input/Output (I/O): by I/O devices to signal completion, error or otherstate change.

Timer: by a clock within the CPU; used to alert the OS at pre-setintervals for time critical activities.

Hardware Error: by hardware faults.

Program: by error conditions within user programs, e.g, division by zero

tr61 Multi-tasking 7th October 2019 7 / 33

Page 17: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Interrupts and the CPU cycle

Types of Interrupts

Input/Output (I/O): by I/O devices to signal completion, error or otherstate change.

Timer: by a clock within the CPU; used to alert the OS at pre-setintervals for time critical activities.

Hardware Error: by hardware faults.

Program: by error conditions within user programs, e.g, division by zero

tr61 Multi-tasking 7th October 2019 7 / 33

Page 18: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Programs and processes

A program is code ready to execute on a system.

A process is a program “in the process of being executed”, i.e, aparticular instance of a program together with its data.

There may be several copies of the same program runningsimultaneously as different processes.

The OS stores information about each process in a processcontrol block (PCB) (also called process descriptor).

Multi-tasking: is the simultaneous running of two or moreprocesses.

tr61 Multi-tasking 7th October 2019 8 / 33

Page 19: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Programs and processes

A program is code ready to execute on a system.

A process is a program “in the process of being executed”, i.e, aparticular instance of a program together with its data.

There may be several copies of the same program runningsimultaneously as different processes.

The OS stores information about each process in a processcontrol block (PCB) (also called process descriptor).

Multi-tasking: is the simultaneous running of two or moreprocesses.

tr61 Multi-tasking 7th October 2019 8 / 33

Page 20: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Programs and processes

A program is code ready to execute on a system.

A process is a program “in the process of being executed”, i.e, aparticular instance of a program together with its data.

There may be several copies of the same program runningsimultaneously as different processes.

The OS stores information about each process in a processcontrol block (PCB) (also called process descriptor).

Multi-tasking: is the simultaneous running of two or moreprocesses.

tr61 Multi-tasking 7th October 2019 8 / 33

Page 21: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Programs and processes

A program is code ready to execute on a system.

A process is a program “in the process of being executed”, i.e, aparticular instance of a program together with its data.

There may be several copies of the same program runningsimultaneously as different processes.

The OS stores information about each process in a processcontrol block (PCB) (also called process descriptor).

Multi-tasking: is the simultaneous running of two or moreprocesses.

tr61 Multi-tasking 7th October 2019 8 / 33

Page 22: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Programs and processes

A program is code ready to execute on a system.

A process is a program “in the process of being executed”, i.e, aparticular instance of a program together with its data.

There may be several copies of the same program runningsimultaneously as different processes.

The OS stores information about each process in a processcontrol block (PCB) (also called process descriptor).

Multi-tasking: is the simultaneous running of two or moreprocesses.

tr61 Multi-tasking 7th October 2019 8 / 33

Page 23: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Sharing Code among Processes

Tetris

Alice’s second document

Operating System

Emacs code (shared)

Alice’s first document

Bob’s document

Alice

Bob

Memory

Shared code must not be changed during execution

Each processes requires its own data area

tr61 Multi-tasking 7th October 2019 9 / 33

Page 24: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Process Creation

E.g, Created by a user via a command:

When a user invokes a program, OS creates a PCB to represent theexecution of this process.OS also allocates resources for the process.So, a process consists of machine code in memory and a PCB.

Created by another process, called spawning a process:

The process that creates a new process is called the parent while thecreated process is the childThe child can also spawn further processes, forming a tree of processes

tr61 Multi-tasking 7th October 2019 10 / 33

Page 25: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Process Creation

E.g, Created by a user via a command:

When a user invokes a program, OS creates a PCB to represent theexecution of this process.OS also allocates resources for the process.So, a process consists of machine code in memory and a PCB.

Created by another process, called spawning a process:

The process that creates a new process is called the parent while thecreated process is the childThe child can also spawn further processes, forming a tree of processes

tr61 Multi-tasking 7th October 2019 10 / 33

Page 26: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Process Creation

E.g, Created by a user via a command:

When a user invokes a program, OS creates a PCB to represent theexecution of this process.OS also allocates resources for the process.So, a process consists of machine code in memory and a PCB.

Created by another process, called spawning a process:

The process that creates a new process is called the parent while thecreated process is the childThe child can also spawn further processes, forming a tree of processes

tr61 Multi-tasking 7th October 2019 10 / 33

Page 27: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Process Creation

E.g, Created by a user via a command:

When a user invokes a program, OS creates a PCB to represent theexecution of this process.OS also allocates resources for the process.So, a process consists of machine code in memory and a PCB.

Created by another process, called spawning a process:

The process that creates a new process is called the parent while thecreated process is the childThe child can also spawn further processes, forming a tree of processes

tr61 Multi-tasking 7th October 2019 10 / 33

Page 28: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Aside about Kernel Mode vs User Mode

OS needs to protect processes from each other for security

If a user’s process had access to all machine instructions, then theuser could do anything the OS can.

The solution is for the CPU to have a special operating mode, kernel orsupervisor mode, which can only be invoked by the OS.

In kernel mode, certain privileged instructions, e.g, interruptprocessing, dispatcher, memory allocation, are possible; normalprocesses run in user mode

tr61 Multi-tasking 7th October 2019 11 / 33

Page 29: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Aside about Kernel Mode vs User Mode

OS needs to protect processes from each other for security

If a user’s process had access to all machine instructions, then theuser could do anything the OS can.

The solution is for the CPU to have a special operating mode, kernel orsupervisor mode, which can only be invoked by the OS.

In kernel mode, certain privileged instructions, e.g, interruptprocessing, dispatcher, memory allocation, are possible; normalprocesses run in user mode

tr61 Multi-tasking 7th October 2019 11 / 33

Page 30: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Aside about Kernel Mode vs User Mode

OS needs to protect processes from each other for security

If a user’s process had access to all machine instructions, then theuser could do anything the OS can.

The solution is for the CPU to have a special operating mode, kernel orsupervisor mode, which can only be invoked by the OS.

In kernel mode, certain privileged instructions, e.g, interruptprocessing, dispatcher, memory allocation, are possible; normalprocesses run in user mode

tr61 Multi-tasking 7th October 2019 11 / 33

Page 31: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Programs and processes

Aside about Kernel Mode vs User Mode

OS needs to protect processes from each other for security

If a user’s process had access to all machine instructions, then theuser could do anything the OS can.

The solution is for the CPU to have a special operating mode, kernel orsupervisor mode, which can only be invoked by the OS.

In kernel mode, certain privileged instructions, e.g, interruptprocessing, dispatcher, memory allocation, are possible; normalprocesses run in user mode

tr61 Multi-tasking 7th October 2019 11 / 33

Page 32: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

The Dispatcher

After an interrupt has been handled, control is passed to theDispatcher (or Low Level Scheduler).

1 Is the current process still the most suitable to run? If so, resumeexecution; otherwise . . .

2 Save the state of the current process in its PCB3 Retrieve the state of the most suitable process from its PCB4 Transfer control to the new process, at the point indicated in the PCB

The action of storing the state of current process and (re-)startinganother process is called a context switch

tr61 Multi-tasking 7th October 2019 12 / 33

Page 33: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

The Dispatcher

After an interrupt has been handled, control is passed to theDispatcher (or Low Level Scheduler).

1 Is the current process still the most suitable to run? If so, resumeexecution; otherwise . . .

2 Save the state of the current process in its PCB3 Retrieve the state of the most suitable process from its PCB4 Transfer control to the new process, at the point indicated in the PCB

The action of storing the state of current process and (re-)startinganother process is called a context switch

tr61 Multi-tasking 7th October 2019 12 / 33

Page 34: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

The Dispatcher

After an interrupt has been handled, control is passed to theDispatcher (or Low Level Scheduler).

1 Is the current process still the most suitable to run? If so, resumeexecution; otherwise . . .

2 Save the state of the current process in its PCB

3 Retrieve the state of the most suitable process from its PCB4 Transfer control to the new process, at the point indicated in the PCB

The action of storing the state of current process and (re-)startinganother process is called a context switch

tr61 Multi-tasking 7th October 2019 12 / 33

Page 35: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

The Dispatcher

After an interrupt has been handled, control is passed to theDispatcher (or Low Level Scheduler).

1 Is the current process still the most suitable to run? If so, resumeexecution; otherwise . . .

2 Save the state of the current process in its PCB3 Retrieve the state of the most suitable process from its PCB

4 Transfer control to the new process, at the point indicated in the PCB

The action of storing the state of current process and (re-)startinganother process is called a context switch

tr61 Multi-tasking 7th October 2019 12 / 33

Page 36: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

The Dispatcher

After an interrupt has been handled, control is passed to theDispatcher (or Low Level Scheduler).

1 Is the current process still the most suitable to run? If so, resumeexecution; otherwise . . .

2 Save the state of the current process in its PCB3 Retrieve the state of the most suitable process from its PCB4 Transfer control to the new process, at the point indicated in the PCB

The action of storing the state of current process and (re-)startinganother process is called a context switch

tr61 Multi-tasking 7th October 2019 12 / 33

Page 37: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

The Dispatcher

After an interrupt has been handled, control is passed to theDispatcher (or Low Level Scheduler).

1 Is the current process still the most suitable to run? If so, resumeexecution; otherwise . . .

2 Save the state of the current process in its PCB3 Retrieve the state of the most suitable process from its PCB4 Transfer control to the new process, at the point indicated in the PCB

The action of storing the state of current process and (re-)startinganother process is called a context switch

tr61 Multi-tasking 7th October 2019 12 / 33

Page 38: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

States of processes

The running process is in the RUNNING state; only one RUNNINGprocess on a processor at a time.

Clock interrupt:

Initiated by a clock within the CPU.Gives the dispatcher an opportunity to context switch.A process stopped by clock interrupt goes to READY state.

I/O interrupt:

Initiated by CPU when an I/O instruction occurs in the runningprocess or by an I/O device signalling completion of an I/OoperationWhen a process starts an I/O operation it goes to the BLOCKEDstateWhen an I/O operation completes for a process, it goes to READYstate

The dispatcher can select a process to run only if it is READY.

tr61 Multi-tasking 7th October 2019 13 / 33

Page 39: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

States of processes

The running process is in the RUNNING state; only one RUNNINGprocess on a processor at a time.

Clock interrupt:

Initiated by a clock within the CPU.Gives the dispatcher an opportunity to context switch.A process stopped by clock interrupt goes to READY state.

I/O interrupt:

Initiated by CPU when an I/O instruction occurs in the runningprocess or by an I/O device signalling completion of an I/OoperationWhen a process starts an I/O operation it goes to the BLOCKEDstateWhen an I/O operation completes for a process, it goes to READYstate

The dispatcher can select a process to run only if it is READY.

tr61 Multi-tasking 7th October 2019 13 / 33

Page 40: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

States of processes

The running process is in the RUNNING state; only one RUNNINGprocess on a processor at a time.

Clock interrupt:

Initiated by a clock within the CPU.Gives the dispatcher an opportunity to context switch.A process stopped by clock interrupt goes to READY state.

I/O interrupt:

Initiated by CPU when an I/O instruction occurs in the runningprocess or by an I/O device signalling completion of an I/OoperationWhen a process starts an I/O operation it goes to the BLOCKEDstateWhen an I/O operation completes for a process, it goes to READYstate

The dispatcher can select a process to run only if it is READY.

tr61 Multi-tasking 7th October 2019 13 / 33

Page 41: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

States of processes

The running process is in the RUNNING state; only one RUNNINGprocess on a processor at a time.

Clock interrupt:

Initiated by a clock within the CPU.Gives the dispatcher an opportunity to context switch.A process stopped by clock interrupt goes to READY state.

I/O interrupt:

Initiated by CPU when an I/O instruction occurs in the runningprocess or by an I/O device signalling completion of an I/OoperationWhen a process starts an I/O operation it goes to the BLOCKEDstateWhen an I/O operation completes for a process, it goes to READYstate

The dispatcher can select a process to run only if it is READY.

tr61 Multi-tasking 7th October 2019 13 / 33

Page 42: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Diagram of process states

Process entry

READY BLOCKED

RUNNING

Dispatch

Termination

I/O wait

I/O completion

Timeout

tr61 Multi-tasking 7th October 2019 14 / 33

Page 43: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Summary of the story and what next

A process can be RUNNING (currently running), READY (ready to run)or BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process.

A clock interrupt causes the current process to become READY.

An I/O interrupt causes the current process to become BLOCKED, uponcompletion of the I/O it becomes READY

The Dispatcher chooses the next process to run from the queue ofREADY processes.

Scheduling: is the policy for choosing among the READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

Page 44: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Summary of the story and what next

A process can be RUNNING (currently running), READY (ready to run)or BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process.

A clock interrupt causes the current process to become READY.

An I/O interrupt causes the current process to become BLOCKED, uponcompletion of the I/O it becomes READY

The Dispatcher chooses the next process to run from the queue ofREADY processes.

Scheduling: is the policy for choosing among the READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

Page 45: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Summary of the story and what next

A process can be RUNNING (currently running), READY (ready to run)or BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process.

A clock interrupt causes the current process to become READY.

An I/O interrupt causes the current process to become BLOCKED, uponcompletion of the I/O it becomes READY

The Dispatcher chooses the next process to run from the queue ofREADY processes.

Scheduling: is the policy for choosing among the READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

Page 46: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Summary of the story and what next

A process can be RUNNING (currently running), READY (ready to run)or BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process.

A clock interrupt causes the current process to become READY.

An I/O interrupt causes the current process to become BLOCKED, uponcompletion of the I/O it becomes READY

The Dispatcher chooses the next process to run from the queue ofREADY processes.

Scheduling: is the policy for choosing among the READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

Page 47: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Summary of the story and what next

A process can be RUNNING (currently running), READY (ready to run)or BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process.

A clock interrupt causes the current process to become READY.

An I/O interrupt causes the current process to become BLOCKED, uponcompletion of the I/O it becomes READY

The Dispatcher chooses the next process to run from the queue ofREADY processes.

Scheduling: is the policy for choosing among the READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

Page 48: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Summary of the story and what next

A process can be RUNNING (currently running), READY (ready to run)or BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process.

A clock interrupt causes the current process to become READY.

An I/O interrupt causes the current process to become BLOCKED, uponcompletion of the I/O it becomes READY

The Dispatcher chooses the next process to run from the queue ofREADY processes.

Scheduling: is the policy for choosing among the READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

Page 49: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Summary of the story and what next

A process can be RUNNING (currently running), READY (ready to run)or BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process.

A clock interrupt causes the current process to become READY.

An I/O interrupt causes the current process to become BLOCKED, uponcompletion of the I/O it becomes READY

The Dispatcher chooses the next process to run from the queue ofREADY processes.

Scheduling: is the policy for choosing among the READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

Page 50: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Summary of the story and what next

A process can be RUNNING (currently running), READY (ready to run)or BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process.

A clock interrupt causes the current process to become READY.

An I/O interrupt causes the current process to become BLOCKED, uponcompletion of the I/O it becomes READY

The Dispatcher chooses the next process to run from the queue ofREADY processes.

Scheduling: is the policy for choosing among the READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

Page 51: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Types of Scheduling policy

Scheduling policy determines:

Which READY process to run next.How long a process is given to use the CPU.

Non-preemptive scheduling:

A process releases CPU only if it BLOCKS on I/O (or it finishes).i.e. the process cannot be stopped during regular operation.

Preemptive scheduling:

A process can be stopped at any point by a clock interrupt.

tr61 Multi-tasking 7th October 2019 16 / 33

Page 52: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Types of Scheduling policy

Scheduling policy determines:

Which READY process to run next.How long a process is given to use the CPU.

Non-preemptive scheduling:

A process releases CPU only if it BLOCKS on I/O (or it finishes).i.e. the process cannot be stopped during regular operation.

Preemptive scheduling:

A process can be stopped at any point by a clock interrupt.

tr61 Multi-tasking 7th October 2019 16 / 33

Page 53: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Types of Scheduling policy

Scheduling policy determines:

Which READY process to run next.How long a process is given to use the CPU.

Non-preemptive scheduling:

A process releases CPU only if it BLOCKS on I/O (or it finishes).i.e. the process cannot be stopped during regular operation.

Preemptive scheduling:

A process can be stopped at any point by a clock interrupt.

tr61 Multi-tasking 7th October 2019 16 / 33

Page 54: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Efficiency measures of scheduling policies

There are many possible ways to measure efficiency ofscheduling policies.

We consider the following 3:

Turnaround time.CPU usage.Response time.

tr61 Multi-tasking 7th October 2019 17 / 33

Page 55: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Efficiency measures of scheduling policies

There are many possible ways to measure efficiency ofscheduling policies.

We consider the following 3:

Turnaround time.CPU usage.Response time.

tr61 Multi-tasking 7th October 2019 17 / 33

Page 56: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Efficiency measures of scheduling policies

There are many possible ways to measure efficiency ofscheduling policies.

We consider the following 3:

Turnaround time.CPU usage.Response time.

tr61 Multi-tasking 7th October 2019 17 / 33

Page 57: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Turnaround time

Turnaround time of a particular process is its duration in realtime, i.e. the difference between the time it is submitted forexecution and the time execution is complete.

Turnaround time of a group of processes is the average of theirindividual turnaround times.

Example:

We have 3 processes with respective start (submitted) and endtimes (100,200), (150,300) and (200,400).The individual turnaround times are 100, 150, 200.The average turnaround time is 150

Generally a scheduling policy aims to keep average turnaroundtime as low as possible.

tr61 Multi-tasking 7th October 2019 18 / 33

Page 58: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Turnaround time

Turnaround time of a particular process is its duration in realtime, i.e. the difference between the time it is submitted forexecution and the time execution is complete.

Turnaround time of a group of processes is the average of theirindividual turnaround times.

Example:

We have 3 processes with respective start (submitted) and endtimes (100,200), (150,300) and (200,400).The individual turnaround times are 100, 150, 200.The average turnaround time is 150

Generally a scheduling policy aims to keep average turnaroundtime as low as possible.

tr61 Multi-tasking 7th October 2019 18 / 33

Page 59: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Turnaround time

Turnaround time of a particular process is its duration in realtime, i.e. the difference between the time it is submitted forexecution and the time execution is complete.

Turnaround time of a group of processes is the average of theirindividual turnaround times.

Example:

We have 3 processes with respective start (submitted) and endtimes (100,200), (150,300) and (200,400).The individual turnaround times are 100, 150, 200.The average turnaround time is 150

Generally a scheduling policy aims to keep average turnaroundtime as low as possible.

tr61 Multi-tasking 7th October 2019 18 / 33

Page 60: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Turnaround time

Turnaround time of a particular process is its duration in realtime, i.e. the difference between the time it is submitted forexecution and the time execution is complete.

Turnaround time of a group of processes is the average of theirindividual turnaround times.

Example:

We have 3 processes with respective start (submitted) and endtimes (100,200), (150,300) and (200,400).The individual turnaround times are 100, 150, 200.The average turnaround time is 150

Generally a scheduling policy aims to keep average turnaroundtime as low as possible.

tr61 Multi-tasking 7th October 2019 18 / 33

Page 61: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

CPU Usage

If all the processes are blocked waiting for completion of their I/Ooperations, the CPU stays idle.

A good scheduling policy minimises the idleness time.

CPU usage: given some amount of time, the percentage of timeduring which the CPU is busy.

Generally a scheduling policy aims to maximise CPU usage.

tr61 Multi-tasking 7th October 2019 19 / 33

Page 62: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

CPU Usage

If all the processes are blocked waiting for completion of their I/Ooperations, the CPU stays idle.

A good scheduling policy minimises the idleness time.

CPU usage: given some amount of time, the percentage of timeduring which the CPU is busy.

Generally a scheduling policy aims to maximise CPU usage.

tr61 Multi-tasking 7th October 2019 19 / 33

Page 63: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

CPU Usage

If all the processes are blocked waiting for completion of their I/Ooperations, the CPU stays idle.

A good scheduling policy minimises the idleness time.

CPU usage: given some amount of time, the percentage of timeduring which the CPU is busy.

Generally a scheduling policy aims to maximise CPU usage.

tr61 Multi-tasking 7th October 2019 19 / 33

Page 64: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

CPU Usage

If all the processes are blocked waiting for completion of their I/Ooperations, the CPU stays idle.

A good scheduling policy minimises the idleness time.

CPU usage: given some amount of time, the percentage of timeduring which the CPU is busy.

Generally a scheduling policy aims to maximise CPU usage.

tr61 Multi-tasking 7th October 2019 19 / 33

Page 65: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as usercommands.

E.g, in Linux, user requests are commands like cp, cd, ls, etc. orapplications.

The response time is average turnaround time of user requests.

The turnaround time of other processes (“batch processes”) isignored.

For interactive users response time should be minimised.

NOTE The terminology “response time” is not completelystandardized (like lots of CS terminology)

tr61 Multi-tasking 7th October 2019 20 / 33

Page 66: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as usercommands.

E.g, in Linux, user requests are commands like cp, cd, ls, etc. orapplications.

The response time is average turnaround time of user requests.

The turnaround time of other processes (“batch processes”) isignored.

For interactive users response time should be minimised.

NOTE The terminology “response time” is not completelystandardized (like lots of CS terminology)

tr61 Multi-tasking 7th October 2019 20 / 33

Page 67: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as usercommands.

E.g, in Linux, user requests are commands like cp, cd, ls, etc. orapplications.

The response time is average turnaround time of user requests.

The turnaround time of other processes (“batch processes”) isignored.

For interactive users response time should be minimised.

NOTE The terminology “response time” is not completelystandardized (like lots of CS terminology)

tr61 Multi-tasking 7th October 2019 20 / 33

Page 68: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as usercommands.

E.g, in Linux, user requests are commands like cp, cd, ls, etc. orapplications.

The response time is average turnaround time of user requests.

The turnaround time of other processes (“batch processes”) isignored.

For interactive users response time should be minimised.

NOTE The terminology “response time” is not completelystandardized (like lots of CS terminology)

tr61 Multi-tasking 7th October 2019 20 / 33

Page 69: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as usercommands.

E.g, in Linux, user requests are commands like cp, cd, ls, etc. orapplications.

The response time is average turnaround time of user requests.

The turnaround time of other processes (“batch processes”) isignored.

For interactive users response time should be minimised.

NOTE The terminology “response time” is not completelystandardized (like lots of CS terminology)

tr61 Multi-tasking 7th October 2019 20 / 33

Page 70: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as usercommands.

E.g, in Linux, user requests are commands like cp, cd, ls, etc. orapplications.

The response time is average turnaround time of user requests.

The turnaround time of other processes (“batch processes”) isignored.

For interactive users response time should be minimised.

NOTE The terminology “response time” is not completelystandardized (like lots of CS terminology)

tr61 Multi-tasking 7th October 2019 20 / 33

Page 71: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Types of operating systems

Batch systems:Run a series of programs without human intervention.Examples: scientific computing, salary calculation in bigcompanies.Turnaround time and CPU usage are more important measuresthan response time.Example: recoding a video

Interactive systems:Designed to continuously communicate with the user.Examples: desktop workstations/laptops, etc; mobile phones,tablets . . .Response time is more important measure than general averageturnaround time or CPU utilisation.Users often don’t care if a background task takes a little bit longer,provided the thing they clicked on shows some sort of responsealmost immediately (i.e., without noticable delay)

tr61 Multi-tasking 7th October 2019 21 / 33

Page 72: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking The Dispatcher

Types of operating systems

Batch systems:Run a series of programs without human intervention.Examples: scientific computing, salary calculation in bigcompanies.Turnaround time and CPU usage are more important measuresthan response time.Example: recoding a video

Interactive systems:Designed to continuously communicate with the user.Examples: desktop workstations/laptops, etc; mobile phones,tablets . . .Response time is more important measure than general averageturnaround time or CPU utilisation.Users often don’t care if a background task takes a little bit longer,provided the thing they clicked on shows some sort of responsealmost immediately (i.e., without noticable delay)

tr61 Multi-tasking 7th October 2019 21 / 33

Page 73: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Generalisations

I/O is not relevant when comparing different scheduling policies— when a process blocks on I/O it is no longer available forscheduling.

Consider each long running process to be split into short burststhat require no I/O; treat these bursts as separate processes asfar as the scheduler is concerned.

tr61 Multi-tasking 7th October 2019 22 / 33

Page 74: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Generalisations

I/O is not relevant when comparing different scheduling policies— when a process blocks on I/O it is no longer available forscheduling.

Consider each long running process to be split into short burststhat require no I/O; treat these bursts as separate processes asfar as the scheduler is concerned.

tr61 Multi-tasking 7th October 2019 22 / 33

Page 75: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

First come first served (FCFS) policy

This policy is non-preemptive.

The first READY process in the queue is started.

The process releases the CPU when it finishes or it BLOCKs onI/O

When a BLOCKed process returns to READY state, it goes to theback of the READY queue.

tr61 Multi-tasking 7th October 2019 23 / 33

Page 76: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

First come first served (FCFS) policy

This policy is non-preemptive.

The first READY process in the queue is started.

The process releases the CPU when it finishes or it BLOCKs onI/O

When a BLOCKed process returns to READY state, it goes to theback of the READY queue.

tr61 Multi-tasking 7th October 2019 23 / 33

Page 77: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

First come first served (FCFS) policy

This policy is non-preemptive.

The first READY process in the queue is started.

The process releases the CPU when it finishes or it BLOCKs onI/O

When a BLOCKed process returns to READY state, it goes to theback of the READY queue.

tr61 Multi-tasking 7th October 2019 23 / 33

Page 78: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

First come first served (FCFS) policy

This policy is non-preemptive.

The first READY process in the queue is started.

The process releases the CPU when it finishes or it BLOCKs onI/O

When a BLOCKed process returns to READY state, it goes to theback of the READY queue.

tr61 Multi-tasking 7th October 2019 23 / 33

Page 79: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Advantages and disadvantages of the FCFS policy

Advantages:

Easy to implement.No indefinite waiting (when a process can potentially wait foreverto be run).

Disadvantage:

likely to be un-responsive; becauseturnaround time of short processes can be much longer than theiractual execution time.

tr61 Multi-tasking 7th October 2019 24 / 33

Page 80: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Advantages and disadvantages of the FCFS policy

Advantages:

Easy to implement.No indefinite waiting (when a process can potentially wait foreverto be run).

Disadvantage:

likely to be un-responsive; becauseturnaround time of short processes can be much longer than theiractual execution time.

tr61 Multi-tasking 7th October 2019 24 / 33

Page 81: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Shortest job first (SJF) policy

The policy is non-preemptive.

Requires knowledge about the runtime of processes before theyactually execute.

From the queue of READY processes choose the one whose(expected) runtime is smallest.

Otherwise, as for FCFS.

tr61 Multi-tasking 7th October 2019 25 / 33

Page 82: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Shortest job first (SJF) policy

The policy is non-preemptive.

Requires knowledge about the runtime of processes before theyactually execute.

From the queue of READY processes choose the one whose(expected) runtime is smallest.

Otherwise, as for FCFS.

tr61 Multi-tasking 7th October 2019 25 / 33

Page 83: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Shortest job first (SJF) policy

The policy is non-preemptive.

Requires knowledge about the runtime of processes before theyactually execute.

From the queue of READY processes choose the one whose(expected) runtime is smallest.

Otherwise, as for FCFS.

tr61 Multi-tasking 7th October 2019 25 / 33

Page 84: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Shortest job first (SJF) policy

The policy is non-preemptive.

Requires knowledge about the runtime of processes before theyactually execute.

From the queue of READY processes choose the one whose(expected) runtime is smallest.

Otherwise, as for FCFS.

tr61 Multi-tasking 7th October 2019 25 / 33

Page 85: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Advantages and disadvantages of the SJF policy

Advantage: favourable to processes with short runtimes.

Disadvantages:

Requires advance knowledge about runtimes.Indefinite waiting is possible: a long process can wait a long timeif multiple new short processes arrive.

tr61 Multi-tasking 7th October 2019 26 / 33

Page 86: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Advantages and disadvantages of the SJF policy

Advantage: favourable to processes with short runtimes.

Disadvantages:

Requires advance knowledge about runtimes.Indefinite waiting is possible: a long process can wait a long timeif multiple new short processes arrive.

tr61 Multi-tasking 7th October 2019 26 / 33

Page 87: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Shortest remaining time first (SRTF) policy

This is a preemptive policy.

For every process, requires knowledge of the remaining runtime.

Very similar to SJF.

The differences are:

Arrival of a process in the READY queue leads to an interrupt.Chooses process with the shortest remaining time instead ofshortest overall time.So if a process with shorter remaining time than the current onearrives in the READY queue, then the current process ispreempted.

tr61 Multi-tasking 7th October 2019 27 / 33

Page 88: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Shortest remaining time first (SRTF) policy

This is a preemptive policy.

For every process, requires knowledge of the remaining runtime.

Very similar to SJF.

The differences are:

Arrival of a process in the READY queue leads to an interrupt.Chooses process with the shortest remaining time instead ofshortest overall time.So if a process with shorter remaining time than the current onearrives in the READY queue, then the current process ispreempted.

tr61 Multi-tasking 7th October 2019 27 / 33

Page 89: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Shortest remaining time first (SRTF) policy

This is a preemptive policy.

For every process, requires knowledge of the remaining runtime.

Very similar to SJF.

The differences are:

Arrival of a process in the READY queue leads to an interrupt.Chooses process with the shortest remaining time instead ofshortest overall time.So if a process with shorter remaining time than the current onearrives in the READY queue, then the current process ispreempted.

tr61 Multi-tasking 7th October 2019 27 / 33

Page 90: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Shortest remaining time first (SRTF) policy

This is a preemptive policy.

For every process, requires knowledge of the remaining runtime.

Very similar to SJF.

The differences are:

Arrival of a process in the READY queue leads to an interrupt.Chooses process with the shortest remaining time instead ofshortest overall time.So if a process with shorter remaining time than the current onearrives in the READY queue, then the current process ispreempted.

tr61 Multi-tasking 7th October 2019 27 / 33

Page 91: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Shortest remaining time first (SRTF) policy

This is a preemptive policy.

For every process, requires knowledge of the remaining runtime.

Very similar to SJF.

The differences are:

Arrival of a process in the READY queue leads to an interrupt.Chooses process with the shortest remaining time instead ofshortest overall time.So if a process with shorter remaining time than the current onearrives in the READY queue, then the current process ispreempted.

tr61 Multi-tasking 7th October 2019 27 / 33

Page 92: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Advantages and disadvantages of the SRTF policy.

Advantage: even more sensitive to the short processes than SJF.

Disadvantages: requires advance knowledge of runtimes;indefinite waiting is still possible.

tr61 Multi-tasking 7th October 2019 28 / 33

Page 93: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Advantages and disadvantages of the SRTF policy.

Advantage: even more sensitive to the short processes than SJF.

Disadvantages: requires advance knowledge of runtimes;indefinite waiting is still possible.

tr61 Multi-tasking 7th October 2019 28 / 33

Page 94: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Round-Robin scheduling policy

This is a preemptive policy used in interactive systems.

Each process in turn gets the CPU for a fixed time quantum(typically 10–100 ms).

When the quantum finishes: a timeout interrupt occurs and theprocess goes to the back of the READY queue.

New processes join the back of the READY queue.

tr61 Multi-tasking 7th October 2019 29 / 33

Page 95: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Round-Robin scheduling policy

This is a preemptive policy used in interactive systems.

Each process in turn gets the CPU for a fixed time quantum(typically 10–100 ms).

When the quantum finishes: a timeout interrupt occurs and theprocess goes to the back of the READY queue.

New processes join the back of the READY queue.

tr61 Multi-tasking 7th October 2019 29 / 33

Page 96: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Round-Robin scheduling policy

This is a preemptive policy used in interactive systems.

Each process in turn gets the CPU for a fixed time quantum(typically 10–100 ms).

When the quantum finishes: a timeout interrupt occurs and theprocess goes to the back of the READY queue.

New processes join the back of the READY queue.

tr61 Multi-tasking 7th October 2019 29 / 33

Page 97: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Round-Robin scheduling policy

This is a preemptive policy used in interactive systems.

Each process in turn gets the CPU for a fixed time quantum(typically 10–100 ms).

When the quantum finishes: a timeout interrupt occurs and theprocess goes to the back of the READY queue.

New processes join the back of the READY queue.

tr61 Multi-tasking 7th October 2019 29 / 33

Page 98: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Quantum duration for round-robin

Context switch: is the procedure of replacing the currentlyrunning process by another one. This takes a non-zero amountof time.

If the quantum is too short (similar to the time taken for acontext switch), the CPU spends a large proportion of its timejust context switching.

If the quantum is too long then the response time may becomeunacceptable (behaves like FCFS).

tr61 Multi-tasking 7th October 2019 30 / 33

Page 99: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Quantum duration for round-robin

Context switch: is the procedure of replacing the currentlyrunning process by another one. This takes a non-zero amountof time.

If the quantum is too short (similar to the time taken for acontext switch), the CPU spends a large proportion of its timejust context switching.

If the quantum is too long then the response time may becomeunacceptable (behaves like FCFS).

tr61 Multi-tasking 7th October 2019 30 / 33

Page 100: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Quantum duration for round-robin

Context switch: is the procedure of replacing the currentlyrunning process by another one. This takes a non-zero amountof time.

If the quantum is too short (similar to the time taken for acontext switch), the CPU spends a large proportion of its timejust context switching.

If the quantum is too long then the response time may becomeunacceptable (behaves like FCFS).

tr61 Multi-tasking 7th October 2019 30 / 33

Page 101: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Quantum duration for round-robin

Context switch: is the procedure of replacing the currentlyrunning process by another one. This takes a non-zero amountof time.

If the quantum is too short (similar to the time taken for acontext switch), the CPU spends a large proportion of its timejust context switching.

If the quantum is too long then the response time may becomeunacceptable (behaves like FCFS).

tr61 Multi-tasking 7th October 2019 30 / 33

Page 102: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Multi-level schedulers

Most real systems use multiple levels of job queue.

For example, 3 queues, high, medium and low priority READYqueues.

Each queue can operate its own policy; e.g. round-robin, FCFS,etc.

Processes that are pre-empted move between the differentlevels depending on various criteria, e.g. age of the process.

Higher level queues are given more priority by the scheduler,e.g. shorter quantum, but more regularly serviced.

tr61 Multi-tasking 7th October 2019 31 / 33

Page 103: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Multi-level schedulers

Most real systems use multiple levels of job queue.

For example, 3 queues, high, medium and low priority READYqueues.

Each queue can operate its own policy; e.g. round-robin, FCFS,etc.

Processes that are pre-empted move between the differentlevels depending on various criteria, e.g. age of the process.

Higher level queues are given more priority by the scheduler,e.g. shorter quantum, but more regularly serviced.

tr61 Multi-tasking 7th October 2019 31 / 33

Page 104: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Multi-level schedulers

Most real systems use multiple levels of job queue.

For example, 3 queues, high, medium and low priority READYqueues.

Each queue can operate its own policy; e.g. round-robin, FCFS,etc.

Processes that are pre-empted move between the differentlevels depending on various criteria, e.g. age of the process.

Higher level queues are given more priority by the scheduler,e.g. shorter quantum, but more regularly serviced.

tr61 Multi-tasking 7th October 2019 31 / 33

Page 105: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Multi-level schedulers

Most real systems use multiple levels of job queue.

For example, 3 queues, high, medium and low priority READYqueues.

Each queue can operate its own policy; e.g. round-robin, FCFS,etc.

Processes that are pre-empted move between the differentlevels depending on various criteria, e.g. age of the process.

Higher level queues are given more priority by the scheduler,e.g. shorter quantum, but more regularly serviced.

tr61 Multi-tasking 7th October 2019 31 / 33

Page 106: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Scheduling policies

Multi-level schedulers

Most real systems use multiple levels of job queue.

For example, 3 queues, high, medium and low priority READYqueues.

Each queue can operate its own policy; e.g. round-robin, FCFS,etc.

Processes that are pre-empted move between the differentlevels depending on various criteria, e.g. age of the process.

Higher level queues are given more priority by the scheduler,e.g. shorter quantum, but more regularly serviced.

tr61 Multi-tasking 7th October 2019 31 / 33

Page 107: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

How to implement multi-tasking Real world schedulers

Schedulers used in actual OSes

MS-DOS: single process (no scheduling);

Windows 3.1: non-preemptive;

Windows NT (and newer): multi-level feedback queue;

Linux: multi-level feedback queue until 2.6; then the O(1)scheduler; now uses completely fair scheduler (CFS)(since 2007);

MacOS 9: Round-robin, with cooperative non-preemptive threads;

MacOS X: multi-level feedback queue;

Android: CFS, but with processes grouped into 5 priority levels;aggressively kills “idle” processes;

iOS (iphone): none until iOS4; cooperative multi-tasking since then.

tr61 Multi-tasking 7th October 2019 32 / 33

Page 108: CO2101 Processes and Multi-tasking - Tom Ridge · 2019-10-08 · CO2101 — Processes and Multi-tasking Tom Ridge (tr61) 7th October 2019 tr61 Multi-tasking 7th October 2019 1/33

Summary

Summary

What a process is.

What interrupts are.

The Dispatcher implements scheduling policy.

Compared various simple scheduling policies.

tr61 Multi-tasking 7th October 2019 33 / 33