Operating Systems Process Synchronization (Ch 6.1 - 6.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

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
Dr. Kalpakis CMSC 421, Operating Systems. Fall Process Synchronization.
Silberschatz, Galvin and Gagne  Operating System Concepts Background Shared-memory solution to bounded-buffer problem allows at most n – 1 items.
Synchronization Principles Gordon College Stephen Brinton.
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
Classical Problems of Concurrency
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)
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Chapter 7: Process Synchronization
CIS 415 Process Synchronization Background The Critical-Section Problem Software Solutions Semaphores Hardware Support Classical Problems of Synchronization.
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.
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
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 Process Synchronization (Ch , )
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.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
Principles of Operating Systems Lecture 6 and 7 - Process Synchronization.
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.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 7 Process Synchronization Slide 1 Chapter 7 Process Synchronization.
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.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
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.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
CSE Operating System Principles Synchronization.
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.
Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 7: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Part II: Process Management Chapter 7 Process Synchronization
Chapter 6: Synchronization Tools
Chapter 6 Process Synchronization
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 F Consider: print spooler –Enter file name in spooler queue –Printer daemon checks queue and prints F “Race conditions” (ugh!) F (Hey, you! Show demo!) letterhw1lab1.c... (empty) AB 678 free9 9

Producer Consumer F Model for cooperating processes F Producer “produces” and item that consumer “consumes” F 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 = 6} {counter = 4} R1 = counter R1 = R1 + 1 R2 = counter R2 = R2 -1 counter = R1 counter = R2 P: C: P: C:

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

First Try: Strict Alternation int turn; /* shared, i or j */ while(1) { while (turn <> i) { /* no-op */} /* critical section */ turn = j /* remainder section */ }

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

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

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

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

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

Synchronization Hardware while(1) { while (Test_and_Set(lock)) { } /* critical section */ lock = false; /* remainder section */ }

Semaphores F Does not require “busy waiting” F 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 */ }

Semaphore Implementation F How do you make sure the signal and the wait operations are atomic?

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

Design Technique: Reducing a Problem to a Special Case F Simple solution not adequate –ex: disabling interrupts F Problem solution requires special case solution –ex: protecting S for semaphores F Simple solution adequate for special case F 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 */

Project 2: Mini Chat F Shared memory F Concurrent processes F Semaphores

Outline F Processes Synchronization (Ch ) –Shared memory  –Hardware  –Semaphores  –Classical Problems –Other methods F Interprocess Communication (Ch 4.6) F Threads (Ch 4.5)

Review F What is “mutual exclusion violation”? –Why do we care? F What is “busy waiting”? F How does a semaphore work?

SOS Semaphore Implementation F Semaphore structure –array in OS –integer id to use in process F AttachSemaphore(key), returns sid F DetachSemaphore(sid) F SignalSemaphore(sid) F WaitSemaphore(sid)

Classical Synchronization Problems F Bounded Buffer F Readers Writers F Dining Philosophers

Dining Philosophers F Phisolophers –Think –Sit –Eat –Think F 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 F Allow at most N-1 to sit at a time F Allow to pick up chopsticks only if both are available F Asymmetric solution (odd L-R, even R-L)

Readers-Writers F Readers only read the content of object F Writers read and write the object F Critical region: –No processes –One or more readers (no writers) –One writer (nothing else) F 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);

“Critical Region” F High-level construct region X do S X is shared variable S is sequence of statements F Compiler says: wait(x-mutex) S signal(x-mutex)

“Critical Region” F Deadlocks still possible: –Process A: region X do region Y do S1; –Process B: region Y do region X do S2; Process A wait(x-mutex)... wait(y-mutex) Process B... wait(y-mutex) wait(x-mutex)... A B X Y “cycle”

Conditional Critical Regions F High-level construct region X when B do S X is shared variable B is boolean expression (based on c.r.) S is sequence of statements

Bounded Buffer Shared: struct record { item pool[MAX]; int count, in, out; }; struct record buffer;

Bounded Buffer Producer region buffer when (count < MAX){ pool[in] = i; /* next item*/ in = in + 1; count = count + 1; }

Bounded Buffer Consumer region buffer when (count > 0){ nextc = pool[out]; out = (out + 1) % n; count = count - 1; }

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

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

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) wait(full); /* add item i */ count = count + 1; if (count == 1) then signal(empty); } item remove () { if (count == 0) then wait(empty); /* remove item into i */ count = count - 1; if (count == N-1) then signal(full); return i; }

Other IPC Synchronization F Sequencers F Path Expressions F Serializers F... F All essentially equivalent in terms of semantics. Can build each other.

Ex: Cond. Crit. Region w/Sem region X when B do S { wait(x-mutex); if (!B) { x-count = x-count + 1; signal(x-mutex); wait(x-delay); /* wakeup loop */ x-count = x-count -1 } /* remainder */

Ex: Wakeup Loop while (!B) { x-temp = x-temp + 1; if (x-temp < x-count) signal(x-delay); else signal(x-mutex); wait(x-delay); }

Ex: Remainder S; if (x-count > 0) { x-temp = 0; signal(x-delay); } else signal(x-mutex);

Trouble? F Monitors and Regions attractive, but... –Not supported by C, C++, Pascal... u semaphores easy to add F Monitors, Semaphores, Regions... –require shared memory –break on multiple CPU (w/own mem) –break distributed systems F Message Passing!

Message Passing F Communicate information from one process to another via primitives: send(dest, &message) receive(source, &message) F Receiver can specify ANY F 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 F Scrambled messages (checksum) F Lost messages (acknowledgements) F Lost acknowledgements (sequence no.) F Process unreachable (down, terminates) F Naming F Authentication F Performance (from copying, message building)