avishai wool lecture 2 - 1 priority scheduling idea: jobs are assigned priorities. always, the job...

45
Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling policies are priority scheduling! Question: How to assign priorities? priority 1 priority 2 priority M

Post on 23-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 2 - 1

Priority Scheduling

Idea: Jobs are assigned priorities.Always, the job with the highest priority runs.

Note: All scheduling policies are priority scheduling!

Question: How to assign priorities?

priority 1

priority 2

priority M

Page 2: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 2 - 2

Example for Priorities

Static priorities can lead to starvation!

Page 3: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 2 - 3

Dynamic Priorities

Example: multilevel feedback

Page 4: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 4

Introduction to Systems Programming Lecture 3

Threads

Page 5: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 5

ProcessesThe Process Model

• Multiprogramming of four programs• Conceptual model of 4 independent, sequential processes• Only one program active at any instant

Page 6: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 6

Concepts of a Process

1. Group of resources– Address space– Program image– file handles, child processes, accounting info…

2. Thread of execution– Instruction address (program counter)– Registers and CPU state– Stack of called procedures

Page 7: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 7

Separating the concepts

• Think of the two concepts separately.

• A thread executes in a process

• … but allow multiple threads in one process.

• All threads share the same address space.

• Every process has at least one thread.

• Also called lightweight process.

Page 8: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 8

Processes vs. Threads• Computer• Process• Shared resources:

– Physical memory

– Disk

– Printer/keyboard/mouse

• Process• Thread• Shared resources:

– Address space

– I/O Device handles

Page 9: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 9

The Thread Model (1)

(a) Three processes each with one thread(b) One process with three threads

Page 10: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 10

Properties of Threads

• Pseudo-parallel: seem to run in parallel, but really take turns.

• No protection between threads:– Share the same address space– Supposed to cooperate

Page 11: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 11

The Thread Model (2)

• Items shared by all threads in a process

• Items private to each thread

Page 12: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 12

The Thread Model (3)

Each thread has its own stack

Page 13: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 13

Example 1: Word Processor

• Edit p.1 need to reformat all pages

• Thread 1: interact with user

• Thread 2: reformat (in background)

• Thread 3: periodic backups

Page 14: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 14

Word Processor with 3 Threads

Page 15: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 15

Properties

• Cannot be separate processes: – All threads need to access the same data (book in

memory)

• Better performance: – By the time the user wants to see page 100,

reformatting may be done

Page 16: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 16

Example 2: Web server

• Gets requests, sends web pages back

• Some pages much more popular than others

• Keep popular pages in memory cache

Page 17: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 17

A Multi-threaded Web server

Page 18: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 18

Thread Code

Dispatcher thread Worker thread

Page 19: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 19

Implementing Threads in User Space

Page 20: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 20

Properties of User-space Threads

• Thread context switch much faster (x10) without trap to OS

• Can work on OS w/o thread support

• BUT:– How to handle blocked threads? We do NOT want

OS to make whole process blocked.– Threads have to yield CPU voluntarily (no timer

interrupt)

Page 21: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 21

Implementing Threads in the Kernel

Page 22: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 22

Scheduling User-space Threads

• 50-msec process quantum• threads run 5 msec/CPU burst

Page 23: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 23

Scheduling Kernel-space Threads

Page 24: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 24

Win2000 Processes and Threads

• Win2000 implements kernel-space threads.• OS does not schedule a process – it schedules a thread

inside a process.

Page 25: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 25

Win2000 Processes and Threads

Kernel

Page 26: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 26

Win2000 Priorities

Mapping of Win32 priorities to Windows 2000 priorities

Page 27: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 27

Win2000 Scheduling

A detailed view of the Win2000 Ready-queue

Page 28: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 28

Win2000 Scheduling Scheme

• Priority scheduling (31 == highest)

• Within each priority: round robin

• Priority of user thread can be – increased (not to exceed 15) when wake up from I/O

wait:• +1 from disk, +6 from kb, +8 from soundcard

– decreased (not below base priority) when thread uses its full quantum

Page 29: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 29

Inter-Process Communication

Mutual Exclusion

Page 30: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 30

Terminology• Historically called “Inter-Process Communication” (IPC)

• Really “Inter-Thread Communication” – only the advanced solutions allow communication between separate processes.

• Basic examples assume shared variables – which are a feature of threads

• We mix the use of thread & process in this chapter

Page 31: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 31

Difficulties with Threads

• Share resources can conflict

• Counter = 0; // global variable• Thread 1 does Counter++ • Thread 2 does Counter–- // “at the same time”• What is the order of values of Counter ?

– 0 : 1 : 0?– 0 : -1 : 0?

Page 32: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 32

Print Spooler Example

• n slots in a queue (FIFO)

• Two shared (global) control variables:– In : position of first free slot (where next new file

will be inserted)– Out : position of first used slot (next to be printed)

• Threads A & B want to print at “same time”

Page 33: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 33

Print Spooler State

thread

thread

Page 34: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 34

Race Conditions

1. NextA = read(In);// NextA == 7

2. Spool(“fA”, NextA);3. NextA++;

//NextA == 84. write(NextA,In);

1. NextB = read(In);// NextB == 7

2. Spool(“fB”, NextB);3. NextB++;

//NextB == 84. write(NextB,In);

Thread A Thread B

Interrupt - context switch

Page 35: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 35

What went wrong?

• We have a shared variable: In• Context switch in the middle of a “read-update-

write” sequence.

• Result: both wrote their file to slot 7.

• Because of scheduling order – B’s update is lost!!

• This is a Race Condition.

Page 36: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 36

Mutual Exclusion

• Need to make sure that shared variables are not read and written “at the same time”.

• Mutual exclusion: if one thread is accessing a shared variable, other threads are excluded.

• Not always a shared variable. In general – a critical region.

Page 37: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 37

Conditions for Mutual Exclusion

1. [Safety] No two threads simultaneously in critical region.

2. [Criticality] No thread running outside its critical region may block another thread.

3. [Liveness] No thread must wait forever to enter its critical region.

No assumptions made about speed of CPU or scheduling.

Page 38: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 38

Desirable Execution

Page 39: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 39

Solution 0: Disable Interrupts

• No interrupt no context switch at bad time

But

• User bugs will freeze system

• Only useful for very short intervals

• OS uses this method internally to protect internal data structures.

Page 40: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 40

int lock; // shared, initially 0

// lock == 1 means “someone in critical region”

// Both threads run this code

while(lock != 0)

/* do nothing */;

lock = 1;

critical_region();

lock = 0;

Software Solution 1: Lock ?

Page 41: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 41

Does not work!

• If both threads reach while(lock != 0) at (about) the same time, both will find a value 0 both enter critical region.

• Violates the Safety property

Page 42: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 42

Mutual Exclusion Terminology

• A tight loop like while(lock != 0) is called busy-waiting.

• Busy waiting should usually be avoided wastes CPU.

• A lock using busy waiting is a spin lock.

Page 43: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 43

Software Solution 2 : Alternation

Thread 0 Thread 1

Busy waiting on spin-lock variable “turn”.Each process sets turn to the other process.

Page 44: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 44

Properties of Alternation

• Each process lets the other process into critical region.

• Satisfies “Safety”.

• A process cannot go into critical region twice in a row (even if other process is outside critical region).

• Violates Criticality property.

Page 45: Avishai Wool lecture 2 - 1 Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling

Avishai Woollecture 3 - 45

Concepts for review• Thread• User-space threads• Kernel-space threads• Thread scheduling• Inter-Process-Communication• Mutual Exclusion• Race Condition• Critical Region / Critical Section• Busy-Wait / Spin-lock