Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.

Similar presentations


Presentation on theme: "Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6."— Presentation transcript:

1 Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6

2 Slide 6-2 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Announcements Homework Set #2 due Friday at 11 am - extension Program Assignment #1 due Thursday Feb. 10 at 11 am Read chapters 6 and 7

3 Slide 6-3 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Program #1: Threads - addenda draw the picture of user space threads versus kernel space threads user space threads yield voluntarily to switch between threads because it’s user space, the CPU doesn’t know about these threads your program just looks like a single-threaded process to CPU Inside that process, use library to create and delete threads, wait a thread, and yield a thread this is what you’re building Advantage: can implement threads on any OS, faster - no trap to kernel, no context switch Disadvantage: only voluntary scheduling, no preemption, blocked I/O on one user thread blocks all threads

4 Slide 6-4 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Program #1: Threads - addenda each process keeps a thread table analogous to process table of PCB’s kept by OS kernel for each process Key question: how do we switch between threads? –need to save thread state and change the PC PA #1 does it like this –scheduler is a global user thread, while your threads a and b are user, but local (hence on the stack) –stack pointer, frame pointer

5 Slide 6-5 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

6 Slide 6-6 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 What is a Process? A process is a program actively executing from main memory –has a Program Counter (PC) and execution state associated with it CPU registers keep state OS keeps process state in memory it’s alive! –has an address space associated with it a limited set of (virtual) addresses that can be accessed by the executing code Code Data Main Memory Program P1 binary CPU Execution Program Counter (PC) Registers ALU Fetch Code and Data Write Data Process Heap Stack

7 Slide 6-7 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 How is a Process Structured in Memory? Run-time memory image Essentially code, data, stack, and heap Code and data loaded from executable file Stack grows downward, heap grows upward User stack Heap Read/write.data,.bss Read-only.init,.text,.rodata Unallocated Run-time memory address 0 max address

8 Slide 6-8 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Multiple Processes Main Memory Code Data Process P1 Heap Stack Code Data Process P2 Heap Stack Process state, e.g. ready, running, or waiting accounting info, e.g. process ID Program Counter CPU registers CPU- scheduling info, e.g. priority Memory management info, e.g. base and limit registers, page tables I/O status info, e.g. list of open files Code More Data, Heap, Stack OS PCB for P2 PCB for P1

9 Slide 6-9 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Multiple Processes Main Memory Code Data Process P1 Heap Stack Code Data Process P2 Heap Stack Code More Data, Heap, Stack OS PCB for P2 PCB for P1 CPU Execution Program Counter (PC) ALU

10 Slide 6-10 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Context Switching Process Manager Interrupt Handler P1P1 P2P2 PnPn Executable Memory Initialization 1 2 3 4 5 7 Interrupt 8 9 6 Each time a process is switched out, its context must be saved, e.g. in the PCB Each time a process is switched in, its context is restored This usually requires copying of registers

11 Slide 6-11 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads A thread is a logical flow of execution that runs within the context of a process –has its own program counter (PC), register state, and stack –shares the memory address space with other threads in the same process, share the same code and data and resources (e.g. open files)

12 Slide 6-12 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads Why would you want multithreaded processes? –reduced context switch overhead In Solaris, context switching between processes is 5x slower than switching between threads –shared resources => less memory consumption => more threads can be supported, especially for a scalable system, e.g. Web server must handle thousands of connections –inter-thread communication is easier and faster than inter-process communication –thread also called a lightweight process

13 Slide 6-13 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads Process P1 is multithreaded Process P2 is single threaded The OS is multiprogram med If there is preemptive timeslicing, the system is multitasked Main Memory Code Data Process P1’s Address Space Heap Code Data Process P2 Heap Stack PC1 Reg. State Thread 1 Stack PC2 Reg. State Thread 2 Stack PC3 Reg. State Thread 3

14 Slide 6-14 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Processes &Threads Address Space Map Stack State Program Static data Resources Stack State Map

15 Slide 6-15 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Thread-Safe/Reentrant Code If two threads share and execute the same code, then the code needs to be thread-safe –the use of global variables is not thread safe –the use of static variables is not thread safe –the use of local variables is thread safe need to govern access to persistent data like global/static variables with locking and synchronization mechanisms reentrant is a special case of thread-safe: –reentrant code does not have any references to global variables –thread-safe code protects and synchronizes access to global variables

