Announcement server: server.o common.o $(CC) -o server server.o common.o $(LIBS) -lsock -lpthread and change it to: server: server.o common.o threadpool.o.

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: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Operating Systems Mehdi Naghavi Winter 1385.
HW/Study Guide. Synchronization Make sure you understand the HW problems!
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Prepared By Sarath S Menon S6 CSE.  Imagine a scenario in which there exists two Distinct processes both operating on a single shared data area.  One.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 2 Processes and Threads
Interprocess Communication
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Classic Synchronization Problems.
Classic Synchronization Problems
Review: Producer-Consumer using Semaphores #define N 100// number of slots in the buffer Semaphore mutex = 1;// controls access to critical region Semaphore.
1 CS 333 Introduction to Operating Systems Class 5 – Classical IPC Problems Jonathan Walpole Computer Science Portland State University.
1 CS 333 Introduction to Operating Systems Class 5 – Semaphores and Classical Synchronization Problems Jonathan Walpole Computer Science Portland State.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
Pthread Synchronization Operating Systems Hebrew University Spring 2004.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
1 Sleeping Barber Problem There is one barber, and n chairs for waiting customers If there are no customers, then the barber sits in his chair and sleeps.
18/02/08Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Multithreaded Web Server.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Outline Monitors Barrier synchronization The sleeping barber problem
Operating Systems Review for Chap.6 & 7 Hung Q. Ngo KyungHee University Spring 2009
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Process Synchronization I Nov 27, 2007 CPE Operating Systems
Thread Synchronization Tutorial #8 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Synchronization, part 3 Monitors, classical sync. problems
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Homework Assignment #2 J. H. Wang Nov. 1, Homework #2 Chap.5: 5.8, 5.19, 5.22 Chap.6: 6.11, 6.12, *6.35 (Optional: End-of-chapter project for Chap.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CSC321 Concurrent Programming: §4 Semaphores 1 Section 4 Semaphores.
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.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Semaphores Reference –text: Tanenbaum ch
Process Synchronization I CPE Operating Systems
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice: Implementation.
Dining Philosophers Five philosophers sit around a table
Semaphores Reference text: Tanenbaum ch
Synchronization, part 3 Monitors, classical sync. problems
Today’s topics Project 2 - Work on CEREAL - Due: Oct 10, :00 pm
                                                                                                                                                                 
Lecture 13: Producer-Consumer and Semaphores
The Producer-Consumer Problem OR, The Bounded-Buffer Problem
Chapter 5: Process Synchronization
Principles of Software Development
Process Synchronization
Synchronization, part 3 Monitors, classical sync. problems
CS399 New Beginnings Jonathan Walpole.
Critical section problem
Barbershop Example We can simulate a barbershop using semaphores.
CS510 Operating System Foundations
Lecture 13: Producer-Consumer and Semaphores
CMPE 135: Object-Oriented Analysis and Design April 30 Class Meeting
Semaphores Reference text: Tanenbaum ch
CS 144 Advanced C++ Programming May 7 Class Meeting
Synchronization, part 3 Monitors, classical sync. problems
Presentation transcript:

Announcement server: server.o common.o $(CC) -o server server.o common.o $(LIBS) -lsock -lpthread and change it to: server: server.o common.o threadpool.o $(CC) -o server server.o common.o threadpool.o $(LIBS) -lsock - pthread

Today’s topics Project 2 How to use condition variables in Pthreads. Classical Synchronization Problems Sample Questions

Using Condition Variable THREAD A .. lock(associated_mutex) If (waiting_condition ) pthread_cond_wait(con_var, associated_mutex) Unlock (associated_mutex ) THREAD B .. lock (associated_mutex ) If (signal_condition) signal(con_var); Unlock(associated_mutex );

How to solve a synchorization problem by using con_var How many condition variables do we need? When to make a thread sleep? When to signal a sleeping thread? How many associated mutexes do we need? The waiting_condition & signal condition. Insert code for wait() & signal(). Insert code for lock(mutex) & unlock(mutex). Verify & check for deadlock, race condition ..

How to solve a synchorization problem by using semaphore?? How many semaphore do we need? When to make a thread sleep? When to signal a sleeping thread? How many mutexes(binary semaphore) do we need? The waiting_condition & signal condition. Insert code for up() & down(). Insert code for down(mutex_sem) & up(mutex_sem) Verify & check for deadlock, race condition ..

procedure producer() { while (true) { item = produceItem() if (itemCount == BUFFER_SIZE) {sleep() } putItemIntoBuffer(item) itemCount = itemCount + 1 if (itemCount == 1) { wakeup(consumer) } } procedure consumer() { if (itemCount == 0) { sleep() } item = removeItemFromBuffer() itemCount = itemCount – 1 if (itemCount == BUFFER_SIZE - 1) { wakeup(producer) } consumeItem(item) } } semaphore fillCount = 0 semaphore emptyCount = BUFFER_SIZE procedure producer() { while (true) { item = produceItem() down(emptyCount) putItemIntoBuffer(item) up(fillCount) } } procedure consumer() { down(fillCount) item = removeItemFromBuffer() up(emptyCount) consumeItem(item)

Using cond_var for SleepingBarber con_var SleepingBarber, waitingCustomers; mutex accessSeats; int NumOfFreeSeats=MAX; procedure Barber() { while (true) { lock(accessSeats); if (NumOfFreeSeats == MAX) _wait(SleepingBarber, accessSeats); signal (waitingCustomers); NumOfFreeSeats++; unlock(accessSeats); } procedure Custommer() { lock(accessSeats); if (NumOfFreeSeats == 0) { unlock(accessBuff); //Leave withou haircut } else { if (NumOfFreeSeats == MAX) _signal(SleepingBarber); NumOfFreeSeats --; wait (waitingCustomers, accessSeats); unlock(accessSeats);

Using semaphore for SleepingBarber + Semaphore Customers = 0 + Semaphore Barber = 0 + Semaphore accessSeats (mutex) = 1 + int NumberOfFreeSeats = N //total number of seats BARBER while(true) { down(Customers) // down(accessSeats) NumberOfFreeSeats++ up(Barber); up(accessSeats) //we don't need the lock on the chairs anymore //here the barber is cutting hair } procedure Custommer() { down(accessSeats) //tries to get access to the chairs if ( NumberOfFreeSeats > 0 ) { NumberOfFreeSeats-- up(Customers) up(accessSeats) down(Barber) //now it's this customers turn, but wait if the barber is busy //here the customer is having his hair cut } else { //there are no free seats up(accessSeats) //customer leaves without a haircut }