146
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主主主 主主主

Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Embed Size (px)

Citation preview

Page 1: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 4:The Operating System Kernel:

Implementing Processes and Threads

主講人:虞台文

Page 2: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Content Kernel Definitions and Objects Queue Structures Threads Implementing Processes and Threads

– Process and Thread Descriptors– Implementing the Operations

Implementing Sync/Comm Mechanisms– Semaphores and Locks– Building Monitor Primitives– Clock and Time Management– Communications Kernel

Interrupt Handling

Page 3: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 4:The Operating System Kernel:

Implementing Processes and Threads

Kernel Definitions and Objects

Page 4: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Windows Kernel

Page 5: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Windows Kernel

Hardware dependent functions are placed in the kernel.

Hardware dependent functions are placed in the kernel.

Page 6: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

OS Kernel

A basic set of objects, primitives, data structures, processes from which the remainder of the system may be built on its top.

In other words, the kernel transforms the hardware into an OS’s machine.

Page 7: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

OS’s Machine

I am staying on the top of an OS

machine.

Page 8: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Kernel Objects

Kernel defines/provides mechanisms to implement various policies.

Four classes of possible functions and objects in a kernel: – Process and thread management– Interrupt and trap handling– Resource management– Input/output

Page 9: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Kernel ObjectsProcess and thread managementInterrupt and trap handlingResource managementInput/output

Process and thread management

– Process Creation

– Process Destruction

– Process Communication/Synchronization

Page 10: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Kernel ObjectsProcess and thread managementInterrupt and trap handlingResource managementInput/output

Interrupt and trap handling– Responding to signals triggered by various

system events. Some system events:

– Process termination– I/O completion– Time-out of clock– Error– Hardware malfunction

Page 11: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

......

......

......

Done

Kernel ObjectsProcess and thread managementInterrupt and trap handlingResource managementInput/output

Interrupt and trap handling– Responding to signals triggered by various

system events.

CPUCPU I/OProcessor

I/OProcessor

Start I/O

Interrupt

......Do_I/O......

InterruptServiceRoutine

Page 12: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Kernel ObjectsProcess and thread managementInterrupt and trap handlingResource managementInput/output

Resource management– Primitives for maintaining, allocating, and

releasing system resources. Some system resources:

– CPUs– Timers– Main memory– Secondary storage– I/O devices– Files

Page 13: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Kernel ObjectsProcess and thread managementInterrupt and trap handlingResource managementInput/output

Input/output– Read, write, and control operations for

initiating and supervising the transfer of data between I/O devices and main memory or registers.

Page 14: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Main Topics in the Lecture

Process and thread management

Interrupt and trap handling

Resource management

Input/output

Main topics

Page 15: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Process Creation Hierarchy

KernelKernel

psps

p1p1 pn

pn

q1q1 qm

qm

. . . . . .

. . .

OS process

user 1 login

pjpj

user j login user n login user

processes

applicationprocesses.

childprocesses

Inte

ract

ion w

ith

kern

el obje

cts

Page 16: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 4:The Operating System Kernel:

Implementing Processes and Threads

Queue Structures

Page 17: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Queues

OS needs many different queues– e.g., ready queues, wait queues.

Single-level queues– Implemented as array

Fixed size Efficient for simple FIFO operations

– Implemented as linked list Unbounded size More overhead, but more flexible operations

Page 18: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Single-Level Queues

Circular Array Implementation

Link List Implementation

Page 19: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queues

Array indexed by

priority

Page 20: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queues

Binary heap of priority

Page 21: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queues

Binary heapof priority

Array implementation of

binary heap

Page 22: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 4:The Operating System Kernel:

Implementing Processes and Threads

Threads

Page 23: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

MemoryMemory

Address SpacesVirtual Memory

Lowest Address, e.g., 00000000

Highest Address, e.g., FFFFFFFF

Page 24: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

MemoryMemory

Address SpacesVirtual Memory

Lowest Address, e.g., 00000000

Highest Address, e.g., FFFFFFFF

OS

UserPrograms

Starting Address of all processes

Page 25: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Processes

OSOS

UserPrograms

UserPrograms

Only one process can be activated at a time.

OSOS

Process 1Process 1

OSOS

Process 2Process 2

OSOS

Process nProcess n

Each process thinks that it owns all memory.

Their address spaces are different.

Page 26: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Context Switching

OSOS

UserPrograms

UserPrograms

Only one process can be activated at a time.

OSOS

Process 1Process 1

OSOS

Process 2Process 2

OSOS

Process nProcess n

Each process thinks that it owns all memory.

ContextSwitching

ContextSwitching

Page 27: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Context Switching