16 Slide 6-16 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 User-Space and Kernel Threads pthreads is a POSIX user space threading API –provides interface to create, delete threads in the same process –threads will synchronize with each other via this package –no need to involve the OS –implementations of pthreads API differ underneath the API Kernel threads are supported by the OS –kernel must be involved in switching threads –mapping of user-level threads to kernel threads is usually one-to- one

17 Slide 6-17 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Model of Process Execution Ready List Ready List Scheduler CPU Resource Manager Resource Manager Resources Preemption or voluntary yield AllocateRequest Done New Process job “Ready” “Running” “Blocked”

18 Slide 6-18 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 The Scheduler Ready Process Enqueuer Ready List Ready List Dispatcher Context Switcher Context Switcher Process Descriptor Process Descriptor CPU From Other States Running Process

19 Slide 6-19 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Invoking the Scheduler Need a mechanism to call the scheduler Voluntary call –Process blocks itself –Calls the scheduler Involuntary call –External force (interrupt) blocks the process –Calls the scheduler

20 Slide 6-20 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Voluntary CPU Sharing yield(p i.pc, p j.pc) { memory[p i.pc] = PC; PC = memory[p j.pc]; } p i can be “automatically” determined from the processor status registers yield(*, p j.pc) { memory[p i.pc] = PC; PC = memory[p j.pc]; }

21 Slide 6-21 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 More on Yield yield(*, p j.pc);... yield(*, p i.pc);... yield(*, p j.pc);... p i and p j can resume one another’s execution Suppose p j is the scheduler: // p_i yields to scheduler yield(*, p j.pc); // scheduler chooses p k yield(*, p k.pc); // p k yields to scheduler yield(*, p j.pc); // scheduler chooses...

22 Slide 6-22 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Voluntary Sharing Every process periodically yields to the scheduler Relies on correct process behavior –process can fail to yield: infinite loop either intentionally (while(1)) or due to logical error (while(!DONE)) Malicious Accidental –process can yield to soon: unfairness for the “nice” processes who give up the CPU, while others do not –process can fail to yield in time: another process urgently needs the CPU to read incoming data flowing into a bounded buffer, but doesn’t get the CPU in time to prevent the buffer from overflowing and dropping information Need a mechanism to override running process

