Chapter 2: Processes & Threads - University of...
Transcript of Chapter 2: Processes & Threads - University of...
Chapter 2 2CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Processes and threads
ProcessesThreadsSchedulingInterprocess communicationClassical IPC problems
Chapter 2 3CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
What is a process?
Code, data, and stackUsually (but not always) has its own address space
Program stateCPU registersProgram counter (current location in the code)Stack pointer
Only one process can be running in the CPU at anygiven time!
Chapter 2 4CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
The process model
Multiprogramming of fourprogramsConceptual model
4 independent processesProcesses run sequentially
Only one program active at anyinstant!
That instant can be very short…
A
C
D
Single PC(CPU’s point of view)
AB
C D
Multiple PCs(process point of view)
B
B
ABCD
Time
Chapter 2 5CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
When is a process created?
Processes can be created in two waysSystem initialization: one or more processes created whenthe OS starts upExecution of a process creation system call: somethingexplicitly asks for a new process
System calls can come fromUser request to create a new process (system call executedfrom user shell)Already running processes
User programsSystem daemons
Chapter 2 6CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
When do processes end?
Conditions that terminate processes can beVoluntaryInvoluntary
VoluntaryNormal exitError exit
InvoluntaryFatal error (only sort of involuntary)Killed by another process
Chapter 2 7CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Process hierarchies
Parent creates a child processChild processes can create their own children
Forms a hierarchyUNIX calls this a “process group”If a process exits, its children are “inherited” by theexiting process’s parent
Windows has no concept of process hierarchyAll processes are created equal
Chapter 2 8CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Blocked(waiting)
Created
Exit
Ready
Running
Process states
Process in one of 5 statesCreatedReadyRunningBlockedExit
Transitions between states1 - Process enters ready queue2 - Scheduler picks this process3 - Scheduler picks a different
process4 - Process waits for event (such as
I/O)5 - Event occurs6 - Process exits7 - Process ended by another
process
1
5
4
3
2
7
76
Chapter 2 9CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Processes in the OS
Two “layers” for processesLowest layer of process-structured OS handles interrupts,schedulingAbove that layer are sequential processes
Processes tracked in the process tableEach process has a process table entry
Scheduler
0 1 N-2 N-1…
Processes
Chapter 2 10CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
What’s in a process table entry?
File managementRoot directoryWorking (current) directoryFile descriptorsUser IDGroup ID
Memory managementPointers to text, data, stack
orPointer to page table
Process managementRegistersProgram counterCPU status wordStack pointerProcess statePriority / scheduling parametersProcess IDParent process IDSignalsProcess start timeTotal CPU usage
May bestored
on stack
Chapter 2 11CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
What happens on a trap/interrupt?
n Hardware saves program counter (on stack or in aspecial register)
n Hardware loads new PC, identifies interruptn Assembly language routine saves registersn Assembly language routine sets up stackn Assembly language calls C to run service routinen Service routine calls schedulern Scheduler selects a process to run next (might be
the one interrupted…)n Assembly language routine loads PC & registers
for the selected process
Chapter 2 12CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Threads: “processes” sharing memory
Process == address spaceThread == program counter / stream of instructionsTwo examples
Three processes, each with one threadOne process with three threads
Kernel Kernel
ThreadsThreadsSystem
space
Userspace
Process 1 Process 2 Process 3 Process 1
Chapter 2 13CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Process & thread information
Per process itemsAddress spaceOpen filesChild processesSignals & handlersAccounting infoGlobal variables
Per thread itemsProgram counterRegistersStack & stack pointerState
Per thread itemsProgram counterRegistersStack & stack pointerState
Per thread itemsProgram counterRegistersStack & stack pointerState
Chapter 2 14CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Threads & Stacks
Kernel
Process
Thread 1 Thread 2 Thread 3
Thread 1’sstack
Thread 3’sstack
Thread 2’sstack
User space
=> Each thread has its own stack!
Chapter 2 15CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Why use threads?
Allow a single applicationto do many things at once
Simpler programming modelLess waiting
Threads are faster to createor destroy
No separate address space
Overlap computation andI/O
Could be done withoutthreads, but it’s harder
Example: word processorThread to read from keyboardThread to format documentThread to write to disk
Kernel
When in the Course of human events, itbecomes necessary for one people todissolve the political bands which haveconnected them with another, and toassume among the powers of the earth,the separate and equal station to whichthe Laws of Nature and of Nature's Godentitle them, a decent respect to theopinions of mankind requires that theyshould declare the causes which impelthem to the separation.
We hold these truths to be self-evident,that all men are created equal, that theyare endowed by their Creator withcertain unalienable Rights, that amongthese are Life, Liberty and the pursuit ofHappiness.--That to secure these rights,Governments are instituted amongMen, deriving their just powers fromthe consent of the governed, --Thatwhenever any Form of Governmentbecomes
destructive of these ends, it is the Rightof the People to alter or to abolish it,and to institute new Government, layingits foundation on such principles andorganizing its powers in such form, asto them shall seem most likely to effecttheir Safety and Happiness. Prudence,indeed, will dictate that Governmentslong established should not be changedfor light and transient causes; andaccordingly all
Chapter 2 16CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Multithreaded Web server
Kernel
Networkconnection
Dispatcherthread
Workerthread
Web pagecache
while(TRUE) {getNextRequest(&buf);handoffWork(&buf);
}
while(TRUE) {waitForWork(&buf);lookForPageInCache(&buf,&page);if(pageNotInCache(&page)) {
readPageFromDisk(&buf,&page);}returnPage(&page);
}
Chapter 2 17CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Three ways to build a server
Thread modelParallelismBlocking system calls
Single-threaded process: slow, but easier to doNo parallelismBlocking system calls
Finite-state machineEach activity has its own stateStates change when system calls complete or interruptsoccurParallelismNonblocking system callsInterrupts
Chapter 2 18CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Implementing threads
Kernel
Run-timesystem
Threadtable
Processtable
Kernel
Thread
Process
Threadtable
Processtable
User-level threads+ No need for kernel support- May be slower than kernel threads- Harder to do non-blocking I/O
Kernel-level threads+ More flexible scheduling+ Non-blocking I/O- Not portable
Chapter 2 19CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Scheduling
What is scheduling?GoalsMechanisms
Scheduling on batch systemsScheduling on interactive systemsOther kinds of scheduling
Real-time scheduling
Chapter 2 20CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Why schedule processes?
Bursts of CPU usage alternate with periods of I/O waitSome processes are CPU-bound: they don’t make many I/OrequestsOther processes are I/O-bound and make many kernelrequests
CPU bound
I/O bound
CPU bursts I/O waits
Total CPU usage
Total CPU usage
Time
Chapter 2 21CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
When are processes scheduled?
At the time they enter the systemCommon in batch systemsTwo types of batch scheduling
Submission of a new job causes the scheduler to runScheduling only done when a job voluntarily gives up the CPU(i.e., while waiting for an I/O request)
At relatively fixed intervals (clock interrupts)Necessary for interactive systemsMay also be used for batch systemsScheduling algorithms at each interrupt, and picks the nextprocess from the pool of “ready” processes
Chapter 2 22CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Scheduling goals
All systemsFairness: give each process a fair share of the CPUEnforcement: ensure that the stated policy is carried outBalance: keep all parts of the system busy
Batch systemsThroughput: maximize jobs per unit time (hour)Turnaround time: minimize time users wait for jobsCPU utilization: keep the CPU as busy as possible
Interactive systemsResponse time: respond quickly to users’ requestsProportionality: meet users’ expectations
Real-time systemsMeet deadlines: missing deadlines is a system failure!Predictability: same type of behavior for each time slice
Chapter 2 23CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Measuring scheduling performance
ThroughputAmount of work completed per second (minute, hour)Higher throughput usually means better utilized system
Response timeResponse time is time from when a command is submitted until resultsare returnedCan measure average, variance, minimum, maximum, …May be more useful to measure time spent waiting
Turnaround timeLike response time, but for batch jobs (response is the completion ofthe process)
Usually not possible to optimize for all metrics with the samescheduling algorithm
Chapter 2 24CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
First Come, First Served (FCFS)
Goal: do jobs in the orderthey arrive
Fair in the same way a bankteller line is fair
Simple algorithm!Problem: long jobs delayevery job after them
Many processes may wait fora single long job
A B C D
4 3 6 3
Current job queue
Execution order
FCFS scheduler
A B C D
4 3 6 3
Chapter 2 25CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Shortest Job First (SJF)
Goal: do the shortest jobfirst
Short jobs complete firstLong jobs delay every jobafter them
Jobs sorted in increasingorder of execution time
Ordering of ties doesn’tmatter
Shortest Remaining TimeFirst (SRTF): preemptiveform of SJFProblem: how does thescheduler know how long ajob will take?
A B C D
4 3 6 3
AB CD
43 63
Current job queue
Execution order
SJF scheduler
Chapter 2 26CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Three-level scheduling
CPU
Mainmemory
CPU scheduler
Memoryscheduler
Admissionscheduler
Inputqueue
Arrivingjobs
Jobs held in input queue until moved into memoryPick “complementary jobs”: small & large, CPU- & I/O-intensiveJobs move into memory when admitted
CPU scheduler picks next job to runMemory scheduler picks some jobs from main memory andmoves them to disk if insufficient memory space
Chapter 2 27CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Round Robin (RR) scheduling
Round Robin schedulingGive each process a fixedtime slot (quantum)Rotate through “ready”processesEach process makes someprogress
What’s a good quantum?Too short: many processswitches hurt efficiencyToo long: poor response tointeractive requestsTypical length: 10–50 ms
A B C D E
Time
ABCDE
Chapter 2 28CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Priority scheduling
Assign a priority to each process“Ready” process with highestpriority allowed to runRunning process may beinterrupted after its quantumexpires
Priorities may be assigneddynamically
Reduced when a process usesCPU timeIncreased when a process waitsfor I/O
Often, processes grouped intomultiple queues based on priority,and run round-robin per queue
Priority 4
Priority 3
Priority 2
Priority 1
High
Low
“Ready” processes
Chapter 2 29CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Shortest process next
Run the process that will finish the soonestIn interactive systems, job completion time is unknown!
Guess at completion time based on previous runsUpdate estimate each time the job is runEstimate is a combination of previous estimate and mostrecent run time
Not often used because round robin with priorityworks so well!
Chapter 2 30CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Lottery scheduling
Give processes “tickets” for CPU timeMore tickets => higher share of CPU
Each quantum, pick a ticket at randomIf there are n tickets, pick a number from 1 to nProcess holding the ticket gets to run for a quantum
Over the long run, each process gets the CPU m/n ofthe time if the process has m of the n existing ticketsTickets can be transferred
Cooperating processes can exchange ticketsClients can transfer tickets to server so it can have a higherpriority
Chapter 2 31CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Policy versus mechanism
Separate what may be done from how it is doneMechanism allows
Priorities to be assigned to processesCPU to select processes with high priorities
Policy set by what priorities are assigned to processes
Scheduling algorithm parameterizedMechanism in the kernelPriorities assigned in the kernel or by users
Parameters may be set by user processesDon’t allow a user process to take over the system!Allow a user process to voluntarily lower its own priorityAllow a user process to assign priority to its threads
Chapter 2 32CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Scheduling user-level threads
Kernel
Run-timesystem
Threadtable
Processtable
Kernel picks a process torun nextRun-time system (at userlevel) schedules threads
Run each thread for less thanprocess quantumExample: processes get 40mseach, threads get 10ms each
Example schedule:A1,A2,A3,A1,B1,B3,B2,B3Not possible:A1,A2,B1,B2,A3,B3,A2,B1
Process A Process B
Chapter 2 33CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Scheduling kernel-level threads
Kernel schedules eachthread
No restrictions on orderingMay be more difficult foreach process to specifypriorities
Example schedule:A1,A2,A3,A1,B1,B3,B2,B3Also possible:A1,A2,B1,B2,A3,B3,A2,B1
Process A Process B
Kernel
Threadtable
Processtable