OSOS

UserPrograms

UserPrograms

Only one process can be activated at a time.

OSOS

Process 1Process 1

OSOS

Process 2Process 2

OSOS

Process nProcess n

Each process thinks that it owns all memory.

The context switching among processes, i.e., to change address space, is very time

consuming.

The context switching among processes, i.e., to change address space, is very time

consuming.

Page 28: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

OSOS

UserPrograms

UserPrograms

OSOS

Process 1Process 1

OSOS

Process 2Process 2

OSOS

Process nProcess n

Threads Each process can have multiple threads. They share the same address space. The context switching among threads in

the process is efficient. Lightweight process Mesa

Page 29: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Processes and Threads

Page 30: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Processes and Threads

Process has one or more threads

All threads in a process share:

– Memory space

– Other resources

Each thread has its own:

– CPU state(registers, program counter)

– Stack

Threads are efficient, but lack protection from each other

Page 31: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

OS Support for Processes/Threads

Create a new Process/thread Initiate or make a thread ready Destroy or terminate a thread Delay or put a thread to sleep for a given

amount of time Synchronize threads through semaphore,

events, or condition variables Perform lower-level operations, such as

blocking, suspending, or scheduling a thread.

Microsoft Windows

Process & Thread Functions

Page 32: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 4:The Operating System Kernel:

Implementing Processes and Threads

ImplementingProcesses and Threads

Page 33: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Process and Thread Descriptors

System needs some data structures to keep track the state and miscellaneous information, e.g., identification, resources used, accounting information, of processes and treads.

In the following, we are dealing with a system composed solely of processes, much of concept will also apply to threads.

Page 34: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Process Control Block (PCB)

Page 35: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Process IdentificationA system-wide unique

identifier.

A system-wide unique identifier.

Page 36: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

State Vector

Page 37: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

CPU’s State

Contain necessary data, e.g., program counter, data register, and flag

register, to restart the process at

the point of last interruption.

Contain necessary data, e.g., program counter, data register, and flag

register, to restart the process at

the point of last interruption.

Page 38: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Processor ID

To identify the processor that is executing the process. Make sense only for multiprocessor

system.

To identify the processor that is executing the process. Make sense only for multiprocessor

system.

Page 39: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Memory

Memory map information.

Physical Memory Virtual

Memory

Memory map information.

Physical Memory Virtual

Memory

Page 40: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Status

Page 41: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Status

runningready

blocked

Point to the list, e.g., ready list or wait list, on which the process may

reside.

Point to the list, e.g., ready list or wait list, on which the process may

reside.

Page 42: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

More on Status Basic process status

– running, ready, and blocked State transition diagram

readyreadyblockedblocked

runningrunning

SchedulerRequest

Release

Create

Page 43: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Process Activation and Suspension

Some applications require a process (or thread) can be suspended by programs.

For examples– Suspension of a debugging program

– Needed by the internal, e.g., to detect or prevent a deadlock.

Suspend Thread

Resume Thread

Page 44: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Finer State Transition Diagram

Page 45: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Finer State Transition Diagram

ActiveProcesses

SuspendedProcesses

Page 46: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Creation Tree KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

Page 47: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Creation Tree KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

Point to the PCB of the parent process.

Point to the PCB of the parent process.

A link list of PCBs of the child processes

A link list of PCBs of the child processes

Page 48: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority

Used by scheduler to decide which

process should be running next.

Used by scheduler to decide which

process should be running next.

Two methods: Single-integer value Two-leveled valued

– Base priority + Changeable part

Page 49: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

PriorityTwo methods: Single-integer value Two-leveled valued

– Base priority + Changeable part

Windows NT priority classes

Page 50: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Others CPU time used Time remaining Resource used Resource claimed Resource quotas Number of I/O requests since creation . . .

Page 51: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Processes and Threads (Windows 2000)

ProcessObject

Handle Table

VAD VAD VAD

object

object

Virtual Address Space Descriptors

Access Token

Thread Thread Thread . . .Access Token

Page 52: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Windows 2000 (EPROCESS)

Executive Process

Page 53: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Kernel Process Block

Page 54: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Processes and Threads (Windows 2000)

EPROCESS

ETHREAD

Page 55: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Windows 2000 (ETHREAD)

Page 56: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Kernel Thread Block

Page 57: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Windows 2000 Thread States

Page 58: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Implement Operations on Processes

Create– Establish a new process

Destroy– Remove one or more process

Suspend– Change process status to suspended

Activate– Change process status to active

cobegin/coend

forall

fork/join/quit

. . . . . .

Page 59: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Implement Operations on Processes

Create– Establish a new process

Destroy– Remove one or more process

