SlideShare a Scribd company logo
1 of 146
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文
Content ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Operating Systems Principles Process 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.
OS Kernel ,[object Object],[object Object]
OS’s Machine I am staying on the top of an  OS machine .
Kernel Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Kernel Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Kernel Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Kernel Objects ,[object Object],[object Object],...... ...... ...... ,[object Object],[object Object],[object Object],[object Object],Start I/O ...... Do_I/O ...... Interrupt Service Routine Done CPU I/O Processor Interrupt
Kernel Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Kernel Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Main Topics in the Lecture ,[object Object],[object Object],[object Object],[object Object],Main topics
Process Creation Hierarchy Kernel p s . . . . . . . . . OS process user  1  login user  j  login user  n  login user processes application processes. Interaction with kernel objects p 1 p n q 1 q m p j child processes
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Queue Structures
Queues ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Single-Level Queues Circular Array  Implementation Link List  Implementation
Priority Queues Array indexed by priority
Priority Queues Binary heap of priority
Priority Queues Binary heap of priority Array implementation of binary heap
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Threads
Address Spaces Memory Virtual Memory Lowest Address, e.g.,  00000000 Highest Address, e.g.,  FFFFFFFF
Address Spaces Memory Virtual Memory OS User Programs Lowest Address, e.g.,  00000000 Highest Address, e.g.,  FFFFFFFF Starting Address of all processes
Processes Only one process can be activated at a time. Each process thinks that it owns all memory. Their address spaces are different. OS User Programs OS Process 1 OS Process 2 OS Process n
Context Switching Only one process can be activated at a time. Each process thinks that it owns all memory. Context Switching Context Switching OS User Programs OS Process 1 OS Process 2 OS Process n
Context 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 . OS User Programs OS Process 1 OS Process 2 OS Process n
Threads ,[object Object],[object Object],[object Object],[object Object],OS User Programs OS Process 1 OS Process 2 OS Process n
Processes and Threads
Processes and Threads ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
OS Support for Processes/Threads ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Microsoft  Windows Process & Thread Functions
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Implementing Processes and Threads
Process and Thread Descriptors ,[object Object],[object Object]
Process Control Block (PCB)
Process Identification 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.
Processor ID To identify the  processor  that is executing the process. Make sense only for  multiprocessor  system.
Memory Memory  map  information. Physical  Memory     Virtual  Memory
Status
Status Point to the list, e.g.,  ready list  or  wait list , on which the process may reside. running ready blocked
More on Status ,[object Object],[object Object],[object Object],ready blocked running Scheduler Request Release Create
Process Activation and Suspension ,[object Object],[object Object],[object Object],[object Object],Suspend  Thread  Resume  Thread
The Finer State Transition Diagram
The Finer State Transition Diagram Active Processes Suspended Processes
Creation Tree
Creation Tree Point to the PCB of the  parent  process. A link list of PCBs of the  child  processes
Priority ,[object Object],[object Object],[object Object],[object Object],Used by scheduler to decide  which process should be running next .
Priority ,[object Object],[object Object],[object Object],[object Object],Windows NT priority classes
Others ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Processes and Threads (Windows 2000) Process Object 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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],cobegin/coend forall fork/join/quit . . . . . .
Implement Operations on Processes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Operating on PCBs CSs must be cared
Create
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(); } s0 m0 pi pid  =  Get_New_ PID (); ready_s RL self NULL Get_New_ PCB ();
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
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 (); }
Suspend
Suspend Suspend (  ) Suspend? We choose  not .
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.
Activate
Activate 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 ’; }
Destroy We need to  release  all  resources  associated with the process. What  special   action  needs to be taken if the process is  running ?
Destroy Destroy (  ) Killed? We choose to  kill child processes .
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);  } subroutine Kill_Tree (  ) killed Marked the corresponding  cpu  free.
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);  } subroutine Kill_Tree (  ) killed
Destroy Destroy (  ) Destroy (pid) {  p =  Get_PCB (pid); Kill_Tree (p); Scheduler (); }
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Implementing Sync/Comm Mechanisms
General Resource Access Scheme Semaphores, locks, monitors, messages, time, and other hardware and software objects are considered  resources . ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implementing Semaphores/Locks ,[object Object],[object Object],[object Object],[object Object],[object Object]
The Story (80x86) Memory
The Story (80x86) Access resource Memory
The Story (80x86) Access resource Lock XCHG Memory
The Story (80x86) Access resource Memory
The Story (80x86) Access resource Lock XCHG The spin locks Memory
The Story (80x86) Access resource Lock XCHG Job Done The spin locks Memory
The Story (80x86) Access resource Job Done The spin locks Memory
The Story (80x86) Access resource Job Done Lock XCHG The spin locks Memory
Test-and-Set CPU Instruction TS ( R ,  X ) R  =  X ; X  =  0 ; 0 : locked is  locked 1 : locked is  unlocked Indivisible Atomic A CPU Register A Memory Location (Lock Value) Read (test)  lock value Lock (set)  the lock Returns the value in  R . The lock is  always   locked  after being called.
Spin Locks on Binary Semaphore ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],sb     {0, 1}
Spin Locks on Binary Semaphore ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],sb     {0, 1} Pb Vb Sb Sb Sb ==1 Sb =1; Sb =0; wait until  Sb ==1
Spin Locks on Binary Semaphore sb     {0, 1} ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Spin Locks on Binary Semaphore sb     {0, 1} ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
General Semaphores w/ Busy Wait ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Disallowing Preemption High Priority e.g., ISR Low Priority Shared Resource Low Priority
Dead Lock Condition High Priority e.g., ISR Low Priority Shared Resource Low Priority
Dead Lock Avoidance Low Priority High Priority e.g., ISR Low Priority Shared Resource Windows  uses different implementations.
Disallowing Preemption ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Disallowed  to be  preempted  by a  higher-priority  process. Disallowed  to be  preempted  by a  higher-priority  process.
Interrupt Inhibition ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Uniprocessor ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Busy Wait ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Avoiding Busy Wait ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implementing Monitors (Hoare) 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; Internal Data Condition Variables Procedure 1 Procedure 2 Procedure 3
Mutual Access of Processes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0;
Mutual Access of Processes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0; P ( mutex ) P ( mutex ) if( urgentcnt )  V ( urgent ); else  V ( mutex ); if( urgentcnt )  V ( urgent ); else  V ( mutex );
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;
wait/signal ,[object Object],[object Object],[object Object],[object Object],[object Object],c.wait: c.signal: Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0; ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Clock and Time Management ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Clock and Time Management ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Countdown Timer (Alarm Clocks) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implementation of  Delay ( tdel )   ,[object Object],To block process for  tdel  time units. ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Logical Countdown Timers ,[object Object],[object Object],[object Object],[object Object],[object Object],How to implement multiple logical timers?
Priority Queue with Absolute Wakeup Times Set_LTimer( tn ,  tdel ) Hardware Timers 103 Wall-clock 12 Countdown p 1 115 p 2 135 p 3 140 p 4 150 Timer Queue TQ
Priority Queue with Absolute Wakeup Times Set_LTimer( ?? ,  35 ) 103 Wall-clock Hardware Timers 12 Countdown p 1 115 p 2 135 p 3 140 p 4 150 Timer Queue TQ p 5 138 p 3 140 p 4 150 p 1 115 p 2 135 TQ +103 +138
Priority Queue with Absolute Wakeup Times 103 Wall-clock 12 Countdown Hardware Timers p 5 138 p 4 150 TQ 104 11 105 10 106 9 107 8 108 7 109 6 110 5 111 4 112 3 113 2 114 1 115 0 p 3 140 p 1 115 p 2 135
Priority Queue with Absolute Wakeup Times 115 Wall-clock Countdown Hardware Timers p 5 138 p 4 150 TQ 115 0 20 p 2 135 p 3 140 p 2 135 135 -115 20
Priority Queue with Time Differences Hardware Timer Set_LTimer( tn ,  tdel ) 12 Countdown p 1 15 p 2 20 p 3 5 p 4 10 Timer Queue TQ
Priority Queue with Time Differences 12 Countdown Hardware Timer Set_LTimer( ?? ,  35 ) 12 32 32 37 37 47 35 p 5 3 p 3 2 p 4 10 p 1 15 p 2 20 TQ 3 2 p 1 15 p 2 20 p 3 5 p 4 10 Timer Queue TQ
Priority Queue with Time Differences 12 Countdown Hardware Timers p 5 3 p 4 10 TQ 11 10 9 8 7 6 5 4 3 2 1 0 p 3 2 p 1 15 p 2 20
Priority Queue with Time Differences Countdown Hardware Timers p 5 3 p 4 10 0 TQ 20 p 3 2 p 2 20
Communication Primitives Memory Memory Process q Process p OS OS Address space for process  q Address space for process  p The same physical memory used. Different physical memory used. Assume that the communication processes are in the same machine. System Space User Space
Communication Primitives OS OS Process q Process p Address space for process  q Address space for process  p Communication send/receive send/receive  can be  blocked  or  nonblocked .
Communication Primitives OS OS Process q Process p 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 .
Communication Primitives OS OS Process q Process p Address space for process  q Address space for process  p send(p,m) receive(q,m) send/receive  can be  blocked  or  nonblocked . Different   address mappings  for  p  and  q . How? sbuf rbuf sbuf rbuf
Communication Primitives send(p,m) OS OS Process q Process p Address space for process  q Address space for process  p send(p,m) send/receive  can be  blocked  or  nonblocked . sbuf’ sbuf’ sbuf
Communication Primitives send(p,m) OS OS Process q Process p Address space for process  q Address space for process  p receive(q,m) send/receive  can be  blocked  or  nonblocked . rbuf sbuf’ sbuf’ rbuf’ rbuf’ sbuf
Communication Primitives send(p,m) OS OS Process q Process p Address space for process  q Address space for process  p receive(q,m) send/receive  can be  blocked  or  nonblocked . rbuf sbuf’ sbuf’ rbuf’ rbuf’ sbuf
Communication Primitives send(p,m) OS OS Process q Process p Address space for process  q Address space for process  p receive(q,m) send/receive  can be  blocked  or  nonblocked . rbuf sbuf’ sbuf’ rbuf’ rbuf’ sbuf
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 Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Interrupt Handling
Why Interrupt Handling? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],The program to serve interrupt is called  interrupt handler  (IH) or  interrupt service routine  (ISR).
Types of Interrupt ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],We deal with  hardware  interrupt in the following.
Interrupt Controller (82C59A)
Interrupt Controller (82C59A) to CPU Signaled By 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 CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an  interrupt service routine  ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job stack
Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an  interrupt service routine  ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job Put the value of  flag register , and  program counter (PC)  into the stack. flag * stack PC (*)
Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an  interrupt service routine  ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job Read Interrupt Vector flag * ISR3: Push used registers (PUSHA) . . . . . . . . . . . . . . . . . . . . . . . Pop used registers (POPA) Return from interrupt (IRET) stack PC (*)
Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an  interrupt service routine  ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job flag * ISR3: Push used registers (PUSHA) . . . . . . . . . . . . . . . . . . . . . . . Pop used registers (POPA) Return from interrupt (IRET) stack PC (*)
Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an  interrupt service routine  ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job stack PC (*)
Standard Interrupt Handing Sequence ,[object Object],[object Object],[object Object],[object Object]
The Typical Sequence for Using a Hardware Device ...... ...... ...... Start I/O ...... Do_I/O ...... Interrupt Service Routine Done CPU I/O Processor Interrupt
The Typical Sequence for Using a Hardware Device
The Typical Sequence for Using a Hardware Device   start I/O       
Synchronization Primitives Needed P/V wait/signal
Monitor: Object-Oriented Approach Device  Driver Implemented Using monitor Called by the processes need to do I/O. 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; } }

