Date post: | 13-Jan-2016 |
Category: |
Documents |
Upload: | vernon-berry |
View: | 217 times |
Download: | 0 times |
Process and Thread
Chapter 3Advanced Operating System
Process
A program in execution The entity that can be assigned to
and executed on a processor Program code: which may be shared
with other processes that are elements of a process
Set of data
Process
OS maintains the following for a process Executable program (.EXE, .DLL, etc) Data (variables, constant, buffer, …) User stack (local variables, function
call) Process control block (PCB)
Process management
Interleave the execution of several processes ... to maximize processor utilization while providing reasonable response
time Allocate resources to processes Support inter-process
communication
Dispatcher
Program that moves the processor from one process to another
Prevents a single process from monopolizing processor time. Every process should be able to use
the processor for a fair amount of time
Two-state process model
Notrunning Running
Enter
Dispatch
Pause
Exit
Two-state process model The operating system creates a new process Operating system creates PCB Operating system enters process into the
system in the not running state The process exists is waiting to execute The currently running process will be interrupted The dispatcher will select some other process to
run The former process moves from the running
state to the not running state, and one of the other processes move to the running state
Process creation
Submission of a batch job User logs on Create to provide a service such as
printing Spawned by an existing process
Process creation Process spawning: When the
operating system creates a process at the explicit request of another process
When one process spawns another, the former is referred to as the parent process
The spawned process is referred to as the child process
Process termination
Batch job issues Halt instruction User logs off Process executes a service request
to terminate Parent terminates so child
processes terminate
Process termination
Operating system intervention E.g. when deadlock occurs
Error and fault conditions E.g. memory unavailable, protection
error, arithmetic error, I/O failure, invalid instruction
Round-robin scheduling
The queue is a first-in-first-out list The processor operates in round-
robin fashion on the available process Each process in the queue is given a
certain amount of time, in turn, to execute and then returned to the queue, unless blocked
Round-robin scheduling
queuedispatch
processor
At timeout, process switching occurs.
At timeout, process switching occurs.
Each process is allowed to execute for at most a quantum.
Each process is allowed to execute for at most a quantum.
Process is preempted when…
In the following two cases, a process cannot continue running and must leave the processor. It is preempted. Timeout I/O, or wait for other events
Two states are not enough... Not-running:
ready to execute blocked, e.g. waiting for I/O
Dispatcher cannot just select the process that has been in the queue the longest (i.e. the one in queue front) because it may be blocked
Running process
Ready process
Blocked process
Five-state Process Model
New ExitAdmit Release
RunningReady
Dispatch
Time-out
Blocked
Event occurs Event wait
Five-state Model
Running – being executed by the processor
Ready – ready to execute Blocked – cannot execute until some
event occurs New – created, but not yet admitted Exit – released
Blocked Queues
Ready queueAdmit
Blocked queue/ Event queue
Release
Timeout
Event waitEvent occurs
A separate queue holds the processes waiting for each event.
A separate queue holds the processes waiting for each event.
DispatchProcessor
Running process
Ready process
Blocked process
How process state changes (1/8)
a := 1b := a + 1c := b + 1read a filea := b - cc := c * bb := 0
a := 1b := a + 1c := b + 1a := b - cc := c * bb := 0c := 0
a := 1read a fileb := a + 1c := b + 1a := b - cc := c * bb := 0
ready ready ready
How process state changes (2/8)
a := 1b := a + 1c := b + 1read a filea := b - cc := c * bb := 0
a := 1b := a + 1c := b + 1a := b - cc := c * bb := 0c := 0
a := 1read a fileb := a + 1c := b + 1a := b - cc := c * bb := 0
running ready ready
TimeoutTimeout
How process state changes (3/8)
a := 1b := a + 1c := b + 1read a filea := b - cc := c * bb := 0
a := 1b := a + 1c := b + 1a := b - cc := c * bb := 0c := 0
a := 1read a fileb := a + 1c := b + 1a := b - cc := c * bb := 0
ready running ready
Start I/OStart I/O
How process state changes (4/8)
a := 1b := a + 1c := b + 1read a filea := b - cc := c * bb := 0
a := 1b := a + 1c := b + 1a := b - cc := c * bb := 0c := 0
a := 1read a fileb := a + 1c := b + 1a := b - cc := c * bb := 0
ready blocked running
zz
TimeoutTimeout
How process state changes (5/8)
a := 1b := a + 1c := b + 1read a filea := b - cc := c * bb := 0
a := 1b := a + 1c := b + 1a := b - cc := c * bb := 0c := 0
a := 1read a fileb := a + 1c := b + 1a := b - cc := c * bb := 0
running blocked ready
zz
Start I/OStart I/O
How process state changes (6/8)
a := 1b := a + 1c := b + 1read a filea := b - cc := c * bb := 0
a := 1b := a + 1c := b + 1a := b - cc := c * bb := 0c := 0
a := 1read a fileb := a + 1c := b + 1a := b - cc := c * bb := 0
blocked blocked running
zz
zz
Suppose the Green process finishes I/O
Suppose the Green process finishes I/O
How process state changes (7/8)
a := 1b := a + 1c := b + 1read a filea := b - cc := c * bb := 0
a := 1b := a + 1c := b + 1a := b - cc := c * bb := 0c := 0
a := 1read a fileb := a + 1c := b + 1a := b - cc := c * bb := 0
blocked ready running
zz
TimeoutTimeout
How process state changes (8/8)
a := 1b := a + 1c := b + 1read a filea := b - cc := c * bb := 0
a := 1b := a + 1c := b + 1a := b - cc := c * bb := 0c := 0
a := 1read a fileb := a + 1c := b + 1a := b - cc := c * bb := 0
blocked running ready
zz
TimeoutTimeout
Suspend Processes Suspend – stop the execution of a proc
ess temporarily OS may suspend a process that causes
a problem Interactive user request Timing: a process that executes
periodically Swapping: sometimes OS may swap a bloc
ked process to disk to free up more memory
Suspend states Two new states: Think about these…
What’s the meaning of the two new states?
Why not a single ‘suspend’ state? Why no ‘running, suspend’ state?
Blocked, suspend
Ready,suspend
Process state transition with suspend states
Dispatch
Blocked
Running Exit
Admit
Timeout
EventOccurs
EventWait
SuspendAdmit
Ready,suspend
Blocked,suspend
EventOccurs
Activate
Activate
Suspend
Suspend
New
ReadyRelease
An example of state transition
Blockedsuspend
Blocked
Blocked
Ready
Running
Readysuspend
Ready
The process starts an I/O operation
The process starts an I/O operation
The process is suspended while waiting for the I/O to finish
The process is suspended while waiting for the I/O to finish
I/O is finishedI/O is finished
The process is activated afterwards
The process is activated afterwards
The process is activated before I/O is finished
The process is activated before I/O is finished
I/O is finishedI/O is finished
Process control block (PCB)
Operating system creates and manages a process control block
PCB is the key tool that enables the operating system to support multiple processes to provide the multiprocessing
Process Control Block
PCB contains data that the OS needs to control the process
PCB consists of Process identification Processor state
information Process control
information
PCBProcess identificationpid:123uid:tom
Processor state infoax, bx, cx, eflags, pc, …cs, ds, ss, …
Process control info……
Process Identification
Process ID, a unique numeric identifier
User identifier who is responsible for the job, or who
runs the process used to determine what access rights
the process has
Processor State Information
Processor state: contents of processor registers, incl. Data registers Address registers: e.g. stack pointer Control and status registers
Used to save and restore the processor state in process switching
Process Control Information Additional information needed by the opera
ting system to control and coordinate the various active processes scheduling and state information data structuring interprocess communication process privileges memory management resource ownership and utilization
Process switching may happen at…
Interrupt – an interruption request from hardware external to the CPU
Trap – an error condition or exceptional condition associated with the current instruction
Supervisor call – call of some special functions of the OS
Process switching may happen at… Interrupts
Clock I/O: I/O completion, I/O error
Traps illegal file access attempt memory fault Supervisor call file open, read a char synchronization primitive
Timeout
Event wait
Event occurs
Process switching at Interrupt
In case of I/O error (interrupt), the blocked process that is waiting for the I/O may be terminated.
In case of I/O error (interrupt), the blocked process that is waiting for the I/O may be terminated.
Timeout checked in clock interrupt
Timeout checked in clock interrupt
(In OS that implements priority) When the I/O operation is finished for a blocked process, it is waken up. If its priority is higher than the running process, it will be dispatched.
(In OS that implements priority) When the I/O operation is finished for a blocked process, it is waken up. If its priority is higher than the running process, it will be dispatched.
Process switching at Trap
In case error conditions caught as traps (memory fault, illegal access..), the process may be terminated, or blocked (or suspended) by the OS.
In case error conditions caught as traps (memory fault, illegal access..), the process may be terminated, or blocked (or suspended) by the OS.
Process switching at Supervisor call
A process may be blocked in executing supervisor call (for I/O, synchronization …)
A process may be blocked in executing supervisor call (for I/O, synchronization …)
Process Switching, Steps
Consists of the following steps Save processor state (incl. program
counter and other registers) in the PCB: processor state information
Update the PCB with the new state and any accounting information
Move the PCB to appropriate queue – ready, blocked
2
1
3
Process Switching, Steps
(Continue) Select another process for execution Update the PCB of the process selected
(new state and accounting information) Update memory-management data
structures Restore context of the selected process
restore the previous value of the program counter and other registers from the PCB
6
4
5
7
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
Steps in Process Switching (1/10)
xx
2 99
xxready ready
0
A0
10
B0
a++a++ b = 8b = 8
Steps in Process Switching (2/10)
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
A0
2 99
0ready
0
A0
10
B0
Process A is dispatched. PCB of A is updated, and the processor state information is copied to the CPU.
Process A is dispatched. PCB of A is updated, and the processor state information is copied to the CPU.
running
Steps in Process Switching (3/10)
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
A1
2 99
2running ready
0
A0
10
B0
After executing A0After executing A0
Steps in Process Switching (4/10)
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
A2
2 99
3running ready
0
A0
10
B0
After executing A1After executing A1
Steps in Process Switching (5/10)
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
A2
2 99
3ready ready
3
A2
10
B0
Timeout for Process A, which state changes from running to ready. PCB of A is updated, and the processor state information is copied from the CPU.
Timeout for Process A, which state changes from running to ready. PCB of A is updated, and the processor state information is copied from the CPU.
Steps in Process Switching (6/10)
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
B0
2 99
10ready
3
A2
10
B0
Process B is dispatched. PCB of B is updated, and the processor state information is copied to the CPU.
Process B is dispatched. PCB of B is updated, and the processor state information is copied to the CPU.
running
Steps in Process Switching (7/10)
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
B2
2 8
8ready running
3
A2
10
B0
After executing B0 and B1After executing B0 and B1
Steps in Process Switching (8/10)
Timeout for Process B, which state changes from running to ready. PCB of B is updated, and the processor state information is copied from the CPU.
Timeout for Process B, which state changes from running to ready. PCB of B is updated, and the processor state information is copied from the CPU.
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
B2
2 8
8ready ready
3
A2
8
B2
Steps in Process Switching (9/10)
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
A2
2 8
3ready
3
A2
8
B2
Process A is dispatched. PCB of A is updated, and the processor state information is copied to the CPU.
Process A is dispatched. PCB of A is updated, and the processor state information is copied to the CPU.
running
Steps in Process Switching (10/10)
B0: mov AX, 8B1: mov b, AX
b
A0: mov AX, aA1: inc AXA2: mov a, AX
a
AX
PC
state
AX
PC
state
AX
PC
CPUPCB of process BPCB of process A
Memory used by A Memory used by B
A3
3 8
3running ready
3
A2
8
B2
After executing A2After executing A2
Process
Each process has Some resources allocated by OS,
including memory to hold the process image
A single thread of execution Only one instruction is being run at a time
More threads of execution For each thread, only one instruction is
being run at a time
Process v.s. Thread
Process – Unit of resource ownership memory, files, I/O
Thread – Unit of dispatching an execution path execution may be interleaved with
other threads / processes A process has one or more threads
“More than one threads..”
Sometimes, we want to have more than one threads of execution inside a single
process. The multiple threads can share memory and other resources, while they can run
‘at the same time’.
OS Support for Threads and Processes
one process one threade.g. MS-DOS
multiple processesone thread per process
e.g. traditional Unix
one processmultiple threads
e.g. JVM
multiple processesmultiple threads per processe.g. Windows 2000, Solaris,
Apple OS X, BeOS, Linux, OS/390
Thread
Has an execution state Running, ready, blocked, suspend states
… Has an execution stack Thread context is saved and restored
in thread switching Read the following slides for how to
change the steps of process switching to the steps of thread switching
Thread Has access to the memory and
resources of its process Has some per-thread static storage
for local variables
Single Threaded and Multithreaded Process Models
Process image in a single-threadedprocess
Process image in a multithreaded
process
stack
Program
Process Control Block
stack
Thread Control Block
Process Control Block
stack
Thread Control Block
In multithreaded process model, the OS keeps a Thread Control Block (TCB) for each thread. Much content of the original PCB is moved to TCB for each thread. Each thread has a TCB and a stack.Data
Program Data
Thread Switching, Steps
Consists of the following steps Save processor state (incl. program
counter and other registers) in the TCB: processor state information
Update the TCB with the new state and any accounting information
Move the TCB to appropriate queue – ready, blocked
2
1
3
Thread Switching, Steps
(Continue) Select another thread for execution Update the TCB of the thread selected
(new state and accounting information) Update memory-management data
structures Restore context of the selected thread
restore the previous value of the program counter and other registers from the TCB
6
4
5
M
Thread Switching, Steps (simplified)
Thread switching from thread A to thread B Save thread context of thread A: CPU TCB A Update state in TCB A Move TCB A to appropriate queue Select another thread (thread B) Update state in TCB B Update memory-management data structures Restore thread context of thread B: TCB B CPU
6
4
5
2
1
3
Benefits of Threads Less time to create / terminate a new
thread than a process Less time to switch between two
threads within the same process Communication among threads of the
same process is easier
Benefits of Threads Less time to create / terminate a new
thread than a process When the OS creates a process, it has to
allocate memory for the process image, load the program and data, and initialize other resources
When the OS creates a thread within an existing process, it does not need to initialize the process image. It only needs to set up a stack and TCB for the new thread.
Benefits of Threads Less time to switch between two
threads within the same process The “update memory management data
structure” step is only necessary in case of thread switching between different process.
Benefits of Threads Communication among threads of the
same process is easier Threads within a process share memory and
files Different processes have to communicate
with the help of the kernelKernel is the core of OS
Multiprocessing Refers to a computer system’s ability to support
more than one process (program) at the same time.
Multiprocessing operating system enable several programs to run concurrently. UNIX is one of the most widely used multiprocessing systems, but there are many others, including OS/2 for high-end PCs.
Multiprocessing systems are much more complicated than single-process systems because the operating system must allocate resources to competing processes in a reasonable manner.
Multithreading
The ability of an operating system to execute different parts of a program, called threads, simultaneously.
The programmer must carefully design the program in such a way that all the threads can run at the same time without interfering with each other.