Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文.

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Chapter 3 Process Description and Control
The OS kernel: Implementing processes and Threads Kernel Definitions and Objects Queue Structures Threads Implementing Processes and Threads Implementing.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 3 – Process Concepts Outline 3.1 Introduction 3.1.1Definition of Process 3.2Process States:
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
6/9/2015B.Ramamurthy1 Process Description and Control B.Ramamurthy.
Process Description and Control
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Page 1 Processes and Threads Chapter 2. Page 2 Processes The Process Model Multiprogramming of four programs Conceptual model of 4 independent, sequential.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
CSCE 351: Operating System Kernels
3.5 Interprocess Communication
OS Spring’03 Introduction Operating Systems Spring 2003.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
CSSE Operating Systems
Chapter 6 Implementing Processes, Threads, and Resources.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Process Description and Control A process is sometimes called a task, it is a program in execution.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Operating Systems1 4. The OS Kernel 4.1 Kernel Definitions and Objects 4.2 Queue Structures 4.3 Threads 4.4 Implementing Processes and Threads –Process.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Introduction to Embedded Systems
1 The OS Kernel Kernel Definitions and Objects Queue Structures Threads Implementing Processes and Threads –Process and Thread Descriptors –Implementing.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Chapter 3 Process Description and Control
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
CompSci 143ASpringr, The OS Kernel 4.1 Kernel Definitions and Objects 4.2 Queue Structures 4.3 Threads 4.4 Implementing Processes and Threads.
ICS The OS Kernel 4.1 Kernel Definitions and Objects 4.2 Queue Structures 4.3 Threads 4.4 Implementing Processes and Threads –Process and Thread.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
CE Operating Systems Lecture 11 Windows – Object manager and process management.
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
REVIEW OF COMMONLY USED DATA STRUCTURES IN OS. NEEDS FOR EFFICIENT DATA STRUCTURE Storage complexity & Computation complexity matter Consider the problem.
Threads G.Anuradha (Reference : William Stallings)
Processes and Process Control 1. Processes and Process Control 2. Definitions of a Process 3. Systems state vs. Process State 4. A 2 State Process Model.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Module 2.0: Threads.
2 Processor(s)Main MemoryDevices Process, Thread & Resource Manager Memory Manager Device Manager File Manager.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
Managing Processors Jeff Chase Duke University. The story so far: protected CPU mode user mode kernel mode kernel “top half” kernel “bottom half” (interrupt.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Copyright © Curt Hill More on Operating Systems Continuation of Introduction.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Process concept.
Process Management Process Concept Why only the global variables?
Operating Systems: A Modern Perspective, Chapter 6
Structure of Processes
Process Description and Control
Threads Chapter 4.
Process Description and Control
Process Description and Control
Process Description and Control
Process Description and Control
Process Description and Control
Implementing Processes, Threads, and Resources
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文.
Process Description and Control
Presentation transcript:

Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文

Content Kernel Definitions and Objects Queue Structures Threads Implementing Processes and Threads – Process and Thread Descriptors – Implementing the Operations Implementing Sync/Comm Mechanisms – Semaphores and Locks – Building Monitor Primitives – Clock and Time Management – Communications Kernel Interrupt Handling

Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Kernel Definitions and Objects

Windows Kernel

Hardware dependent functions are placed in the kernel.

OS Kernel A basic set of objects, primitives, data structures, processes from which the remainder of the system may be built on its top. In other words, the kernel transforms the hardware into an OS’s machine.

OS’s Machine I am staying on the top of an OS machine.

Kernel Objects Kernel defines/provides mechanisms to implement various policies. Four classes of possible functions and objects in a kernel: – Process and thread management – Interrupt and trap handling – Resource management – Input/output

Kernel Objects Process and thread management Interrupt and trap handling Resource management Input/output Process and thread management – Process Creation – Process Destruction – Process Communication/Synchronization

Kernel 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

Done Kernel 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. CPU I/O Processor I/O Processor Start I/O Interrupt Do_I/O Interrupt Service Routine

Kernel 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

Kernel Objects Process and thread management Interrupt and trap handling Resource management Input/output – Read, write, and control operations for initiating and supervising the transfer of data between I/O devices and main memory or registers.

Main Topics in the Lecture Process and thread management Interrupt and trap handling Resource management Input/output Main topics

Process Creation Hierarchy Kernel psps psps p1p1 p1p1 pnpn pnpn q1q1 q1q1 qmqm qmqm... OS process user 1 login pjpj pjpj user j loginuser n login user processes application processes. child processes Interaction with kernel objects

Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Queue Structures

Queues OS needs many different queues – e.g., ready queues, wait queues. Single-level queues – Implemented as array Fixed size Efficient for simple FIFO operations – Implemented as linked list Unbounded size More overhead, but more flexible operations

Single-Level Queues Circular Array Implementation Link List Implementation

Priority Queues Array indexed by priority

Priority Queues Binary heap of priority

Priority Queues Binary 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

Memory Address Spaces Virtual Memory Lowest Address, e.g., Highest Address, e.g., FFFFFFFF

Memory Address Spaces Virtual Memory Lowest Address, e.g., Highest Address, e.g., FFFFFFFF OS User Programs Starting Address of all processes

Processes OS User Programs User Programs Only one process can be activated at a time. OS Process 1 OS Process 2 OS Process n Each process thinks that it owns all memory. Their address spaces are different.

Context Switching OS User Programs User Programs Only one process can be activated at a time. OS Process 1 OS Process 2 OS Process n Each process thinks that it owns all memory. Context Switching Context Switching

Context Switching OS User Programs User Programs Only one process can be activated at a time. OS Process 1 OS Process 2 OS Process n 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 User Programs OS Process 1 OS Process 2 OS Process n Threads Each process can have multiple threads. They share the same address space. The context switching among threads in the process is efficient. Lightweight process  Mesa

Processes and Threads

Process has one or more threads All threads in a process share: – Memory space – Other resources Each thread has its own: – CPU state (registers, program counter) – Stack Threads are efficient, but lack protection from each other

OS Support for Processes/Threads Create a new Process/threadProcessthread Initiate or make a thread ready Destroy or terminate a thread Delay or put a thread to sleep for a given amount of time Synchronize threads through semaphore, events, or condition variables Perform lower-level operations, such as blocking, suspending, or scheduling a thread. Microsoft Windows Process & Thread Functions

Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Implementing Processes and Threads

Process and Thread Descriptors System needs some data structures to keep track the state and miscellaneous information, e.g., identification, resources used, accounting information, of processes and treads. In the following, we are dealing with a system composed solely of processes, much of concept will also apply to threads.

Process Control Block (PCB)

Process 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. 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 Memory map information. Physical Memory  Virtual Memory

Status

running ready blocked Point to the list, e.g., ready list or wait list, on which the process may reside.

More on Status Basic process status – running, ready, and blocked State transition diagram ready blocked running Scheduler Request Release Create

Process Activation and Suspension Some applications require a process (or thread) can be suspended by programs. For examples – Suspension of a debugging program – Needed by the internal, e.g., to detect or prevent a deadlock. Suspend Thread Resume Thread

The Finer State Transition Diagram

Active Processes Suspended Processes

Creation Tree

Point to the PCB of the parent process. A link list of PCBs of the child processes

Priority Used by scheduler to decide which process should be running next. Two methods: Single-integer value Two-leveled valued – Base priority + Changeable part

Priority Two methods: Single-integer value Two-leveled valued – Base priority + Changeable part Windows NT priority classes

Others CPU time used Time remaining Resource used Resource claimed Resource quotas Number of I/O requests since creation...

Processes and Threads (Windows 2000) Process Object Handle Table VAD object Virtual Address Space Descriptors Access Token Thread... Access Token

Windows 2000 (EPROCESS) Executive Process

Kernel Process Block

Processes and Threads (Windows 2000) EPROCESS ETHREAD

Windows 2000 (ETHREAD)

Kernel Thread Block

Windows 2000 Thread States

Implement Operations on Processes Create – Establish a new process Destroy – Remove one or more process Suspend – Change process status to suspended Activate – Change process status to active cobegin/coend forall fork/join/quit......

Implement Operations on Processes Create – Establish a new process Destroy – Remove one or more process Suspend – Change process status to suspended Activate – Change process status to active Operating on PCBs CSs must be cared

Create

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 selfNULL 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? 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(); } 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? We choose not.