23 Slide 6-23 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Involuntary CPU Sharing Interval timer –Device to produce a periodic interrupt –Programmable period IntervalTimer() { InterruptCount--; if(InterruptCount <= 0) { InterruptRequest = TRUE; InterruptCount = K; } SetInterval(programmableValue) { K = programmableValue: InterruptCount = K; }

24 Slide 6-24 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Involuntary CPU Sharing (cont) Interval timer device handler –Keeps an in-memory clock up-to-date (see Chap 4 lab exercise) –Invokes the scheduler IntervalTimerHandler() { Time++; // update the clock TimeToSchedule--; if(TimeToSchedule <= 0) { ; TimeToSchedule = TimeSlice; }

25 Slide 6-25 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Contemporary Scheduling Involuntary CPU sharing – timer interrupts –Time quantum determined by interval timer – usually fixed size for every process using the system –Sometimes called the time slice length

26 Slide 6-26 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Choosing a Process to Run Mechanism never changes Strategy = policy the dispatcher uses to select a process from the ready list Different policies for different requirements Ready Process Enqueue Ready List Ready List Dispatch Context Switch Context Switch Process Descriptor Process Descriptor CPU Running Process

27 Slide 6-27 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Policy Considerations Policy can control/influence: –CPU utilization –Average time a process waits for service –Average amount of time to complete a job Could strive for any of: –Equitability –Favor very short or long jobs –Meet priority requirements –Meet deadlines

28 Slide 6-28 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Optimal Scheduling Suppose the scheduler knows each process p i ’s service time,  p i  -- or it can estimate each  p i  : Policy can optimize on any criteria, e.g., –CPU utilization –Waiting time –Deadline To find an optimal schedule: –Have a finite, fixed # of p i –Know  p i  for each p i –Enumerate all schedules, then choose the best

29 Slide 6-29 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 However... The  (p i ) are almost certainly just estimates General algorithm to choose optimal schedule is O(n 2 ) Other processes may arrive while these processes are being serviced Usually, optimal schedule is only a theoretical benchmark – scheduling policies try to approximate an optimal schedule

30 Slide 6-30 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Model of Process Execution Ready List Ready List Scheduler CPU Resource Manager Resource Manager Resources Preemption or voluntary yield AllocateRequest Done New Process job “Ready” “Running” “Blocked”

31 Slide 6-31 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Talking About Scheduling... Let P = {p i | 0  i < n} = set of processes Let S(p i )  {running, ready, blocked} Let  (p i ) = Time process needs to be in running state (the service time) Let W(p i ) = Time p i is in ready state before first transition to running (wait time) Let T TRnd (p i ) = Time from p i first enter ready to last exit ready (turnaround time) Batch Throughput rate = inverse of avg T TRnd Timesharing response time = W(p i )

32 Slide 6-32 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Simplified Model Ready List Ready List Scheduler CPU Resource Manager Resource Manager Resources AllocateRequest Done New Process job “Ready” “Running” “Blocked” Simplified, but still provide analysis result Easy to analyze performance No issue of voluntary/involuntary sharing Preemption or voluntary yield

33 Slide 6-33 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Estimating CPU Utilization Ready List Ready List Scheduler CPU Done New Process System p i per second Each p i uses 1/  units of the CPU Let = the average rate at which processes are placed in the Ready List, arrival rate Let  = the average service rate  1/  = the average  (p i )

34 Slide 6-34 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Estimating CPU Utilization Ready List Ready List Scheduler CPU Done New Process Let = the average rate at which processes are placed in the Ready List, arrival rate Let  = the average service rate  1/  = the average  (p i ) Let  = the fraction of the time that the CPU is expected to be busy  = # p i that arrive per unit time * avg time each spends on CPU  = * 1/  = /  Notice must have <  (i.e.,  < 1) What if  approaches 1?

35 Slide 6-35 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Nonpreemptive Schedulers Ready List Ready List Scheduler CPU Done New Process Try to use the simplified scheduling model Only consider running and ready states Ignores time in blocked state: –“New process created when it enters ready state” –“Process is destroyed when it enters blocked state” –Really just looking at “small phases” of a process Blocked or preempted processes

36 Slide 6-36 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 First-Come-First-Served i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 T TRnd (p 0 ) =  (p 0 ) = 350 W(p 0 ) = 0 0350

37 Slide 6-37 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 First-Come-First-Served i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 p1p1 T TRnd (p 0 ) =  (p 0 ) = 350 T TRnd (p 1 ) = (  (p 1 ) +T TRnd (p 0 )) = 125+350 = 475 W(p 0 ) = 0 W(p 1 ) = T TRnd (p 0 ) = 350 475350

38 Slide 6-38 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 First-Come-First-Served i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 p1p1 p2p2 T TRnd (p 0 ) =  (p 0 ) = 350 T TRnd (p 1 ) = (  (p 1 ) +T TRnd (p 0 )) = 125+350 = 475 T TRnd (p 2 ) = (  (p 2 ) +T TRnd (p 1 )) = 475+475 = 950 W(p 0 ) = 0 W(p 1 ) = T TRnd (p 0 ) = 350 W(p 2 ) = T TRnd (p 1 ) = 475 475950

39 Slide 6-39 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 First-Come-First-Served i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 p1p1 p2p2 p3p3 T TRnd (p 0 ) =  (p 0 ) = 350 T TRnd (p 1 ) = (  (p 1 ) +T TRnd (p 0 )) = 125+350 = 475 T TRnd (p 2 ) = (  (p 2 ) +T TRnd (p 1 )) = 475+475 = 950 T TRnd (p 3 ) = (  (p 3 ) +T TRnd (p 2 )) = 250+950 = 1200 W(p 0 ) = 0 W(p 1 ) = T TRnd (p 0 ) = 350 W(p 2 ) = T TRnd (p 1 ) = 475 W(p 3 ) = T TRnd (p 2 ) = 950 1200950

40 Slide 6-40 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 First-Come-First-Served i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 p1p1 p2p2 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 ) = 350 T TRnd (p 1 ) = (  (p 1 ) +T TRnd (p 0 )) = 125+350 = 475 T TRnd (p 2 ) = (  (p 2 ) +T TRnd (p 1 )) = 475+475 = 950 T TRnd (p 3 ) = (  (p 3 ) +T TRnd (p 2 )) = 250+950 = 1200 T TRnd (p 4 ) = (  (p 4 ) +T TRnd (p 3 )) = 75+1200 = 1275 W(p 0 ) = 0 W(p 1 ) = T TRnd (p 0 ) = 350 W(p 2 ) = T TRnd (p 1 ) = 475 W(p 3 ) = T TRnd (p 2 ) = 950 W(p 4 ) = T TRnd (p 3 ) = 1200 12001275

41 Slide 6-41 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 FCFS Average Wait Time i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 p1p1 p2p2 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 ) = 350 T TRnd (p 1 ) = (  (p 1 ) +T TRnd (p 0 )) = 125+350 = 475 T TRnd (p 2 ) = (  (p 2 ) +T TRnd (p 1 )) = 475+475 = 950 T TRnd (p 3 ) = (  (p 3 ) +T TRnd (p 2 )) = 250+950 = 1200 T TRnd (p 4 ) = (  (p 4 ) +T TRnd (p 3 )) = 75+1200 = 1275 W(p 0 ) = 0 W(p 1 ) = T TRnd (p 0 ) = 350 W(p 2 ) = T TRnd (p 1 ) = 475 W(p 3 ) = T TRnd (p 2 ) = 950 W(p 4 ) = T TRnd (p 3 ) = 1200 W avg = (0+350+475+950+1200)/5 = 2974/5 = 595 127512009004753500 Easy to implement Ignores service time, etc Not a great performer

