Operating Systems Process Synchronization (Ch 4.4-4.6, 7.1-7.7)

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Operating Systems Part III: Process Management (Process Synchronization)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Interprocess Communication
Synchronization Principles Gordon College Stephen Brinton.
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
Operating System Inter-Process Communication. IPC F How does one process communicate with another process? –semaphores -- signal notifies waiting process.
Operating Systems Process Synchronization. Too Much Pizza 3:00 3:05 3:10 3:15 3:20 3:25 3:30 Person A Look in fridge. Pizza! Leave for store. Arrive at.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Operating Systems Process Synchronization (Ch 2.3, 2.4)
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Synchronization and IPC 1 CS502 Spring 2006 More on Synchronization Interprocess Communication (IPC) CS-502 Spring 2006.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
CS 3013 & CS 502 Summer 2006 IPC, Synchronization, and Monitors 1 More on Synchronization Interprocess Communication (IPC) CS-3013 & CS-502 Summer 2006.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
Operating Systems Process Synchronization. Too Much Pizza 3:00 3:05 3:10 3:15 3:20 3:25 3:30 Person A Look in fridge. Pizza! Leave for store. Arrive at.
Operating Systems Process Synchronization (Ch )
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating System I Process Synchronization. Too Much Pizza 3:00 3:05 3:10 3:15 3:20 3:25 3:30 Person A Look in fridge. Pizza! Leave for store. Arrive.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CY2003 Computer Systems Lecture 06 Interprocess Communication Monitors.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Principles of Operating Systems Lecture 6 and 7 - Process Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
7.1 Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Process Synchronization
Chapter 5: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Inter-Process Communication
Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
Process Synchronization (Ch , )
Presentation transcript:

Operating Systems Process Synchronization (Ch , )

Too Much Pizza 3:00 3:05 3:10 3:15 3:20 3:25 3:30 Person A Look in fridge. Pizza! Leave for store. Arrive at store. Buy pizza. Arrive home. Put away pizza. Person B Look in fridge. Pizza! Leave for store. Arrive at store. Buy pizza. Arrive home. Put pizza away. Oh no!

Cooperating Processes Consider: print spooler –Enter file name in spooler queue –Printer daemon checks queue and prints “Race conditions” (ugh!) (Hey, you! Show demo!) letterhw1lab1.c... (empty) AB 678 free9 9

Outline Need for synchronization –why? Solutions that require busy waiting –what? Semaphores –what are they? Classical problems –dining philosophers –reader/writers

Producer Consumer Model for cooperating processes Producer “produces” and item that consumer “consumes” Bounded buffer (shared memory) item buffer[MAX]; /* queue */ int counter; /* num items */

Producer item i; /* item produced */ int in; /* put next item */ while (1) { produce an item while (counter == MAX){/*no-op*/} buffer[in] = item; in = (in + 1) % MAX; counter = counter + 1; }

Consumer item i; /* item consumed */ int out; /* take next item */ while (1) { while (counter == 0) {/*no-op*/} item = buffer[out]; out = (out + 1) % MAX; counter = counter - 1; consume the item }

Trouble! {R1 = 5} {R1 = 6} {R2 = 5} {R2 = 4} {counter = 4} {counter = 6} R1 = counter R1 = R1 + 1 R2 = counter R2 = R2 -1 counter = R2 counter = R1 P: C: P:

Critical Section Mutual Exclusion –Only one process inside critical region Progress –No process outside critical region may block other processes wanting in Bounded Waiting –No process should have to wait forever (starvation) Note, no assumptions about speed!

First Try: Strict Alternation int turn; /* shared, id of turn */ while(1) { while (turn <> my_pid) { /* no-op */} /* critical section */ turn = your_pid /* remainder section */ }

Second Try int flag[1]; /* boolean */ while(1) { flag[my_pid] = true; while (flag[your_pid]) { /* no-op */} /* critical section */ flag[my_pid] = false; /* remainder section */ }

