210 likes | 332 Views
ITFN 3601 Introduction to Operating Systems. Lecture 5 Inter-Process Communication. Agenda. IPC Basics Semaphores & Mutex Monitors & Barriers Message Passing IPC Problems Dining Philosophers Readers & Writers. IPC Basics. Race Conditions Critical Region One at a Time
E N D
ITFN 3601Introduction to Operating Systems Lecture 5 Inter-Process Communication
Agenda • IPC Basics • Semaphores & Mutex • Monitors & Barriers • Message Passing • IPC Problems • Dining Philosophers • Readers & Writers
IPC Basics • Race Conditions • Critical Region • One at a Time • Speed Independent • Non-Critical Regions Immaterial • Finite Waiting • Solution? • Mutual Exclusion
Race Conditions • Race conditions generally involve one or more processes accessing a shared resource (such a file or variable), where this multiple access has not been properly controlled • Race conditions appear in three situations: • implicit calls to schedule from within a function • blocking operations • access to data shared by interrupt code and system calls
Definition of Critical Sections • The overlapping portion of each process, where the shared variables are being accessed. Mutual Exclusion – if Pi is executing in one of its critical sections, noPj, i ≠ j, is executing in its critical sections
Critical Regions • No two processes may be simultaneously inside their critical regions • No assumptions may be made about speeds or the number of CPUs • No process running outside its critical region may block another process • No process should have to wait forever to enter its critical region
Mutual Exclusion • Busy Waiting or Spin Lock • Priority inversion • Producer-Consumer Problem
Basic Mutual Exclusion • Disable Interrupts (BAD!) • Only works for one processor • User must remember to re-enable interrupts • Lock variable (GETTING CLOSER) • How do you lock the lock? • Lockstep Alternation (ALMOST) • Violates 3rd rule of critical regions
Basic Mutual Exclusion (cont.) • “Interested” Flags • Test & Set • Atomic Action • Read Value to see locked • Automatically set to locked, just in case • Sleep & Wakeup
Semaphores • Tracks number of Sleeps & Wakeups • Down & Up • Historically P & V – Dutch (Thanks, Dijkstra!) • Special Cases • Down from a 0 • Process or Thread Sleeps until a new Wakeup • Up from 0 • Random Sleeper for this Wakeup is Triggered
Binary Semaphores • Mutex • Like a Standard Lock • Atomicity provides self-locking • Idle Wait • If locked, thread yields • Simpler Semaphores • Fundamental Mutual Exclusion Tool
Monitors & Barriers • Monitors • Compiler based mutual exclusion • Condition Variables • Wait • Signal • Barriers • Forced resynchronization of processes
Networked Critical Region? • How do you enforce Critical Regions across a network? • Message Passing • Each signal is explicitly given a destination • Authentication? • Acknowledgement of Receipt?
IPC (Dining Philosophers) • Multiple processes attempting to obtain multiple resources • Not enough resources to satisfy all requests at all times (Wait necessary) • Why is this relevant?
Philosopher Resolution? • Patterned locking? • Take left, then take right? • Locking the entire ‘find fork’ area? • Steal your forks while no one else can • State Array? • Track who has forks, who is waiting
Readers & Writers • Multiple readers can co-exist • Two people can read the same page • Multiple writers cannot • Two people cannot write the same page, they will forever be getting in the way • Writers and readers cannot co-exist • Cannot read what is not written
Readers & Writers (cont) • Reader on Reader • Allow it in • Reader on Writer • Writer on Writer • Must wait for Writer to finish • Writer on Reader • Must wait for Reader to finish
Summary • Coordination between processes how? • Locking & Sleeping • TSL Command • Counting Wakeups • Monitors • Networked Coordination? • Message Passing • Barriers