Slide 6-1 6 Processes and Threads
Announcements Slide 6-2 Homework Set #2 due Thursday at 11 am Program Assignment #1 due Thursday Feb. 10 at 11 am TA will introduce in recitation Wednesday Read chapters 6 and 7
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 What is a Process? executing code Main Memory Program P1 binary Code Data Heap Stack Process Fetch Code and Data Registers Write Data Slide 6-3 CPU Execution Program Counter (PC) ALU
How is a Process Structured in Slide 6-4 Run-time memory image Essentially code, data, stack, and heap Code and data loaded from executable file Stack grows downward, heap grows upward Memory? max address address 0 Run-time memory User stack Unallocated Heap Read/write.data,.bss Read-only.init,.text,.rodata
Multiple Processes Slide 6-5 Main Memory Process P1 Code Data Heap Stack Process P2 Code Data Heap Stack OS Code PCB for P1 PCB for P2 More Data, Heap, Stack Process state, e.g. ready, running, or waiting accounting info, e.g. process ID Program Counter CPU registers CPUscheduling 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
Multiple Processes Slide 6-6 Main Memory Process P1 Code Process P2 Code OS Code CPU Execution Data Data Heap PCB for P1 Program Counter (PC) Heap Stack PCB for P2 ALU Stack More Data, Heap, Stack
Context Switching Slide 6-7 Interrupt Initialization 1 7 9 6 4 Executable Memory 2 Process Manager Interrupt Handler P 1 P 2 P n 8 3 5 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
Threads Slide 6-8 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)
Threads Slide 6-9 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
Threads Slide 6-10 Main Memory Process P1 s Address Space Code Thread 1 PC1 Reg. State Stack Data Thread 2 PC2 Reg. State Stack Heap Thread 3 PC3 Reg. State Stack Process P2 Code Data Heap Stack Process P1 is multithreaded Process P2 is single threaded The OS is multiprogram med If there is preemptive timeslicing, the system is multitasked
Processes &Threads Slide 6-11 State State Address Space Map Map Resources Static data Stack Map Map Stack Program
Thread-Safe/Reentrant Code Slide 6-12 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
User-Space and Kernel Threads Slide 6-13 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-toone
Model of Process Execution Slide 6-14 Preemption or voluntary yield New Process Ready Ready List List job job Ready Allocate Scheduler Resource Manager Resources job job Request Blocked CPU Done CPU job Running
The Scheduler Slide 6-15 From Other States Ready Process Process Process Descriptor Enqueuer Ready Ready List List Dispatcher Context Context Switcher CPU CPU Running Process
Invoking the Scheduler Slide 6-16 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
Voluntary CPU Sharing Slide 6-17 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]; }
More on Yield Slide 6-18 p i and p j can resume one another s execution yield(*, p j.pc);... yield(*, p i.pc);... yield(*, p j.pc);... 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...
Voluntary Sharing Slide 6-19 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
Involuntary CPU Sharing Slide 6-20 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; } }
Involuntary CPU Sharing (cont) Slide 6-21 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) { <invoke scheduler>; TimeToSchedule = TimeSlice; } }
Contemporary Scheduling Slide 6-22 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
Choosing a Process to Run Slide 6-23 Ready Process Process Process Descriptor Enqueue Ready Ready List List Mechanism never changes Strategy = policy the dispatcher uses to select a process from the ready list Different policies for different requirements Dispatch Context Context Switch Switch CPU CPU Running Process
Policy Considerations Slide 6-24 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
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 Slide 6-25
However... Slide 6-26 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
Talking About Scheduling... Slide 6-27 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 )
Simplified Model Slide 6-28 Preemption or voluntary yield New Process Ready Ready List List job job Ready Allocate Scheduler Resource Manager Resources job job Request Blocked CPU Done CPU job Running Simplified, but still provide analysis result Easy to analyze performance No issue of voluntary/involuntary sharing
Estimating CPU Utilization Slide 6-29 New Process Ready Ready List List Scheduler CPU CPU Done 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 ) λ p i per second System Each p i uses 1/ µ units of the CPU
Slide 6-30 Estimating CPU Utilization New Process Ready Ready List List Scheduler CPU CPU Done 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?
Nonpreemptive Schedulers Slide 6-31 Blocked or preempted processes New Process Ready Ready List List Scheduler CPU CPU Done 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