CO2101 Processes and Multi-tasking Tom Ridge (tr61) 7th October - - PowerPoint PPT Presentation

co2101 processes and multi tasking
SMART_READER_LITE
LIVE PREVIEW

CO2101 Processes and Multi-tasking Tom Ridge (tr61) 7th October - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

CO2101 — Processes and Multi-tasking

Tom Ridge (tr61) 7th October 2019

tr61 Multi-tasking 7th October 2019 1 / 33

slide-2
SLIDE 2

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 a time

1

Fetch next instruction from memory

2

Execute the instruction

Execute Start process Fetch instruction instruction Process terminated

tr61 Multi-tasking 7th October 2019 2 / 33

slide-3
SLIDE 3

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 a time

1

Fetch next instruction from memory

2

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

slide-4
SLIDE 4

How to implement multi-tasking Why multi-tasking?

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

For convenience of the user.

tr61 Multi-tasking 7th October 2019 3 / 33

slide-5
SLIDE 5

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 of background tasks.

tr61 Multi-tasking 7th October 2019 3 / 33

slide-6
SLIDE 6

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 of background tasks. For better CPU utilisation.

tr61 Multi-tasking 7th October 2019 3 / 33

slide-7
SLIDE 7

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

tr61 Multi-tasking 7th October 2019 4 / 33

slide-8
SLIDE 8

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 is achieved by the CPU performing a little bit of each process then suspending it and moving to the next.

tr61 Multi-tasking 7th October 2019 4 / 33

slide-9
SLIDE 9

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 is achieved by the CPU performing a little bit of each process then suspending it and moving to the next. CPU needs a mechanism to determine that a particular process can be suspended to allow the next one to take over.

tr61 Multi-tasking 7th October 2019 4 / 33

slide-10
SLIDE 10

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 is achieved by the CPU performing a little bit of each process then suspending it and moving to the next. CPU needs a mechanism to determine that a particular process can be suspended to allow the next one to take over. Mechanism is interrupts

tr61 Multi-tasking 7th October 2019 4 / 33

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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 process and switch to the special process for handling the interrupt.

tr61 Multi-tasking 7th October 2019 6 / 33

slide-13
SLIDE 13

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 process and 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 process is the next to run.

tr61 Multi-tasking 7th October 2019 6 / 33

slide-14
SLIDE 14

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 other state change.

tr61 Multi-tasking 7th October 2019 7 / 33

slide-15
SLIDE 15

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 other state change. Timer: by a clock within the CPU; used to alert the OS at pre-set intervals for time critical activities.

tr61 Multi-tasking 7th October 2019 7 / 33

slide-16
SLIDE 16

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 other state change. Timer: by a clock within the CPU; used to alert the OS at pre-set intervals for time critical activities. Hardware Error: by hardware faults.

tr61 Multi-tasking 7th October 2019 7 / 33

slide-17
SLIDE 17

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 other state change. Timer: by a clock within the CPU; used to alert the OS at pre-set intervals 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

slide-18
SLIDE 18

How to implement multi-tasking Programs and processes

Programs and processes

A program is code ready to execute on a system.

tr61 Multi-tasking 7th October 2019 8 / 33

slide-19
SLIDE 19

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, a particular instance of a program together with its data.

tr61 Multi-tasking 7th October 2019 8 / 33

slide-20
SLIDE 20

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, a particular instance of a program together with its data. There may be several copies of the same program running simultaneously as different processes.

tr61 Multi-tasking 7th October 2019 8 / 33

slide-21
SLIDE 21

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, a particular instance of a program together with its data. There may be several copies of the same program running simultaneously as different processes. The OS stores information about each process in a process control block (PCB) (also called process descriptor).

tr61 Multi-tasking 7th October 2019 8 / 33

slide-22
SLIDE 22

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, a particular instance of a program together with its data. There may be several copies of the same program running simultaneously as different processes. The OS stores information about each process in a process control block (PCB) (also called process descriptor). Multi-tasking: is the simultaneous running of two or more processes.

tr61 Multi-tasking 7th October 2019 8 / 33

slide-23
SLIDE 23

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

slide-24
SLIDE 24

How to implement multi-tasking Programs and processes

Process Creation

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

tr61 Multi-tasking 7th October 2019 10 / 33

slide-25
SLIDE 25

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 the execution of this process. OS also allocates resources for the process. So, a process consists of machine code in memory and a PCB.