Suspend– Change process status to suspended

Activate– Change process status to active

Operating on PCBs

CSs must be cared

Page 60: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Create

Page 61: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

CreateCreate(s0, m0, pi, pid) { p = Get_New_PCB(); pid = Get_New_PID(); p->ID = pid; p->CPU_State = s0; p->Memory = m0; p->Priority = pi; p->Status.Type = ’ready_s’; p->Status.List = RL; p->Creation_Tree.Parent = self; p->Creation_Tree.Child = NULL; insert(self-> Creation_Tree.Child, p); insert(RL, p); Activate(); Scheduler();}

s0

m0

pi

pid = Get_New_PID();

ready_s RLself NULL

Get_New_PCB();

Page 62: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

CreateCreate(s0, m0, pi, pid) { p = Get_New_PCB(); pid = Get_New_PID(); p->ID = pid; p->CPU_State = s0; p->Memory = m0; p->Priority = pi; p->Status.Type = ’ready_s’; p->Status.List = RL; p->Creation_Tree.Parent = self; p->Creation_Tree.Child = NULL; insert(self-> Creation_Tree.Child, p); insert(RL, p); Activate(); Scheduler();}

s0

m0

pi

pid = Get_New_PID();

ready_s RLself NULL

Get_New_PCB(); s0

m0

pi

pid = Get_New_PID();

ready_s RLself NULL

Get_New_PCB();

The calling process

The calling process

Page 63: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

CreateCreate(s0, m0, pi, pid) { p = Get_New_PCB(); pid = Get_New_PID(); p->ID = pid; p->CPU_State = s0; p->Memory = m0; p->Priority = pi; p->Status.Type = ’ready_s’; p->Status.List = RL; p->Creation_Tree.Parent = self; p->Creation_Tree.Child = NULL; insert(self-> Creation_Tree.Child, p); insert(RL, p); Activate(); Scheduler();}

s0

m0

pi

pid = Get_New_PID();

ready_s RLself NULL

Get_New_PCB(); s0

m0

pi

pid = Get_New_PID();

ready_s RLself NULL

Get_New_PCB();

Page 64: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Suspend

Page 65: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Suspend KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

Suspend ( )

Suspend?

We choose not.

Page 66: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

SuspendSuspend(pid) { p = Get_PCB(pid); s = p->Status.Type; if ((s==’blocked_a’)||(s==’blocked_s’)) p->Status.Type = ’blocked_s’; else p->Status.Type = ’ready_s’; if (s==’running’) { cpu = p->Processor_ID; p->CPU_State = Interrupt(cpu); Scheduler(); } }

Suspend(pid) { p = Get_PCB(pid); s = p->Status.Type; if ((s==’blocked_a’)||(s==’blocked_s’)) p->Status.Type = ’blocked_s’; else p->Status.Type = ’ready_s’; if (s==’running’) { cpu = p->Processor_ID; p->CPU_State = Interrupt(cpu); Scheduler(); } }

returns all registers’ values of the cpu and frees the cp

u.

returns all registers’ values of the cpu and frees the cp

u.

Page 67: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Activate

Page 68: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Activate KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

Activate ( )

Activate?

We choose not.

Page 69: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Activate

Activate(pid) {

p = Get_PCB(pid);

if (p->Status.Type == ’ready_s’) {

p->Status.Type = ’ready_a’;

Scheduler();

}

else

p->Status.Type = ’blocked_a’;

}

Activate(pid) {

p = Get_PCB(pid);

if (p->Status.Type == ’ready_s’) {

p->Status.Type = ’ready_a’;

Scheduler();

}

else

p->Status.Type = ’blocked_a’;

}

An optionto do this.

An optionto do this.

Page 70: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Destroy

We need to release all resources associated with the process.

What special action needs to be taken if the process is running?

Page 71: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Destroy KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

Destroy ( )

Killed?

We choose to kill child processes.

Page 72: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Kill_Tree(p) {

for (each q in p->Creation_Tree.Child)

Kill_Tree(q);

if (p->Status.Type == ’running’) {

cpu = p->Processor_ID;

Interrupt(cpu);

}

Remove(p->Status.List, p);

Release_all(p->Memory);

Release_all(p->Other_Resources);

Close_all(p->Open_Files);

Delete_PCB(p);

}

Kill_Tree(p) {

for (each q in p->Creation_Tree.Child)

Kill_Tree(q);

if (p->Status.Type == ’running’) {

cpu = p->Processor_ID;

Interrupt(cpu);

}

Remove(p->Status.List, p);

Release_all(p->Memory);

Release_all(p->Other_Resources);

Close_all(p->Open_Files);

Delete_PCB(p);

}

