Processes: A System View
CS 241 February 17, 2012 Copyright University of Illinois CS 241 Staff
What the OS does: 2 State Model
Processes
dispatch enter not running pause exit running
What the OS does: 2 State Model
Processes
dispatch enter not running pause queue dispatch exit exit running
System
enter
processor
pause
3
2 State Model
Processes
dispatch enter not running pause queue dispatch exit exit running
System
enter What information do we need to keep while in the queue?
processor
pause
4
What the OS stores: PCB
!
OS stores Process Control Block (PCB) for each process
! In-memory OS structure ! User processes cannot access it
Contents:
! Identifiers
pid & ppid (process ID & parent process ID) ! Processor state information !! User-visible registers, control and status, stack ! Scheduling information !! Process state, priority, what event the process is waiting for, ...
!!
What the OS stores: PCB
!
Contents (contd):
! Inter-process communication
Signals ! Privileges !! CPU instructions, memory ! Memory Management !! e.g., Page tables ! Resource ownership and utilization
!!
Five State Process Model
All models are wrong. Some Models are Useful
! George Box, statistician
!
2 state model
! Too simplistic ! What does Not Running mean?
7 state model
! Considers suspending process to disk ! See Stallings book, section 3.2
Next: 5 state model
5 State Model: States
running
not running
5 State Model: States
running
ready
blocked
5 State Model: States
running
done
new
ready
blocked
10
5 State Model: Summary
!
Running
! Currently executing ! On a single processor machine, at most one process in the running state
Ready
! Prepared to execute
Blocked
! Waiting on some event
New
! Created, but not loaded into memory
Done
! Released from pool of executing processes
11
5 State Model: Transitions
!
Null (nothing) to New
! New process creation
running
done
enter
new
ready
blocked
12
5 State Model: Transitions
!
New to Ready
! Move to pool of
executable processes
running
done
new
ready
blocked
13
5 State Model: Transitions
!
Ready to Running
! Chosen to run from
the pool of processes (How?)
running
done
new
ready
blocked
14
5 State Model: Transitions
!
What events cause these transitions?
running
done
new
ready
blocked
15
5 State Model: Transitions
!
Running to Ready
! Preempted by OS
Running to Blocked
! Request for an
unavailable resource
running done
Running to Done
! Terminated / completed
new ready blocked
16
5 State Model: Transitions
!
Blocked to Ready
! Resource is now available
running
done
new
ready
blocked
17
5 State Model: Transitions
!
Ready to Done
! Terminated by another process
Blocked to Done
! Terminated by another process
running done
new
ready
blocked
18
5 State Model: Transitions
normal or abnormal termination
enter created
selected to run
running
done
I/O request quantum expired I/O complete
19
new
ready
blocked
Process Queue Model
2 State Model: What is missing? queue dispatch
processor
enter
exit
enter
ready queue
dispatch
processor
exit
Process exceeds time quanta Process makes systems call
20
timeout blocked queue event wait
Process Queue Model
ready queue dispatch
processor
enter
exit
timeout event 1 queue event 1 wait event 2 queue event 2 wait event 3 queue event n wait
21
What do we gain with multiple queues?
Process Queue Model
ready queue dispatch
processor
enter
exit
timeout priority 1 queue priority 1 wait priority 2 queue priority 2 wait priority 3 queue priority n wait
22
What do we gain with multiple queues?
Take-away questions
What would happen if user processes were allowed to disable interrupts?
!
In a single CPU system what is the maximum number of processes that can be in the running state?
!
23
From Processes to Threads
CS 241 February 17, 2012 Copyright University of Illinois CS 241 Staff
24
Processes vs. threads
!
Process
! Fork is expensive (time & memory)
Thread
! A lightweight process: little memory, fast startup ! Shared memory among threads in a process
25
Processes vs. threads
Environment (resource) execution
Three processes each with one thread
One process with three threads
26
Processes vs. threads
!
Each process can include many threads All threads of a process share:
! ! ! ! ! !
Process ID Memory (program code and global data) Open file/socket descriptors Working environment (current directory, user ID, etc.) Semaphores (covered later in the course) Signal handlers and signal dispositions (covered later in the course)
27
Thread usage: word processor
!
Working file can only be accessed by one process at a time
What would happen if this were singlethreaded?
28
Thread usage: word processor
!
Working file can only be accessed by one process at a time
29
Thread usage: web server
What would happen if this were singlethreaded?
30
Thread of execution
!
Sequential set of instructions
! Each has its own function calls & automatic (local) variables ! Need program counter and stack for each thread
32
Normal 1-thread function call
Calling program
processfd();
Called function
processfd() {
Thread of execution
33
Compare: Threaded function call
Creating program
pthread_create();
Created thread
processfd() {
Calling program
processfd();
Called function
processfd() {
Thread creation Thread of execution
34
Thread Execution States
!
Events associated with a change in thread state:
! Spawn (another thread) ! Block
Should blocking a thread block other, or all, threads? ! Unblock ! Finish (thread) !! De-allocate register context and stacks
!!
35
Thread-Specic Resources
!
Each thread has its own
! Thread ID (integer) ! Stack, Registers, Program Counter
Threads in one process can communicate via shared memory
! Must be done carefully!
36
Processes vs. Threads
Per Process Items Address space Global variables Open files Child processes Pending alarms Signals and signal handlers Accounting information Per Thread Items Program counter Registers Stack State
Each thread executes separately Threads in the same process share many resources No protection among threads!! (What?)
37
Process vs. thread creation
Platform AMD 2.3 GHz Opteron (16 cpus) AMD 2.4 GHz Opteron (8 cpus) IBM 4.0 GHz POWER6 (8 cpus) IBM 1.9 GHz POWER5 p5-575 (8 cpus) IBM 1.5 GHz POWER4 (8 cpus) INTEL 2.4 GHz Xeon (2 cpus) INTEL 1.4 GHz Itanium2 (4 cpus)
!
fork() real 12.5 17.6 9.5 64.2 104.5 54.9 54.5 user 1.0 2.2 0.6 30.7 48.6 1.5 1.1 sys 12.5 15.7 8.8 27.6 47.2 20.8 22.2
pthread_create() real 1.2 1.4 1.6 1.7 2.1 1.6 2.0 user 0.2 0.3 0.1 0.6 1.0 0.7 1.2 sys 1.3 1.3 0.4 1.1 1.5 0.9 0.6
http://www.llnl.gov/computing/tutorials/pthreads. Timings reflect 50,000 process/thread Creations were performed with the time utility, and units are in seconds, no optimization flags.
38
Key points
!
Threads are lightweight
! Is this good or bad?
Threads share memory and other resources
! (Still have own stack, registers, PC, state) ! Is this good or bad?
39
Next time
!
Monday: Using threads Tuesday: MP3 Shell due
41