Activate Activate(pid) { p = Get_PCB(pid); if (p->Status.Type == ’ready_s’) { p->Status.Type = ’ready_a’; Scheduler(); } else p->Status.Type = ’blocked_a’; } Activate(pid) { p = Get_PCB(pid); if (p->Status.Type == ’ready_s’) { p->Status.Type = ’ready_a’; Scheduler(); } else p->Status.Type = ’blocked_a’; }

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.

Kill_Tree(p) { for (each q in p->Creation_Tree.Child) Kill_Tree(q); if (p->Status.Type == ’running’) { cpu = p->Processor_ID; Interrupt(cpu); } Remove(p->Status.List, p); Release_all(p->Memory); Release_all(p->Other_Resources); Close_all(p->Open_Files); Delete_PCB(p); } Kill_Tree(p) { for (each q in p->Creation_Tree.Child) Kill_Tree(q); if (p->Status.Type == ’running’) { cpu = p->Processor_ID; Interrupt(cpu); } Remove(p->Status.List, p); Release_all(p->Memory); Release_all(p->Other_Resources); Close_all(p->Open_Files); Delete_PCB(p); } Destroy subroutine Kill_Tree ( ) killed Marked the corresponding cpu free.

Kill_Tree(p) { for (each q in p->Creation_Tree.Child) Kill_Tree(q); if (p->Status.Type == ’running’) { cpu = p->Processor_ID; Interrupt(cpu); } Remove(p->Status.List, p); Release_all(p->Memory); Release_all(p->Other_Resources); Close_all(p->Open_Files); Delete_PCB(p); } Kill_Tree(p) { for (each q in p->Creation_Tree.Child) Kill_Tree(q); if (p->Status.Type == ’running’) { cpu = p->Processor_ID; Interrupt(cpu); } Remove(p->Status.List, p); Release_all(p->Memory); Release_all(p->Other_Resources); Close_all(p->Open_Files); Delete_PCB(p); } Destroy subroutine Kill_Tree ( ) killed