Destroy KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

subroutineKill_Tree ( )

killed

Marked the corresponding cpu free.Marked the corresponding cpu free.

Page 73: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Kill_Tree(p) {

for (each q in p->Creation_Tree.Child)

Kill_Tree(q);

if (p->Status.Type == ’running’) {

cpu = p->Processor_ID;

Interrupt(cpu);

}

Remove(p->Status.List, p);

Release_all(p->Memory);

Release_all(p->Other_Resources);

Close_all(p->Open_Files);

Delete_PCB(p);

}

Kill_Tree(p) {

for (each q in p->Creation_Tree.Child)

Kill_Tree(q);

if (p->Status.Type == ’running’) {

cpu = p->Processor_ID;

Interrupt(cpu);

}

Remove(p->Status.List, p);

Release_all(p->Memory);

Release_all(p->Other_Resources);

Close_all(p->Open_Files);

Delete_PCB(p);

}

Destroy KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

subroutineKill_Tree ( )

killed

Page 74: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Destroy KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

KernelKernel

psps

p1p1p1p1 pn

pnpnpn

q1q1q1q1 qm

qmqmqm

. . . . . .

. . .

OS process

user 1 login

pjpjpjpj

user j login user n login

userprocesses

applicationprocesses.

childprocesses

childprocesses

Inte

ract

ion

wit

h k

erne

l ob

ject

s

Destroy ( )

Kill_Tree(p) {

for (each q in p->Creation_Tree.Child)

Kill_Tree(q);

if (p->Status.Type == ’running’) {

cpu = p->Processor_ID;

Interrupt(cpu);

}

Remove(p->Status.List, p);

Release_all(p->Memory);

Release_all(p->Other_Resources);

Close_all(p->Open_Files);

Delete_PCB(p);

}

Kill_Tree(p) {

for (each q in p->Creation_Tree.Child)

Kill_Tree(q);

if (p->Status.Type == ’running’) {

cpu = p->Processor_ID;

Interrupt(cpu);

}

Remove(p->Status.List, p);

Release_all(p->Memory);

Release_all(p->Other_Resources);

Close_all(p->Open_Files);

Delete_PCB(p);

}

Destroy(pid) {

p = Get_PCB(pid);

Kill_Tree(p);

Scheduler();

}

Destroy(pid) {

p = Get_PCB(pid);

Kill_Tree(p);

Scheduler();

}

Page 75: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 4:The Operating System Kernel:

Implementing Processes and Threads

ImplementingSync/Comm Mechanisms

Page 76: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

General Resource Access Scheme

Semaphores, locks, monitors, messages, time, and other hardware and software objects are considered resources.

Request(res) { if (Free(res)) Allocate(res, self) else { Block(self, res); Scheduler(); }}

Release(res) { Deallocate(res, self); if (Process_Blocked_in(res,pr)) { Allocate(res, pr); Unblock(pr, res); Scheduler(); }}

Page 77: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Implementing Semaphores/Locks

CPU usually doesn’t support P and V operations directly.

Test-and-Set instruction supported mostly– It atomically tests and modifies the contents of a memor

y location.– It can be used to implement general semaphores in multi

processor systems. In uniprocessor systems, it is sufficient to disable i

nterrupts before accessing a semaphore.

Page 78: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Story (80x86)

Memory

Page 79: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Story (80x86)

Memory

Acce

ssre

source

Acce

ssre

sou

rce

Page 80: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Story (80x86)

Memory

Acce

ssre

source

Acce

ssre

sou

rce

Lock XCHG

Page 81: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Story (80x86)

Memory

Acce

ssre

source

Acce

ssre

sou

rce

Page 82: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Story (80x86)

Memory

Acce

ssre

source

Acce

ssre

sou

rce

Lock XCHG

The spin locks

Page 83: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Story (80x86)

Memory

Acce

ssre

source

Acce

ssre

sou

rce

Lock XCHG

Job

Don

e

The spin locks

Page 84: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Story (80x86)

Memory

Acce

ssre

source

Acce

ssre

sou

rce

Job

Don

e

The spin locks

Page 85: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Story (80x86)

Memory

Acce

ssre

source

Acce

ssre

sou

rce

Job

Don

e

Lock XCHG

The spin locks

Page 86: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Test-and-Set CPU Instruction

TS(R, X)A CPU Register

A Memory Location(Lock Value)

R = X;

X = 0;

Read (test) lock value

Lock (set) the lock

0: locked is locked

1: locked is unlocked

Returns the value in R.

The lock is always locked after being called.

Indivisible

Atomic

Page 87: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Spin Locks on Binary Semaphore

