lec02-concurrency
lec02-concurrency
Concurrency:
Processes, Threads, and Address
Spaces
January 28, 2008
Prof. Anthony D. Joseph
http://inst.eecs.berkeley.edu/~cs162
Review: Virtual Machine Abstraction
Application
Virtual Machine Interface
Operating
Physical Machine Interface
System
Hardware
• Software Engineering Problem:
– Turn hardware/software quirks
what programmers want/need
– Optimize for convenience, utilization, security,
reliability, etc…
• For Any OS area (e.g. file systems, virtual
memory, networking, scheduling):
– What’s the hardware interface? (physical reality)
– What’s the application interface? (nicer
abstraction)
1/28/08 Joseph CS162 ©UCB Spring 2008 Lec 2.2
Example: Protecting Processes from Each
Other
• Problem: Run multiple applications in such a
way that they are protected from one another
• Goal:
– Keep User Programs from Crashing OS
– Keep User Programs from Crashing each other
– [Keep Parts of OS from crashing other parts?]
• (Some of the required) Mechanisms:
– Address Translation
– Dual Mode Operation
• Simple Policy:
– Programs are not allowed to read/write memory
of other Programs or of Operating System
OS code
• “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
– Multics, UNIX/Linux, OS/2, Windows NT/2000/XP,
Mac OS X
– Often called “multitasking”, but multitasking
has other meanings (talk about this later)
1/28/08 Joseph CS162 ©UCB Spring 2008 Lec 2.8
The Basic Problem of Concurrency
OS code
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 upJoseph
1/28/08 cpp,CS162
cc1,©UCB
cc2, as,2008
Spring and ld Lec 2.25
Multiple Processes Collaborate on a
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
1/28/08 problems Joseph CS162 ©UCB Spring 2008 Lec 2.27
BREAK
Inter-process Communication (IPC)
• 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
Stack ret=C+1
B() {
Pointer
C();
} Stack Growth
C() {
• Stack holds temporary results
A(2); • Permits recursive execution
} • Crucial to modern languages
A(1);
# of addr
spaces:
One Many
#
threads
Per AS:
MS/DOS, early
One Traditional UNIX
Macintosh
Embedded systems Mach, OS/2, Linux
(Geoworks, Windows 9x???
Many VxWorks,
Win NT to XP,
JavaOS,etc)
Solaris, HP-UX, OS
JavaOS, Pilot(PC) 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
1/28/08 Joseph CS162 ©UCB Spring 2008 Lec 2.36
DLLs
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 Java APPS
» Windows NT: 32-64MB
» Windows 2000/XP: 64-128MB OS
– What if we want a cheap network
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
1/28/08
direct accessJoseph
to HW/memory
CS162 ©UCB Spring 2008 Lec 2.37
management
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
1/28/08 Joseph CS162 ©UCB Spring 2008 Lec 2.38