Destroy Destroy ( ) Destroy(pid) { p = Get_PCB(pid); Kill_Tree(p); Scheduler(); } 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. Request(res) { if (Free(res)) Allocate(res, self) else { Block(self, res); Scheduler(); } } Release(res) { Deallocate(res, self); if (Process_Blocked_in(res,pr)) { Allocate(res, pr); Unblock(pr, res); Scheduler(); } }

Implementing Semaphores/Locks CPU usually doesn’t support P and V operations directly. Test-and-Set instruction  supported mostly – It atomically tests and modifies the contents of a 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.

The Story (80x86) Memory

The Story (80x86) Memory Access resource Access resource

The Story (80x86) Memory Access resource Access resource Lock XCHG

The Story (80x86) Memory Access resource Access resource

The Story (80x86) Memory Access resource Access resource Lock XCHG The spin locks

The Story (80x86) Memory Access resource Access resource Lock XCHG Job Done The spin locks

The Story (80x86) Memory Access resource Access resource Job Done The spin locks

The Story (80x86) Memory Access resource Access resource Job Done Lock XCHG The spin locks

Test-and-Set CPU Instruction TS(R, X) A CPU Register A Memory Location (Lock Value) R = X; X = 0; Read (test) lock value Lock (set) the lock 0: locked is locked 1: locked is unlocked Returns the value in R. The lock is always locked after being called. Indivisible Atomic