More Related Content

What's hot

Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device DriverGary Yeh
 
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERSVTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERSvtunotesbysree
 
Capturing comprehensive storage workload traces in windows
Capturing comprehensive storage workload traces in windowsCapturing comprehensive storage workload traces in windows
Capturing comprehensive storage workload traces in windowsBruce Worthington
 
11.file system implementation
11.file system implementation11.file system implementation
11.file system implementationSenthil Kanth
 
Chapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System ConceptsChapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System ConceptsMeenalJabde
 
Chapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix ConceptsChapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix ConceptsMeenalJabde
 
Monitoring MySQL with DTrace/SystemTap
Monitoring MySQL with DTrace/SystemTapMonitoring MySQL with DTrace/SystemTap
Monitoring MySQL with DTrace/SystemTapPadraig O'Sullivan
 
Root file system
Root file systemRoot file system
Root file systemBindu U
 
Chapter 3 Using Unix Commands
Chapter 3 Using Unix CommandsChapter 3 Using Unix Commands
Chapter 3 Using Unix CommandsMeenalJabde
 
Basics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubBasics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubDevang Garach
 
Dba 3+ exp qus
Dba 3+ exp qusDba 3+ exp qus
Dba 3+ exp quskrreddy21
 
RHEL Log-files, RPM, Backup & Recovery
RHEL Log-files, RPM, Backup & RecoveryRHEL Log-files, RPM, Backup & Recovery
RHEL Log-files, RPM, Backup & RecoveryAneesa Rahman
 
Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linuxLiran Ben Haim
 