tr61 Multi-tasking 7th October 2019 10 / 33

slide-26
SLIDE 26

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 the execution 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:

tr61 Multi-tasking 7th October 2019 10 / 33

slide-27
SLIDE 27

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 the execution 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 the created process is the child The child can also spawn further processes, forming a tree of processes

tr61 Multi-tasking 7th October 2019 10 / 33

slide-28
SLIDE 28

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

tr61 Multi-tasking 7th October 2019 11 / 33

slide-29
SLIDE 29

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 the user could do anything the OS can.

tr61 Multi-tasking 7th October 2019 11 / 33

slide-30
SLIDE 30

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 the user could do anything the OS can. The solution is for the CPU to have a special operating mode, kernel or supervisor mode, which can only be invoked by the OS.

tr61 Multi-tasking 7th October 2019 11 / 33

slide-31
SLIDE 31

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 the user could do anything the OS can. The solution is for the CPU to have a special operating mode, kernel or supervisor mode, which can only be invoked by the OS. In kernel mode, certain privileged instructions, e.g, interrupt processing, dispatcher, memory allocation, are possible; normal processes run in user mode

tr61 Multi-tasking 7th October 2019 11 / 33

slide-32
SLIDE 32

How to implement multi-tasking The Dispatcher

The Dispatcher

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

tr61 Multi-tasking 7th October 2019 12 / 33

slide-33
SLIDE 33

How to implement multi-tasking The Dispatcher

The Dispatcher

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

1

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

tr61 Multi-tasking 7th October 2019 12 / 33

slide-34
SLIDE 34

How to implement multi-tasking The Dispatcher

The Dispatcher

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

1

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

2

Save the state of the current process in its PCB

tr61 Multi-tasking 7th October 2019 12 / 33

slide-35
SLIDE 35

How to implement multi-tasking The Dispatcher

The Dispatcher

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

1

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

2

Save the state of the current process in its PCB

3

Retrieve the state of the most suitable process from its PCB

tr61 Multi-tasking 7th October 2019 12 / 33

slide-36
SLIDE 36

How to implement multi-tasking The Dispatcher

The Dispatcher

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

1

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

2

Save the state of the current process in its PCB

3

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

tr61 Multi-tasking 7th October 2019 12 / 33

slide-37
SLIDE 37

How to implement multi-tasking The Dispatcher

The Dispatcher

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

1

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

2

Save the state of the current process in its PCB

3

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-)starting another process is called a context switch

tr61 Multi-tasking 7th October 2019 12 / 33

slide-38
SLIDE 38

How to implement multi-tasking The Dispatcher

States of processes

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

tr61 Multi-tasking 7th October 2019 13 / 33

slide-39
SLIDE 39

How to implement multi-tasking The Dispatcher

States of processes

The running process is in the RUNNING state; only one RUNNING process 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.

tr61 Multi-tasking 7th October 2019 13 / 33

slide-40
SLIDE 40

How to implement multi-tasking The Dispatcher

States of processes

The running process is in the RUNNING state; only one RUNNING process 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 running process or by an I/O device signalling completion of an I/O

  • peration

When a process starts an I/O operation it goes to the BLOCKED state When an I/O operation completes for a process, it goes to READY state

tr61 Multi-tasking 7th October 2019 13 / 33

slide-41
SLIDE 41

How to implement multi-tasking The Dispatcher

States of processes

The running process is in the RUNNING state; only one RUNNING process 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 running process or by an I/O device signalling completion of an I/O

  • peration

When a process starts an I/O operation it goes to the BLOCKED state When an I/O operation completes for a process, it goes to READY state The dispatcher can select a process to run only if it is READY.

tr61 Multi-tasking 7th October 2019 13 / 33

slide-42
SLIDE 42

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

slide-43
SLIDE 43

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)

  • r BLOCKED (waiting on I/O).

tr61 Multi-tasking 7th October 2019 15 / 33

slide-44
SLIDE 44

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)

  • r BLOCKED (waiting on I/O).

tr61 Multi-tasking 7th October 2019 15 / 33

slide-45
SLIDE 45

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)

  • r BLOCKED (waiting on I/O).

tr61 Multi-tasking 7th October 2019 15 / 33

slide-46
SLIDE 46

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)

  • r BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process.