Spin Locks on Binary Semaphore Request( res ) { if (Free(res)) Allocate(res, self) else { Block(self, res); Scheduler(); } Release( res ) { Deallocate(res, self); if (Process_Blocked_in(res,pr)) { Allocate(res, pr); Unblock(pr, res); Scheduler(); } sb  {0, 1}

Spin Locks on Binary Semaphore Request( res ) { if (Free(res)) Allocate(res, self) else { Block(self, res); Scheduler(); } } Release( res ) { Deallocate(res, self); if (Process_Blocked_in(res,pr)) { Allocate(res, pr); Unblock(pr, res); Scheduler(); } } sb  {0, 1} Pb Vb Sb Sb==1 Sb=1; Sb=0; wait until Sb==1

Spin Locks on Binary Semaphore sb  {0, 1} Pb(Sb) { do TS(R, Sb) while(!R); /* wait loop */ } Vb(Sb) { Sb=1; }

Spin Locks on Binary Semaphore sb  {0, 1} Pb(Sb) { do TS(R, Sb) while(!R); /* wait loop */ } Vb(Sb) { Sb=1; }

General Semaphores w/ Busy Wait P(s) { Inhibit_Interrupts; Pb(mutex_s); s = s-1; if (s < 0) { Vb(mutex_s); Enable_Interrupts; Pb(delay_s); } Vb(mutex_s); Enable_Interrupts; } V(s) { Inhibit_Interrupts; Pb(mutex_s); s = s+1; if (s <= 0) Vb(delay_s); else Vb(mutex_s); Enable_Interrupts; }

Disallowing Preemption High Priority e.g., ISR High Priority e.g., ISR Low Priority Shared Resource Low Priority

High Priority e.g., ISR High Priority e.g., ISR Low Priority Dead Lock Condition Shared Resource Low Priority

High Priority e.g., ISR High Priority e.g., ISR Low Priority Dead Lock Avoidance Shared Resource Windows uses different implementations.

Disallowing Preemption 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; } Disallowed to be preempted by a higher-priority process.

Interrupt Inhibition 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;; }

Uniprocessor 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;; }

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;; }

Avoiding Busy Wait P(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s-1; if (s < 0) { Block(self, Ls); Vb(mutex_s); Enable_Interrupts;; Scheduler(); } else { Vb(mutex_s); Enable_Interrupts; } } V(s) { Inhibit_Interrupts;; Pb(mutex_s); s = s+1; if (s <= 0) { Unblock(q, Ls); Vb(mutex_s); Enable_Interrupts; Scheduler(); } else{ Vb(mutex_s); Enable_Interrupts; }; }

Implementing Monitors (Hoare) Internal Data Condition Variables Procedure 1 Procedure 2 Procedure 3 c.wait c.signal mutually exclusive access for processes Need a mutex, say mutex. Need a semaphore for processes blocked on c, say condsem_c. Need a semaphore for signaling processes, say urgent. condcnt_c : #processes wait on c urgentcnt : #signalers Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0;

Mutual Access of 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 */ }; monitor QueueHandler{ struct Queue queue; condition itemAvail, freenodeAvail; void AddToQueue( int val ) { if ( queue is full ) { freenodeAvail.wait; }... add val to the end of the queue... itemAvail.signal; } /* AddToQueue */ int RemoveFromQueue() { if ( queue is empty ) { itemAvail.wait; }... remove value from queue... freenodeAvail.signal; return value; } /* RemoveFromQueue */ }; Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0;

Mutual Access of 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 */ }; 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); if(urgentcnt) V(urgent); else V(mutex); Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0;

Mutual Access of Processes Procedure_body P(mutex) if(urgentcnt) V(urgent); else V(mutex); Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0;

condcnt_c = condcnt_c + 1; if (urgentcnt) V(urgent); else V(mutex); P(condsem_c); /* wait */ condcnt_c = condcnt_c - 1; wait/signal c.wait: c.signal: Initial values: mutex = 1; condsem_c = 0; urgent = 0; condcnt_c = 0; urgentcnt = 0; if (condcnt_c) { urgentcnt = urgentcnt + 1; V(condsem_c); P(urgent); /* wait */ urgentcnt = urgentcnt - 1; }

Clock and Time Management Why OS needs time? – Performance measurement – Processor Scheduling – Time-Stamping Events e.g., I/O and file system call – Deadlock and other fault detection –

Clock and Time Management Most systems provide hardware – ticker: issues periodic interrupt – countdown timer: issues interrupt after a set number of ticks Build higher-level services with this h/w Wall clock timers – Typical functions: Update_Clock : increment tnow (invoked each time tick) Get_Time : return current time Set_Time(tnew) : set time to tnew – Must maintain monotonicity

