主講人:虞台文
-
Upload
urielle-clayton -
Category
Documents
-
view
42 -
download
0
description
Transcript of 主講人:虞台文
![Page 1: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/1.jpg)
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
主講人:虞台文
![Page 2: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/2.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/3.jpg)
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
Kernel Definitions and Objects
![Page 4: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/4.jpg)
Windows Kernel
![Page 5: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/5.jpg)
Windows Kernel
Hardware dependent functions are placed in the kernel.
Hardware dependent functions are placed in the kernel.
![Page 6: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/6.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/7.jpg)
OS’s Machine
I am staying on the top of an OS
machine.
![Page 8: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/8.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/9.jpg)
Kernel ObjectsProcess and thread managementInterrupt and trap handlingResource managementInput/output
Process and thread management
– Process Creation
– Process Destruction
– Process Communication/Synchronization
![Page 10: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/10.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/11.jpg)
......
......
......
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/12.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/13.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/14.jpg)
Main Topics in the Lecture
Process and thread management
Interrupt and trap handling
Resource management
Input/output
Main topics
![Page 15: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/15.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/16.jpg)
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
Queue Structures
![Page 17: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/17.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/18.jpg)
Single-Level Queues
Circular Array Implementation
Link List Implementation
![Page 19: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/19.jpg)
Priority Queues
Array indexed by
priority
![Page 20: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/20.jpg)
Priority Queues
Binary heap of priority
![Page 21: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/21.jpg)
Priority Queues
Binary heapof priority
Array implementation of
binary heap
![Page 22: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/22.jpg)
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
Threads
![Page 23: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/23.jpg)
MemoryMemory
Address SpacesVirtual Memory
Lowest Address, e.g., 00000000
Highest Address, e.g., FFFFFFFF
![Page 24: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/24.jpg)
MemoryMemory
Address SpacesVirtual Memory
Lowest Address, e.g., 00000000
Highest Address, e.g., FFFFFFFF
OS
UserPrograms
Starting Address of all processes
![Page 25: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/25.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/26.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/27.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/28.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/29.jpg)
Processes and Threads
![Page 30: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/30.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/31.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/32.jpg)
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
ImplementingProcesses and Threads
![Page 33: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/33.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/34.jpg)
Process Control Block (PCB)
![Page 35: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/35.jpg)
Process IdentificationA system-wide unique
identifier.
A system-wide unique identifier.
![Page 36: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/36.jpg)
State Vector
![Page 37: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/37.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/38.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/39.jpg)
Memory
Memory map information.
Physical Memory Virtual
Memory
Memory map information.
Physical Memory Virtual
Memory
![Page 40: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/40.jpg)
Status
![Page 41: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/41.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/42.jpg)
More on Status Basic process status
– running, ready, and blocked State transition diagram
readyreadyblockedblocked
runningrunning
SchedulerRequest
Release
Create
![Page 43: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/43.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/44.jpg)
The Finer State Transition Diagram
![Page 45: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/45.jpg)
The Finer State Transition Diagram
ActiveProcesses
SuspendedProcesses
![Page 46: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/46.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/47.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/48.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/49.jpg)
PriorityTwo methods: Single-integer value Two-leveled valued
– Base priority + Changeable part
Windows NT priority classes
![Page 50: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/50.jpg)
Others CPU time used Time remaining Resource used Resource claimed Resource quotas Number of I/O requests since creation . . .
![Page 51: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/51.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/52.jpg)
Windows 2000 (EPROCESS)
Executive Process
![Page 53: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/53.jpg)
Kernel Process Block
![Page 54: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/54.jpg)
Processes and Threads (Windows 2000)
EPROCESS
ETHREAD
![Page 55: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/55.jpg)
Windows 2000 (ETHREAD)
![Page 56: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/56.jpg)
Kernel Thread Block
![Page 57: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/57.jpg)
Windows 2000 Thread States
![Page 58: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/58.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/59.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/60.jpg)
Create
![Page 61: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/61.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/62.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/63.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/64.jpg)
Suspend
![Page 65: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/65.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/66.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/67.jpg)
Activate
![Page 68: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/68.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/69.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/70.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/71.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/72.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/73.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/74.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/75.jpg)
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
ImplementingSync/Comm Mechanisms
![Page 76: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/76.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/77.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/78.jpg)
The Story (80x86)
Memory
![Page 79: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/79.jpg)
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
![Page 80: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/80.jpg)
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Lock XCHG
![Page 81: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/81.jpg)
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
![Page 82: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/82.jpg)
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Lock XCHG
The spin locks
![Page 83: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/83.jpg)
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Lock XCHG
Job
Don
e
The spin locks
![Page 84: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/84.jpg)
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Job
Don
e
The spin locks
![Page 85: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/85.jpg)
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Job
Don
e
Lock XCHG
The spin locks
![Page 86: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/86.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/87.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/88.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/89.jpg)
Spin Locks on Binary Semaphore
sb {0, 1}
Pb(Sb) { do TS(R, Sb) while(!R);/* wait loop */
}
Vb(Sb) { Sb=1;}
![Page 90: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/90.jpg)
Spin Locks on Binary Semaphore
sb {0, 1}
Pb(Sb) { do TS(R, Sb) while(!R);/* wait loop */
}
Vb(Sb) { Sb=1;}
![Page 91: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/91.jpg)
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: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/92.jpg)
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 93: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/93.jpg)
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 94: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/94.jpg)
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 95: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/95.jpg)
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 96: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/96.jpg)
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 97: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/97.jpg)
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 98: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/98.jpg)
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 99: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/99.jpg)
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 100: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/100.jpg)
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 101: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/101.jpg)
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 102: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/102.jpg)
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 103: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/103.jpg)
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 104: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/104.jpg)
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 105: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/105.jpg)
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 106: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/106.jpg)
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 107: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/107.jpg)
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 108: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/108.jpg)
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 109: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/109.jpg)
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 110: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/110.jpg)
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 111: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/111.jpg)
Priority Queue with Time Differences
1212
Countdown
HardwareTimer
p1 15 p2 20 p3 5 p4 10
TimerQueue
TQ
Set_LTimer(tn, tdel)
![Page 112: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/112.jpg)
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 113: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/113.jpg)
Priority Queue with Time Differences
1212
Countdown
HardwareTimers
p5 3 p3 2 p4 10p1 15 p2 20TQ
1111101099887766554433221100
![Page 114: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/114.jpg)
Priority Queue with Time Differences
Countdown
HardwareTimers
p5 3 p3 2 p4 10p2 20
00
TQ
2020
![Page 115: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/115.jpg)
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 116: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/116.jpg)
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 117: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/117.jpg)
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 118: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/118.jpg)
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 119: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/119.jpg)
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 120: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/120.jpg)
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 121: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/121.jpg)
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 122: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/122.jpg)
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 123: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/123.jpg)
Communication Primitives
Copying through system buffers(Processes in the same machine)
Use pool of system buffers
![Page 124: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/124.jpg)
Communication Primitives
Use pool of system buffers
Copying accross network(Processes in the different
machines)
![Page 125: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/125.jpg)
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
Interrupt Handling
![Page 126: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/126.jpg)
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 127: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/127.jpg)
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 128: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/128.jpg)
Interrupt Controller (82C59A)
![Page 129: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/129.jpg)
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 130: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/130.jpg)
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 131: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/131.jpg)
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 132: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/132.jpg)
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 133: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/133.jpg)
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 134: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/134.jpg)
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 135: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/135.jpg)
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 136: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/136.jpg)
......
......
......
Done
The Typical Sequence for Using a Hardware Device
CPUCPU I/OProcessor
I/OProcessor
Start I/O
Interrupt
......Do_I/O......
InterruptServiceRoutine
![Page 137: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/137.jpg)
The Typical Sequence for Using a Hardware Device
![Page 138: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/138.jpg)
The Typical Sequence for Using a Hardware Device
start I/O
![Page 139: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/139.jpg)
Synchronization Primitives Needed
P/Vwait/signal
![Page 140: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/140.jpg)
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 141: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/141.jpg)
Implementing Using Monitor
![Page 142: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/142.jpg)
Example: Monitor Clock Server
![Page 143: 主講人:虞台文](https://reader031.fdocument.pub/reader031/viewer/2022033018/56812a5a550346895d8dbb8b/html5/thumbnails/143.jpg)
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; }}