Introduction to Kernel and Device Drivers
Introduction to Kernel and Device DriversIntroduction to Kernel and Device Drivers
Introduction to Kernel and Device DriversRajKumar Rampelli
 
8 0-os file-system management
8 0-os file-system management8 0-os file-system management
8 0-os file-system managementGol D Roger
 

What's hot (18)

Aix commands
Aix commandsAix commands
Aix commands
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
 
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERSVTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
 
Capturing comprehensive storage workload traces in windows
Capturing comprehensive storage workload traces in windowsCapturing comprehensive storage workload traces in windows
Capturing comprehensive storage workload traces in windows
 
11.file system implementation
11.file system implementation11.file system implementation
11.file system implementation
 
Chapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System ConceptsChapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System Concepts
 
OS - Process
OS - ProcessOS - Process
OS - Process
 
Chapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix ConceptsChapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix Concepts
 
Monitoring MySQL with DTrace/SystemTap
Monitoring MySQL with DTrace/SystemTapMonitoring MySQL with DTrace/SystemTap
Monitoring MySQL with DTrace/SystemTap
 
Root file system
Root file systemRoot file system
Root file system
 
Chapter 3 Using Unix Commands
Chapter 3 Using Unix CommandsChapter 3 Using Unix Commands
Chapter 3 Using Unix Commands
 
