Title: Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Ker
1Operating Systems PrinciplesProcess Management
and CoordinationLecture 4The Operating System
KernelImplementing Processes and Threads
2Content
- 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
3Operating Systems PrinciplesProcess Management
and CoordinationLecture 4The Operating System
KernelImplementing Processes and Threads
- Kernel Definitions and Objects
4Windows Kernel
5Windows Kernel
Hardware dependent functions are placed in the
kernel.
6OS 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 OSs machine.
7OSs Machine
I am staying on the top of an OS machine.
8Kernel 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
9Kernel Objects
- Process and thread management
- Interrupt and trap handling
- Resource management
- Input/output
- Process and thread management
- Process Creation
- Process Destruction
- Process Communication/Synchronization
10Kernel Objects
- Process and thread management
- Interrupt and trap handling
- Resource management
- Input/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
11Kernel Objects
- Process and thread management
- Interrupt and trap handling
- Resource management
- Input/output
- Interrupt and trap handling
- Responding to signals triggered by various system
events.
...... ...... ......
...... Do_I/O ......
Start I/O
Interrupt Service Routine
12Kernel Objects
- Process and thread management
- Interrupt and trap handling
- Resource management
- Input/output
- Resource management
- Primitives for maintaining, allocating, and
releasing system resources. - Some system resources
- CPUs
- Timers
- Main memory
- Secondary storage
- I/O devices
- Files
13Kernel Objects
- Process and thread management
- Interrupt and trap handling
- Resource management
- Input/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.
14Main Topics in the Lecture
- Process and thread management
- Interrupt and trap handling
- Resource management
- Input/output
Main topics
15Process Creation Hierarchy
Kernel
ps
OS process
user 1 login
user j login
user n login
user processes
. . .
. . .
application processes.
. . .
Interaction with kernel objects
16Operating Systems PrinciplesProcess Management
and CoordinationLecture 4The Operating System
KernelImplementing Processes and Threads
17Queues
- 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
18Single-Level Queues
Circular Array Implementation
Link List Implementation
19Priority Queues
Array indexed by priority
20Priority Queues
Binary heap of priority
21Priority Queues
Binary heap of priority
Array implementation of binary heap
22Operating Systems PrinciplesProcess Management
and CoordinationLecture 4The Operating System
KernelImplementing Processes and Threads
23Address Spaces
Virtual Memory
Memory
24Address Spaces
Virtual Memory
Memory
OS
User Programs
25Processes
Only one process can be activated at a time.
Each process thinks that it owns all memory.
Their address spaces are different.
26Context Switching
Only one process can be activated at a time.
Each process thinks that it owns all memory.
Context Switching
Context Switching
27Context Switching
Only one process can be activated at a time.
Each process thinks that it owns all memory.
The context switching among processes, i.e., to
change address space, is very time consuming.
28Threads
- 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
29Processes and Threads
30Processes 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
31OS Support for Processes/Threads
Microsoft Windows
Process Thread Functions
- 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.
32Operating Systems PrinciplesProcess Management
and CoordinationLecture 4The Operating System
KernelImplementing Processes and Threads
- Implementing
- Processes and Threads
33Process 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.
34Process Control Block (PCB)
35Process Identification
A system-wide unique identifier.
36State Vector
37CPUs State
Contain necessary data, e.g., program counter,
data register, and flag register, to restart
the process at the point of last interruption.
38Processor ID
To identify the processor that is executing the
process. Make sense only for multiprocessor
system.
39Memory
Memory map information. Physical Memory ? Virtual
Memory
40Status
41Status
Point to the list, e.g., ready list or wait list,
on which the process may reside.
42More on Status
- Basic process status
- running, ready, and blocked
- State transition diagram
43Process Activation and Suspension
Suspend Thread
Resume Thread
- 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.
44The Finer State Transition Diagram
45The Finer State Transition Diagram
Active Processes
Suspended Processes
46Creation Tree
47Creation Tree
A link list of PCBs of the child processes
Point to the PCB of the parent process.
48Priority
- Two methods
- Single-integer value
- Two-leveled valued
- Base priority Changeable part
Used by scheduler to decide which process should
be running next.
49Priority
- Two methods
- Single-integer value
- Two-leveled valued
- Base priority Changeable part
Windows NT priority classes
50Others
- CPU time used
- Time remaining
- Resource used
- Resource claimed
- Resource quotas
- Number of I/O requests since creation
- . . .
51Processes and Threads (Windows 2000)
52Windows 2000 (EPROCESS)
Executive Process
53Kernel Process Block
54Processes and Threads (Windows 2000)
EPROCESS
ETHREAD
55Windows 2000 (ETHREAD)
56Kernel Thread Block
57Windows 2000 Thread States
58Implement 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
. . . . . .
59Implement 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
60Create
61Create
Create(s0, m0, pi, pid) p Get_New_PCB()
pid Get_New_PID() p-gtID pid
p-gtCPU_State s0 p-gtMemory m0
p-gtPriority pi p-gtStatus.Type
ready_s p-gtStatus.List RL
p-gtCreation_Tree.Parent self
p-gtCreation_Tree.Child NULL insert(self-gt
Creation_Tree.Child, p) insert(RL, p)
Activate() Scheduler()
pid Get_New_PID()
Get_New_PCB()
s0
m0
ready_s
RL
self
NULL
pi
62Create
Create(s0, m0, pi, pid) p Get_New_PCB()
pid Get_New_PID() p-gtID pid
p-gtCPU_State s0 p-gtMemory m0
p-gtPriority pi p-gtStatus.Type
ready_s p-gtStatus.List RL
p-gtCreation_Tree.Parent self
p-gtCreation_Tree.Child NULL insert(self-gt
Creation_Tree.Child, p) insert(RL, p)
Activate() Scheduler()
The calling process
63Create
Create(s0, m0, pi, pid) p Get_New_PCB()
pid Get_New_PID() p-gtID pid
p-gtCPU_State s0 p-gtMemory m0
p-gtPriority pi p-gtStatus.Type
ready_s p-gtStatus.List RL
p-gtCreation_Tree.Parent self
p-gtCreation_Tree.Child NULL insert(self-gt
Creation_Tree.Child, p) insert(RL, p)
Activate() Scheduler()
64Suspend
65Suspend
Suspend ( )
Suspend?
We choose not.
66Suspend
Suspend(pid) p Get_PCB(pid) s
p-gtStatus.Type if ((sblocked_a)(sblo
cked_s)) p-gtStatus.Type blocked_s
else p-gtStatus.Type ready_s if
(srunning) cpu p-gtProcessor_ID
p-gtCPU_State Interrupt(cpu) Scheduler()
returns all registers values of the cpu and
frees the cpu.
67Activate
68Activate
Activate ( )
Activate?
We choose not.
69Activate
Activate(pid) p Get_PCB(pid) if
(p-gtStatus.Type ready_s)
p-gtStatus.Type ready_a Scheduler()
else p-gtStatus.Type blocked_a
70Destroy
We need to release all resources associated with
the process.
What special action needs to be taken if the
process is running?
71Destroy
Destroy ( )
Killed?
We choose to kill child processes.
72Destroy
subroutine Kill_Tree ( )
killed
Kill_Tree(p) for (each q in
p-gtCreation_Tree.Child) Kill_Tree(q) if
(p-gtStatus.Type running) cpu
p-gtProcessor_ID Interrupt(cpu)
Remove(p-gtStatus.List, p) Release_all(p-gtMemo
ry) Release_all(p-gtOther_Resources)
Close_all(p-gtOpen_Files) Delete_PCB(p)
Marked the corresponding cpu free.
73Destroy
subroutine Kill_Tree ( )
killed
Kill_Tree(p) for (each q in
p-gtCreation_Tree.Child) Kill_Tree(q) if
(p-gtStatus.Type running) cpu
p-gtProcessor_ID Interrupt(cpu)
Remove(p-gtStatus.List, p) Release_all(p-gtMemo
ry) Release_all(p-gtOther_Resources)
Close_all(p-gtOpen_Files) Delete_PCB(p)
74Destroy
Destroy ( )
Destroy(pid) p Get_PCB(pid)
Kill_Tree(p) Scheduler()
75Operating Systems PrinciplesProcess Management
and CoordinationLecture 4The Operating System
KernelImplementing Processes and Threads
- Implementing
- Sync/Comm Mechanisms
76General 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()
77Implementing Semaphores/Locks
- CPU usually doesnt support P and V operations
directly. - Test-and-Set instruction ? supported mostly
- It atomically tests and modifies the contents of
a memory location. - It can be used to implement general semaphores in
multiprocessor systems. - In uniprocessor systems, it is sufficient to
disable interrupts before accessing a semaphore.
78The Story (80x86)
79The Story (80x86)
Access resource
80The Story (80x86)
Access resource
Lock XCHG
81The Story (80x86)
Access resource
82The Story (80x86)
The spin locks
Access resource
Lock XCHG
83The Story (80x86)
The spin locks
Job Done
Access resource
Lock XCHG
84The Story (80x86)
The spin locks
Job Done
Access resource
85The Story (80x86)
The spin locks
Job Done
Access resource
Lock XCHG
86Test-and-Set CPU Instruction
TS(R, X)
R X
Indivisible Atomic
0 locked is locked
1 locked is unlocked
X 0
87Spin Locks on Binary Semaphore
sb ? 0, 1
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()
88Spin Locks on Binary Semaphore
sb ? 0, 1
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()
Pb
Sb
Sb
Vb
Sb1
Sb1
Sb0
wait until Sb1
89Spin Locks on Binary Semaphore
sb ? 0, 1
Pb(Sb) do TS(R, Sb) while(!R)/ wait
loop /
Vb(Sb) Sb1
90Spin Locks on Binary Semaphore
sb ? 0, 1
Pb(Sb) do TS(R, Sb) while(!R)/ wait
loop /
Vb(Sb) Sb1
91General Semaphores w/ Busy Wait
P(s) Inhibit_Interrupts Pb(mutex_s) s
s-1 if (s lt 0) Vb(mutex_s)
Enable_Interrupts Pb(delay_s)
Vb(mutex_s) Enable_Interrupts
V(s) Inhibit_Interrupts Pb(mutex_s) s
s1 if (s lt 0) Vb(delay_s) else
Vb(mutex_s) Enable_Interrupts
92Disallowing Preemption
P(s) Inhibit_Interrupts Pb(mutex_s) s
s-1 if (s lt 0) Vb(mutex_s)
Enable_Interrupts Pb(delay_s)
Vb(mutex_s) Enable_Interrupts
V(s) Inhibit_Interrupts Pb(mutex_s) s
s1 if (s lt 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.
93Interrupt Inhibition
P(s) Inhibit_Interrupts Pb(mutex_s)
s s-1 if (s lt 0) Vb(mutex_s)
Enable_Interrupts Pb(delay_s)
Vb(mutex_s) Enable_Interrupts
V(s) Inhibit_Interrupts Pb(mutex_s)
s s1 if (s lt 0) Vb(delay_s) else
Vb(mutex_s) Enable_Interrupts
94Uniprocessor
P(s) Inhibit_Interrupts Pb(mutex_s)
s s-1 if (s lt 0) Vb(mutex_s)
Enable_Interrupts Pb(delay_s)
Vb(mutex_s) Enable_Interrupts
V(s) Inhibit_Interrupts Pb(mutex_s)
s s1 if (s lt 0) Vb(delay_s) else
Vb(mutex_s) Enable_Interrupts
95Busy Wait
P(s) Inhibit_Interrupts Pb(mutex_s)
s s-1 if (s lt 0) Vb(mutex_s)
Enable_Interrupts Pb(delay_s)
Vb(mutex_s) Enable_Interrupts
V(s) Inhibit_Interrupts Pb(mutex_s)
s s1 if (s lt 0) Vb(delay_s) else
Vb(mutex_s) Enable_Interrupts
96Avoiding Busy Wait
P(s) Inhibit_Interrupts Pb(mutex_s)
s s-1 if (s lt 0) Block(self, Ls)
Vb(mutex_s) Enable_Interrupts
Scheduler() else Vb(mutex_s)
Enable_Interrupts
V(s) Inhibit_Interrupts Pb(mutex_s)
s s1 if (s lt 0) Unblock(q, Ls)
Vb(mutex_s) Enable_Interrupts
Scheduler() else Vb(mutex_s)
Enable_Interrupts
97Implementing Monitors (Hoare)
Initial values
mutex 1 condsem_c 0 urgent 0
condcnt_c 0 urgentcnt 0
condcnt_c processes wait on c
Need a semaphore for processes blocked on c, say
condsem_c.
c.wait c.signal
Need a semaphore for signaling processes, say
urgent.
mutually exclusive access for processes
urgentcnt signalers
Need a mutex, say mutex.
98Mutual Access of Processes
Initial values
mutex 1 condsem_c 0 urgent 0
condcnt_c 0 urgentcnt 0
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 ret
urn value / RemoveFromQueue /
99Mutual Access of Processes
Initial values
mutex 1 condsem_c 0 urgent 0
condcnt_c 0 urgentcnt 0
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 ret
urn value / RemoveFromQueue /
100Mutual Access of Processes
Initial values
mutex 1 condsem_c 0 urgent 0
condcnt_c 0 urgentcnt 0
P(mutex)
Procedure_body
if(urgentcnt) V(urgent) else V(mutex)
101wait/signal
Initial values
mutex 1 condsem_c 0 urgent 0
condcnt_c 0 urgentcnt 0
c.wait
c.signal
condcnt_c condcnt_c 1 if (urgentcnt)
V(urgent) else V(mutex) P(condsem_c) / wait
/ condcnt_c condcnt_c - 1
if (condcnt_c) urgentcnt urgentcnt 1
V(condsem_c) P(urgent) / wait /
urgentcnt urgentcnt - 1
102Clock 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
- . . . . . . . .
103Clock 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
104Countdown 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
105Implementation 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/ Timeou
t() /called at interrupt/ V(delsem)
106Logical Countdown Timers
How to implement multiple logical 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)
107Priority Queue withAbsolute Wakeup Times
Set_LTimer(tn, tdel)
Hardware Timers
108Priority Queue withAbsolute Wakeup Times
Set_LTimer(??, 35)
Wall-clock
Hardware Timers
103
109Priority Queue withAbsolute Wakeup Times
Wall-clock
Countdown
Hardware Timers
104
105
106
107
108
109
110
111
112
113
114
115
103
12
11
10
9
8
7
6
5
4
3
2
1
0
TQ
p5
138
p4
150
110Priority Queue withAbsolute Wakeup Times
Wall-clock
Countdown
Hardware Timers
115
115
0
20
TQ
p5
138
p4
150
111Priority Queue with Time Differences
Set_LTimer(tn, tdel)
Hardware Timer
112Priority Queue with Time Differences
Set_LTimer(??, 35)
Countdown
Hardware Timer
12
12
32
37
32
37
47
35
113Priority Queue with Time Differences
Countdown
Hardware Timers
12
11
10
9
8
7
6
5
4
3
2
1
0
TQ
p5
3
p4
10
114Priority Queue with Time Differences
Countdown
Hardware Timers
0
20
TQ
p5
3
p4
10
115Communication Primitives
Assume that the communication processes are in
the same machine.
Memory
Memory
OS
OS
The same physical memory used.
Process q
Process p
Different physical memory used.
Address space for process q
Address space for process p
116Communication Primitives
send/receive can be blocked or nonblocked.
OS
OS
Communication send/receive
Process q
Process p
Address space for process q
Address space for process p
117Communication Primitives
send/receive can be blocked or nonblocked.
OS
OS
Process q
Process p
send(p,m)
receive(q,m)
sbuf
rbuf
Address space for process q
Address space for process p
118Communication Primitives
send/receive can be blocked or nonblocked.
OS
OS
Different address mappings for p and q.
Process q
Process p
send(p,m)
receive(q,m)
How?
Address space for process q
Address space for process p
119Communication Primitives
send/receive can be blocked or nonblocked.
OS
OS
sbuf
sbuf
send(p,m)
Process q
Process p
send(p,m)
Address space for process q
Address space for process p
120Communication Primitives
send/receive can be blocked or nonblocked.
OS
OS
rbuf
rbuf
sbuf
sbuf
send(p,m)
Process q
Process p
receive(q,m)
rbuf
Address space for process q
Address space for process p
121Communication Primitives
send/receive can be blocked or nonblocked.
OS
OS
rbuf
rbuf
sbuf
sbuf
send(p,m)
Process q
Process p
receive(q,m)
rbuf
Address space for process q
Address space for process p
122Communication Primitives
send/receive can be blocked or nonblocked.
OS
OS
rbuf
rbuf
sbuf
sbuf
send(p,m)
Process q
Process p
receive(q,m)
rbuf
Address space for process q
Address space for process p
123Communication Primitives
Use pool of system buffers
Copying through system buffers (Processes in the
same machine)
124Communication Primitives
Use pool of system buffers
Copying accross network (Processes in the
different machines)
125Operating Systems PrinciplesProcess Management
and CoordinationLecture 4The Operating System
KernelImplementing Processes and Threads
126Why Interrupt Handling?
The program to serve interrupt is called
interrupt handler (IH) or interrupt service
routine (ISR).
- 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 dont deal with
I/O completion event directly.
127Types of Interrupt
We deal with hardware interrupt in the following.
- 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
128Interrupt Controller (82C59A)
129Interrupt Controller (82C59A)
80x86 uses STI and CLI to temporarily enable and
disable all interrupts, respectively.
Interrupt requests are priority-leveled. IHs of
high-priority events can preempt those of lower
priority.
to CPU
Signaled By I/O Devices
Interrupt requests are maskable.
130Interrupt Handling
Each interrupt has an interrupt service routine
(ISR).
ISR7 . . . . . . . . . . IRET
ISR6 . . . . . . . . . . IRET
ISR5 . . . . . . . . . . IRET
Normal Job
ISR4 . . . . . . . . . . IRET
ISR3 . . . . . . . . . . IRET
ISR2 . . . . . . . . . . IRET
CPU
ISR1 . . . . . . . . . . IRET
ISR0 . . . . . . . . . . IRET
IRQ0
Interrupt Controller (e.g., 8259)
INT
IRQ1
IRQ2
IRQ3
IRQ4
IRQ5
IRQ6
IRQ7
stack
131Interrupt Handling
Each interrupt has an interrupt service routine
(ISR).
ISR7 . . . . . . . . . . IRET
ISR6 . . . . . . . . . . IRET
ISR5 . . . . . . . . . . IRET
Normal Job
ISR4 . . . . . . . . . . IRET
ISR3 . . . . . . . . . . IRET
ISR2 . . . . . . . . . . IRET
CPU
ISR1 . . . . . . . . . . IRET
ISR0 . . . . . . . . . . IRET
Put the value of flag register, and program
counter (PC) into the stack.
IRQ0
Interrupt Controller (e.g., 8259)
INT
IRQ1
IRQ2
IRQ3
IRQ4
IRQ5
IRQ6
IRQ7
flag
stack
132Interrupt Handling
Each interrupt has an interrupt service routine
(ISR).
ISR3 Push used registers (PUSHA) . .
. . . . . . . . . . . . . . . . . . . . .
Pop used registers (POPA) Return from
interrupt (IRET)
ISR7 . . . . . . . . . . IRET
ISR6 . . . . . . . . . . IRET
ISR5 . . . . . . . . . . IRET
Normal Job
ISR4 . . . . . . . . . . IRET
ISR3 . . . . . . . . . . IRET
ISR2 . . . . . . . . . . IRET
CPU
ISR1 . . . . . . . . . . IRET
ISR0 . . . . . . . . . . IRET
IRQ0
Interrupt Controller (e.g., 8259)
INT
IRQ1
IRQ2
Read Interrupt Vector
IRQ3
IRQ4
IRQ5
IRQ6
IRQ7
flag
stack
133Interrupt Handling
Each interrupt has an interrupt service routine
(ISR).
ISR3 Push used registers (PUSHA) . .
. . . . . . . . . . . . . . . . . . . . .
Pop used registers (POPA) Return from
interrupt (IRET)
ISR7 . . . . . . . . . . IRET
ISR6 . . . . . . . . . . IRET
ISR5 . . . . . . . . . . IRET
Normal Job
ISR4 . . . . . . . . . . IRET
ISR3 . . . . . . . . . . IRET
ISR2 . . . . . . . . . . IRET
CPU
ISR1 . . . . . . . . . . IRET
ISR0 . . . . . . . . . . IRET
IRQ0
Interrupt Controller (e.g., 8259)
INT
IRQ1
IRQ2
IRQ3
IRQ4
IRQ5
IRQ6
IRQ7
flag
stack
134Interrupt Handling
Each interrupt has an interrupt service routine
(ISR).
ISR7 . . . . . . . . . . IRET
ISR6 . . . . . . . . . . IRET
ISR5 . . . . . . . . . . IRET
Normal Job
ISR4 . . . . . . . . . . IRET
ISR3 . . . . . . . . . . IRET
ISR2 . . . . . . . . . . IRET
CPU
ISR1 . . . . . . . . . . IRET
ISR0 . . . . . . . . . . IRET
IRQ0
Interrupt Controller (e.g., 8259)
INT
IRQ1
IRQ2
IRQ3
IRQ4
IRQ5
IRQ6
IRQ7
stack
135Standard Interrupt Handing Sequence
- Save state of interrupted process/thread
- Identify interrupt type and invoke IH
- IH services interrupt
- Restore state of interrupted process (or of
another one if the interrupt for awakening a
waiting process)
136The Typical Sequence for Using a Hardware Device
...... ...... ......
...... Do_I/O ......
Start I/O
Interrupt Service Routine
137The Typical Sequence for Using a Hardware Device
138The Typical Sequence for Using a Hardware Device
?
start I/O
?
?
?
?
?
?
?
?
139Synchronization Primitives Needed
P/V wait/signal
140Monitor Object-Oriented Approach
Device Driver Implemented Using monitor
Called while I/O completion.
Called by the processes need to do I/O.
141Implementing Using Monitor
142Example Monitor Clock Server
143Example 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