CS444/CS544 Operating Systems Review: Synchronization 3/9/2007 Prof. Searleman

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Operating Systems Lecture 7.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Ch 7 B.
3. Higher-Level Synchronization 3.1 Shared Memory Methods –Monitors –Protected Types 3.2 Distributed Synchronization/Comm. –Message-Based Communication.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Deadlocks, Message Passing Brief refresh from last week Tore Larsen Oct
Concurrency: mutual exclusion and synchronization Slides are mainly taken from «Operating Systems: Internals and Design Principles”, 8/E William Stallings.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
CS444/CS544 Operating Systems Introduction to Synchronization 2/07/2007 Prof. Searleman
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
A. Frank - P. Weisberg Operating Systems Inter-Process Communication (IPC)
Today From threads to file systems
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
CS444/CS544 Operating Systems Atomic Transactions 3/26/2007 Prof. Searleman
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems 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,
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
CS444/CS544 Operating Systems Deadlock 3/07/2006 Prof. Searleman
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.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Tutorial 2: Homework 1 and Project 1
CSE 120 Principles of Operating
Operating systems Deadlocks.
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Applied Operating System Concepts -
Synchronization, part 3 Monitors, classical sync. problems
PThreads.
Chapter 5: Process Synchronization (Con’t)
Chapter 5: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Operating systems Deadlocks.
The relation between the process and
Chapter 6 Synchronization Principles
Lecture 27 Syed Mansoor Sarwar
EECE.4810/EECE.5730 Operating Systems
CSE 542: Operating Systems
Synchronization CS Spring 2002.
CSE 542: Operating Systems
Presentation transcript:

CS444/CS544 Operating Systems Review: Synchronization 3/9/2007 Prof. Searleman

Generalize to Messaging Synchronization based on data transfer (atomic) across a channel Blocking send/receive: send(destination, &msg); receive(source, &msg); if no message available, receiver could block alternative, if no message, could return with error code Mailbox variant mailbox is a bounded buffer of messages attempt to send to full mailbox blocks thread attempt to read from empty mailbox blocks thread

Bounded-Buffer using Message Passing Synchronization void producer() { message pmsg; while (1) { receive(mayproduce, pmsg); pmsg = produceMsg(); send(mayconsume, pmsg); } } /* end producer */ void consumer() { message cmsg; while (1) { receive(mayconsume, cmsg); consumeMsg(cmsg); send(mayproduce, NULL); } } /* end producer */ void main() { /* create 2 mailboxes, init mayproduce w/ N empty messages */ create_mailbox(mayproduce); create_mailbox(mayconsume); for (i = 0; i<CAPACITY; i++) send(mayproduce, NULL); /* create thread for producer()*/ /* create thread for consumer() */ } #define CAPACITY = /* size of mailbox, N */ #define NULL = /* empty message */ idea: synchronize using shared mailboxes

Other Classic Synchronization Problems Sleeping Barber Traffic lights for two lane road through a one lane tunnel (similar to Vermont Farmers) Monkeys & Gorillas

Monkeys and Gorillas capacity of rope: 12 monkeys or 1 gorilla cliff chasm rope

1.Sharing a single resource multiple processes (of two or more distinct types) sharing a single resource; the order of waking up a waiting process can be random structure of the solution, where t is the type of the process: each process executes request_resource(); use_resource(); release_resource(); ResourceMonitor.request(t); type t process uses the resource ResourceMonitor.release(t);

Reasons for a type t process to wait: 1. condition t1 2. condition t2 … k. condition tk Need to declare and initialize shared variables which represent the current “state” of the system so that you can test those values to determine if a particular process should be blocked Note that one process might have to wait to ensure fairness

monitor ResourceMonitor { /* declare shared variables representing state */ /* declare one condition variable for each waiting queue (i.e. class of process) */ condition aOK; /* type a */ condition bOK; /* type b */ … condition tOK; /* type t */ request (type t) { … } release (type t) { … } /* initialization of shared variables */ } // end ResourceMonitor /* alternatively, could have a separate request/release procedure for each type t */

void request_t() { /* type “t” process is making this request */ if (condition t1 || condition t2 || … || condition tk ) { /* update state – about to block */ tOK.wait(); /* update state – just woke up */ } /* update state – about to use resource */ }

void release_t() { /* type “t” process is releasing the resource */ /* update state – this process is done using the resource */ /* decide if another waiting process should be awakened; if so, who to wake up next */ if (wakeup a waiting, type a process) aOK.signal(); else if (wakeup a waiting, type b process) bOK.signal(); … etc.} }

Monkeys and Gorillas capacity of rope: 12 monkeys or 1 gorilla cliff chasm rope

MESA semantics void request_t() { /* type “t” process */ while ( condition t1 || condition t2 || … || condition tk ) { /* update state – about to block */ tOK.wait(); /* update state – just woke up */ } /* update state – about to use resource */ } void release_t() { /* “t” process is releasing the resource */ /* update state – this process is done using the resource */ /* wake up all waiting processes */ tOK.broadcast(); /* they will awaken within the while loop */ }

2. Sharing a single resource multiple processes sharing a single resource; the order of waking up a waiting process should be first come, first served structure of the solution, where N is the size of the process table, 0 <= pid < N is the unique pid of the process request_resource(); use_resource(); release_resource(); ResourceMonitor2.request(pid); process pid uses the resource ResourceMonitor2.release(pid);

monitor ResourceMonitor2 { /* declare shared variables representing state */ declare a FIFO queue of pids; /* declare one condition variable for each process; N is size of process table; pids range from 0 to N-1 */ condition self[N]; request (pid) { … } release (pid) { … } /* initialization of shared variables */ } // end ResourceMonitor2

void request(int pid) { /* process pid is making this request */ if (condition 1 || condition 2 || … || condition k ) { /* update state – about to block */ add pid to FIFO queue self[pid].wait(); /* update state – just woke up */ } /* update state – about to use resource */ }

void release(int pid) { /* process pid is releasing the resource */ /* update state – this process is done using the resource */ if the queue of pids is not empty { /* the process to wake up next is the first one on the queue */ nextpid = remove from FIFO queue; self[nextpid].signal(); }

Exercise 6.18 Consider a system consisting of processes p1, p2, … pn, each of which has a unique priority number. Write a monitor that allocates three identical line printers to these processes, using the priority numbers for deciding the order of allocation.

Exercise 6.19 A file is to be shared among different processes, each of which has a unique number. The file can be accessed simultaneously by several processes, subject to the following constraint: the sum of all unique numbers associated with all the processes currently accessing the file must be less than N. Write a monitor to control access to the file.

Exercise 6.22 Write a monitor that implements an alarm clock that enables a calling process to delay itself for a specified number of time units (ticks). You may assume the existance of a real hardware clock that invokes a procedure tick() in your monitor at regular intervals.

Exercise 6.19 A file is to be shared among different processes, each of which has a unique number. The file can be accessed simultaneously by several processes, subject to the following constraint: the sum of all unique numbers associated with all the processes currently accessing the file must be less than N. Write a monitor to control access to the file.

Recap: Methods for Handling Deadlock Allow deadlock to occur, but… Ignore it (ostrich algorithm) Detection and recovery Ensure that deadlock never occurs, by… Prevention (negate at least 1 of the 4 necessary conditions for deadlock to occur) Dynamic avoidance (be careful) What are the consequences? May be expensive Constrains how requests for resources can be made Processes must give maximum requests in advance