1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 2: UNIX Processes.
-
Upload
cleopatra-harris -
Category
Documents
-
view
214 -
download
0
Transcript of 1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 2: UNIX Processes.
1GWU CS 259 Brad Taylor Spring 2004
Systems Programming
Meeting 2:Meeting 2:
UNIX ProcessesUNIX Processes
2GWU CS 259 Brad Taylor Spring 2004
Objectives
•ProcessesProcesses
•Student Background / Expectations Student Background / Expectations
•Systems Programming ProjectSystems Programming Project
3GWU CS 259 Brad Taylor Spring 2004
Processes
• What are they? Why use What are they? Why use them? them?
• How do they work? How do they work? • How do they compare with How do they compare with
Threads? Threads? • How deal with multiple How deal with multiple
processes? Concurrencyprocesses? Concurrency
4GWU CS 259 Brad Taylor Spring 2004
Hundreds of things going on in systemHundreds of things going on in system
How to simplify? How to simplify? – Separate (Separate (wrap) each as isolated process: OS deals each as isolated process: OS deals
with one thing at a time, they just deal with OS. with one thing at a time, they just deal with OS. Limits what you need to reason about
– *THE* universal trick for managing complexity: *THE* universal trick for managing complexity: decomposition (“reductionism”)decomposition (“reductionism”)
Why Processes? Simplicity
gccemacsnfsd
lprlswww
emacsnfsd
lprlswww
OSOS
Doesn’t matter that you and your buddies keep crashing processesDoesn’t matter that you and your buddies keep crashing processes
5GWU CS 259 Brad Taylor Spring 2004
I/O parallelism: (I/O parallelism: (I/O jobs bursty))
– overlap execution: make 1 CPU into manyoverlap execution: make 1 CPU into many– Real parallelism: > 1 CPU (multiprocessing)Real parallelism: > 1 CPU (multiprocessing)
Completion time:Completion time:
– B’s completion time = 100s (A + B) So overlap! What if B’s completion time = 100s (A + B) So overlap! What if A=B?A=B?
Why Processes? Speed
emacs (Wait for input) (Wait for input)
gcc
A B
A
B
20 s80 s
Completion time for B? A?10 s
Previous slide: decomposed, can throw processes onto different CPUs easilyPrevious slide: decomposed, can throw processes onto different CPUs easily
6GWU CS 259 Brad Taylor Spring 2004
Processes in the Real World
Processes, parallelism fact of life much longer Processes, parallelism fact of life much longer than OSes have been aroundthan OSes have been around– companies use parallelism for more throughput: 1 companies use parallelism for more throughput: 1
worker = 100 widgets? hire 100 to make 10,000.worker = 100 widgets? hire 100 to make 10,000.
Can you always partition work to speed up job?Can you always partition work to speed up job?– Ideal: N-fold speedupIdeal: N-fold speedup– Reality: bottlenecks + coordination overheadReality: bottlenecks + coordination overhead– Example: CS259 group projectExample: CS259 group project– More abstractly: easy to increase throughput, More abstractly: easy to increase throughput,
reducing latency more difficultreducing latency more difficult
GWU wants more tuition money, just admit more studentsGWU wants more tuition money, just admit more students
7GWU CS 259 Brad Taylor Spring 2004
What is a Thread?
In theory: Turing MachineIn theory: Turing Machine– tape (state), tape head (position)tape (state), tape head (position)
In practice: What’s needed to run code on CPUIn practice: What’s needed to run code on CPU– ““execution stream in an execution context” execution stream in an execution context” – Execution stream: sequential seq. of instructionsExecution stream: sequential seq. of instructions
CPU execution context (1 thread)CPU execution context (1 thread)– state: stack, heap, registers state: stack, heap, registers – position: program counter registerposition: program counter register
OS execution context (n threads):OS execution context (n threads):– identity + open file descriptors, page table, …identity + open file descriptors, page table, …
add r1, r2, r3 sub r2, r3, r10 st r2, 0(r1) …
In practice, each thread has own copy of program counter, stack, registers. Share memoryIn practice, each thread has own copy of program counter, stack, registers. Share memory
8GWU CS 259 Brad Taylor Spring 2004
What is a Process?
Process: thread + address spaceProcess: thread + address space– Abstraction representing what you need to run Abstraction representing what you need to run
thread on OS (open files, etc)thread on OS (open files, etc)
Address space: encapsulates protectionAddress space: encapsulates protection– address state passive, threads activeaddress state passive, threads active
Why separate thread, process?Why separate thread, process?– Many situations where you want multiple Many situations where you want multiple
threads per address space (servers, OS, parallel threads per address space (servers, OS, parallel program)program)
9GWU CS 259 Brad Taylor Spring 2004
int c; int main() {
printf(“hello”);}
Process != Program
Program: code + dataProgram: code + data– passivepassive
Process: running programProcess: running program– state: registers, stack, heap…state: registers, stack, heap…– position: Program counterposition: Program counter
We both run Netscape:We both run Netscape:– same program, different same program, different
process process
stackheap
data
code
int b=5;
main()
int a;
10GWU CS 259 Brad Taylor Spring 2004
How to make one?
Creation:Creation:– Load code and data into memory; create empty Load code and data into memory; create empty
call stackcall stack– Initialize state to same as after a process switchInitialize state to same as after a process switch– Put on OS’s list of processesPut on OS’s list of processes
Clone:Clone:– Stop current process and save stateStop current process and save state– Make copy of current code, Make copy of current code,
data, stack and OS statedata, stack and OS state
– Add new process to OS’s list of processesAdd new process to OS’s list of processes
gcc
gcc gcc
11GWU CS 259 Brad Taylor Spring 2004
Example: Unix
How to make processes:How to make processes:– fork clones a processfork clones a process– exec overlays the current processexec overlays the current process– no create! Fork then exec.no create! Fork then exec.
Pros: Simple, clean. Con: duplicate operationsPros: Simple, clean. Con: duplicate operations
if((pid = fork()) == 0) { /* child process */
exec(“foo”); /* exec does not return */else
/* parent */ wait(pid); /* wait for child to finish */
12GWU CS 259 Brad Taylor Spring 2004
Process Environments
Uniprogramming: 1 process at a timeUniprogramming: 1 process at a time– ““Cooperative timesharing”: mostly PCs, vintage OSesCooperative timesharing”: mostly PCs, vintage OSes– Easy for OS, generally hard for user (peripherals: Easy for OS, generally hard for user (peripherals:
printer, keyboard, etc.)printer, keyboard, etc.)– Violates isolation: Infinite loops? When should Violates isolation: Infinite loops? When should
process yield?process yield?Multiprogramming: > 1 process at timeMultiprogramming: > 1 process at time
– Time-sharing: CTSS, Multics, Unix, VMS, NT, ... Time-sharing: CTSS, Multics, Unix, VMS, NT, ...
– Multiprogramming != multiprocessing Multiprogramming != multiprocessing OS
OS
13GWU CS 259 Brad Taylor Spring 2004
The Multithreading Illusion
Each thread has its illusion: own CPUEach thread has its illusion: own CPU– yet on a uni-processor all threads share the yet on a uni-processor all threads share the
same physical CPU!same physical CPU!– How does this work?How does this work?
Two key pieces:Two key pieces:– thread control block: one per thread, holds thread control block: one per thread, holds
execution stateexecution state– dispatching loop:dispatching loop:
CPU
while(1) interrupt threadsave stateget next threadload state, jump to it
14GWU CS 259 Brad Taylor Spring 2004
Process States
After creation & before After creation & before completion, Processes incompletion, Processes in1 of 3 states:1 of 3 states:
– Running: executing nowRunning: executing now– Ready: waiting for CPUReady: waiting for CPU– Blocked: waiting for another event (I/O, lock)Blocked: waiting for another event (I/O, lock)
Which ready process to pick?Which ready process to pick?– 0 ready processes: run idle loop0 ready processes: run idle loop– 1 ready process: easy!1 ready process: easy!– > 1: what to do? > 1: what to do?
Transitions? N threads, Max, min in each state?Transitions? N threads, Max, min in each state?
running ready
blockedI/O I/O
? ?
?
?
15GWU CS 259 Brad Taylor Spring 2004
Choosing Process to Run
Scan process table for first runnable?Scan process table for first runnable?– Expensive. Strange priorities (small pid’s better)Expensive. Strange priorities (small pid’s better)– Divide into runnable and blocked processesDivide into runnable and blocked processes
FIFO?FIFO?– put threads on back of list, pull them off from put threads on back of list, pull them off from
frontfront
Priority?Priority?– give some threads a better shot at the CPUgive some threads a better shot at the CPU– problems?problems?
16GWU CS 259 Brad Taylor Spring 2004
Scheduling Policies
Scheduling issuesScheduling issues– fairness: don’t starve processfairness: don’t starve process– prioritize: more important firstprioritize: more important first– deadlines: must do by time ‘x’ (car brakes)deadlines: must do by time ‘x’ (car brakes)– optimization: some schedules >> faster than othersoptimization: some schedules >> faster than others
No universal policy:No universal policy:– many variables, can’t maximize them allmany variables, can’t maximize them all– conflicting goals conflicting goals
» more important jobs vs starving othersmore important jobs vs starving others» I want my job to run first, you want yours.I want my job to run first, you want yours.
Given some policy, how to get control? Switch? Given some policy, how to get control? Switch?
17GWU CS 259 Brad Taylor Spring 2004
How to get control?
Traps: events generated by current processTraps: events generated by current process– system callssystem calls– errors (illegal instructions)errors (illegal instructions)– page faultspage faults
Interrupts: events external to the processInterrupts: events external to the process– I/O interruptI/O interrupt– timer interrupt (quantum ≈ 100 milliseconds)timer interrupt (quantum ≈ 100 milliseconds)
Process perspective: Process perspective: – explicit: process yields processor to anotherexplicit: process yields processor to another– implicit: error handling causes an expensive blocking implicit: error handling causes an expensive blocking
event, gets switchedevent, gets switched
Waiting for network event, disk transfer
Client/server: donate CPU to other, have a lock donate cpu
Waiting for network event, disk transfer
Client/server: donate CPU to other, have a lock donate cpu
18GWU CS 259 Brad Taylor Spring 2004
How to “context switch”?
Very machine dependent. Must save:Very machine dependent. Must save:– general-purpose & floating point registers, any co-general-purpose & floating point registers, any co-
processor state, shadow registers (Alpha, sparc)processor state, shadow registers (Alpha, sparc)
Tricky:Tricky:– OS code must save state without changing any state (ex: OS code must save state without changing any state (ex:
Harvard Architecture PIC)Harvard Architecture PIC)– How to run without touching any registers??How to run without touching any registers??
» Some CISC machines have single instruction to save all Some CISC machines have single instruction to save all registers on stackregisters on stack
» RISC: reserve registers for kernel (MIPS) or have way to RISC: reserve registers for kernel (MIPS) or have way to carefully save one and then continue carefully save one and then continue
How expensive? How expensive? Direct cost of saving; opportunity cost of Direct cost of saving; opportunity cost of flushing useful caches (cache, TLB, etc.)flushing useful caches (cache, TLB, etc.)
Anything process could damageAnything process could damage
19GWU CS 259 Brad Taylor Spring 2004
Fundamentals of Process Switching
““execution” Grand Theme of CS:execution” Grand Theme of CS:– procedure calls, threads, processes just variations procedure calls, threads, processes just variations
What’s the minimum to execute code?What’s the minimum to execute code?– Position (pointer to current instruction)Position (pointer to current instruction)– State (captures result of computation)State (captures result of computation)
Minimum to switch from one to another?Minimum to switch from one to another?– Save old instruction pointer and load new oneSave old instruction pointer and load new one
What about state? What about state? – If per-thread state have to save and restoreIf per-thread state have to save and restore– In practice can save everything, nothing or In practice can save everything, nothing or
combination.combination.
20GWU CS 259 Brad Taylor Spring 2004
Switching Between Procedures
Procedure call:Procedure call:
How is state saved?How is state saved?– saved proactively? saved lazily? Not saved?saved proactively? saved lazily? Not saved?
save active caller registerscall foo
restore caller regs
Stack, head, global variablesStack, head, global variables
saves used callee registers …do stuff...restores callee
registersjumps back to pc
21GWU CS 259 Brad Taylor Spring 2004
Threads vs Procedures
Threads may resume out of order:Threads may resume out of order: – cannot use LIFO stack to save statecannot use LIFO stack to save state– general solution: duplicate stackgeneral solution: duplicate stack
Threads switch less often:Threads switch less often: – don’t partition registers (why?)don’t partition registers (why?)
Threads involuntarily interrupted:Threads involuntarily interrupted:– synchronous: proc call can use compiler to save statesynchronous: proc call can use compiler to save state– asynchronous: thread switch code saves all registersasynchronous: thread switch code saves all registers
> 1 Thread can run> 1 Thread can run– scheduling: what to overlay on CPU next? scheduling: what to overlay on CPU next? – proc call scheduling obvious: run called procedureproc call scheduling obvious: run called procedure
22GWU CS 259 Brad Taylor Spring 2004
Generic Abstraction Template
Abstraction: Abstraction: how OS abstracts underlying resourcehow OS abstracts underlying resource
Virtualization: Virtualization: how OS makes small number of how OS makes small number of resources seem like an “infinite” numberresources seem like an “infinite” number
Partitioning: Partitioning: how OS divides resource how OS divides resource
Protection: Protection: how OS prevents bad people from using how OS prevents bad people from using pieces they shouldn’tpieces they shouldn’t
Sharing: Sharing: how different instances sharedhow different instances shared
Speed: Speed: how OS reduceshow OS reduces managementmanagement overheadoverhead
23GWU CS 259 Brad Taylor Spring 2004
CPU Abstraction
CPU state representation as processCPU state representation as process
Virtualization: Virtualization: processes interleaved transparently processes interleaved transparently (run ~1/n slower than real CPU)(run ~1/n slower than real CPU)
Partitioning: Partitioning: CPU shared across timeCPU shared across time
Protection: Protection: (1) pigs: forcibly interrupted; (1) pigs: forcibly interrupted; (2) corruption: process’ state saved in OS; (2) corruption: process’ state saved in OS; (3) imposter: cannot assume another’s identity (3) imposter: cannot assume another’s identity
Sharing: Sharing: yield your CPU time slice to another processyield your CPU time slice to another process
Speed: Speed: (1) large scheduling quanta; (2) minimize state (1) large scheduling quanta; (2) minimize state needed to switch; (3) share common state (code); needed to switch; (3) share common state (code); (4) duplicate state lazily(4) duplicate state lazily
24GWU CS 259 Brad Taylor Spring 2004
Past: isolated processesPast: isolated processes– modularize systemmodularize system– share resourcesshare resources– speedspeed
Today: safe non-isolated processesToday: safe non-isolated processes– Processes share state (computer, files, Processes share state (computer, files,
memory).memory).– Concurrent access = bugs.Concurrent access = bugs.
» Example: single lane road, two approaching carsExample: single lane road, two approaching cars
gcc
Past and Present
hardwareOS
www lsvi
25GWU CS 259 Brad Taylor Spring 2004
Multiple Processes, one world: Safe?
No. Bugs if one process writes state that may be No. Bugs if one process writes state that may be simultaneously read/written by other. simultaneously read/written by other. – emacs writes out file while gcc compiles it. emacs writes out file while gcc compiles it.
– Result? Hard to predict, except that its probably not Result? Hard to predict, except that its probably not going to be correct (or repeatable: have fun). going to be correct (or repeatable: have fun).
– Always dangerous? (No. More later.) But often enough Always dangerous? (No. More later.) But often enough that you better think carefully.that you better think carefully.
When safe? Isolated processesWhen safe? Isolated processes– ““isolated” = shares no state with any other processisolated” = shares no state with any other process– doesn’t really exist: share file system, memory, …doesn’t really exist: share file system, memory, …
new
oldemacs gcc
Multiple readersMultiple readers
26GWU CS 259 Brad Taylor Spring 2004
Isolated vs Non-Isolated Processes
Isolated: no shared data between processesIsolated: no shared data between processes– If P produces result x, then running any other set of If P produces result x, then running any other set of
independent processes P’, P’’, … wouldn’t change it.independent processes P’, P’’, … wouldn’t change it.– Scheduling independence: any order = same resultScheduling independence: any order = same result– Consider: internet, lots of independent machines. If Consider: internet, lots of independent machines. If
don’t share state, doesn’t matter what other people don’t share state, doesn’t matter what other people do.do.
Non-isolated: share stateNon-isolated: share state– Result can be influenced by what other processes Result can be influenced by what other processes
runningrunning– Scheduling can alter resultsScheduling can alter results– Big problem: non-deterministic. Same inputs != Big problem: non-deterministic. Same inputs !=
same result. Makes debugging very very hard.same result. Makes debugging very very hard.newold
emacs gcc newemacs gcc
Can run each sequentially, or interlace at level of instruction: doesn’t matterCan run each sequentially, or interlace at level of instruction: doesn’t matter
27GWU CS 259 Brad Taylor Spring 2004
Why share?Two core reasons
CostCost: buy m, amortize cost by letting n share (n > m): buy m, amortize cost by letting n share (n > m)– One computer, many jobs; one road, many cars; this One computer, many jobs; one road, many cars; this
classroom, many other classes.classroom, many other classes.
InformationInformation: need results from other processes: need results from other processes
– Gives speed: parallel threads working on same stateGives speed: parallel threads working on same state– Gives modularity: ability to share state allows tasks split Gives modularity: ability to share state allows tasks split
into isolated processes (gcc, emacs) that communicate into isolated processes (gcc, emacs) that communicate – Sharing information hugely important. Consider impact of Sharing information hugely important. Consider impact of
new ways to share information (print, telephone, internet, new ways to share information (print, telephone, internet, www, human voice)www, human voice)
emacs gccc.c
28GWU CS 259 Brad Taylor Spring 2004
Example: Two Threads, One Counter
Google gets millions of hits a day. Uses Google gets millions of hits a day. Uses multiple threads (on multiple multiple threads (on multiple processors) to speed things up. processors) to speed things up.
Simple shared state error: each thread Simple shared state error: each thread increments a shared counter to track increments a shared counter to track the number of hits today:the number of hits today:
What happens when two threads execute What happens when two threads execute this code concurrently?this code concurrently?
…hits = hits +
1;…
29GWU CS 259 Brad Taylor Spring 2004
Fun with Shared CountersOne possible result: lost update!One possible result: lost update!
One other possible result: everything works.One other possible result: everything works.– Bugs in parallel code are frequently intermittent. Makes Bugs in parallel code are frequently intermittent. Makes
debugging hard.debugging hard. Called a “race condition”Called a “race condition”
hits = 0 + 1
read hits (0)
hits = 0 + 1read hits (0)
T1 T2
hits = 1
hits = 0
time
30GWU CS 259 Brad Taylor Spring 2004
Race ConditionsRace condition: timing dependent error involving Race condition: timing dependent error involving
shared state.shared state. – Whether it happens depends on Whether it happens depends on how threads scheduledhow threads scheduled
*Hard* because:*Hard* because:– Must ensure all possible schedules are safe: Number of Must ensure all possible schedules are safe: Number of
possible schedules permutations hugepossible schedules permutations huge
» Some bad schedules? Some that will work sometimes?Some bad schedules? Some that will work sometimes?– Intermittent, timing dependent => small changes Intermittent, timing dependent => small changes
(printf’s, different machine) hide bugs (writes solve …)(printf’s, different machine) hide bugs (writes solve …)
if(n == stack_size) /* A */return full; /* B */
stack[n] = v; /* C */n = n + 1; /* D */
31GWU CS 259 Brad Taylor Spring 2004
– Who wins?Who wins?– Guaranteed that someone wins?Guaranteed that someone wins?– What if both threads run on own identical speed CPU What if both threads run on own identical speed CPU
executing in parallel? (Guaranteed to go on forever?)executing in parallel? (Guaranteed to go on forever?)– What to do???What to do???
More Race Condition Fun
Thread b: i = 0; while(i > -10)
i = i - 1; print “B won!”;
Thread a: i = 0; while(i < 10)
i = i +1; print “A won!”;
32GWU CS 259 Brad Taylor Spring 2004
Dealing with Race ConditionsNothing. Can be a fine responseNothing. Can be a fine response
– if “hits” a performance counter, lost updates may not matter.if “hits” a performance counter, lost updates may not matter.– Pros: simple, fast. Cons: usually doesn’t help.Pros: simple, fast. Cons: usually doesn’t help.
Don’t share: duplicate state, or partition:Don’t share: duplicate state, or partition: – Do this whenever possible! One counter per process, two lane Do this whenever possible! One counter per process, two lane
highways instead of single, …highways instead of single, …
– Pros: simple again. Cons: never enough to go around or may have to Pros: simple again. Cons: never enough to go around or may have to share (gcc eventually needs to compile file)share (gcc eventually needs to compile file)
Is there a general solution? Yes!Is there a general solution? Yes! – What was our problem? Bad interleaving. So prevent!What was our problem? Bad interleaving. So prevent!
hits[1] = hits[1] + 1; hits[2] = hits[2] + 1;
T2T1
One room per professor
One room per professor
33GWU CS 259 Brad Taylor Spring 2004
Atomicity: Controlling Race Conditions
atomic unit = instruction sequence guaranteed atomic unit = instruction sequence guaranteed to execute indivisibly (or “critical section”)to execute indivisibly (or “critical section”)– If two threads execute same atomic unit at same If two threads execute same atomic unit at same
time, one thread executes the whole sequence time, one thread executes the whole sequence before other begins.before other begins.
– How to make multiple instructions seem like one How to make multiple instructions seem like one atomic one??atomic one??
hits = hits + 1
T1 T2
hits = 2
hits = 0
timehits = hits + 1
Get there at same time, one will winGet there at same time, one will win
34GWU CS 259 Brad Taylor Spring 2004
Making Atomicity: Uniprocessor
Requirement: thread not preempted in critical section.Requirement: thread not preempted in critical section. – Have scheduler check thread’s program counter: Have scheduler check thread’s program counter:
– Pro: fast atomicity. Con: need compiler support.Pro: fast atomicity. Con: need compiler support.OS Traditional: threads disable/enable interrupts:OS Traditional: threads disable/enable interrupts:
– Pro: works. Con: infinite loop = stop the world.Pro: works. Con: infinite loop = stop the world.
disable_interrupts();hits = hits + 1;enable_interrupts();
while(1) { /* naïve dispatcher loop */interrupt thread;if pc != critical section
save old thread statepick threadload new thread state
jump to thread
/* openbsd */int s = splhigh(); hits = hits + 1;splx(s);
save_flags(flags); /* linux */cli()hits = hits + 1;restore_flags(flags);
What’s a problem if we leave it like this?
What’s a problem if we leave it like this?
35GWU CS 259 Brad Taylor Spring 2004
Making Atomicity: Multiprocessor
Must prevent any other thread from executing critical Must prevent any other thread from executing critical sectionsection
Hardware support: could wire in atomic incrementHardware support: could wire in atomic increment– pro: works. Con: not a general approachpro: works. Con: not a general approach– instead, we do a variant: provide a hardware building block instead, we do a variant: provide a hardware building block
that can construct atomic primitivesthat can construct atomic primitives
General solution: locks (just like on door)General solution: locks (just like on door)– when thread enters critical section, locks it so no other when thread enters critical section, locks it so no other
thread can enter. when it leaves, thread unlocks it.thread can enter. when it leaves, thread unlocks it.
– Pro: general. Con: manual, low level (better later…)Pro: general. Con: manual, low level (better later…)
unlocklock
36GWU CS 259 Brad Taylor Spring 2004
Locks: Making Code Atomic
Lock: shared variable, two operations:Lock: shared variable, two operations: – ““acquire” (lock): acquire exclusive access to lock, wait if acquire” (lock): acquire exclusive access to lock, wait if
lock already acquired.lock already acquired.– ““release” (unlock): release exclusive access to lock.release” (unlock): release exclusive access to lock.
How to use? Bracket critical section in lock/unlock:How to use? Bracket critical section in lock/unlock:
– Result: only one thread updating counter at a time.Result: only one thread updating counter at a time.– Access is “mutually exclusive”: locks in this way called Access is “mutually exclusive”: locks in this way called
“mutexes”“mutexes”– What have we done? Bootstrap big atomic units from What have we done? Bootstrap big atomic units from
smaller ones (locks)smaller ones (locks)
lock hit_lock;...lock(hit_lock); hit = hit + 1; unlock(hit_lock);
37GWU CS 259 Brad Taylor Spring 2004
Lock Rules for Easy Concurrency
Every shared variable protected by a lockEvery shared variable protected by a lock– shared = touched by more than one threadshared = touched by more than one thread
Must hold lock for a shared variable before you Must hold lock for a shared variable before you touchtouch– essential property: two threads can’t hold same lock essential property: two threads can’t hold same lock
at same timeat same time
Atomic operation on several shared variables:Atomic operation on several shared variables: – acquire all locks before touching, don’t release until acquire all locks before touching, don’t release until
donedone
int stack[ ], n;lock s_l, n_l;
lock(s_l); lock(n_l); stack[n++] = v; unlock(s_l); unlock(n_l);
Private data has implicit lockPrivate data has implicit lock
Why do locks give big actions? No well behaved thread can touch variable Why do locks give big actions? No well behaved thread can touch variable
Two phase locks: acquire, don’t release, release, don’t acquire.Two phase locks: acquire, don’t release, release, don’t acquire.
38GWU CS 259 Brad Taylor Spring 2004
Implementing multiprocessing locks
How?How?– Turning off other processors probably too expensive. Or Turning off other processors probably too expensive. Or
impossible (OSes don’t let user-level threads do so)impossible (OSes don’t let user-level threads do so)– Instead: have hardware support.Instead: have hardware support.
» Do we need a hardware lock instruction? No. Can build locks Do we need a hardware lock instruction? No. Can build locks from more primitive instructions. from more primitive instructions.
» Common primitives: test & set, atomic swap, ...Common primitives: test & set, atomic swap, ...
Example instruction: atomic swap (aswap):Example instruction: atomic swap (aswap):– aswap mem, R: atomically swap values in reg and memoryaswap mem, R: atomically swap values in reg and memory
– hardware guarantees the two assignments are atomic. hardware guarantees the two assignments are atomic. – This primitive lets us implement any other concurrency This primitive lets us implement any other concurrency
primitive!primitive!temp = R; R = mem; mem = temp;
39GWU CS 259 Brad Taylor Spring 2004
General atomicity requirements
We’ve shown one way to implement critical sections We’ve shown one way to implement critical sections (locks). There are many others. However, they all (locks). There are many others. However, they all share three common safety requirements:share three common safety requirements:– Mutual exclusion: at most one process at a time is in the Mutual exclusion: at most one process at a time is in the
critical sectioncritical section– Progress (deadlock free)Progress (deadlock free)
» If several simultaneous requests, must allow one to proceed.If several simultaneous requests, must allow one to proceed.» Must not depend on processes outside critical sectionMust not depend on processes outside critical section
– Bounded (starvation free): A process attempting to enter Bounded (starvation free): A process attempting to enter critical section will eventually succeed.critical section will eventually succeed.
Some nice properties: Some nice properties: – fair: don’t make some wait longer than others.fair: don’t make some wait longer than others.– efficient: don’t waste resources waiting.efficient: don’t waste resources waiting.– oh yeah: and simple.oh yeah: and simple.
40GWU CS 259 Brad Taylor Spring 2004
Process SummaryThread = pointer to instruction + stateProcess = thread + address spaceKey aspects:
– per-thread state– picking a thread to run– switching between threads
Many threads + sharing state = race conditions– one thread modifying while others reading/writing
How to solve? Intuition:– private state doesn’t need to be protected– multiple threads run one after another can’t have race conditions– SO: to make multiple threads behave like one safe sequential
thread, force only one thread at a time to use shared state. – General solution is to use locks.
41GWU CS 259 Brad Taylor Spring 2004
Student Background / Expectations
• Why are YOU here for Systems Why are YOU here for Systems Programming?Programming?• Info Info KEYKEY to project group formation to project group formation• NameName• Level of StudyLevel of Study• Academic backgroundAcademic background• Work ExperienceWork Experience• Personal Strengths / ChallengesPersonal Strengths / Challenges• Interesting Stories?Interesting Stories?
42GWU CS 259 Brad Taylor Spring 2004
Project Alternatives
Nathan will describe these …Nathan will describe these …
[email protected]@gimp.org