Basics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubBasics of Linux Commands, Git and Github
Basics of Linux Commands, Git and Github
 
Dba 3+ exp qus
Dba 3+ exp qusDba 3+ exp qus
Dba 3+ exp qus
 
RHEL Log-files, RPM, Backup & Recovery
RHEL Log-files, RPM, Backup & RecoveryRHEL Log-files, RPM, Backup & Recovery
RHEL Log-files, RPM, Backup & Recovery
 
Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linux
 
Introduction to Kernel and Device Drivers
Introduction to Kernel and Device DriversIntroduction to Kernel and Device Drivers
Introduction to Kernel and Device Drivers
 
Ch12
Ch12Ch12
Ch12
 
8 0-os file-system management
8 0-os file-system management8 0-os file-system management
8 0-os file-system management
 

Viewers also liked

Ch11 input output systems
Ch11 input output systemsCh11 input output systems
Ch11 input output systemsissbp
 
Class5
 Class5 Class5
Class5issbp
 
Os6 2
Os6 2Os6 2
Os6 2issbp
 
Class9
 Class9 Class9
Class9issbp
 
Lecture 7: Definite Clause Grammars
Lecture 7: Definite Clause GrammarsLecture 7: Definite Clause Grammars
Lecture 7: Definite Clause GrammarsCS, NcState
 
Os5 2
Os5 2Os5 2
Os5 2issbp
 
Os2 2
Os2 2Os2 2
Os2 2issbp
 
Os10 2
Os10 2Os10 2
Os10 2issbp
 
Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Abhimanyu Mishra
 