42 Slide 6-42 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Predicting Wait Time in FCFS In FCFS, when a process arrives, all in ready list will be processed before this job Let  be the service rate Let L be the ready list length W avg (p) = L*1/  1/  L  Compare predicted wait with actual in earlier examples

43 Slide 6-43 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Shortest Job Next i  (p i ) 0 350 1 125 2 475 3 250 4 75 p4p4 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 4 ) = 0 750

44 Slide 6-44 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Shortest Job Next i  (p i ) 0 350 1 125 2 475 3 250 4 75 p1p1 p4p4 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = 125+75 = 200 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 1 ) = 75 W(p 4 ) = 0 200750

45 Slide 6-45 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Shortest Job Next i  (p i ) 0 350 1 125 2 475 3 250 4 75 p1p1 p3p3 p4p4 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = 125+75 = 200 T TRnd (p 3 ) =  (p 3 )+  (p 1 )+  (p 4 ) = 250+125+75 = 450 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 1 ) = 75 W(p 3 ) = 200 W(p 4 ) = 0 450200750

46 Slide 6-46 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Shortest Job Next i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 p1p1 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = 350+250+125+75 = 800 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = 125+75 = 200 T TRnd (p 3 ) =  (p 3 )+  (p 1 )+  (p 4 ) = 250+125+75 = 450 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 0 ) = 450 W(p 1 ) = 75 W(p 3 ) = 200 W(p 4 ) = 0 800450200750

47 Slide 6-47 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Shortest Job Next i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 p1p1 p2p2 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = 350+250+125+75 = 800 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = 125+75 = 200 T TRnd (p 2 ) =  (p 2 )+  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = 475+350+250+125+75 = 1275 T TRnd (p 3 ) =  (p 3 )+  (p 1 )+  (p 4 ) = 250+125+75 = 450 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 0 ) = 450 W(p 1 ) = 75 W(p 2 ) = 800 W(p 3 ) = 200 W(p 4 ) = 0 1275800450200750

48 Slide 6-48 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Shortest Job Next i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 p1p1 p2p2 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = 350+250+125+75 = 800 T TRnd (p 1 ) =  (p 1 )+  (p 4 ) = 125+75 = 200 T TRnd (p 2 ) =  (p 2 )+  (p 0 )+  (p 3 )+  (p 1 )+  (p 4 ) = 475+350+250+125+75 = 1275 T TRnd (p 3 ) =  (p 3 )+  (p 1 )+  (p 4 ) = 250+125+75 = 450 T TRnd (p 4 ) =  (p 4 ) = 75 W(p 0 ) = 450 W(p 1 ) = 75 W(p 2 ) = 800 W(p 3 ) = 200 W(p 4 ) = 0 W avg = (450+75+800+200+0)/5 = 1525/5 = 305 1275800450200750 Minimizes wait time May starve large jobs Must know service times