Request( res ) { if (Free(res)) Allocate(res, self) else { Block(self, res); Scheduler(); }}

Release( res ) { Deallocate(res, self); if (Process_Blocked_in(res,pr)) { Allocate(res, pr); Unblock(pr, res); Scheduler(); }}

sb {0, 1}

Page 88: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Spin Locks on Binary Semaphore

Request( res ) { if (Free(res)) Allocate(res, self) else { Block(self, res); Scheduler(); }}

Release( res ) { Deallocate(res, self); if (Process_Blocked_in(res,pr)) { Allocate(res, pr); Unblock(pr, res); Scheduler(); }}

sb {0, 1}

Pb VbSb SbSb==1 Sb=1;

Sb=0;

wait until Sb==1

Page 89: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Spin Locks on Binary Semaphore

sb {0, 1}

Pb(Sb) { do TS(R, Sb) while(!R);/* wait loop */

}

Vb(Sb) { Sb=1;}

Page 90: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Spin Locks on Binary Semaphore

sb {0, 1}

Pb(Sb) { do TS(R, Sb) while(!R);/* wait loop */

}

Vb(Sb) { Sb=1;}

Page 91: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

General Semaphores w/ Busy Wait

P(s) { Inhibit_Interrupts; Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts; Pb(delay_s); } Vb(mutex_s); Enable_Interrupts;}

V(s) { Inhibit_Interrupts; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts;}

Page 92: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Disallowing Preemption

High Prioritye.g., ISR

High Prioritye.g., ISR

Low PriorityLow Priority

Shared Resourc

e

Low PriorityLow Priority

Page 93: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

High Prioritye.g., ISR

High Prioritye.g., ISR

Low PriorityLow Priority

Dead Lock Condition

Shared Resourc

e

Low PriorityLow Priority

Page 94: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Low PriorityLow Priority

High Prioritye.g., ISR

High Prioritye.g., ISR

Low PriorityLow Priority

Dead Lock Avoidance

Shared Resourc

e

Windows uses different implementations.

Page 95: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Disallowing PreemptionP(s) { Inhibit_Interrupts; Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts; Pb(delay_s); } Vb(mutex_s); Enable_Interrupts;}

V(s) { Inhibit_Interrupts; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts;}

Disallowed to be preempted by a higher-

priority process.

Disallowed to be preempted by a higher-

priority process.

Page 96: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Interrupt InhibitionP(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts;; Pb(delay_s); } Vb(mutex_s); Enable_Interrupts;;}

V(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts;;}

Page 97: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

UniprocessorP(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts;; Pb(delay_s); } Vb(mutex_s); Enable_Interrupts;;}

V(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts;;}

Page 98: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Busy WaitP(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts;; Pb(delay_s); } Vb(mutex_s); Enable_Interrupts;;}

V(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts;;}

Page 99: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Avoiding Busy Wait

P(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s-1; if (s < 0) { Block(self, Ls); Vb(mutex_s); Enable_Interrupts;; Scheduler(); } else { Vb(mutex_s); Enable_Interrupts; }}

V(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s+1; if (s <= 0) { Unblock(q, Ls); Vb(mutex_s); Enable_Interrupts; Scheduler(); } else{ Vb(mutex_s); Enable_Interrupts; };}

Page 100: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Implementing Monitors (Hoare)

Internal Data Internal Data

Condition VariablesCondition Variables

Procedure 1 Procedure 1

Procedure 2 Procedure 2

Procedure 3 Procedure 3

c.wait

c.signal

mutually exclusive access for processes

Need a mutex, say mutex.

Need a semaphore for processes blocked on c, say condsem_c.

Need a semaphore for signaling processes, say urgent.

condcnt_c: #processes wait on c

urgentcnt: #signalers

Initial values: mutex = 1;condsem_c = 0;urgent = 0;

condcnt_c = 0;urgentcnt = 0;

Page 101: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Mutual Access of Processesmonitor QueueHandler{

struct Queue queue;condition itemAvail, freenodeAvail;

void AddToQueue( int val ) {if ( queue is full ) { freenodeAvail.wait;}

. . . add val to the end of the queue . . .itemAvail.signal;

} /* AddToQueue */

int RemoveFromQueue() {if ( queue is empty ) { itemAvail.wait;}

. . . remove value from queue . . .freenodeAvail.signal;return value;

} /* RemoveFromQueue */};

monitor QueueHandler{struct Queue queue;condition itemAvail, freenodeAvail;

void AddToQueue( int val ) {if ( queue is full ) { freenodeAvail.wait;}

. . . add val to the end of the queue . . .itemAvail.signal;

} /* AddToQueue */

int RemoveFromQueue() {if ( queue is empty ) { itemAvail.wait;}

. . . remove value from queue . . .freenodeAvail.signal;return value;

} /* RemoveFromQueue */};

