Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

2 Avishai Wool lecture 2 - 2 Example for Priorities Static priorities can lead to starvation!

3 Avishai Wool lecture 2 - 3 Dynamic Priorities Example: multilevel feedback

4 Avishai Wool lecture 3 - 4 Introduction to Systems Programming Lecture 3 Threads

5 Avishai Wool lecture 3 - 5 Processes The Process Model Multiprogramming of four programs Conceptual model of 4 independent, sequential processes Only one program active at any instant

6 Avishai Wool lecture 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

7 Avishai Wool lecture 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.

8 Avishai Wool lecture 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

9 Avishai Wool lecture 3 - 9 The Thread Model (1) (a) Three processes each with one thread (b) One process with three threads

10 Avishai Wool lecture 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

11 Avishai Wool lecture 3 - 11 The Thread Model (2) Items shared by all threads in a process Items private to each thread

12 Avishai Wool lecture 3 - 12 The Thread Model (3) Each thread has its own stack

13 Avishai Wool lecture 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

14 Avishai Wool lecture 3 - 14 Word Processor with 3 Threads

15 Avishai Wool lecture 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

16 Avishai Wool lecture 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

17 Avishai Wool lecture 3 - 17 A Multi-threaded Web server

18 Avishai Wool lecture 3 - 18 Thread Code Dispatcher threadWorker thread

19 Avishai Wool lecture 3 - 19 Implementing Threads in User Space

20 Avishai Wool lecture 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)

21 Avishai Wool lecture 3 - 21 Implementing Threads in the Kernel

22 Avishai Wool lecture 3 - 22 Scheduling User-space Threads 50-msec process quantum threads run 5 msec/CPU burst

23 Avishai Wool lecture 3 - 23 Scheduling Kernel-space Threads

24 Avishai Wool lecture 3 - 24 Win2000 Processes and Threads Win2000 implements kernel-space threads. OS does not schedule a process – it schedules a thread inside a process.

25 Avishai Wool lecture 3 - 25 Win2000 Processes and Threads Kernel

26 Avishai Wool lecture 3 - 26 Win2000 Priorities Mapping of Win32 priorities to Windows 2000 priorities

27 Avishai Wool lecture 3 - 27 Win2000 Scheduling A detailed view of the Win2000 Ready-queue

28 Avishai Wool lecture 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

29 Avishai Wool lecture 3 - 29 Inter-Process Communication Mutual Exclusion

30 Avishai Wool lecture 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

31 Avishai Wool lecture 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?

32 Avishai Wool lecture 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”

33 Avishai Wool lecture 3 - 33 Print Spooler State thread

34 Avishai Wool lecture 3 - 34 Race Conditions 1.NextA = read(In); // NextA == 7 2.Spool(“fA”, NextA); 3.NextA++; //NextA == 8 4.write(NextA,In); 1.NextB = read(In); // NextB == 7 2.Spool(“fB”, NextB); 3.NextB++; //NextB == 8 4.write(NextB,In); Thread A Thread B Interrupt - context switch

35 Avishai Wool lecture 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.

36 Avishai Wool lecture 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.

37 Avishai Wool lecture 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.

38 Avishai Wool lecture 3 - 38 Desirable Execution

39 Avishai Wool lecture 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.

40 Avishai Wool lecture 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 ?

41 Avishai Wool lecture 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

42 Avishai Wool lecture 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.

43 Avishai Wool lecture 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.

44 Avishai Wool lecture 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.

45 Avishai Wool lecture 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


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

Similar presentations


Ads by Google