Pattern detection in mealy machine
Pattern detection in mealy machinePattern detection in mealy machine
Pattern detection in mealy machineAnimesh Chaturvedi
 
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...Animesh Chaturvedi
 
Class6
 Class6 Class6
Class6issbp
 
0227 regularlanguages
 0227 regularlanguages 0227 regularlanguages
0227 regularlanguagesissbp
 

Viewers also liked (20)

Os4
Os4Os4
Os4
 
Ch11 input output systems
Ch11 input output systemsCh11 input output systems
Ch11 input output systems
 
Os6
Os6Os6
Os6
 
Class5
 Class5 Class5
Class5
 
Os2
Os2Os2
Os2
 
Os6 2
Os6 2Os6 2
Os6 2
 
Class9
 Class9 Class9
Class9
 
Lecture 7: Definite Clause Grammars
Lecture 7: Definite Clause GrammarsLecture 7: Definite Clause Grammars
Lecture 7: Definite Clause Grammars
 
Os5 2
Os5 2Os5 2
Os5 2
 
Os2 2
Os2 2Os2 2
Os2 2
 
Design1
Design1Design1
Design1
 
Os5
Os5Os5
Os5
 
Os10 2
Os10 2Os10 2
Os10 2
 
Cspc final
Cspc finalCspc final
Cspc final
 
Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5
 
Pattern detection in mealy machine
Pattern detection in mealy machinePattern detection in mealy machine
Pattern detection in mealy machine
 
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
 
Os3
Os3Os3
Os3
 
Class6
 Class6 Class6
Class6
 
0227 regularlanguages
 0227 regularlanguages 0227 regularlanguages
0227 regularlanguages
 

Similar to Os4 2

Operating System Process Scheduling.pptx
Operating System Process Scheduling.pptxOperating System Process Scheduling.pptx
Operating System Process Scheduling.pptxMuhammad Athar
 
Operating System 3
Operating System 3Operating System 3
Operating System 3tech2click
 
Process management
Process managementProcess management
Process managementBirju Tank
 
Process management os concept
Process management os conceptProcess management os concept
Process management os conceptpriyadeosarkar91
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptxDiptoRoy21
 
My ppt @ bec doms on process management
My ppt @ bec doms on process managementMy ppt @ bec doms on process management
My ppt @ bec doms on process managementBabasab Patil
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsPeter Tröger
 
CS9222 Advanced Operating System
CS9222 Advanced Operating SystemCS9222 Advanced Operating System
CS9222 Advanced Operating SystemKathirvel Ayyaswamy
 
UNIT-2-Process-Management.pdf
UNIT-2-Process-Management.pdfUNIT-2-Process-Management.pdf
UNIT-2-Process-Management.pdfaakritii765
 
OperatingSystem02..(B.SC Part 2)
OperatingSystem02..(B.SC Part 2)OperatingSystem02..(B.SC Part 2)
OperatingSystem02..(B.SC Part 2)Muhammad Osama
 
Processes in Operating System
Processes in Operating SystemProcesses in Operating System
Processes in Operating SystemArafat Hossan
 
OS - Chapter # 3 for the development of os
OS - Chapter # 3 for the development of osOS - Chapter # 3 for the development of os
OS - Chapter # 3 for the development of osTahaShahid18
 

Similar to Os4 2 (20)

Operating System Process Scheduling.pptx
Operating System Process Scheduling.pptxOperating System Process Scheduling.pptx
Operating System Process Scheduling.pptx
 
Supply chain mgmt
Supply chain mgmtSupply chain mgmt
Supply chain mgmt
 
Operating System 3
Operating System 3Operating System 3
Operating System 3
 
Process management
Process managementProcess management
Process management
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
Os
OsOs
Os
 
Os
OsOs
Os
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptx
 
My ppt @ bec doms on process management
My ppt @ bec doms on process managementMy ppt @ bec doms on process management
My ppt @ bec doms on process management
 
OS Chapter03
OS Chapter03OS Chapter03
OS Chapter03
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
UNIT II.pptx
UNIT II.pptxUNIT II.pptx
UNIT II.pptx
 