Initial values: mutex = 1;condsem_c = 0;urgent = 0;

condcnt_c = 0;urgentcnt = 0;

Page 102: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Mutual Access of Processesmonitor QueueHandler{

struct Queue queue;condition itemAvail, freenodeAvail;

void AddToQueue( int val ) {if ( queue is full ) { freenodeAvail.wait;}

. . . add val to the end of the queue . . .itemAvail.signal;

} /* AddToQueue */

int RemoveFromQueue() {if ( queue is empty ) { itemAvail.wait;}

. . . remove value from queue . . .freenodeAvail.signal;return value;

} /* RemoveFromQueue */};

monitor QueueHandler{struct Queue queue;condition itemAvail, freenodeAvail;

void AddToQueue( int val ) {if ( queue is full ) { freenodeAvail.wait;}

. . . add val to the end of the queue . . .itemAvail.signal;

} /* AddToQueue */

int RemoveFromQueue() {if ( queue is empty ) { itemAvail.wait;}

. . . remove value from queue . . .freenodeAvail.signal;return value;

} /* RemoveFromQueue */};

P(mutex)

P(mutex)

if(urgentcnt) V(urgent);else V(mutex);

if(urgentcnt) V(urgent);else V(mutex);

Initial values: mutex = 1;condsem_c = 0;urgent = 0;

condcnt_c = 0;urgentcnt = 0;

Page 103: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Mutual Access of Processes

Procedure_body

P(mutex)

if(urgentcnt) V(urgent);else V(mutex);

Initial values: mutex = 1;condsem_c = 0;urgent = 0;

condcnt_c = 0;urgentcnt = 0;

Page 104: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

condcnt_c = condcnt_c + 1;

if (urgentcnt) V(urgent);

else V(mutex);

P(condsem_c); /* wait */

condcnt_c = condcnt_c - 1;

wait/signal

c.wait: c.signal:

Initial values: mutex = 1;condsem_c = 0;urgent = 0;

condcnt_c = 0;urgentcnt = 0;

if (condcnt_c) {

urgentcnt = urgentcnt + 1;

V(condsem_c);

P(urgent); /* wait */

urgentcnt = urgentcnt - 1;}

Page 105: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Clock and Time Management

Why OS needs time?– Performance measurement– Processor Scheduling– Time-Stamping Events

e.g., I/O and file system call

– Deadlock and other fault detection– . . . . . . . .

Page 106: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Clock and Time Management

Most systems provide hardware– ticker: issues periodic interrupt – countdown timer: issues interrupt after a set number of ticks

Build higher-level services with this h/w Wall clock timers

– Typical functions: Update_Clock : increment tnow (invoked each time tick) Get_Time : return current time Set_Time(tnew) : set time to tnew

– Must maintain monotonicity

Page 107: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Countdown Timer (Alarm Clocks)

Processes or threads may need timeout signal at some specified time in the future.

Examples:– Delay an amount of time to wait I/O completion– Sleep to hand over CPU time– Block until awakened by timer signal events

Typical Function:– Delay(tdel) block process for tdel time units

Page 108: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Implementation of Delay(tdel)

To block process for tdel time units.

Implementation using hardware countdown:

semaphore delsem;

Delay(tdel) { Set_Timer(tdel); /*set hardware timer*/ P(delsem); /*wait for interrupt*/}

Timeout() { /*called at interrupt*/ V(delsem);}

Page 109: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Logical Countdown Timers

Implement multiple logical countdown timers using a single hardware timer

Functions:– tn = Create_LTimer() create new timer– Destroy_LTimer(tn)– Set_LTimer(tn, tdel) logically equivalent

to Set_Timer(tdel)

How to implement multiple logical timers?

Page 110: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queue withAbsolute Wakeup Times

Set_LTimer(tn, tdel)

103103

Wall-clock

1212

Countdown

HardwareTimers

p1 115 p2 135 p3 140 p4 150

TimerQueue

TQ

Page 111: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queue withAbsolute Wakeup Times

Set_LTimer(??, 35)

103103

Wall-clock

1212

Countdown

HardwareTimers

p1 115 p2 135 p3 140 p4 150

TimerQueue

TQ

p5 138 p3 140 p4 150p1 115 p2 135TQ

+103+138

Page 112: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queue withAbsolute Wakeup Times

103103

Wall-clock

1212

Countdown

HardwareTimers

p5 138 p3 140 p4 150p1 115 p2 135TQ

