Ch04 Network Design
Ch04 Network Design
Ch04 Network Design
The OS Kernel
Where a calculator on the ENIAC is equipped with 18,000 vacuum tubes and weighs 30 tons, computers in the future may have only 1,000 vacuum tubes and weigh only 1.5 tons. Popular Mechanics, 1949
Overview
4.1 Kernel Denitions and Objects 4.2 Queue Structures 4.3 Threads 4.4 Implementing Processes and Threads
Process and Thread Descriptors Implementing the Operations
Overview
4.5 Implementing Synchronization and Communication Mechanisms
Semaphores and Locks Building Monitor Primitives Clock and Time Management Communications Kernel
Queues
OS needs many different queues Single-level queues
implemented as array
xed size efcient for simple FIFO operations unbounded size more overhead, but more exible operations
Priority queues
implemented as array of linked lists
Fundamentals of Modern Operating Systems 6
FIFO Queues
Figure 4-2
Fundamentals of Modern Operating Systems 7
Priority Queues
Figure 4-3(a)
Fundamentals of Modern Operating Systems 8
Priority Queues
Figure 4-3(b)
Fundamentals of Modern Operating Systems 9
Implemented in user space or kernel space Threads are efcient, but lack protection from each other
Fundamentals of Modern Operating Systems
Figure 4-4
10
Implementing Processes/Threads
Process Control Block (PCB)
State Vector = Information necessary to run process p Status
basic types:
Running, Ready, Blocked
additional types:
Figure 4-5
11
Figure 4-6
Fundamentals of Modern Operating Systems 12
15
Synchronization/Communication
Semaphores, locks, monitors, etc. are 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(); } }
Fundamentals of Modern Operating Systems 17
Semaphores/Locks
Need special test_and_set instruction: TS(R,X) Behavior: R = X; X = 0;
Spin/Spinning Locks
Binary semaphore sb (only 0 or 1) Behavior of Pb/Vb:
if (sb==1) sb=0; else wait sb=1; Indivisible implementation of Pb/Vb: Pb(sb): do (TS(R,sb)) while (!R);/*wait loop*/ Vb(sb): sb=1; Pb(sb): Vb(sb):
GeneralSemaphoresw/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; }
Inhibiting interrupts prevents deadlock due to context switching mutex_s needed to implement critical section with multiple
CPUs
Fundamentals of Modern Operating Systems 20
V(s) { Inhibit_Interrupts; Pb(mutex_s); s = s+1; if (s <= 0) { Unblock(q, Ls); Vb(mutex_s); Scheduler(); } else { Vb(mutex_s); Enable_Interrupts; } }
21
Implementing Monitors
Need to insert code to:
guarantee mutual exclusion of procedures (entering/ leaving) implement c.wait implement c.signal
for mutual exclusion condsem_c: for blocking on each condition c urgent: for blocking process after signal
22
Monitor Primitives
23
Monitor Primitives
c.wait:
condcnt_c = condcnt_c + 1; if (urgentcnt) V(urgent); else V(mutex); P(condsem_c); condcnt_c = condcnt_c - 1;
c.signal:
if (condcnt_c) { urgentcnt = urgentcnt + 1; V(condsem_c); P(urgent); urgentcnt = urgentcnt - 1; }
Fundamentals of Modern Operating Systems 24
26
27
Figure 4-8
Fundamentals of Modern Operating Systems 29
X
Figure 4-9
Fundamentals of Modern Operating Systems 31
Communication Primitives
Message Passing as basic form of communication between processes/threads
most natural with processes executing in separate address spaces or machines heavily used in distributed, cluster computing environment (MPI library)
SM-Message Passing
1
Figure 4-10a
In a shared-memory system:
send and receive each use a buffer to hold message
in the user-space of sender/receiver
33 Fundamentals of Modern Operating Systems
SM-Message Passing
System copies message from sbuf to rbuf upon a send call Important issues:
1. How does sender know that sbuf may be reused (overwritten)? 2. How does system know that rbuf may be reused (overwritten)?
SM-Message Passing
Reusing sbuf:
use blocking send:
reuse as soon as send returns sender blocked even if sbuf not reused! sender needs to poll ag!
Reusing rbuf:
provide a ag to system to indicate release of rbuf
receiver must explicitly set ag and system must test repeatedly!
35
SB-Message Passing
1
Figure 4-10b
SB-Message Passing
Send copies sbuf to a system buffer
sender is free immediately after copy is mode
non-blocking with large pool of buffers
SB-Message Passing
Receive copies a system buffer to rbuf
frees consumed system buffer receiver decides when to reuse rbuf for next message
Interrupts
Unpredictable event that forces transfer of control from running process
abstraction and handling of asynchronous events external: generated by hardware (asynchronous) I/O completion or GUI handling internal: consequence of current computation (synchronous)
exceptional (error) conditions (also called exceptions or traps)
40
Interrupt Handling
OS schedules other processes while device busy upon termination of device an interrupt is generated
Fn has to be unblocked and completed
41
Interrupt Handling
Figure 4-11a
save state of interrupted process/thread (q) identify interrupt type and invoke IH IH services interrupt restore state of interrupted process (q or of another one p waiting for that interrupt)
42
Interrupt Handling
Main challenges:
Fn must be able to block itself on a given event
Interrupt Handling
View hardware device as a process:
implement Fn and IH as monitor functions
Fn waits on c
after initializing device
45