Classical Synchronization Problems

Slides:



Advertisements
Similar presentations
Ch 7 B.
Advertisements

Producer-Consumer One common situation: Producers and consumers communicate via a buffer.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Classical Problems of Concurrency
1 CS 333 Introduction to Operating Systems Class 5 – Classical IPC Problems Jonathan Walpole Computer Science Portland State University.
CS444/CS544 Operating Systems Synchronization 3/21/2006 Prof. Searleman
CS444/CS544 Operating Systems Classic Synchronization Problems 3/5/2007 Prof. Searleman
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
CS 470/570 Lecture 7 Dot Product Examples Odd-even transposition sort More OpenMP Directives.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
CS Introduction to Operating Systems
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Synchronization II: CPE Operating Systems
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 7: Process Synchronization Background The Critical-Section Problem.
Chapter 3 Process Scheduling Bernard Chen Spring 2007.
Synchronizing Threads with Semaphores
Chapter 2 Chapter 2: Processes & Threads Part 2 Interprocess Communication (IPC) & Synchronization.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION3 OPERATING SYSTEMS.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
7.1 Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors.
Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Producer-Consumer Problem David Monismith cs550 Operating Systems.
Classic problems of Synchronization : Producers/Consumers problem: The producers/ consumers problem may be stated as follows: Given a set of cooperating.
Web Server Architecture Client Main Thread for(j=0;j
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chien-Chung Shen CIS/UD
CS 537 – Introduction to Operating Systems
Instructor: Junfeng Yang
Semaphores Reference text: Tanenbaum ch
Synchronization, Part 2 Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Chapter 5: Process Synchronization – Part II
Classical Synchronization Problems
סמפורים.
Recitation 14: Proxy Lab Part 2
CSCE 513 Computer Architecture
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
BOOM! count is [ ], should be
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
CMPT 300: Operating Systems I
Chapter 30 Condition Variables
Programming with Shared Memory
Synchronization Primitives – Semaphore and Mutex
Programming with Shared Memory
Chapter 7: Synchronization Examples
Programming with Shared Memory - 2 Issues with sharing data
Synchronization.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Semaphores Reference text: Tanenbaum ch
Lecture 12 CV and Semaphores
CSE 451 Section 1/27/2000.
Computer Architecture Multi-threaded Matrix Multiply
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Classical Synchronization Problems CS 537 - Introduction to Operating Systems

Classical Problem 1 Producer-Consumer

#include <stdio.h> #include <semaphore.h> #define SIZE 50 #define NUM_THREADS 5 struct object* buffer; sem_t mutex, empty, full; void nap() { int sleepTime = (int)(5 * random()); sleep(sleepTime); } void enter(struct object item) { empty.down(); mutex.down(); count++; buffer[in] = item; in = (in + 1) % SIZE; mutex.up(); full.up(); struct object remove() { full.down(); count--; struct object item = buffer[out]; out = (out + 1) % SIZE; empty.up(); return item;

void producer() { struct object newObj; for(;;) { nap(); newObj = getObject(); enter(newObj); } void consumer() { struct object item; item = remove(); useItem(item); int main(int argc, char** argv) { pthread_t threads[NUM_THREADS]; int i; sem_init(&mutex, 0, 1); sem_init(&empty, 0, SIZE); sem_init(&full, 0, 0); for(i=0; i<NUM_THREADS; i++) { if(i % 2) pthread_create(&threads[i], NULL, (void*)producer, NULL); else pthread_create(&threads[i], NULL, (void*)consumer, NULL); consumer(); return 0;

Classical Problem 2 Readers-Writers

Readers-Writers Several threads accessing a database Some threads read the database (readers) Some threads write the database (writers) Multiple readers can be accessing database at a time if no writers accessing it Only one thread can be accessing database if it is a writer 2 major variations no reader waits if no writer in database writers can starve all readers wait if a writer is waiting writer has to wait until current reader (or writer) is finished with database readers may starve Many more variations to deal with starvation issues Following example is for the first variant starvation is disregarded

#include <stdio.h> #include <pthreads.h> #define NUM_THREADS 5 sem_t mutex, database; void reader() { while(true) { nap(); startRead(); endRead(); } void writer() { startWrite(); endWrite(); void nap() { int sleepTime = (int)(5 * random()); sleep(sleepTime);

void startRead() { mutex.down(); readerCount++; if(readerCount == 1) { database.down(); } mutex.up(); } void endRead() { readerCount--; if(readerCount == 0) { database.up(); } void startWrite() { database.down(); } void endWrite() { database.up(); } int main() { pthread_t threads[NUM_THREADS]; int i; sem_init(&mutex, 0, 1); sem_init(&database, 0, 1); for(i=0; i<NUM_THREADS; i++) { if(i % 2) pthread_create(&threads[i], NULL, (void*)reader, NULL); else pthread_create(&threads[i], NULL, (void*)writer, NULL); writer(); return 0;