104104 1111105105 1010106106 99107107 88108108 77109109 66110110 55111111 44112112 33113113 22114114 11115115 00

Page 113: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

115115

p2 135

Priority Queue withAbsolute Wakeup Times

Wall-clock Countdown

HardwareTimers

p5 138 p3 140 p4 150p2 135TQ

115115 00

135-115

20

2020

Page 114: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queue with Time Differences

1212

Countdown

HardwareTimer

p1 15 p2 20 p3 5 p4 10

TimerQueue

TQ

Set_LTimer(tn, tdel)

Page 115: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queue with Time Differences

1212

Countdown

HardwareTimer

Set_LTimer(??, 35)

p5 3 p3 2 p4 10p1 15 p2 20TQ

12

32

32

37

37

4735

3 2

p1 15 p2 20 p3 5 p4 10

TimerQueue

TQ

Page 116: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queue with Time Differences

1212

Countdown

HardwareTimers

p5 3 p3 2 p4 10p1 15 p2 20TQ

1111101099887766554433221100

Page 117: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Priority Queue with Time Differences

Countdown

HardwareTimers

p5 3 p3 2 p4 10p2 20

00

TQ

2020

Page 118: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

MemoryMemoryMemoryMemoryProcess

qProcess

p

OS OS

Communication Primitives

Address space for process q

Address space for process p

System Space

User Space

The same physical memory used.

Different physical memory used.

Assume that the communication processes are in the same machine.

Page 119: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

OSOS OSOS

Process

qProcess

qProcess

pProcess

p

Communication Primitives

Address space for process q

Address space for process p

Communication

send/receive

Communication

send/receive

send/receive can be blocked or nonblocked.

Page 120: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

OSOS OSOS

Process

qProcess

qProcess

pProcess

p

Communication Primitives

Address space for process q

Address space for process p

send(p,m)

sbuf

receive(q,m)

rbuf

send/receive can be blocked or nonblocked.

Page 121: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

OSOS OSOS

Process

qProcess

qProcess

pProcess

p

Communication Primitives

Address space for process q

Address space for process p

send(p,m) receive(q,m)

send/receive can be blocked or nonblocked.

sbufrbuf

sbufrbuf

Different address mappings for p and q.

How?

Page 122: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

send(p,m)

OSOS OSOS

Process

qProcess

qProcess

pProcess

p

Communication Primitives

Address space for process q

Address space for process p

send(p,m)

send/receive can be blocked or nonblocked.

sbuf

sbuf’ sbuf’

Page 123: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

send(p,m)

OSOS OSOS

Process

qProcess

qProcess

pProcess

p

Communication Primitives

Address space for process q

Address space for process p

receive(q,m)

send/receive can be blocked or nonblocked.

sbufrbuf

sbuf’ sbuf’

rbuf’rbuf’

Page 124: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

send(p,m)

OSOS OSOS

Process

qProcess

qProcess

pProcess

p

Communication Primitives

Address space for process q

Address space for process p

receive(q,m)

send/receive can be blocked or nonblocked.

sbufrbuf

sbuf’ sbuf’

rbuf’rbuf’

Page 125: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

send(p,m)

OSOS OSOS

Process

qProcess

qProcess

pProcess

p

Communication Primitives

Address space for process q

Address space for process p

receive(q,m)

send/receive can be blocked or nonblocked.

sbufrbuf

sbuf’ sbuf’

rbuf’rbuf’

Page 126: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Communication Primitives

Copying through system buffers(Processes in the same machine)

Use pool of system buffers

Page 127: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Communication Primitives

Use pool of system buffers

Copying accross network(Processes in the different

machines)

Page 128: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Operating Systems PrinciplesProcess Management and Coordination

Lecture 4:The Operating System Kernel:

Implementing Processes and Threads

Interrupt Handling

Page 129: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Why Interrupt Handling?

Many events occur at an unpredictable time, i.e., asynchronously– Polling is impractical– Transfer of control out of normal process temporarily

upon coming of an event and, then, back.

To remove the notion of asynchronous events from higher levels of kernel, the OS, and applications– Process abstraction: processes are almost to have

independent activities and operating in parallel.– For example, OS and applications don’t deal with I/O

completion event directly.

The program to serve interrupt is called interrupt handler (IH) or interrupt service routine (ISR).

Page 130: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Types of Interrupt

External Interrupts– Generated by hardware– Asynchronous– E.g., I/O completion, time-out, the arrival of

message (network card), …

Internal Interrupts– Generated by software– Synchronous– E.g., Exceptions (instruction errors), SVC

We deal with hardware interrupt in the following.

Page 131: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Interrupt Controller (82C59A)

