Process and Thread Chapter 3 Advanced Operating System.

68
Process and Thread Chapter 3 Advanced Operating System

Transcript of Process and Thread Chapter 3 Advanced Operating System.

Page 1: Process and Thread Chapter 3 Advanced Operating System.

Process and Thread

Chapter 3Advanced Operating System

Page 2: Process and Thread Chapter 3 Advanced 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

Page 3: Process and Thread Chapter 3 Advanced Operating System.

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)

Page 4: Process and Thread Chapter 3 Advanced Operating System.

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

Page 5: Process and Thread Chapter 3 Advanced Operating System.

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

Page 6: Process and Thread Chapter 3 Advanced Operating System.

Two-state process model

Notrunning Running

Enter

Dispatch

Pause

Exit

Page 7: Process and Thread Chapter 3 Advanced Operating System.

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

Page 8: Process and Thread Chapter 3 Advanced Operating System.

Process creation

Submission of a batch job User logs on Create to provide a service such as

printing Spawned by an existing process

Page 9: Process and Thread Chapter 3 Advanced Operating System.

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

Page 10: Process and Thread Chapter 3 Advanced Operating System.

Process termination

Batch job issues Halt instruction User logs off Process executes a service request

to terminate Parent terminates so child

processes terminate

Page 11: Process and Thread Chapter 3 Advanced Operating System.

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

Page 12: Process and Thread Chapter 3 Advanced Operating System.

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

Page 13: Process and Thread Chapter 3 Advanced Operating System.

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.

Page 14: Process and Thread Chapter 3 Advanced Operating System.

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

Page 15: Process and Thread Chapter 3 Advanced Operating System.

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

Page 16: Process and Thread Chapter 3 Advanced Operating System.

Five-state Process Model

New ExitAdmit Release

RunningReady

Dispatch

Time-out

Blocked

Event occurs Event wait

Page 17: Process and Thread Chapter 3 Advanced Operating System.

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

Page 18: Process and Thread Chapter 3 Advanced Operating System.

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

Page 19: Process and Thread Chapter 3 Advanced Operating System.

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

Page 20: Process and Thread Chapter 3 Advanced Operating System.

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

Page 21: Process and Thread Chapter 3 Advanced Operating System.

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

Page 22: Process and Thread Chapter 3 Advanced Operating System.

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

Page 23: Process and Thread Chapter 3 Advanced Operating System.

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

Page 24: Process and Thread Chapter 3 Advanced Operating System.

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

Page 25: Process and Thread Chapter 3 Advanced Operating System.

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

Page 26: Process and Thread Chapter 3 Advanced Operating System.

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

Page 27: Process and Thread Chapter 3 Advanced Operating System.

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

Page 28: Process and Thread Chapter 3 Advanced Operating System.

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

Page 29: Process and Thread Chapter 3 Advanced Operating System.

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

Page 30: Process and Thread Chapter 3 Advanced Operating System.

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

Page 31: Process and Thread Chapter 3 Advanced Operating System.

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

Page 32: Process and Thread Chapter 3 Advanced Operating System.

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……

Page 33: Process and Thread Chapter 3 Advanced Operating System.

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

Page 34: Process and Thread Chapter 3 Advanced Operating System.

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

Page 35: Process and Thread Chapter 3 Advanced Operating System.

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

Page 36: Process and Thread Chapter 3 Advanced Operating System.

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

Page 37: Process and Thread Chapter 3 Advanced Operating System.

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

Page 38: Process and Thread Chapter 3 Advanced Operating System.

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.

Page 39: Process and Thread Chapter 3 Advanced Operating System.

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.

Page 40: Process and Thread Chapter 3 Advanced Operating System.

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 …)

Page 41: Process and Thread Chapter 3 Advanced Operating System.

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

Page 42: Process and Thread Chapter 3 Advanced Operating System.

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

Page 43: Process and Thread Chapter 3 Advanced Operating System.

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

Page 44: Process and Thread Chapter 3 Advanced Operating System.

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

Page 45: Process and Thread Chapter 3 Advanced Operating System.

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

Page 46: Process and Thread Chapter 3 Advanced Operating System.

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

Page 47: Process and Thread Chapter 3 Advanced Operating System.

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.

Page 48: Process and Thread Chapter 3 Advanced Operating System.

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

Page 49: Process and Thread Chapter 3 Advanced Operating System.

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

Page 50: Process and Thread Chapter 3 Advanced Operating System.

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

Page 51: Process and Thread Chapter 3 Advanced Operating System.

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

Page 52: Process and Thread Chapter 3 Advanced Operating System.

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

Page 53: Process and Thread Chapter 3 Advanced Operating System.

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

Page 54: Process and Thread Chapter 3 Advanced Operating System.

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

Page 55: Process and Thread Chapter 3 Advanced Operating System.

“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’.

Page 56: Process and Thread Chapter 3 Advanced Operating System.

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

Page 57: Process and Thread Chapter 3 Advanced Operating System.

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

Page 58: Process and Thread Chapter 3 Advanced Operating System.

Thread Has access to the memory and

resources of its process Has some per-thread static storage

for local variables

Page 59: Process and Thread Chapter 3 Advanced Operating System.

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

Page 60: Process and Thread Chapter 3 Advanced Operating System.

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

Page 61: Process and Thread Chapter 3 Advanced Operating System.

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

Page 62: Process and Thread Chapter 3 Advanced Operating System.

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

Page 63: Process and Thread Chapter 3 Advanced Operating System.

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

Page 64: Process and Thread Chapter 3 Advanced Operating System.

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.

Page 65: Process and Thread Chapter 3 Advanced Operating System.

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.

Page 66: Process and Thread Chapter 3 Advanced Operating System.

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

Page 67: Process and Thread Chapter 3 Advanced Operating System.

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.

Page 68: Process and Thread Chapter 3 Advanced Operating System.

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.