CS307 Operating Systems Project 2: Inter-Process Communication and Synchronization Fan Wu Department of Computer Science and Engineering Shanghai Jiao.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Programming project #2 1 CS502 Spring 2006 Programming Project #2 CS-502 Operating Systems Spring 2006.
CS162B: POSIX Threads Jacob Chan. Objectives ▪ Review on fork() and exec() – Some issues on forking and exec-ing ▪ POSIX Threads ▪ Lab 8.
Introduction to Operating Systems with Dr. Ramamurthy Substitution lecture: Project Tips, IPC Scott Settembre, TA September 21, 2010.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
10/04/2011CS4961 CS4961 Parallel Programming Lecture 12: Advanced Synchronization (Pthreads) Mary Hall October 4, 2011.
Project 2 Data Communication Spring 2010, ICE Stephen Kim, Ph.D.
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Operating Systems Chapter 5 Threads. Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures.
The University of Adelaide, School of Computer Science
CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Review COMPSCI 210 Recitation 7th Dec 2012 Vamsi Thummala.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
ICS 145B -- L. Bic1 Project: Process/Thread Synchronization Textbook: pages ICS 145B L. Bic.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
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.
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.
1 Outline Objective: –Message Passing Administrative details: –Sign up for demo slots on Demo Scheduler –Check for demo location with grader –Submit details.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Thread API Xiaohua Lu Office : CS 3310 Tel. : Office hours: 11-12,3-5 T,TR.
Lecture 5 Barriers and MPI Introduction Topics Barriers Uses implementations MPI Introduction Readings – Semaphore handout dropboxed January 24, 2012 CSCE.
Synchronizing Threads with Semaphores
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
CS307 Operating Systems Process Synchronization Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
CS 838: Pervasive Parallelism Introduction to pthreads Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references.
Department of Computer Science and Software Engineering
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.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
CS 241 Section (03/15/2012). MP6 This MP is simple: – Create a ‘make’ utility.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Semaphores Reference –text: Tanenbaum ch
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Web Server Architecture Client Main Thread for(j=0;j
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Chien-Chung Shen CIS/UD
Outline Objective: Administrative details: Message Passing
Semaphores Reference text: Tanenbaum ch
Chapter 5: Process Synchronization – Part 3
“Language Mechanism for Synchronization”
Chapter 5: Process Synchronization – Part II
Threads Threads.
Process Management Presented By Aditya Gupta Assistant Professor
Threads and Cooperation
Multithreading Tutorial
ICS 143 Principles of Operating Systems
Synchronization and Semaphores
Thread Synchronization including Mutual Exclusion
Semaphores Reference text: Tanenbaum ch
CS 144 Advanced C++ Programming May 7 Class Meeting
“The Little Book on Semaphores” Allen B. Downey
EECE.4810/EECE.5730 Operating Systems
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

CS307 Operating Systems Project 2: Inter-Process Communication and Synchronization Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Fall 2011

1.2Operating Systems Objectives Solve inter-process communication problems during concurrent execution of processes. Use Posix Pthread library for concurrency. Use semaphores.

1.3Operating Systems Enviroment VMWare (optional) Ubuntu Linux (recommended)

1.4Operating Systems What to Submit A“tar” file of your DIRECTORY, containing: Your makefile Any “.cc”, “.c”, and “.h” files Any “readme” or “.pdf” files asked for in the project A text file containing the runs of your programs for each of the project parts “testscript”  Do not submit ALL runs you have done, just the output required to demonstrate a successful (or unsuccessful) run  If you cannot get your program to work, submit a run of whatever you can get to work as you can get partial credit  Copy & paste, or use “>” to redirect output to a file DO NOT SUBMIT your object or executable files, remove them before you pack your directory

1.5Operating Systems How to Submit Remove your “.o” files and executables rm *.o rm basic_server rm basic_client Pack your entire directory (including the directory) tar –cvf Prj2+StudentID.tar project1 Send your Prj2+StudentID.tar file to

1.6Operating Systems For Help? n Teaching Assistant l Zuying Wei  l Mengjie Yu  Instructor Fan Wu   Office: SEIEE Building  Office hours: 1~2PM Mon&Wed

1.7Operating Systems Resources Unix programming process/multi-process.html#%0Ashmem process/multi-process.html#%0Ashmem Posix Thread Programming POSIX Semaphores ds/posixsem.html ds/posixsem.html

1.8Operating Systems Larry digs the holes. Moe places a seed in each hole. Curly then fills the hole up. Moe cannot plant a seed unless at least one empty hole exists. Curly cannot fill a hole unless at least one hole exists in which Moe has planted a seed. If there are MAX unfilled holes, Larry has to wait. There is only one shovel with which both Larry and Curly need to dig and fill the holes, respectively. Problem 2.1 – Stooge Farmers Problem Larry digs holesMoe seedsCurly fills the hole Larry and Curly share a shovel

1.9Operating Systems Problem 2.1 – General Requirement Source file: LarryCurlyMoe.c Executable file: LCM g++ LarryCurlyMoe.c –o LCM –lpthread Run: LCM Maxnum Maxnum: Max number of unfilled holes. Sample run: “LCM 5”

1.10Operating Systems Problem 2.1 – Sample Output >./LCM 3 Maximum number of unfilled holes: 3 Begin run. Larry digs another hole #1. Moe plants a seed in a hole #1. Curly fills a planted hole #1. Larry digs another hole #2. Larry digs another hole #3. Moe plants a seed in a hole #2. Curly fills a planted hole #2. Moe plants a seed in a hole #3. Curly fills a planted hole #3. …… Larry digs another hole #11. Moe plants a seed in a hole #9. Curly fills a planted hole #9. Larry digs another hole #12. Moe plants a seed in a hole #10. Curly fills a planted hole #10. End run.

1.11Operating Systems Problem 2.1 – Functions You Will Need Header: #include sem_t: structure of semaphore sem_t Shovel; sem_init: initialize semaphore sem_init(&Shovel,0,1); // set up a binary semaphore to act as a mutex for the critical section "use of shovel", initialize it to 1 sem_destroy: destroy a semaphore sem_destroy(&Shovel);

1.12Operating Systems Problem 2.1 – Functions You Will Need sem_wait: wait for/block on a semaphore sem_post: signal/post on a semaphore // Have Larry wait until he can use the shovel // [in other words, wait to use the critical resource] sem_wait(&Shovel); // Larry now has the shovel, so dig the hole // [thereby using the critical resource, in this case // it is the global variable “HolesDug”] printf("Larry digs another hole #%i.\n",++HolesDug); // Larry is done with the shovel, so signal on the semaphore // [thereby freeing the critical resource CR and unblocking // a thread/process that is waiting on the CR] sem_post(&Shovel);

1.13Operating Systems Problem 2.1 – Understanding Semaphore sem_wait: decrease the value of the semaphore, and if the value is <0, it will BLOCK the thread sem_post: increase the value of the semaphore, and if the value becomes <= 0, the OS will unblock a blocked thread (a thread who called sem_wait and was blocked)

1.14Operating Systems Problem 2.1 – Basic Program Flow 1. Create 3 functions called “Larry”, “Curly”, “Moe” 2. Create a few semaphores 3. Initialize the semaphores 4. Create 3 threads, run the three functions from step 1 on each of the threads. 5. Run for AT LEAST 100 steps (so have at least 100 holes filled by Curly)

1.15Operating Systems Problem 2.1 – What you want to see... You need to see some interleaving of the functions running on the threads. At first, one thread will execute and dig some holes, then another will seed the holes, then the other will fill the holes Then, given enough time, there should be an interleaving showing a dig, a seed, and a fill

1.16Operating Systems Problem 2.2 – Burger Buddies Problem Cooks, Cashiers, and Customers are each modeled as a thread. Cashiers sleep until a customer is present. A Customer approaching a cashier can start the order process. A Customer cannot order until the cashier is ready. Once the order is placed, a cashier has to get a burger from the rack. If a burger is not available, a cashier must wait until one is made. The cook will always make burgers and place them on the rack. The cook will wait if the rack is full. There are NO synchronization constraints for a cashier presenting food to the customer.

1.17Operating Systems Problem 2.2 – Burger Buddies Problem

1.18Operating Systems Problem 2.2 – Sample Output >./BBC Cooks [2], Cashiers [4], Customers [41] Begin run. Cook [1] make a burger. Cook [2] make a burger. Customer [10] come. Casher [3] accepts an order. Casher [3] take a burger to customor. Customer [19] come. Casher [2] accepts an order. Casher [2] take a burger to customor. Customer [7] come. Casher [3] accepts an order. Casher [3] take a burger to customor. Customer [17] come. Casher [2] accepts an order. Cook [1] make a burger. Casher [2] take a burger to customor. ……

1.19Operating Systems Tips Simulating time passing sleep(rand() % NUM); Assigning ID id_cook[i] = i +1; create_thread (Cook, &id_cook[i]); void *Cook(void *args){ int id = *(int *)args; … }