49 Slide 6-49 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Priority Scheduling i  (p i ) Pri 0 350 5 1 125 2 2 475 3 3 250 1 4 75 4 p0p0 p1p1 p2p2 p3p3 p4p4 T TRnd (p 0 ) =  (p 0 )+  (p 4 )+  (p 2 )+  (p 1 ) )+  (p 3 ) = 350+75+475+125+250 = 1275 T TRnd (p 1 ) =  (p 1 )+  (p 3 ) = 125+250 = 375 T TRnd (p 2 ) =  (p 2 )+  (p 1 )+  (p 3 ) = 475+125+250 = 850 T TRnd (p 3 ) =  (p 3 ) = 250 T TRnd (p 4 ) =  (p 4 )+  (p 2 )+  (p 1 )+  (p 3 ) = 75+475+125+250 = 925 W(p 0 ) = 925 W(p 1 ) = 250 W(p 2 ) = 375 W(p 3 ) = 0 W(p 4 ) = 850 W avg = (925+250+375+0+850)/5 = 2400/5 = 480 12759258503752500 Reflects importance of external use May cause starvation Can address starvation with aging

50 Slide 6-50 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Deadline Scheduling i  (p i ) Deadline 0 350 575 1 125 550 2 475 1050 3 250 (none) 4 75 200 p0p0 p1p1 p2p2 p3p3 p4p4 1275 1050550200 0 Allocates service by deadline May not be feasible p0p0 p1p1 p2p2 p3p3 p4p4 p0p0 p1p1 p2p2 p3p3 p4p4 575

51 Slide 6-51 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Preemptive Schedulers Ready List Ready List Scheduler CPU Preemption or voluntary yield Done New Process Highest priority process is guaranteed to be running at all times –Or at least at the beginning of a time slice Dominant form of contemporary scheduling But complex to build & analyze

52 Slide 6-52 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 W(p 0 ) = 0 050

53 Slide 6-53 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 W(p 0 ) = 0 W(p 1 ) = 50 1000 p1p1

54 Slide 6-54 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 1000 p2p2 p1p1

55 Slide 6-55 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 2001000 p3p3 p2p2 p1p1

56 Slide 6-56 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = 200 2001000 p4p4 p3p3 p2p2 p1p1

57 Slide 6-57 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = 200 3002001000 p0p0 p4p4 p3p3 p2p2 p1p1

58 Slide 6-58 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = 200 4754003002001000 p4p4 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3

59 Slide 6-59 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 T TRnd (p 1 ) =  T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = 200 4754003002001000 p4p4 p1p1 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3 p0p0 550

60 Slide 6-60 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 T TRnd (p 1 ) =  T TRnd (p 3 ) =  T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = 200 4754003002001000 p4p4 p1p1 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 550650 750850950

61 Slide 6-61 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 T TRnd (p 0 ) =  T TRnd (p 1 ) =  T TRnd (p 3 ) =  T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = 200 4754003002001000 p4p4 p1p1 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p2p2 p0p0 550650 7508509501050

62 Slide 6-62 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 T TRnd (p 0 ) =  T TRnd (p 1 ) =  T TRnd (p 2 ) =  T TRnd (p 3 ) =  T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = 200 4754003002001000 p4p4 p1p1 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p2p2 p0p0 p2p2 p2p2 p2p2 p2p2 550650 7508509501050115012501275

