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

Post on 23-Jan-2016

215 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

Transcript

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

Avishai Woollecture 2 - 2

Example for Priorities

Static priorities can lead to starvation!

Avishai Woollecture 2 - 3

Dynamic Priorities

Example: multilevel feedback

Avishai Woollecture 3 - 4

Introduction to Systems Programming Lecture 3

Threads

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

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

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.

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

Avishai Woollecture 3 - 9

The Thread Model (1)

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

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

Avishai Woollecture 3 - 11

The Thread Model (2)

• Items shared by all threads in a process

• Items private to each thread

Avishai Woollecture 3 - 12

The Thread Model (3)

Each thread has its own stack

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

Avishai Woollecture 3 - 14

Word Processor with 3 Threads

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

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

Avishai Woollecture 3 - 17

A Multi-threaded Web server

Avishai Woollecture 3 - 18

Thread Code

Dispatcher thread Worker thread

Avishai Woollecture 3 - 19

Implementing Threads in User Space

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)

Avishai Woollecture 3 - 21

Implementing Threads in the Kernel

Avishai Woollecture 3 - 22

Scheduling User-space Threads

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

Avishai Woollecture 3 - 23

Scheduling Kernel-space Threads

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.

Avishai Woollecture 3 - 25

Win2000 Processes and Threads

Kernel

Avishai Woollecture 3 - 26

Win2000 Priorities

Mapping of Win32 priorities to Windows 2000 priorities

Avishai Woollecture 3 - 27

Win2000 Scheduling

A detailed view of the Win2000 Ready-queue

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

Avishai Woollecture 3 - 29

Inter-Process Communication

Mutual Exclusion

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

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?

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”

Avishai Woollecture 3 - 33

Print Spooler State

thread

thread

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

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.

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.

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.

Avishai Woollecture 3 - 38

Desirable Execution

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.

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 ?

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

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.

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.

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.

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

top related