Lec03 Concurrency
Lec03 Concurrency
Concurrency:
Processes, Threads, and Address Spaces
September 7, 2005
Prof. John Kubiatowicz
http://inst.eecs.berkeley.edu/~cs162
Review: History of OS
• Why Study?
– To understand how user needs and hardware constraints
influenced (and will influence) operating systems
• Several Distinct Phases:
– Hardware Expensive, Humans Cheap
» Eniac, … Multics
– Hardware Cheaper, Humans Expensive
» PCs, Workstations, Rise of GUIs
– Hardware Really Cheap, Humans Really Expensive
» Ubiquitous devices, Widespread networking
• Rapid Change in Hardware Leads to changing OS
– Batch Multiprogramming Timeshare Graphical UI
Ubiquitous Devices Cyberspace/Metaverse/??
– Gradual Migration of Features into Smaller Machines
• Situation today is much like the late 60s
– Small OS: 100K lines/Large: 10M lines (5M browser!)
– 100-1000 people-years
9/07/05 Kubiatowicz CS162 ©UCB Fall 2005 Lec 3.2
Review: Migration of OS Concepts and Features
• “Thread” of execution
– Independent Fetch/Decode/Execute loop
– Operating in some Address space
• Uniprogramming: one thread at a time
– MS/DOS, early Macintosh, Batch processing
– Easier for operating system builder
– Get rid concurrency by defining it away
– Does this make sense for personal computers?
• Multiprogramming: more than one thread at a time
– MULTIX, UNIX, OS/2, Windows NT/2000/XP
– Often called “multitasking”, but multitasking has
other meanings (talk about this later)
Addr 232-1
R0
… …
R31
Fetch Data1
F0
… Exec Data0
F30 Inst237
PC Inst236
…
• Execution sequence: Inst5
Inst4
– Fetch Instruction at PC
Inst3 PC
– Decode
Inst2 PC
– Execute (possibly using registers) Inst1 PC
– Write Results to registers Inst0 PC
– PC = Next Instruction(PC)
Addr 0
– Repeat
9/07/05 Kubiatowicz CS162 ©UCB Fall 2005 Lec 3.8
How can we give the illusion of multiple processors?
OS code
• Cs162-xx accounts:
– Make sure you got an account form
– If you haven’t logged in yet, you need to do so
• Email addresses
– We need an email address from you
– If you haven’t given us one already, you should get
prompted when you log in again
• Nachos readers:
– Available from Northside Copy Central
– Includes printouts of all of the code
• Next Week: Start Project 1
– Go to Nachos page and start reading up
A() { A() { A
… … main
} Program } Process
• More to a process than just a program:
– Program is just part of the process state
– I run emacs on lectures.txt, you run it on
homework.java – Same program, different processes
• Less to a process than a program:
– A program can invoke more than one process
– cc starts up cpp, cc1, cc2, ld, etc
9/07/05 Kubiatowicz CS162 ©UCB Fall 2005 Lec 3.23
Multiple Processes to Contribute on Task
Data 2
Code Code
Stack 1
Data Data
Heap 1
Heap Heap
Code 1 Stack
Stack
Stack 2 Shared
Shared
Data 1
Prog 1 Prog 2
Heap 2 Virtual
Virtual
Address Code 2 Address
Space 1 Space 2
Shared
• Communication occurs by “simply” reading/writing
to shared address page
– Really low overhead communication
– Introduces complex synchronization problems
9/07/05 Kubiatowicz CS162 ©UCB Fall 2005 Lec 3.25
Inter-process Communication (IPC)
• Network Servers
– Concurrent requests from network
– Again, single program, multiple concurrent operations
– File server, Web server, and airline reservation
systems
• Parallel Programming (More than one physical CPU)
– Split program into multiple threads for parallelism
– This is called Multiprocessing
• Execution Stack
– Parameters, Temporary variables
– return PCs are kept while called procedures are
executing
A: tmp=1
A(int tmp) { ret=exit
if (tmp<2)
B: ret=A+2
B();
printf(tmp); C: ret=b+1
} A: tmp=2
B() { Stack ret=C+1
Pointer
C();
} Stack Growth
C() {
• Stack holds temporary results
A(2);
• Permits recursive execution
}
• Crucial to modern languages
A(1);
spaces:
# of addr
One Many
# threads
Per AS:
MS/DOS, early
One Traditional UNIX
Macintosh
Embedded systems Mach, OS/2, Linux
(Geoworks, VxWorks, Windows 95???
Many JavaOS,etc)
Win NT to XP,
JavaOS, Pilot(PC) Solaris, HP-UX, OS X
• Real operating systems have either
– One or many address spaces
– One or many threads per address space
• Did Windows 95/98/ME have real memory protection?
– No: Users could overwrite process tables/System DLLs
9/07/05 Kubiatowicz CS162 ©UCB Fall 2005 Lec 3.33
Example: Implementation Java OS
• Many threads, one Address Space
• Why another OS?
Java OS
– Recommended Minimum memory sizes:
Structure
» UNIX + X Windows: 32MB
» Windows 98: 16-32MB
» Windows NT: 32-64MB Java APPS
» Windows 2000/XP: 64-128MB
– What if want a cheap network OS
point-of-sale computer?
» Say need 1000 terminals Hardware
» Want < 8MB
• What language to write this OS in?
– C/C++/ASM? Not terribly high-level.
Hard to debug.
– Java/Lisp? Not quite sufficient – need
direct access to HW/memory management
9/07/05 Kubiatowicz CS162 ©UCB Fall 2005 Lec 3.34
Summary
• Processes have two parts
– Threads (Concurrency)
– Address Spaces (Protection)
• Concurrency accomplished by multiplexing CPU Time:
– Unloading current thread (PC, registers)
– Loading new thread (PC, registers)
– Such context switching may be voluntary (yield(),
I/O operations) or involuntary (timer, other interrupts)
• Protection accomplished restricting access:
– Memory mapping isolates processes from each other
– Dual-mode for isolating I/O, other resources
• Book talks about processes
– When this concerns concurrency, really talking about
thread portion of a process
– When this concerns protection, talking about address
space portion of a process
9/07/05 Kubiatowicz CS162 ©UCB Fall 2005 Lec 3.35