CS9222 Advanced Operating System
CS9222 Advanced Operating SystemCS9222 Advanced Operating System
CS9222 Advanced Operating System
 
UNIT-2-Process-Management.pdf
UNIT-2-Process-Management.pdfUNIT-2-Process-Management.pdf
UNIT-2-Process-Management.pdf
 
OperatingSystem02..(B.SC Part 2)
OperatingSystem02..(B.SC Part 2)OperatingSystem02..(B.SC Part 2)
OperatingSystem02..(B.SC Part 2)
 
unit-2.pdf
unit-2.pdfunit-2.pdf
unit-2.pdf
 
OS-Process.pdf
OS-Process.pdfOS-Process.pdf
OS-Process.pdf
 
Processes in Operating System
Processes in Operating SystemProcesses in Operating System
Processes in Operating System
 
Cs8493 unit 2
Cs8493 unit 2Cs8493 unit 2
Cs8493 unit 2
 
OS - Chapter # 3 for the development of os
OS - Chapter # 3 for the development of osOS - Chapter # 3 for the development of os
OS - Chapter # 3 for the development of os
 

More from issbp

Os10
Os10Os10
Os10issbp
 
Os9 2
Os9 2Os9 2
Os9 2issbp
 
Os8 2
Os8 2Os8 2
Os8 2issbp
 
Os7 2
Os7 2Os7 2
Os7 2issbp
 
Os3 2
Os3 2Os3 2
Os3 2issbp
 
Class8
 Class8 Class8
Class8issbp
 
Class7
 Class7 Class7
Class7issbp
 
Class4
 Class4 Class4
Class4issbp
 
Class3
 Class3 Class3
Class3issbp
 
Class2
 Class2 Class2
Class2issbp
 
Class1
 Class1 Class1
Class1issbp
 

More from issbp (14)

Os10
Os10Os10
Os10
 
Os9 2
Os9 2Os9 2
Os9 2
 
Os9
Os9Os9
Os9
 
Os8 2
Os8 2Os8 2
Os8 2
 
Os8
Os8Os8
Os8
 
Os7 2
Os7 2Os7 2
Os7 2
 
Os7
Os7Os7
Os7
 
Os3 2
Os3 2Os3 2
Os3 2
 
Class8
 Class8 Class8
Class8
 
Class7
 Class7 Class7
Class7
 
Class4
 Class4 Class4
Class4
 
Class3
 Class3 Class3
Class3
 
Class2
 Class2 Class2
Class2
 
Class1
 Class1 Class1
Class1
 