int flag[1]; /* boolean */ int turn; while(1) { flag[my_pid] = true; turn = your_pid; while (flag[your_pid] && turn==your_pid){ /* noop */} /* critical section */ flag[my_pid] = false; /* remainder section */ } Third Try: Peterson’s Solution

Multiple-Processes “Bakery Algorithm” Common data structures boolean choosing[n]; int num[n]; Ordering of processes –If same number, can decide “winner”

Multiple-Processes choosing[my_pid] = true; num[my_pid] = max(num[0],num[1] …)+1 choosing[my_pid] = false; for (j=0; j<n; j++) { while(choosing[j]) { } while(num[j]!=0 && (num[j],j)<(num[my_pid],my_pid)){} } /* critical section */ num[my_pid] = 0;

Synchronization Hardware Test-and-Set: returns and modifies atomically int Test_and_Set(int &target) { int temp; temp = target; target = true; return temp; }

Using Test_and_Set while(1) { while (Test_and_Set(lock)) { } /* critical section */ lock = false; /* remainder section */ } All the solutions so far have required “Busy Waiting” … what is that?

Outline Need for synchronization(done) –why? Solutions that require busy waiting(done) –what? Semaphores –what are they? Classical problems –dining philosophers –reader/writers

Semaphores Do not require “busy waiting” Semaphore S (shared, often initially =1) –integer variable –accessed via two (indivisible) atomic operations wait(S): S = S - 1 if S<0 then block(S) signal(S): S = S + 1 if S<=0 then wakeup(S)

Critical Section w/Semaphores semaphore mutex; /* shared */ while(1) { wait(mutex); /* critical section */ signal(mutex); /* remainder section */ } (Hey, you! Show demo!)

Semaphore Implementation Disable interrupts –Why is this not evil? –Multi-processors? Use correct software solution Use special hardware, i.e.- Test-and-Set

Design Technique: Reducing a Problem to a Special Case Simple solution not adequate –ex: disabling interrupts Problem solution requires special case solution –ex: protecting S for semaphores Simple solution adequate for special case Other examples: –name servers, on-line help

Trouble! Process A wait(S) wait(Q) … Process B wait(Q) wait(S) … signal(S) /* cr */ wait(S) /* cr */ wait(S) /* cr */

Classical Synchronization Problems Bounded Buffer Readers Writers Dining Philosophers

Philosophers –Think –Sit –Eat –Think Need 2 chopsticks to eat

Philosopher i: while (1) { /* think… */ wait(chopstick[i]); wait(chopstick[i+1 % 5]); /* eat */ signal(chopstick[i]); signal(chopstick[i+1 % 5]); } Dining Philosophers (Other solutions?)

Other Solutions Allow at most N-1 to sit at a time Allow to pick up chopsticks only if both are available Asymmetric solution (odd L-R, even R-L)

Readers-Writers Readers only read the content of object Writers read and write the object Critical region: –No processes –One or more readers (no writers) –One writer (nothing else) Solutions favor Reader or Writer

Readers-Writers Shared: semaphore mutex, wrt; int readcount; Writer: wait(wrt) /* write stuff */ signal(wrt);

Readers-Writers Reader: wait(mutex); readcount = readcount + 1; if (readcount==1) wait(wrt); signal(mutex); /* read stuff */ wait(mutex); readcount = readcount - 1; if (readcount==0) signal(wrt); signal(mutex);

Monitors High-level construct Collection of: –variables –data structures –functions –Like C++ class One process active inside “Condition” variable –not counters like semaphores

Monitor Producer-Consumer monitor ProducerConsumer { condition full, empty; integer count; /* function prototypes */ void enter(item i); item remove(); } void producer(); void consumer();