Countdown Timer (Alarm Clocks) Processes or threads may need timeout signal at some specified time in the future. Examples: – Delay an amount of time to wait I/O completion – Sleep to hand over CPU time – Block until awakened by timer signal events Typical Function: – Delay(tdel)  block process for tdel time units

Implementation of Delay(tdel) To block process for tdel time units. Implementation using hardware countdown: semaphore delsem; Delay(tdel) { Set_Timer(tdel); /*set hardware timer*/ P(delsem); /*wait for interrupt*/ } Timeout() { /*called at interrupt*/ V(delsem); }

Logical Countdown Timers Implement multiple logical countdown timers using a single hardware timer Functions: – tn = Create_LTimer()  create new timer – Destroy_LTimer(tn) – Set_LTimer(tn, tdel)  logically equivalent to Set_Timer(tdel) How to implement multiple logical timers?

Priority Queue with Absolute Wakeup Times Set_LTimer(tn, tdel) 103 Wall-clock 12 Countdown Hardware Timers p1p1 115 p2p2 135 p3p3 140 p4p4 150 Timer Queue TQ

Priority Queue with Absolute Wakeup Times Set_LTimer(??, 35) 103 Wall-clock 12 Countdown Hardware Timers p1p1 115 p2p2 135 p3p3 140 p4p4 150 Timer Queue TQ p5p5 138 p3p3 140 p4p4 150 p1p1 115 p2p2 135 TQ

Priority Queue with Absolute Wakeup Times 103 Wall-clock 12 Countdown Hardware Timers p5p5 138 p3p3 140 p4p4 150 p1p1 115 p2p2 135 TQ

p2p2 135 Priority Queue with Absolute Wakeup Times Wall-clock Countdown Hardware Timers p5p5 138 p3p3 140 p4p4 150 p2p2 135 TQ

Priority Queue with Time Differences 12 Countdown Hardware Timer p1p1 15 p2p2 20 p3p3 5 p4p4 10 Timer Queue TQ Set_LTimer(tn, tdel)

Priority Queue with Time Differences 12 Countdown Hardware Timer Set_LTimer(??, 35) p5p5 3 p3p3 2 p4p4 10 p1p1 15 p2p2 20 TQ p1p1 15 p2p2 20 p3p3 5 p4p4 10 Timer Queue TQ

Priority Queue with Time Differences 12 Countdown Hardware Timers p5p5 3 p3p3 2 p4p4 10 p1p1 15 p2p2 20 TQ

Priority Queue with Time Differences Countdown Hardware Timers p5p5 3 p3p3 2 p4p4 10 p2p TQ 20

Memory Process q Process p OS Communication Primitives Address space for process q Address space for process p System Space User Space The same physical memory used. Different physical memory used. Assume that the communication processes are in the same machine.

OS Process q Process q Process p Process p Communication Primitives Address space for process q Address space for process p Communication send/receive Communication send/receive send/receive can be blocked or nonblocked.

OS Process q Process q Process p Process p Communication Primitives Address space for process q Address space for process p send(p,m) sbuf receive(q,m) rbuf send/receive can be blocked or nonblocked.

OS Process q Process q Process p Process p Communication Primitives Address space for process q Address space for process p send(p,m)receive(q,m) send/receive can be blocked or nonblocked. sbuf rbuf sbuf rbuf Different address mappings for p and q. How?

send(p,m) OS Process q Process q Process p Process p Communication Primitives Address space for process q Address space for process p send(p,m) send/receive can be blocked or nonblocked. sbuf sbuf’

send(p,m) OS Process q Process q Process p Process p Communication Primitives Address space for process q Address space for process p receive(q,m) send/receive can be blocked or nonblocked. sbuf rbuf sbuf’ rbuf’

send(p,m) OS Process q Process q Process p Process p Communication Primitives Address space for process q Address space for process p receive(q,m) send/receive can be blocked or nonblocked. sbuf rbuf sbuf’ rbuf’

send(p,m) OS Process q Process q Process p Process p Communication Primitives Address space for process q Address space for process p receive(q,m) send/receive can be blocked or nonblocked. sbuf rbuf sbuf’ rbuf’

