Download COS 318: Operating Systems Processes and Threads

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Unix security wikipedia , lookup

DNIX wikipedia , lookup

Burroughs MCP wikipedia , lookup

Thread (computing) wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
COS 318: Operating Systems
Processes and Threads
Jaswinder Pal Singh
Computer Science Department
Princeton University
(http://www.cs.princeton.edu/courses/cos318/)
Today’s Topics
Concurrency
u  Processes
u  Threads
u 
u 
Reminder:
l 
Hope you’re all busy working on your implementations
2
Concurrency and Processes
u 
Concurrency
l 
l 
u 
Concurrency via Processes
l 
l 
l 
u 
Hundreds of jobs going on in a system
CPU is shared, as are I/O devices
Decompose complex problems into simple ones
Make each simple one a process
Processes run ‘concurrently’ but each process feels like it has its own
computer
Example: gcc (via “gcc –pipe –v”) launches the following
l 
/usr/libexec/cpp | /usr/libexec/cc1 | /usr/libexec/as | /usr/libexec/elf/ld
l 
Each instance of cpp, cc1, as and ld running is a process
3
Process Concurrency
u 
Virtualization
l 
u 
I/O concurrency
l 
l 
l 
u 
Processes interleaved on CPU
I/O for P1 overlapped with CPU
for P2
Each runs almost as fast as if it
has its own computer
Reduce total completion time
P1:
CPU
CPU
P2:
P1:
CPU
CPU
3s
P2:
I/O
2s
CPU
3s
CPU
3s
I/O
2s
CPU parallelism
l 
l 
l 
Multiple CPUs (such as SMP)
Processes running in parallel
Speedup
P1:
P2:
CPU1
3s
CPU2
3s
4
Parallelism
u 
Parallelism is common in real life
l 
l 
u 
Speedup
l 
l 
u 
A single sales person sells $1M annually
Hire 100 sales people to generate $100M revenue
Ideal speedup is factor of N
Reality: bottlenecks + coordination overhead reduce speedup
Questions
l 
l 
l 
Can you speed up by working with a partner?
Can you speed up by working with 20 partners?
Can you get super-linear (more than a factor of N) speedup?
5
Simplest Process
u 
Sequential execution
l 
l 
l 
u 
No concurrency inside a process
Everything happens sequentially
Some coordination may be required
Process state
l 
l 
l 
Registers
Main memory
I/O devices
•  File system
•  Communication ports
l 
…
6
Program and Process
main()
{
...
foo()
...
}
main()
{
...
foo()
...
}
bar()
{
...
}
bar()
{
...
}
Program
heap
stack
registers
PC
Process
7
Process vs. Program
u 
Process > program
l 
l 
u 
Program is just the code; just part of process state
Example: many users can run the same program
Process < program
l 
l 
l 
A program can invoke more than one process
Example: Fork off processes
Many processes can be running the same program
8
Managing Processes: Process Control
Block (PCB)
u 
Process management info
l 
l 
l 
l 
l 
u 
Memory management info
l 
u 
Segments, page table, stats, etc
I/O and file management
l 
u 
Identification
State
Ready: ready to run.
Running: currently running.
Blocked: waiting for resources
Registers, EFLAGS, EIP, and other CPU state
Stack, code and data segment
Parents, etc
Communication ports, directories, file descriptors, etc.
Resource allocation and accounting information
9
API for Process Management
u 
Creation and termination
l 
u 
Signals
l 
u 
Action, Return, Handler
Operations
l 
u 
Exec, Fork, Wait, Kill
Block, Yield
Synchronization
l 
We will talk about this a lot more later
10
Create A Process
u 
Creation
l 
l 
l 
l 
u 
Load code and data into memory
Create an empty call stack
Initialize state
Make the process ready to run
Cloning a process
l 
l 
l 
Save state of current process
Make copy of current code, data, stack and OS state
Make the process ready to run
11
Unix Example
u 
Methods to make processes:
l 
l 
fork clones a process
exec overlays the current process
pid = fork();
if (pid == 0)
/* child process */
exec(“foo”); /* does not return */
Else
/* parent */
wait(pid);
/* wait for child to die */
12
Fork and Exec in Unix
foo:
pid = fork();
if (pid == 0)
exec(“foo”);
else
wait(pid);
pid = fork();
if (pid == 0)
exec(“foo”);
else
wait(pid);
Main()
{
…
}
pid = fork();
if (pid == 0)
exec(“foo”);
else
wait(pid);
Wait
13
More on Fork
u 
Parent process has a
PCB and an address
space
Create and initialize PCB
u  Create an address space
u  Copy the content of the
parent address space to
the new address space
u  Inherit the execution
context of the parent
u 
u 
New process is ready
New
address
space
Parent
address
space
PCB
PCB
14
Process Context Switch
u 
Save a context (everything that a process may damage)
l 
l 
l 
l 
u 
Start a context
l 
u 
All registers (general purpose and floating point)
All co-processor state
Save all memory to disk?
What about cache and TLB?
Does the reverse
Challenge
l 
l 
OS code must save state without changing any state
E.g. how should OS run without touching any registers?
•  CISC machines have a special instruction to save and restore all
registers on stack
•  RISC: reserve registers for kernel or have way to carefully save
one and then continue
15
(Reduced) Process State Transition
Terminate
Running
Create
Ready
Blocked
Resource becomes
available
16
Threads
u 
Thread
l 
l 
u 
A sequential execution stream within a process (also called
lightweight process)
Threads in a process share the same address space
Thread concurrency
l 
l 
l 
l 
Easier to program overlapping I/O and CPU with threads than
with signals
Human being likes to do several things at a time
A server (e.g. file server) serves multiple requests
Multiple CPUs sharing the same memory
17
Thread Control Block (TCB)
l  State
•  Ready: ready to run
•  Running: currently running
•  Blocked: waiting for resources
l  Registers
l  Status
(EFLAGS)
l  Program counter (EIP)
l  Stack
l  Code
18
Typical Thread API
u  Creation
l 
Fork, Join
u  Mutual
l 
exclusion
Acquire (lock), Release (unlock)
u  Condition
l 
variables
Wait, Signal, Broadcast
u  Alert
l 
Alert, AlertWait, TestAlert
19
Revisit Process
u 
Process
l 
l 
l 
u 
Threads
Address space
Environment for the threads to run on OS (open files, etc)
Simplest process has 1 thread
Process
20
Thread Context Switch
u 
Save a context (everything that a thread may damage)
l 
l 
l 
l 
u 
Start a context
l 
u 
All registers (general purpose and floating point)
All co-processor state
Need to save stack?
What about cache and TLB?
Does the reverse
May trigger a process context switch
21
Procedure Call
Caller or callee save some context (same stack)
u  Caller saved example:
u 
save active caller registers
call foo
foo() {
do stuff
}
restore caller regs
22
Threads vs. Procedures
u 
Threads may resume out of order
l 
l 
u 
Threads switch less often
l 
l 
u 
Do not partition registers
Each thread “has” its own CPU
Threads can be asynchronous
l 
l 
u 
Cannot use LIFO stack to save state
Each thread has its own stack
Procedure call can use compiler to save state synchronously
Threads can run asynchronously
Multiple threads
l 
l 
Multiple threads can run on multiple CPUs in parallel
Procedure calls are sequential
23
Process vs. Threads
u 
Address space
l 
l 
l 
u 
Privileges
l 
l 
u 
Processes do not usually share memory (address space)
Process context switch page table and other memory
mechanisms
Threads in a process share the entire address space
Processes have their own privileges (file accesses, e.g.)
Threads in a process share all privileges
Question
l 
Do you really want to share the “entire” address space?
24
Real Operating Systems
One or many address spaces
u  One or many threads per address space
u 
1 address space
Many address spaces
1 thread per
address space
MSDOS
Macintosh
Traditional Unix
Many threads per
address spaces
Embedded OS,
Pilot
VMS, Mach (OS-X), OS/2,
Windows NT/XP/Vista/7,
Solaris, HP-UX, Linux
25
Summary
u 
Concurrency
l 
l 
l 
u 
Processes
l 
u 
CPU and I/O
Among applications
Within an application
Abstraction for application concurrency
Threads
l 
Abstraction for concurrency within an application
26