tr61 Multi-tasking 7th October 2019 15 / 33

slide-47
SLIDE 47

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)

  • r BLOCKED (waiting on I/O).

Interrupts are used to stop the currently RUNNING process. A clock interrupt causes the current process to become READY.

tr61 Multi-tasking 7th October 2019 15 / 33

slide-48
SLIDE 48

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)

  • r 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, upon completion of the I/O it becomes READY

tr61 Multi-tasking 7th October 2019 15 / 33

slide-49
SLIDE 49

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)

  • r 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, upon completion of the I/O it becomes READY The Dispatcher chooses the next process to run from the queue of READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

slide-50
SLIDE 50

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)

  • r 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, upon completion of the I/O it becomes READY The Dispatcher chooses the next process to run from the queue of READY processes. Scheduling: is the policy for choosing among the READY processes.

tr61 Multi-tasking 7th October 2019 15 / 33

slide-51
SLIDE 51

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.

tr61 Multi-tasking 7th October 2019 16 / 33

slide-52
SLIDE 52

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.

tr61 Multi-tasking 7th October 2019 16 / 33

slide-53
SLIDE 53

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

slide-54
SLIDE 54

How to implement multi-tasking The Dispatcher

Efficiency measures of scheduling policies

There are many possible ways to measure efficiency of scheduling policies. We consider the following 3:

Turnaround time. CPU usage. Response time.

tr61 Multi-tasking 7th October 2019 17 / 33

slide-55
SLIDE 55

How to implement multi-tasking The Dispatcher

Efficiency measures of scheduling policies

There are many possible ways to measure efficiency of scheduling policies. We consider the following 3:

Turnaround time. CPU usage. Response time.

tr61 Multi-tasking 7th October 2019 17 / 33

slide-56
SLIDE 56

How to implement multi-tasking The Dispatcher

Efficiency measures of scheduling policies

There are many possible ways to measure efficiency of scheduling policies. We consider the following 3:

Turnaround time. CPU usage. Response time.

tr61 Multi-tasking 7th October 2019 17 / 33

slide-57
SLIDE 57

How to implement multi-tasking The Dispatcher

Turnaround time

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

tr61 Multi-tasking 7th October 2019 18 / 33

slide-58
SLIDE 58

How to implement multi-tasking The Dispatcher

Turnaround time

Turnaround time of a particular process is its duration in real time, i.e. the difference between the time it is submitted for execution and the time execution is complete. Turnaround time of a group of processes is the average of their individual turnaround times.

tr61 Multi-tasking 7th October 2019 18 / 33

slide-59
SLIDE 59

How to implement multi-tasking The Dispatcher

Turnaround time

Turnaround time of a particular process is its duration in real time, i.e. the difference between the time it is submitted for execution and the time execution is complete. Turnaround time of a group of processes is the average of their individual turnaround times. Example:

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

tr61 Multi-tasking 7th October 2019 18 / 33

slide-60
SLIDE 60

How to implement multi-tasking The Dispatcher

Turnaround time

Turnaround time of a particular process is its duration in real time, i.e. the difference between the time it is submitted for execution and the time execution is complete. Turnaround time of a group of processes is the average of their individual turnaround times. Example:

We have 3 processes with respective start (submitted) and end times (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 turnaround time as low as possible.

tr61 Multi-tasking 7th October 2019 18 / 33

slide-61
SLIDE 61

How to implement multi-tasking The Dispatcher

CPU Usage

If all the processes are blocked waiting for completion of their I/O

  • perations, the CPU stays idle.

tr61 Multi-tasking 7th October 2019 19 / 33

slide-62
SLIDE 62

How to implement multi-tasking The Dispatcher

CPU Usage

If all the processes are blocked waiting for completion of their I/O

  • perations, the CPU stays idle.

A good scheduling policy minimises the idleness time.

tr61 Multi-tasking 7th October 2019 19 / 33

slide-63
SLIDE 63

How to implement multi-tasking The Dispatcher

CPU Usage

If all the processes are blocked waiting for completion of their I/O

  • perations, the CPU stays idle.

A good scheduling policy minimises the idleness time. CPU usage: given some amount of time, the percentage of time during which the CPU is busy.

tr61 Multi-tasking 7th October 2019 19 / 33

slide-64
SLIDE 64

How to implement multi-tasking The Dispatcher

CPU Usage

If all the processes are blocked waiting for completion of their I/O

  • perations, the CPU stays idle.

A good scheduling policy minimises the idleness time. CPU usage: given some amount of time, the percentage of time during which the CPU is busy. Generally a scheduling policy aims to maximise CPU usage.

tr61 Multi-tasking 7th October 2019 19 / 33

slide-65
SLIDE 65

How to implement multi-tasking The Dispatcher

Response time

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

tr61 Multi-tasking 7th October 2019 20 / 33

slide-66
SLIDE 66

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as user commands. E.g, in Linux, user requests are commands like cp, cd, ls, etc. or applications.

tr61 Multi-tasking 7th October 2019 20 / 33

slide-67
SLIDE 67

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as user commands. E.g, in Linux, user requests are commands like cp, cd, ls, etc. or applications. The response time is average turnaround time of user requests.

tr61 Multi-tasking 7th October 2019 20 / 33

slide-68
SLIDE 68

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as user commands. E.g, in Linux, user requests are commands like cp, cd, ls, etc. or applications. The response time is average turnaround time of user requests. The turnaround time of other processes (“batch processes”) is ignored.

tr61 Multi-tasking 7th October 2019 20 / 33

slide-69
SLIDE 69

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as user commands. E.g, in Linux, user requests are commands like cp, cd, ls, etc. or applications. The response time is average turnaround time of user requests. The turnaround time of other processes (“batch processes”) is ignored. For interactive users response time should be minimised.

tr61 Multi-tasking 7th October 2019 20 / 33

slide-70
SLIDE 70

How to implement multi-tasking The Dispatcher

Response time

The OS (or something else) classifies some processes as user commands. E.g, in Linux, user requests are commands like cp, cd, ls, etc. or applications. The response time is average turnaround time of user requests. The turnaround time of other processes (“batch processes”) is ignored. For interactive users response time should be minimised. NOTE The terminology “response time” is not completely standardized (like lots of CS terminology)

tr61 Multi-tasking 7th October 2019 20 / 33

slide-71
SLIDE 71

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 big companies. Turnaround time and CPU usage are more important measures than response time. Example: recoding a video

tr61 Multi-tasking 7th October 2019 21 / 33

slide-72
SLIDE 72

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 big companies. Turnaround time and CPU usage are more important measures than 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 average turnaround 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 response almost immediately (i.e., without noticable delay)

tr61 Multi-tasking 7th October 2019 21 / 33

slide-73
SLIDE 73

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 for scheduling.

tr61 Multi-tasking 7th October 2019 22 / 33

slide-74
SLIDE 74

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 for scheduling. Consider each long running process to be split into short bursts that require no I/O; treat these bursts as separate processes as far as the scheduler is concerned.

tr61 Multi-tasking 7th October 2019 22 / 33

slide-75
SLIDE 75

How to implement multi-tasking Scheduling policies

First come first served (FCFS) policy

This policy is non-preemptive.

tr61 Multi-tasking 7th October 2019 23 / 33

slide-76
SLIDE 76

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.

tr61 Multi-tasking 7th October 2019 23 / 33

slide-77
SLIDE 77

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 on I/O

tr61 Multi-tasking 7th October 2019 23 / 33

slide-78
SLIDE 78

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 on I/O When a BLOCKed process returns to READY state, it goes to the back of the READY queue.

tr61 Multi-tasking 7th October 2019 23 / 33

slide-79
SLIDE 79

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 forever to be run).

tr61 Multi-tasking 7th October 2019 24 / 33

slide-80
SLIDE 80

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 forever to be run).

Disadvantage:

likely to be un-responsive; because turnaround time of short processes can be much longer than their actual execution time.

tr61 Multi-tasking 7th October 2019 24 / 33

slide-81
SLIDE 81

How to implement multi-tasking Scheduling policies

Shortest job first (SJF) policy

The policy is non-preemptive.

tr61 Multi-tasking 7th October 2019 25 / 33

slide-82
SLIDE 82

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 they actually execute.

tr61 Multi-tasking 7th October 2019 25 / 33

slide-83
SLIDE 83

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 they actually execute. From the queue of READY processes choose the one whose (expected) runtime is smallest.

tr61 Multi-tasking 7th October 2019 25 / 33

slide-84
SLIDE 84

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 they actually 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

slide-85
SLIDE 85

