Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System...
-
Upload
dwayne-craig -
Category
Documents
-
view
229 -
download
0
Transcript of Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System...
Operating Systems PrinciplesProcess 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
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
Kernel Definitions and Objects
Windows Kernel
Windows Kernel
Hardware dependent functions are placed in the kernel.
Hardware dependent functions are placed in the kernel.
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.
OS’s Machine
I am staying on the top of an OS
machine.
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
Kernel ObjectsProcess and thread managementInterrupt and trap handlingResource managementInput/output
Process and thread management
– Process Creation
– Process Destruction
– Process Communication/Synchronization
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
......
......
......
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
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
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.
Main Topics in the Lecture
Process and thread management
Interrupt and trap handling
Resource management
Input/output
Main topics
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
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
Queue Structures
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
Single-Level Queues
Circular Array Implementation
Link List Implementation
Priority Queues
Array indexed by
priority
Priority Queues
Binary heap of priority
Priority Queues
Binary heapof priority
Array implementation of
binary heap
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
Threads
MemoryMemory
Address SpacesVirtual Memory
Lowest Address, e.g., 00000000
Highest Address, e.g., FFFFFFFF
MemoryMemory
Address SpacesVirtual Memory
Lowest Address, e.g., 00000000
Highest Address, e.g., FFFFFFFF
OS
UserPrograms
Starting Address of all processes
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.
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
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.
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
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
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
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
ImplementingProcesses 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.
Process Control Block (PCB)
Process IdentificationA system-wide unique
identifier.
A system-wide unique identifier.
State Vector
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.
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.
Memory
Memory map information.
Physical Memory Virtual
Memory
Memory map information.
Physical Memory Virtual
Memory
Status
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.
More on Status Basic process status
– running, ready, and blocked State transition diagram
readyreadyblockedblocked
runningrunning
SchedulerRequest
Release
Create
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
The Finer State Transition Diagram
The Finer State Transition Diagram
ActiveProcesses
SuspendedProcesses
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
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
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
PriorityTwo methods: Single-integer value Two-leveled valued
– Base priority + Changeable part
Windows NT priority classes
Others CPU time used Time remaining Resource used Resource claimed Resource quotas Number of I/O requests since creation . . .
Processes and Threads (Windows 2000)
ProcessObject
Handle Table
VAD VAD VAD
object
object
Virtual Address Space Descriptors
Access Token
Thread Thread Thread . . .Access Token
Windows 2000 (EPROCESS)
Executive Process
Kernel Process Block
Processes and Threads (Windows 2000)
EPROCESS
ETHREAD
Windows 2000 (ETHREAD)
Kernel Thread Block
Windows 2000 Thread States
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
. . . . . .
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
Create
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();
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
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();
Suspend
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.
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.
Activate
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.
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.
Destroy
We need to release all resources associated with the process.
What special action needs to be taken if the process is running?
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.
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.
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
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();
}
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
ImplementingSync/Comm Mechanisms
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(); }}
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.
The Story (80x86)
Memory
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Lock XCHG
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Lock XCHG
The spin locks
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Lock XCHG
Job
Don
e
The spin locks
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Job
Don
e
The spin locks
The Story (80x86)
Memory
Acce
ssre
source
Acce
ssre
sou
rce
Job
Don
e
Lock XCHG
The spin locks
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
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}
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
Spin Locks on Binary Semaphore
sb {0, 1}
Pb(Sb) { do TS(R, Sb) while(!R);/* wait loop */
}
Vb(Sb) { Sb=1;}
Spin Locks on Binary Semaphore
sb {0, 1}
Pb(Sb) { do TS(R, Sb) while(!R);/* wait loop */
}
Vb(Sb) { Sb=1;}
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;}
Disallowing Preemption
High Prioritye.g., ISR
High Prioritye.g., ISR
Low PriorityLow Priority
Shared Resourc
e
Low PriorityLow Priority
High Prioritye.g., ISR
High Prioritye.g., ISR
Low PriorityLow Priority
Dead Lock Condition
Shared Resourc
e
Low PriorityLow Priority
Low PriorityLow Priority
High Prioritye.g., ISR
High Prioritye.g., ISR
Low PriorityLow Priority
Dead Lock Avoidance
Shared Resourc
e
Windows uses different implementations.
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.
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;;}
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;;}
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;;}
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; };}
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;
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;
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;
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;
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;}
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– . . . . . . . .
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
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
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);}
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?
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
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
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
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
Priority Queue with Time Differences
1212
Countdown
HardwareTimer
p1 15 p2 20 p3 5 p4 10
TimerQueue
TQ
Set_LTimer(tn, tdel)
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
Priority Queue with Time Differences
1212
Countdown
HardwareTimers
p5 3 p3 2 p4 10p1 15 p2 20TQ
1111101099887766554433221100
Priority Queue with Time Differences
Countdown
HardwareTimers
p5 3 p3 2 p4 10p2 20
00
TQ
2020
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.
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.
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.
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?
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’
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’
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’
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’
Communication Primitives
Copying through system buffers(Processes in the same machine)
Use pool of system buffers
Communication Primitives
Use pool of system buffers
Copying accross network(Processes in the different
machines)
Operating Systems PrinciplesProcess Management and Coordination
Lecture 4:The Operating System Kernel:
Implementing Processes and Threads
Interrupt Handling
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).
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.
Interrupt Controller (82C59A)
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.
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
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
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
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
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
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)
......
......
......
Done
The Typical Sequence for Using a Hardware Device
CPUCPU I/OProcessor
I/OProcessor
Start I/O
Interrupt
......Do_I/O......
InterruptServiceRoutine
The Typical Sequence for Using a Hardware Device
The Typical Sequence for Using a Hardware Device
start I/O
Synchronization Primitives Needed
P/Vwait/signal
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.
Implementing Using Monitor
Example: Monitor Clock Server
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; }}