Monitor Producer-Consumer void producer() { item i; while (1) { /* produce item i */ ProducerConsumer.enter(i); } void consumer() { item i; while (1) { i = ProducerConsumer.remove(); /* consume item i */ }

Monitor Producer-Consumer void enter (item i) { if (count == N) sleep(full); /* add item i */ count = count + 1; if (count == 1) then wakeup(empty); } item remove () { if (count == 0) then sleep(empty); /* remove item into i */ count = count - 1; if (count == N-1) then wakeup(full); return i; }

Other Process Synchronization Methods Sequencers Path Expressions Serializers... All essentially equivalent in terms of semantics. Can build each other!

Trouble? Monitors and Regions attractive, but... –Not supported by C, C++, Pascal... + semaphores easy to add Monitors, Semaphores, Regions... –require shared memory –break on multiple CPU (w/own mem) –break distributed systems In general, Inter-Process Communication (IPC) –Move towards Message Passing

Inter Process Communication How does one process communicate with another process? Some of the ways: –shared memory – read/write to shared region + shmget(), shmctl() in Unix + Memory mapped files in WinNT/2000 –semaphores - signal notifies waiting process –software interrupts - process notified asynchronously –pipes - unidirectional stream communication –message passing - processes send and receive messages.

Pipes One process writes, 2nd process reads % ls | more Shell: 1create a pipe 2create a process for ls command, setting stdout to write side of pipe 3create a process for more command, setting stdin to read side of pipe Shell ls more 1 stdout 2 3 stdin (Hey, see sample code!)

The Pipe Bounded Buffer –shared buffer (Unix 4096K) –block writes to full pipe –block reads to empty pipe b la h. c \0 write fd read fd

The Pipe Process inherits file descriptors from parent –file descriptor 0 stdin, 1 stdout, 2 stderr Process doesn't know (or care!) when reading from keyboard, file, or process or writing to terminal, file, or process System calls: –read(fd, buffer, nbytes) ( scanf() built on top) –write(fd, buffer, nbytes) ( printf() built on top) –pipe(rgfd) creates a pipe + rgfd array of 2 fd. Read from rgfd[0], write to rgfd[1] (Hey, show sample code!)

Message Passing Communicate information from one process to another via primitives: send(dest, &message) receive(source, &message) Receiver can specify ANY Receiver can block (or not)

Producer-Consumer void Producer() { while (TRUE) { /* produce item */ build_message(&m, item); send(consumer, &m); receive(consumer, &m); /* wait for ack */ }} void Consumer { while(1) { receive(producer, &m); extract_item(&m, &item); send(producer, &m); /* ack */ /* consume item */ }} “Rendezvous”

Consumer Mailbox void Consumer { for (i=0; i<N; i++) send(producer, &m); /* N empties */ while(1) { receive(producer, &m); extract_item(&m, &item); send(producer, &m); /* ack */ /* consume item */ }

New Troubles with Messages?

New Troubles with Message Passing Scrambled messages (checksum) Lost messages (acknowledgements) Lost acknowledgements (sequence no.) Process unreachable (down, terminates) Naming Authentication Performance (from copying, message building)

Software Interrupts Similar to hardware interrupt. Processes interrupt each other (often for system call) Asynchronous! Stops execution then restarts –cntl-C –child process completes –alarm scheduled by the process expires + Unix: SIGALRM from alarm() or setitimer() –resource limit exceeded (disk quota, CPU time...) –programming errors: invalid data, divide by zero

Software Interrupts SendInterrupt(pid, num) –type num to process pid, –kill() in Unix –(NT doesn’t allow signals to processes) HandleInterrupt(num, handler) –type num, use function handler –signal() in Unix –Use exception handler in WinNT/2000 Typical handlers: –ignore –terminate (maybe w/core dump) –user-defined (Hey, show demos!)

Unreliable Signals Before POSIX.1 standard: signal(SIGINT, sig_int);... sig_int() { /* re-establish handler */ signal(SIGINT, sig_int); } Another signal could come before handler re-established!