Os4 2

  • 1. Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文
  • 2.
  • 3. Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Kernel Definitions and Objects
  • 5. Windows Kernel Hardware dependent functions are placed in the kernel.
  • 6.
  • 7. OS’s Machine I am staying on the top of an OS machine .
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. Process Creation Hierarchy Kernel p s . . . . . . . . . OS process user 1 login user j login user n login user processes application processes. Interaction with kernel objects p 1 p n q 1 q m p j child processes
  • 16. Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Queue Structures
  • 17.
  • 18. Single-Level Queues Circular Array Implementation Link List Implementation
  • 19. Priority Queues Array indexed by priority
  • 20. Priority Queues Binary heap of priority
  • 21. 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 Memory Virtual Memory Lowest Address, e.g., 00000000 Highest Address, e.g., FFFFFFFF
  • 24. Address Spaces Memory Virtual Memory OS User Programs Lowest Address, e.g., 00000000 Highest Address, e.g., FFFFFFFF Starting Address of all processes
  • 25. Processes Only one process can be activated at a time. Each process thinks that it owns all memory. Their address spaces are different. OS User Programs OS Process 1 OS Process 2 OS Process n
  • 26. Context Switching Only one process can be activated at a time. Each process thinks that it owns all memory. Context Switching Context Switching OS User Programs OS Process 1 OS Process 2 OS Process n
  • 27. Context 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 . OS User Programs OS Process 1 OS Process 2 OS Process n
  • 28.
  • 30.
  • 31.
  • 32. Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Implementing Processes and Threads
  • 33.
  • 35. Process Identification A system-wide unique identifier.
  • 37. 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
  • 41. Status Point to the list, e.g., ready list or wait list , on which the process may reside. running ready blocked
  • 42.
  • 43.
  • 44. The Finer State Transition Diagram
  • 45. The Finer State Transition Diagram Active Processes Suspended Processes
  • 47. Creation Tree Point to the PCB of the parent process. A link list of PCBs of the child processes
  • 48.
  • 49.
  • 50.
  • 51. Processes and Threads (Windows 2000) Process Object Handle Table VAD VAD VAD object object Virtual Address Space Descriptors Access Token Thread Thread Thread . . . Access Token
  • 52. Windows 2000 (EPROCESS) Executive Process
  • 54. Processes and Threads (Windows 2000) EPROCESS ETHREAD
  • 58.
  • 59.
  • 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(); } s0 m0 pi pid = Get_New_ PID (); ready_s RL self NULL Get_New_ PCB ();
  • 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 (); }
  • 65. Suspend Suspend ( ) Suspend? We choose not .
  • 66. 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.
  • 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. 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); } subroutine Kill_Tree ( ) killed Marked the corresponding cpu free.
  • 73. 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); } subroutine Kill_Tree ( ) killed
  • 74. Destroy Destroy ( ) Destroy (pid) { p = Get_PCB (pid); Kill_Tree (p); Scheduler (); }
  • 75. Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Implementing Sync/Comm Mechanisms
  • 76.
  • 77.
  • 79. The Story (80x86) Access resource Memory
  • 80. The Story (80x86) Access resource Lock XCHG Memory
  • 81. The Story (80x86) Access resource Memory
  • 82. The Story (80x86) Access resource Lock XCHG The spin locks Memory
  • 83. The Story (80x86) Access resource Lock XCHG Job Done The spin locks Memory
  • 84. The Story (80x86) Access resource Job Done The spin locks Memory
  • 85. The Story (80x86) Access resource Job Done Lock XCHG The spin locks Memory
  • 86. Test-and-Set CPU Instruction TS ( R , X ) R = X ; X = 0 ; 0 : locked is locked 1 : locked is unlocked Indivisible Atomic A CPU Register A Memory Location (Lock Value) Read (test) lock value Lock (set) the lock Returns the value in R . The lock is always locked after being called.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92. Disallowing Preemption High Priority e.g., ISR Low Priority Shared Resource Low Priority
  • 93. Dead Lock Condition High Priority e.g., ISR Low Priority Shared Resource Low Priority
  • 94. Dead Lock Avoidance Low Priority High Priority e.g., ISR Low Priority Shared Resource Windows uses different implementations.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100. Implementing Monitors (Hoare) 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; Internal Data Condition Variables Procedure 1 Procedure 2 Procedure 3
  • 101.
  • 102.
  • 103. 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;
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110. Priority Queue with Absolute Wakeup Times Set_LTimer( tn , tdel ) Hardware Timers 103 Wall-clock 12 Countdown p 1 115 p 2 135 p 3 140 p 4 150 Timer Queue TQ
  • 111. Priority Queue with Absolute Wakeup Times Set_LTimer( ?? , 35 ) 103 Wall-clock Hardware Timers 12 Countdown p 1 115 p 2 135 p 3 140 p 4 150 Timer Queue TQ p 5 138 p 3 140 p 4 150 p 1 115 p 2 135 TQ +103 +138
  • 112. Priority Queue with Absolute Wakeup Times 103 Wall-clock 12 Countdown Hardware Timers p 5 138 p 4 150 TQ 104 11 105 10 106 9 107 8 108 7 109 6 110 5 111 4 112 3 113 2 114 1 115 0 p 3 140 p 1 115 p 2 135
  • 113. Priority Queue with Absolute Wakeup Times 115 Wall-clock Countdown Hardware Timers p 5 138 p 4 150 TQ 115 0 20 p 2 135 p 3 140 p 2 135 135 -115 20
  • 114. Priority Queue with Time Differences Hardware Timer Set_LTimer( tn , tdel ) 12 Countdown p 1 15 p 2 20 p 3 5 p 4 10 Timer Queue TQ
  • 115. Priority Queue with Time Differences 12 Countdown Hardware Timer Set_LTimer( ?? , 35 ) 12 32 32 37 37 47 35 p 5 3 p 3 2 p 4 10 p 1 15 p 2 20 TQ 3 2 p 1 15 p 2 20 p 3 5 p 4 10 Timer Queue TQ
  • 116. Priority Queue with Time Differences 12 Countdown Hardware Timers p 5 3 p 4 10 TQ 11 10 9 8 7 6 5 4 3 2 1 0 p 3 2 p 1 15 p 2 20
  • 117. Priority Queue with Time Differences Countdown Hardware Timers p 5 3 p 4 10 0 TQ 20 p 3 2 p 2 20
  • 118. Communication Primitives Memory Memory Process q Process p OS OS Address space for process q Address space for process p The same physical memory used. Different physical memory used. Assume that the communication processes are in the same machine. System Space User Space
  • 119. Communication Primitives OS OS Process q Process p Address space for process q Address space for process p Communication send/receive send/receive can be blocked or nonblocked .
  • 120. Communication Primitives OS OS Process q Process p 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 .
  • 121. Communication Primitives OS OS Process q Process p Address space for process q Address space for process p send(p,m) receive(q,m) send/receive can be blocked or nonblocked . Different address mappings for p and q . How? sbuf rbuf sbuf rbuf
  • 122. Communication Primitives send(p,m) OS OS Process q Process p Address space for process q Address space for process p send(p,m) send/receive can be blocked or nonblocked . sbuf’ sbuf’ sbuf
  • 123. Communication Primitives send(p,m) OS OS Process q Process p Address space for process q Address space for process p receive(q,m) send/receive can be blocked or nonblocked . rbuf sbuf’ sbuf’ rbuf’ rbuf’ sbuf
  • 124. Communication Primitives send(p,m) OS OS Process q Process p Address space for process q Address space for process p receive(q,m) send/receive can be blocked or nonblocked . rbuf sbuf’ sbuf’ rbuf’ rbuf’ sbuf
  • 125. Communication Primitives send(p,m) OS OS Process q Process p Address space for process q Address space for process p receive(q,m) send/receive can be blocked or nonblocked . rbuf sbuf’ sbuf’ rbuf’ rbuf’ sbuf
  • 126. Communication Primitives Copying through system buffers (Processes in the same machine) Use pool of system buffers
  • 127. Communication Primitives Use pool of system buffers Copying accross network (Processes in the different machines)
  • 128. Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Interrupt Handling
  • 129.
  • 130.
  • 132. Interrupt Controller (82C59A) to CPU Signaled By 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.
  • 133. Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job stack
  • 134. Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job Put the value of flag register , and program counter (PC) into the stack. flag * stack PC (*)
  • 135. Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job Read Interrupt Vector flag * ISR3: Push used registers (PUSHA) . . . . . . . . . . . . . . . . . . . . . . . Pop used registers (POPA) Return from interrupt (IRET) stack PC (*)
  • 136. Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job flag * ISR3: Push used registers (PUSHA) . . . . . . . . . . . . . . . . . . . . . . . Pop used registers (POPA) Return from interrupt (IRET) stack PC (*)
  • 137. Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine ( ISR ). ISR7: . . . . . . . . . . IRET ISR6: . . . . . . . . . . IRET ISR5: . . . . . . . . . . IRET ISR4: . . . . . . . . . . IRET ISR3: . . . . . . . . . . IRET ISR2: . . . . . . . . . . IRET ISR1: . . . . . . . . . . IRET ISR0: . . . . . . . . . . IRET Normal Job stack PC (*)
  • 138.
  • 139. The Typical Sequence for Using a Hardware Device ...... ...... ...... Start I/O ...... Do_I/O ...... Interrupt Service Routine Done CPU I/O Processor Interrupt
  • 140. The Typical Sequence for Using a Hardware Device
  • 141. The Typical Sequence for Using a Hardware Device   start I/O       
  • 143. Monitor: Object-Oriented Approach Device Driver Implemented Using monitor Called by the processes need to do I/O. Called while I/O completion.
  • 146. 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; } }