Page 132: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Interrupt Controller (82C59A)

to CPU

SignaledBy I/O

Devices

SignaledBy I/O

Devices

Interrupt requests are priority-leveled.

IH’s of high-priority events can preempt those of lower priority.

Interrupt requests are maskable.

80x86 uses STI and CLI to temporarily enable and disable all interrupts, respectively.

Page 133: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Interrupt Handling

CPUCPU INT

InterruptController

(e.g., 8259)

InterruptController

(e.g., 8259)

IRQ0IRQ1IRQ2IRQ3IRQ4IRQ5IRQ6IRQ7

Each interrupt has an interrupt service routine (ISR).

ISR7:

. . . . . . . . . .

IRET

ISR7:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

Normal Job

stack

Page 134: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Interrupt Handling

CPUCPU INT

InterruptController

(e.g., 8259)

InterruptController

(e.g., 8259)

IRQ0IRQ1IRQ2IRQ3IRQ4IRQ5IRQ6IRQ7

Each interrupt has an interrupt service routine (ISR).

ISR7:

. . . . . . . . . .

IRET

ISR7:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

Normal Job

Put the value of flag register, and program

counter (PC) into the stack.

PC (*)

flag

*

stack

Page 135: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Interrupt Handling

CPUCPU INT

InterruptController

(e.g., 8259)

InterruptController

(e.g., 8259)

IRQ0IRQ1IRQ2IRQ3IRQ4IRQ5IRQ6IRQ7

Each interrupt has an interrupt service routine (ISR).

ISR7:

. . . . . . . . . .

IRET

ISR7:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

Normal Job

Read Interrupt Vector

flag

*

ISR3:

Push used registers (PUSHA)

. . . . . . . . . . . . . . . . . . . . . . .

Pop used registers (POPA)

Return from interrupt (IRET)

ISR3:

Push used registers (PUSHA)

. . . . . . . . . . . . . . . . . . . . . . .

Pop used registers (POPA)

Return from interrupt (IRET)

PC (*)

stack

Page 136: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Interrupt Handling

CPUCPU INT

InterruptController

(e.g., 8259)

InterruptController

(e.g., 8259)

IRQ0IRQ1IRQ2IRQ3IRQ4IRQ5IRQ6IRQ7

Each interrupt has an interrupt service routine (ISR).

ISR7:

. . . . . . . . . .

IRET

ISR7:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

Normal Job

flag

*

ISR3:

Push used registers (PUSHA)

. . . . . . . . . . . . . . . . . . . . . . .

Pop used registers (POPA)

Return from interrupt (IRET)

ISR3:

Push used registers (PUSHA)

. . . . . . . . . . . . . . . . . . . . . . .

Pop used registers (POPA)

Return from interrupt (IRET)

PC (*)

stack

Page 137: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Interrupt Handling

CPUCPU INT

InterruptController

(e.g., 8259)

InterruptController

(e.g., 8259)

IRQ0IRQ1IRQ2IRQ3IRQ4IRQ5IRQ6IRQ7

Each interrupt has an interrupt service routine (ISR).

ISR7:

. . . . . . . . . .

IRET

ISR7:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR6:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR5:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR4:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR3:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR2:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR1:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

ISR0:

. . . . . . . . . .

IRET

Normal Job

PC (*)

stack

Page 138: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Standard Interrupt Handing Sequence

1. Save state of interrupted process/thread

2. Identify interrupt type and invoke IH

3. IH services interrupt

4. Restore state of interrupted process (or of

another one if the interrupt for awakening a

waiting process)

Page 139: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

......

......

......

Done

The Typical Sequence for Using a Hardware Device

CPUCPU I/OProcessor

I/OProcessor

Start I/O

Interrupt

......Do_I/O......

InterruptServiceRoutine

Page 140: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Typical Sequence for Using a Hardware Device

Page 141: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

The Typical Sequence for Using a Hardware Device

start I/O

Page 142: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Synchronization Primitives Needed

P/Vwait/signal

Page 143: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Monitor: Object-Oriented Approach

Device DriverImplementedUsing monitor

Called by the processes need to do I/O.

Called by the processes need to do I/O.

Called while I/O completion.

Called while I/O completion.

Page 144: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Implementing Using Monitor

Page 145: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Example: Monitor Clock Server

Page 146: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Example: Monitor Clock Server

monitor Clock_Server { int tnow; Update_Clock() { . . . tnow = tnow + 1; /* Perhapes update time structure also */ }

int Get_Time() { . . . return(tnow); /* Perhaps return some more complex structure instead */ }

Set_Clock(int tnew) { . . . tnow = tnew; }}