How to implement multi-tasking Scheduling policies

Advantages and disadvantages of the SJF policy

Advantage: favourable to processes with short runtimes.

tr61 Multi-tasking 7th October 2019 26 / 33

slide-86
SLIDE 86

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 time if multiple new short processes arrive.

tr61 Multi-tasking 7th October 2019 26 / 33

slide-87
SLIDE 87

How to implement multi-tasking Scheduling policies

Shortest remaining time first (SRTF) policy

This is a preemptive policy.

tr61 Multi-tasking 7th October 2019 27 / 33

slide-88
SLIDE 88

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.

tr61 Multi-tasking 7th October 2019 27 / 33

slide-89
SLIDE 89

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 of shortest overall time. So if a process with shorter remaining time than the current one arrives in the READY queue, then the current process is preempted.

tr61 Multi-tasking 7th October 2019 27 / 33

slide-90
SLIDE 90

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 of shortest overall time. So if a process with shorter remaining time than the current one arrives in the READY queue, then the current process is preempted.

tr61 Multi-tasking 7th October 2019 27 / 33

slide-91
SLIDE 91

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 of shortest overall time. So if a process with shorter remaining time than the current one arrives in the READY queue, then the current process is preempted.

tr61 Multi-tasking 7th October 2019 27 / 33

slide-92
SLIDE 92

How to implement multi-tasking Scheduling policies

Advantages and disadvantages of the SRTF policy.

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

tr61 Multi-tasking 7th October 2019 28 / 33

slide-93
SLIDE 93

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

slide-94
SLIDE 94

How to implement multi-tasking Scheduling policies

Round-Robin scheduling policy

This is a preemptive policy used in interactive systems.

tr61 Multi-tasking 7th October 2019 29 / 33

slide-95
SLIDE 95

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

tr61 Multi-tasking 7th October 2019 29 / 33

slide-96
SLIDE 96

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 the process goes to the back of the READY queue.

tr61 Multi-tasking 7th October 2019 29 / 33

slide-97
SLIDE 97

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 the process 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

slide-98
SLIDE 98

How to implement multi-tasking Scheduling policies

Quantum duration for round-robin

Context switch: is the procedure of replacing the currently running process by another one. This takes a non-zero amount

  • f time.

tr61 Multi-tasking 7th October 2019 30 / 33

slide-99
SLIDE 99

How to implement multi-tasking Scheduling policies

Quantum duration for round-robin

Context switch: is the procedure of replacing the currently running process by another one. This takes a non-zero amount

  • f time.

tr61 Multi-tasking 7th October 2019 30 / 33

slide-100
SLIDE 100

How to implement multi-tasking Scheduling policies

Quantum duration for round-robin

Context switch: is the procedure of replacing the currently running process by another one. This takes a non-zero amount

  • f time.

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

tr61 Multi-tasking 7th October 2019 30 / 33

slide-101
SLIDE 101

How to implement multi-tasking Scheduling policies

Quantum duration for round-robin

Context switch: is the procedure of replacing the currently running process by another one. This takes a non-zero amount

  • f time.

If the quantum is too short (similar to the time taken for a context switch), the CPU spends a large proportion of its time just context switching. If the quantum is too long then the response time may become unacceptable (behaves like FCFS).

tr61 Multi-tasking 7th October 2019 30 / 33

slide-102
SLIDE 102

How to implement multi-tasking Scheduling policies

Multi-level schedulers

Most real systems use multiple levels of job queue.

tr61 Multi-tasking 7th October 2019 31 / 33

slide-103
SLIDE 103

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 READY queues.

tr61 Multi-tasking 7th October 2019 31 / 33

slide-104
SLIDE 104

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 READY queues. Each queue can operate its own policy; e.g. round-robin, FCFS, etc.

tr61 Multi-tasking 7th October 2019 31 / 33

slide-105
SLIDE 105

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 READY queues. Each queue can operate its own policy; e.g. round-robin, FCFS, etc. Processes that are pre-empted move between the different levels depending on various criteria, e.g. age of the process.

tr61 Multi-tasking 7th October 2019 31 / 33

slide-106
SLIDE 106

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 READY queues. Each queue can operate its own policy; e.g. round-robin, FCFS, etc. Processes that are pre-empted move between the different levels 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

slide-107
SLIDE 107

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

slide-108
SLIDE 108

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