63 Slide 6-63 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Round Robin (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 T TRnd (p 0 ) =  T TRnd (p 1 ) =  T TRnd (p 2 ) =  T TRnd (p 3 ) =  T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 50 W(p 2 ) = 100 W(p 3 ) = 150 W(p 4 ) = 200 W avg = (0+50+100+150+200)/5 = 500/5 = 100 4754003002001000 Equitable Most widely-used Fits naturally with interval timer p4p4 p1p1 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p2p2 p0p0 p2p2 p2p2 p2p2 p2p2 550650 7508509501050115012501275 T TRnd _ avg = (1100+550+1275+950+475)/5 = 4350/5 = 870

64 Slide 6-64 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 RR with Overhead=10 (TQ=50) i  (p i ) 0 350 1 125 2 475 3 250 4 75 p0p0 T TRnd (p 0 ) =  T TRnd (p 1 ) =  T TRnd (p 2 ) =  T TRnd (p 3 ) =  T TRnd (p 4 ) =  W(p 0 ) = 0 W(p 1 ) = 60 W(p 2 ) = 120 W(p 3 ) = 180 W(p 4 ) = 240 W avg = (0+60+120+180+240)/5 = 600/5 = 120 5404803602401200 Overhead must be considered p4p4 p1p1 p0p0 p4p4 p3p3 p2p2 p1p1 p1p1 p2p2 p3p3 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p3p3 p2p2 p0p0 p2p2 p0p0 p2p2 p2p2 p2p2 p2p2 575790 910103011501270139015101535 T TRnd _ avg = (1320+660+1535+1140+565)/5 = 5220/5 = 1044 635670 790

65 Slide 6-65 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Multi-Level Queues Ready List 0 Ready List 1 Ready List 2 Ready List 3 Scheduler CPU Preemption or voluntary yield Done New Process All processes at level i run before any process at level j At a level, use another policy, e.g. RR

66 Slide 6-66 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Contemporary Scheduling Involuntary CPU sharing -- timer interrupts –Time quantum determined by interval timer -- usually fixed for every process using the system –Sometimes called the time slice length Priority-based process (job) selection –Select the highest priority process –Priority reflects policy With preemption Usually a variant of Multi-Level Queues

67 Slide 6-67 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 BSD 4.4 Scheduling Involuntary CPU Sharing Preemptive algorithms 32 Multi-Level Queues –Queues 0-7 are reserved for system functions –Queues 8-31 are for user space functions –nice influences (but does not dictate) queue level

68 Slide 6-68 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT/2K Scheduling Involuntary CPU Sharing across threads Preemptive algorithms 32 Multi-Level Queues –Highest 16 levels are “real-time” –Next lower 15 are for system/user threads Range determined by process base priority –Lowest level is for the idle thread

69 Slide 6-69 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Bank Teller Simulation Tellers at the Bank T1T1 T1T1 T2T2 T2T2 TnTn TnTn … Model of Tellers at the Bank Customers Arrivals

70 Slide 6-70 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Simulation Kernel Loop simulated_time = 0; while (true) { event = select_next_event(); if (event->time > simulated_time) simulated_time = event->time; evaluate(event->function, …); }

71 Slide 6-71 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Simulation Kernel Loop(2) void runKernel(int quitTime) { Event *thisEvent; // Stop by running to elapsed time, or by causing quit execute if(quitTime next; simTime = thisEvent->getTime(); // Set the time // Execute this event thisEvent->fire(); delete(thisEvent); }; }

72 Slide 6-72 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

73 Slide 6-73 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Simple State Diagram Ready Blocked Running Start Schedule Request Done Request Allocate

74 Slide 6-74 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 UNIX State Transition Diagram Runnable Uninterruptible Sleep Running Start Schedule Request Done I/O Request Allocate zombie Wait by parent Sleeping Traced or Stopped Request I/O Complete Resume

75 Slide 6-75 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT Thread States Initialized CreateThread Ready Activate Select Standby Running Terminated Waiting Transition Reinitialize Exit Preempt Dispatch Wait Wait Complete Dispatch

76 Slide 6-76 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Resources R = {R j | 0  j < m} = resource types C = {c j  0 |  R j  R (0  j < m)} = units of R j available Reusable resource: After a unit of the resource has been allocated, it must ultimately be released back to the system. E.g., CPU, primary memory, disk space, … The maximum value for c j is the number of units of that resource Consumable resource: There is no need to release a resource after it has been acquired. E.g., a message, input data, … Notice that c j is unbounded. Resource: Anything that a process can request, then be blocked because that thing is not available.

77 Slide 6-77 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Hierarchies Parent-child relationship may be significant: parent controls children’s execution Ready-Active Blocked-Active Running Start Schedule Request Done Request Allocate Ready-Suspended Blocked-Suspended Suspend Yield Allocate Suspend Activate

78 Slide 6-78 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 UNIX Organization System Call Interface File Manager Memory Manager Device Manager Protection Deadlock Synchronization Process Description Process Description CPU Other H/W Scheduler Resource Manager Resource Manager Resource Manager Resource Manager Resource Manager Resource Manager Memory Devices Libraries Process Monolithic Kernel

79 Slide 6-79 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT Organization Processor(s)Main MemoryDevices Libraries Process Subsystem User Subsystem Hardware Abstraction Layer NT Kernel NT Executive I/O Subsystem T T T T T T TT T


Download ppt "Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6."

Similar presentations


Ads by Google