Download presentation
Presentation is loading. Please wait.
1
Chapter 3 Advanced Operating System
Process and Thread Chapter 3 Advanced Operating System
2
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
3
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)
4
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
5
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
6
Two-state process model
Not running Running Enter Dispatch Pause Exit
7
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
8
Process creation Submission of a batch job User logs on
Create to provide a service such as printing Spawned by an existing process
9
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
10
Process termination Batch job issues Halt instruction User logs off
Process executes a service request to terminate Parent terminates so child processes terminate
11
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
12
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
13
Round-robin scheduling
queue dispatch processor Each process is allowed to execute for at most a quantum. At timeout, process switching occurs.
14
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
15
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
16
Five-state Process Model
New Exit Admit Release Running Ready Dispatch Time-out Blocked Event occurs Event wait
17
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
18
Blocked Queues Timeout Ready queue Processor Admit Dispatch Release Blocked queue/ Event queue Event occurs Event wait Running process Ready process Blocked process A separate queue holds the processes waiting for each event.
19
How process state changes (1/8)
ready ready ready a := 1 b := a + 1 c := b + 1 read a file a := b - c c := c * b b := 0 a := 1 read a file b := a + 1 c := b + 1 a := b - c c := c * b b := 0 a := 1 b := a + 1 c := b + 1 a := b - c c := c * b b := 0 c := 0
20
How process state changes (2/8)
running ready ready a := 1 b := a + 1 c := b + 1 read a file a := b - c c := c * b b := 0 a := 1 read a file b := a + 1 c := b + 1 a := b - c c := c * b b := 0 a := 1 b := a + 1 c := b + 1 a := b - c c := c * b b := 0 c := 0 Timeout
21
How process state changes (3/8)
ready running ready a := 1 b := a + 1 c := b + 1 read a file a := b - c c := c * b b := 0 a := 1 read a file b := a + 1 c := b + 1 a := b - c c := c * b b := 0 a := 1 b := a + 1 c := b + 1 a := b - c c := c * b b := 0 c := 0 Start I/O
22
How process state changes (4/8)
ready blocked running a := 1 b := a + 1 c := b + 1 read a file a := b - c c := c * b b := 0 a := 1 read a file b := a + 1 c := b + 1 a := b - c c := c * b b := 0 a := 1 b := a + 1 c := b + 1 a := b - c c := c * b b := 0 c := 0 zz Timeout
23
How process state changes (5/8)
running blocked ready a := 1 b := a + 1 c := b + 1 read a file a := b - c c := c * b b := 0 a := 1 read a file b := a + 1 c := b + 1 a := b - c c := c * b b := 0 a := 1 b := a + 1 c := b + 1 a := b - c c := c * b b := 0 c := 0 zz Start I/O
24
How process state changes (6/8)
blocked blocked running a := 1 b := a + 1 c := b + 1 read a file a := b - c c := c * b b := 0 a := 1 read a file b := a + 1 c := b + 1 a := b - c c := c * b b := 0 a := 1 b := a + 1 c := b + 1 a := b - c c := c * b b := 0 c := 0 zz zz Suppose the Green process finishes I/O
25
How process state changes (7/8)
blocked ready running a := 1 b := a + 1 c := b + 1 read a file a := b - c c := c * b b := 0 a := 1 read a file b := a + 1 c := b + 1 a := b - c c := c * b b := 0 a := 1 b := a + 1 c := b + 1 a := b - c c := c * b b := 0 c := 0 zz Timeout
26
How process state changes (8/8)
blocked running ready a := 1 b := a + 1 c := b + 1 read a file a := b - c c := c * b b := 0 a := 1 read a file b := a + 1 c := b + 1 a := b - c c := c * b b := 0 a := 1 b := a + 1 c := b + 1 a := b - c c := c * b b := 0 c := 0 zz Timeout
27
Suspend Processes Suspend – stop the execution of a process 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 blocked process to disk to free up more memory
28
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? Ready, suspend Blocked, suspend
29
Process state transition with suspend states
New Suspend Admit Ready, suspend Blocked, suspend Event Occurs Activate Admit Dispatch Ready Running Exit Release Timeout Event Wait Event Occurs Blocked
30
An example of state transition
Running The process starts an I/O operation Blocked The process is suspended while waiting for the I/O to finish Blocked suspend I/O is finished The process is activated before I/O is finished Ready suspend Blocked The process is activated afterwards I/O is finished Ready Ready
31
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
32
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 PCB Process identification pid:123 uid:tom Processor state info ax, bx, cx, eflags, pc, … cs, ds, ss, … Process control info …
33
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
34
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
35
Process Control Information
Additional information needed by the operating 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
36
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
37
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 occurs Event wait
38
Process switching at 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 case of I/O error (interrupt), the blocked process that is waiting for the I/O may be terminated.
39
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.
40
Process switching at Supervisor call
A process may be blocked in executing supervisor call (for I/O, synchronization …)
41
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 1 2 3
42
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 4 5 6 7
43
Steps in Process Switching (1/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B ready ready xx 10 A0 B0 xx 2 99 a++ b = 8
44
Steps in Process Switching (2/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B running ready 10 A0 B0 A0 2 99 Process A is dispatched. PCB of A is updated, and the processor state information is copied to the CPU.
45
Steps in Process Switching (3/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B running ready 2 10 A0 B0 A1 2 99 After executing A0
46
Steps in Process Switching (4/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B running ready 3 10 A0 B0 A2 2 99 After executing A1
47
Steps in Process Switching (5/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B ready ready 3 3 10 A2 B0 A2 2 99 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.
48
Steps in Process Switching (6/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B ready running 10 3 10 A2 B0 B0 2 99 Process B is dispatched. PCB of B is updated, and the processor state information is copied to the CPU.
49
Steps in Process Switching (7/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B ready running 8 3 10 A2 B0 B2 2 8 After executing B0 and B1
50
Steps in Process Switching (8/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B ready ready 8 3 8 A2 B2 B2 2 8 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.
51
Steps in Process Switching (9/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B running ready 3 3 8 A2 B2 A2 2 8 Process A is dispatched. PCB of A is updated, and the processor state information is copied to the CPU.
52
Steps in Process Switching (10/10)
B0: mov AX, 8 B1: mov b, AX b A0: mov AX, a A1: inc AX A2: mov a, AX a AX PC state CPU PCB of process B PCB of process A Memory used by A Memory used by B running ready 3 3 8 A2 B2 A3 3 8 After executing A2
53
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
54
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
55
“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’.
56
OS Support for Threads and Processes
one process multiple threads e.g. JVM one process one thread e.g. MS-DOS multiple processes one thread per process e.g. traditional Unix multiple processes multiple threads per process e.g. Windows 2000, Solaris, Apple OS X, BeOS, Linux, OS/390
57
Thread Has an execution state Has an execution stack
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
58
Thread Has access to the memory and resources of its process
Has some per-thread static storage for local variables
59
Single Threaded and Multithreaded Process Models
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. Process Control Block stack Program Data Process image in a single-threaded process stack Thread Control Block stack Thread Control Block Process Control Block Program Data Process image in a multithreaded process
60
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 1 2 3
61
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 4 5 M 6
62
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 1 2 3 4 5 6
63
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
64
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.
65
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.
66
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 kernel Kernel is the core of OS
67
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.
68
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.