Download presentation
Presentation is loading. Please wait.
Published byJuha-Pekka Albert Majanlahti Modified over 6 years ago
1
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文
2
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
3
Kernel Definitions and Objects
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Kernel Definitions and Objects
4
Windows Kernel
5
Windows Kernel Hardware dependent functions are placed in the kernel.
6
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.
7
I am staying on the top of an OS machine.
OS’s Machine I am staying on the top of an OS machine.
8
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
9
Kernel Objects Process and thread management Process Creation
Interrupt and trap handling Resource management Input/output Kernel Objects Process and thread management Process Creation Process Destruction Process Communication/Synchronization
10
Kernel Objects Interrupt and trap handling Some system events:
Process and thread management Interrupt and trap handling Resource management Input/output Kernel Objects 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
11
Kernel Objects Interrupt and trap handling
Process and thread management Interrupt and trap handling Resource management Input/output Kernel Objects Interrupt and trap handling Responding to signals triggered by various system events. ...... CPU I/O Processor ...... Do_I/O Start I/O Done Interrupt Interrupt Service Routine
12
Kernel Objects Resource management Some system resources:
Process and thread management Interrupt and trap handling Resource management Input/output Kernel Objects Resource management Primitives for maintaining, allocating, and releasing system resources. Some system resources: CPUs Timers Main memory Secondary storage I/O devices Files
13
Kernel Objects Input/output
Process and thread management Interrupt and trap handling Resource management Input/output Kernel Objects Input/output Read, write, and control operations for initiating and supervising the transfer of data between I/O devices and main memory or registers.
14
Main Topics in the Lecture
Process and thread management Interrupt and trap handling Resource management Input/output Main topics
15
Process Creation Hierarchy
Kernel ps OS process p1 pn pj user 1 login user j login user n login user processes . . . . . . q1 qm application processes. Interaction with kernel objects . . . child processes
16
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Queue Structures
17
Queues OS needs many different queues Single-level 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
18
Single-Level Queues Circular Array Implementation
Link List Implementation
19
Array indexed by priority
Priority Queues Array indexed by priority
20
Binary heap of priority
Priority Queues Binary heap of priority
21
Array implementation of binary heap
Priority Queues Binary heap of priority Array implementation of binary heap
22
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Threads
23
Address Spaces Virtual Memory Lowest Address, e.g., 00000000 Memory
Highest Address, e.g., FFFFFFFF
24
Address Spaces Virtual Memory OS Lowest Address, e.g., 00000000 Memory
Starting Address of all processes User Programs Highest Address, e.g., FFFFFFFF
25
Their address spaces are different.
Only one process can be activated at a time. Each process thinks that it owns all memory. Processes OS User Programs OS Process 1 OS Process 2 OS Process n Their address spaces are different.
26
Context Switching OS OS OS OS
Only one process can be activated at a time. Each process thinks that it owns all memory. Context Switching OS User Programs OS Process 1 OS Process 2 OS Process n Context Switching Context Switching
27
Context Switching OS OS OS OS
Only one process can be activated at a time. Each process thinks that it owns all memory. Context Switching OS User Programs OS Process 1 OS Process 2 OS Process n The context switching among processes, i.e., to change address space, is very time consuming.
28
Threads OS OS OS OS User Programs Process 1 Process 2 Process n
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 Threads OS User Programs OS Process 1 OS Process 2 OS Process n
29
Processes and Threads
30
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
31
OS Support for Processes/Threads
Microsoft Windows Process & Thread Functions 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.
32
Implementing Processes and Threads
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Implementing Processes and Threads
33
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.
34
Process Control Block (PCB)
35
Process Identification
A system-wide unique identifier.
36
State Vector
37
to restart the process at the point of last interruption.
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.
38
Processor ID To identify the processor that is executing the process. Make sense only for multiprocessor system.
39
Memory Memory map information. Physical Memory Virtual Memory
40
Status
41
Point to the list, e.g., ready list or wait list, on which the process may reside.
Status running ready blocked
42
More on Status Basic process status State transition diagram
running, ready, and blocked State transition diagram ready blocked running Scheduler Request Release Create
43
Process Activation and Suspension
Suspend Thread Process Activation and Suspension 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.
44
The Finer State Transition Diagram
45
The Finer State Transition Diagram
Active Processes Suspended Processes
46
Creation Tree
47
Creation Tree A link list of PCBs of the child processes
Point to the PCB of the parent process.
48
Used by scheduler to decide which process should be running next.
Two methods: Single-integer value Two-leveled valued Base priority + Changeable part Priority Used by scheduler to decide which process should be running next.
49
Windows NT priority classes
Two methods: Single-integer value Two-leveled valued Base priority + Changeable part Priority Windows NT priority classes
50
Others CPU time used Time remaining Resource used Resource claimed
Resource quotas Number of I/O requests since creation . . .
51
Processes and Threads (Windows 2000)
Object Handle Table VAD object Virtual Address Space Descriptors Access Token Thread . . .
52
Executive Process Windows 2000 (EPROCESS)
53
Kernel Process Block
54
Processes and Threads (Windows 2000)
EPROCESS ETHREAD
55
Windows 2000 (ETHREAD)
56
Kernel Thread Block
57
Windows 2000 Thread States
58
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
59
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
60
Create
61
Create Create(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(); } pid = Get_New_PID(); Get_New_PCB(); s0 m0 ready_s RL self NULL pi
62
Create Create(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(); } The calling process
63
Create Create(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(); }
64
Suspend
65
Suspend Suspend ( ) Suspend? We choose not.
66
returns all registers’ values of the cpu and frees the cpu.
Suspend 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 cpu.
67
Activate
68
Activate Activate ( ) Activate? We choose not.
69
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’;
70
Destroy We need to release all resources associated with the process.
What special action needs to be taken if the process is running?
71
Destroy Destroy ( ) Killed? We choose to kill child processes.
72
Marked the corresponding cpu free.
Destroy subroutine Kill_Tree ( ) killed 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); Marked the corresponding cpu free.
73
Destroy killed subroutine Kill_Tree ( ) 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);
74
Destroy Destroy(pid) { p = Get_PCB(pid); Kill_Tree(p); Scheduler(); }
75
Implementing Sync/Comm Mechanisms
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Implementing Sync/Comm Mechanisms
76
General Resource Access Scheme
Semaphores, locks, monitors, messages, time, and other hardware and software objects are considered resources. General Resource Access Scheme 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(); }
77
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 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.
78
The Story (80x86) Memory
79
The Story (80x86) resource Access Memory
80
The Story (80x86) resource Access Lock XCHG Memory
81
The Story (80x86) resource Access Memory
82
The spin locks The Story (80x86) resource Access Lock XCHG Memory
83
The Story (80x86) The spin locks Job Done resource Access Lock XCHG
Memory
84
The spin locks The Story (80x86) Job Done resource Access Memory
85
The Story (80x86) The spin locks Job Done resource Access Lock XCHG
Memory
86
Test-and-Set CPU Instruction
TS(R, X) Returns the value in R. The lock is always locked after being called. A Memory Location (Lock Value) A CPU Register R = X; Read (test) lock value Indivisible Atomic 0: locked is locked 1: locked is unlocked X = 0; Lock (set) the lock
87
Spin Locks on Binary Semaphore
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(); }
88
Spin Locks on Binary Semaphore
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(); } Pb Sb Vb Sb Sb==1 Sb=1; Sb=0; wait until Sb==1
89
Spin Locks on Binary Semaphore
sb {0, 1} Spin Locks on Binary Semaphore Pb(Sb) { do TS(R, Sb) while(!R);/* wait loop */ } Vb(Sb) { Sb=1; }
90
Spin Locks on Binary Semaphore
sb {0, 1} Spin Locks on Binary Semaphore Pb(Sb) { do TS(R, Sb) while(!R);/* wait loop */ } Vb(Sb) { Sb=1; }
91
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); } V(s) { Inhibit_Interrupts; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts; }
92
Disallowing Preemption
P(s) { Inhibit_Interrupts; Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts; Pb(delay_s); } 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.
93
Interrupt Inhibition P(s) { V(s) { Inhibit_Interrupts;;
Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts;; Pb(delay_s); } V(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts;; }
94
Uniprocessor P(s) { V(s) { Inhibit_Interrupts;; Inhibit_Interrupts;;
Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts;; Pb(delay_s); } V(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts;; }
95
Busy Wait P(s) { V(s) { Inhibit_Interrupts;; Inhibit_Interrupts;;
Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts;; Pb(delay_s); } V(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts;; }
96
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 { 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{ };
97
Implementing Monitors (Hoare)
Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0; Implementing Monitors (Hoare) Internal Data Condition Variables Procedure 1 Procedure 2 Procedure 3 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.
98
Mutual Access of Processes
Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0; Mutual Access of Processes 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 */ };
99
Mutual Access of Processes
Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0; Mutual Access of Processes 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) if(urgentcnt) V(urgent); else V(mutex); P(mutex) if(urgentcnt) V(urgent); else V(mutex);
100
Mutual Access of Processes
Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0; Mutual Access of Processes P(mutex) Procedure_body if(urgentcnt) V(urgent); else V(mutex);
101
wait/signal c.wait: c.signal: Initial values: mutex = 1;
condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0; wait/signal 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; }
102
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
103
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
104
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
105
Implementation of Delay(tdel)
To block process for tdel time units. Implementation of Delay(tdel) 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);
106
Logical Countdown Timers
How to implement multiple logical timers? 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)
107
Priority Queue with Absolute Wakeup Times
Set_LTimer(tn, tdel) Priority Queue with Absolute Wakeup Times 103 Wall-clock 12 Countdown Hardware Timers p1 115 p2 135 p3 140 p4 150 Timer Queue TQ
108
Priority Queue with Absolute Wakeup Times
Set_LTimer(??, 35) +103 +138 Priority Queue with Absolute Wakeup Times Wall-clock 12 Countdown Hardware Timers 103 p1 115 p2 135 p3 140 p4 150 Timer Queue TQ p1 115 p2 135 TQ p5 138 p3 140 p4 150
109
Priority Queue with Absolute Wakeup Times
Wall-clock Countdown Hardware Timers 109 108 107 110 113 115 114 105 112 111 106 103 104 5 11 4 2 1 12 3 6 9 10 8 7 TQ p1 115 p2 135 p5 138 p3 140 p4 150
110
Priority Queue with Absolute Wakeup Times
Wall-clock Countdown Hardware Timers 115 115 20 135 -115 20 TQ p2 135 p2 135 p5 138 p3 140 p4 150
111
Priority Queue with Time Differences
Set_LTimer(tn, tdel) Priority Queue with Time Differences 12 Countdown Hardware Timer p1 15 p2 20 p3 5 p4 10 Timer Queue TQ
112
Priority Queue with Time Differences
Set_LTimer(??, 35) Priority Queue with Time Differences Countdown Hardware Timer 12 p1 15 p2 20 p3 5 p4 10 Timer Queue TQ 12 32 37 32 35 37 47 3 2 p1 15 p2 20 TQ p5 3 p3 2 p4 10
113
Priority Queue with Time Differences
Countdown Hardware Timers 6 11 9 8 5 10 3 12 1 2 7 4 TQ p1 15 p2 20 p5 3 p3 2 p4 10
114
Priority Queue with Time Differences
Countdown Hardware Timers 20 TQ p2 20 p5 3 p3 2 p4 10
115
Communication Primitives
Assume that the communication processes are in the same machine. Communication Primitives Memory OS System Space User Space Memory OS The same physical memory used. Process q Process p Different physical memory used. Address space for process q Address space for process p
116
Communication Primitives
send/receive can be blocked or nonblocked. Communication Primitives OS OS Communication send/receive Process q Process p Address space for process q Address space for process p
117
Communication Primitives
send/receive can be blocked or nonblocked. Communication Primitives OS OS Process q send(p,m) receive(q,m) Process p sbuf rbuf Address space for process q Address space for process p
118
Communication Primitives
send/receive can be blocked or nonblocked. Communication Primitives OS Different address mappings for p and q. OS Process q send(p,m) receive(q,m) Process p sbuf sbuf rbuf rbuf How? Address space for process q Address space for process p
119
Communication Primitives
send/receive can be blocked or nonblocked. Communication Primitives OS OS sbuf’ sbuf’ Process q send(p,m) send(p,m) Process p sbuf Address space for process q Address space for process p
120
Communication Primitives
send/receive can be blocked or nonblocked. Communication Primitives OS OS rbuf’ rbuf’ sbuf’ sbuf’ Process q send(p,m) receive(q,m) Process p sbuf rbuf Address space for process q Address space for process p
121
Communication Primitives
send/receive can be blocked or nonblocked. Communication Primitives OS OS rbuf’ rbuf’ sbuf’ sbuf’ Process q send(p,m) receive(q,m) Process p sbuf rbuf Address space for process q Address space for process p
122
Communication Primitives
send/receive can be blocked or nonblocked. Communication Primitives OS OS rbuf’ rbuf’ sbuf’ sbuf’ Process q send(p,m) receive(q,m) Process p sbuf rbuf Address space for process q Address space for process p
123
Communication Primitives
Use pool of system buffers Communication Primitives Copying through system buffers (Processes in the same machine)
124
Communication Primitives
Use pool of system buffers Communication Primitives Copying accross network (Processes in the different machines)
125
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Interrupt Handling
126
Why Interrupt Handling?
The program to serve interrupt is called interrupt handler (IH) or interrupt service routine (ISR). 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.
127
Types of Interrupt External Interrupts Internal Interrupts
We deal with hardware interrupt in the following. 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
128
Interrupt Controller (82C59A)
129
Interrupt Controller (82C59A)
80x86 uses STI and CLI to temporarily enable and disable all interrupts, respectively. Interrupt Controller (82C59A) Interrupt requests are priority-leveled. IH’s of high-priority events can preempt those of lower priority. to CPU Signaled By I/O Devices Interrupt requests are maskable.
130
Interrupt Handling CPU
Each interrupt has an interrupt service routine (ISR). Interrupt Handling ISR7: IRET ISR6: IRET ISR5: IRET Normal Job ISR4: IRET ISR3: IRET ISR2: IRET CPU ISR1: IRET ISR0: IRET Interrupt Controller (e.g., 8259) IRQ0 INT IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 stack
131
Interrupt Handling CPU
Each interrupt has an interrupt service routine (ISR). Interrupt Handling ISR7: IRET ISR6: IRET ISR5: IRET Normal Job ISR4: IRET ISR3: IRET ISR2: IRET CPU ISR1: IRET ISR0: IRET PC (*) Put the value of flag register, and program counter (PC) into the stack. Interrupt Controller (e.g., 8259) IRQ0 INT IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 * IRQ7 flag stack
132
Interrupt Handling CPU
Each interrupt has an interrupt service routine (ISR). ISR3: Push used registers (PUSHA) Pop used registers (POPA) Return from interrupt (IRET) Interrupt Handling ISR7: IRET ISR6: IRET ISR5: IRET Normal Job ISR4: IRET ISR3: IRET ISR2: IRET CPU ISR1: IRET ISR0: IRET PC (*) Interrupt Controller (e.g., 8259) IRQ0 INT IRQ1 IRQ2 Read Interrupt Vector IRQ3 IRQ4 IRQ5 IRQ6 * IRQ7 flag stack
133
Interrupt Handling CPU
Each interrupt has an interrupt service routine (ISR). ISR3: Push used registers (PUSHA) Pop used registers (POPA) Return from interrupt (IRET) Interrupt Handling ISR7: IRET ISR6: IRET ISR5: IRET Normal Job ISR4: IRET ISR3: IRET ISR2: IRET CPU ISR1: IRET ISR0: IRET PC (*) Interrupt Controller (e.g., 8259) IRQ0 INT IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 * IRQ7 flag stack
134
Interrupt Handling CPU
Each interrupt has an interrupt service routine (ISR). Interrupt Handling ISR7: IRET ISR6: IRET ISR5: IRET Normal Job ISR4: IRET ISR3: IRET ISR2: IRET CPU ISR1: IRET ISR0: IRET PC (*) Interrupt Controller (e.g., 8259) IRQ0 INT IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 stack
135
Standard 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)
136
The Typical Sequence for Using a Hardware Device
...... CPU I/O Processor ...... Do_I/O Start I/O Done Interrupt Interrupt Service Routine
137
The Typical Sequence for Using a Hardware Device
138
The Typical Sequence for Using a Hardware Device
start I/O
139
Synchronization Primitives Needed
P/V wait/signal Synchronization Primitives Needed
140
Monitor: Object-Oriented Approach
Device Driver Implemented Using monitor Called while I/O completion. Called by the processes need to do I/O.
141
Implementing Using Monitor
142
Example: Monitor Clock Server
143
Example: 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;
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.