Communication Primitives Copying through system buffers (Processes in the same machine) Use pool of system buffers

Communication Primitives Use pool of system buffers Copying accross network (Processes in the different machines)

Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads Interrupt Handling

Why Interrupt Handling? Many events occur at an unpredictable time, i.e., asynchronously – Polling is impractical – Transfer of control out of normal process temporarily upon coming of an event and, then, back. To remove the notion of asynchronous events from higher levels of kernel, the OS, and applications – Process abstraction: processes are almost to have independent activities and operating in parallel. – For example, OS and applications don’t deal with I/O completion event directly. The program to serve interrupt is called interrupt handler (IH) or interrupt service routine (ISR).

Types of Interrupt External Interrupts – Generated by hardware – Asynchronous – E.g., I/O completion, time-out, the arrival of message (network card), … Internal Interrupts – Generated by software – Synchronous – E.g., Exceptions (instruction errors), SVC We deal with hardware interrupt in the following.

Interrupt Controller (82C59A)

to CPU Signaled By I/O Devices 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) Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine (ISR). ISR7: IRET ISR7: IRET ISR6: IRET ISR6: IRET ISR5: IRET ISR5: IRET ISR4: IRET ISR4: IRET ISR3: IRET ISR3: IRET ISR2: IRET ISR2: IRET ISR1: IRET ISR1: IRET ISR0: IRET ISR0: IRET Normal Job stack

Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine (ISR). ISR7: IRET ISR7: IRET ISR6: IRET ISR6: IRET ISR5: IRET ISR5: IRET ISR4: IRET ISR4: IRET ISR3: IRET ISR3: IRET ISR2: IRET ISR2: IRET ISR1: IRET ISR1: IRET ISR0: IRET ISR0: IRET Normal Job Put the value of flag register, and program counter (PC) into the stack. PC (*) flag * stack

Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine (ISR). ISR7: IRET ISR7: IRET ISR6: IRET ISR6: IRET ISR5: IRET ISR5: IRET ISR4: IRET ISR4: IRET ISR3: IRET ISR3: IRET ISR2: IRET ISR2: IRET ISR1: IRET ISR1: IRET ISR0: IRET ISR0: IRET Normal Job Read Interrupt Vector flag * ISR3: Push used registers (PUSHA) Pop used registers (POPA) Return from interrupt (IRET) ISR3: Push used registers (PUSHA) Pop used registers (POPA) Return from interrupt (IRET) PC (*) stack

Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine (ISR). ISR7: IRET ISR7: IRET ISR6: IRET ISR6: IRET ISR5: IRET ISR5: IRET ISR4: IRET ISR4: IRET ISR3: IRET ISR3: IRET ISR2: IRET ISR2: IRET ISR1: IRET ISR1: IRET ISR0: IRET ISR0: IRET Normal Job flag * ISR3: Push used registers (PUSHA) Pop used registers (POPA) Return from interrupt (IRET) ISR3: Push used registers (PUSHA) Pop used registers (POPA) Return from interrupt (IRET) PC (*) stack

Interrupt Handling CPU INT Interrupt Controller (e.g., 8259) Interrupt Controller (e.g., 8259) IRQ0 IRQ1 IRQ2 IRQ3 IRQ4 IRQ5 IRQ6 IRQ7 Each interrupt has an interrupt service routine (ISR). ISR7: IRET ISR7: IRET ISR6: IRET ISR6: IRET ISR5: IRET ISR5: IRET ISR4: IRET ISR4: IRET ISR3: IRET ISR3: IRET ISR2: IRET ISR2: IRET ISR1: IRET ISR1: IRET ISR0: IRET ISR0: IRET Normal Job PC (*) stack

Standard Interrupt Handing Sequence 1. Save state of interrupted process/thread 2. Identify interrupt type and invoke IH 3. IH services interrupt 4. Restore state of interrupted process (or of another one if the interrupt for awakening a waiting process)

Done The Typical Sequence for Using a Hardware Device CPU I/O Processor I/O Processor Start I/O Interrupt Do_I/O Interrupt Service Routine

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

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; } }