CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
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 ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
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.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Producer-Consumer One common situation: Producers and consumers communicate via a buffer.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Classic Synchronization Problems
1 Synchronization Coordinating the Activity of Mostly Independent Entities.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
Language Support for Concurrency. 2 Common programming errors Process i P(S) CS P(S) Process j V(S) CS V(S) Process k P(S) CS.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
ISP – 5 th Recitation Mutexes Code example. Mutex Wikipedia definition: Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent.
Classical Synchronization Problems. Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread.
CS470 Lab3 TA Notes. Step 1 This is just a simple program that reads in from the command line the time the program should run.
IPC and Classical Synchronization Problems
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
© 2004, D. J. Foreman 1 Mutual Exclusion © 2004, D. J. Foreman 2  Mutual exclusion ■ Critical sections ■ Primitives  Implementing it  Dekker's alg.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Classical Synchronization Problems. Announcements CS 414 grades and solutions available in CMS soon. –Average 74.3 –High of 95. –Score out of 100 pts.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Nachos Phase 1 Code -Hints and Comments
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
Synchronizing Threads with Semaphores
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
Operating Systems NWEN 301 Lecture 6 More Concurrency.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
CSE 451: Operating Systems Spring 2006 Module 8 Semaphores and Monitors John Zahorjan Allen Center 534.
Chien-Chung Shen CIS/UD
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice: Implementation.
Background on the need for Synchronization
Interprocess Communication (3)
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.
Chapter 5: Process Synchronization
The Critical-Section Problem (Two-Process Solution)
Sarah Diesburg Operating Systems COP 4610
Semaphores and Bounded Buffer
Process Synchronization
CSE 451: Operating Systems Winter Module 8 Semaphores and Monitors
Chapter 6 Synchronization Principles
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Chapter 6: Synchronization Tools
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CIS 720 Lecture 6.
“The Little Book on Semaphores” Allen B. Downey
Steve’s Concurrency Slides
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

CS470 Lab 4 TA Notes

Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer problem – Page 329 You will need to use Full & Empty Buffers Simulate this with an array of size 25

Objective cont. If the current EMPTY slot is k and producer fills it, the the next EMPTY slot will be (k+1)%N where N is the size of the array In this simulation the consumer thread is slower than the producer thread. The producer produces widgets which are represented by an ever increasing widget number.

Objective cont. The consumer consumes a widget by replacing it with an EMPTY indicator (-1)

Sample Output Producer: Produce widget #0 in buffer #0 0|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #0 in buffer #0 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #1 in buffer #1 -1|1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #1 in buffer #1 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #2 in buffer #2 -1|-1|2|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #2 in buffer #2 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|

Output cont. Producer: Produce widget #3 in buffer #3 -1|-1|-1|3|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #3 in buffer #3 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #4 in buffer #4 -1|-1|-1|-1|4|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #4 in buffer #4 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #5 in buffer #5 -1|-1|-1|-1|-1|5|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #6 in buffer #6 -1|-1|-1|-1|-1|5|6|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #5 in buffer #5 -1|-1|-1|-1|-1|-1|6|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|

Shared Variables An integer array (size 25 for our simulation) Two other fields nextEmpty(initial=0) and nextFull(initial=0) Three handles for the synchronization objects: full, empty, and mutex Keep a running flag as in Project 2

CS 470 Lab 4 Program Layout

Overall Layout Producer Function Consumer Function Driver (main)

Producer Say that its running Initialize Widget Number While(runFlag) { Simulate producing by sleeping a random time (0..200) And incrementing the widget number P(empty) /*If there is an empty spot we can add to the buffer*/ P(mutex) /*If the consumer is not in it’s critical section the producer can be in the critical section*/ Store the widget number in the next available empty slot Say what you did Say what the buffer looks like (show the buffer) Update the next available empty position V(mutex) /*Exit critical section*/ V(full) /*Make sure to let the consumer know there is at least one widget*/ } Say terminating Return (DWORD) 0

Consumer Say running While(runFlag) { P(full) /*If there is a Widget we (consumer) can go, wait otherwise*/ P(mutex) /* If the producer is not in its critical section we can go*/ Get the widget number in the next available full slot, And store an empty holder (-1) there Say what you did Say what the buffer looks like (show the buffer) Update the next available full position V(mutex) /*Exit our critical section */ V(empty) /* Let the producer know there is an open spot for a widget */ Simulate consuming by sleeping a random time (0..300) } Say terminating Return (DWORD) 0

Driver Driver Error check input argc (the number of parameters) Create synchronization objects(mutex as a MutexObject, full and empty as SemaphoreObjects) Initialize the buffer record so that nextFull = 0 nextEmpty = 0, And all array positions represent empty(-1) Generate the producer thread Generate the consumer thread Generate and set a waitable timer Wait for the timer’s signal Change the keep running signal to FALSE Wait for thread objects to terminate Clean up by closing handles Say done

2006 addition Program 4: Create a program that has two threads, one that fills a buffer and another that empties it. The rate at which the buffer is filled and emptied is variable. In doing this, one thread might have to wait for the other to process the buffer to continue. General program comments: Define handles globally, there should be a mutex, full, empty, and one for each thread. Define a producer and consumer sleep time, or use random times. Up to you. Producer and consumer methods should be defined as: DWORD WINAPI producer( LPVOID ) DWORD WINAPI consumer( LPVOID ) Main pseudo-code: initialize variables, widgets, random number generator error check run time input create semaphores and mutex set the timer say running create consumer and producer threads wait until timer is done say finishing, set run flag to false close handles

2006 addition Producer pseudo-code: say running initialize widget number while run flag sleep for sleep time or random time wait for empty and mutex store widget in empty slot say what the buffer looks like update next available widget position release mutex and full say terminating

2006 addition Consumer pseudo-code: say running while run flag wait for full and mutex say what you did get widget number from next available full slot, store -1 there say what buffer looks like update next available full position release mutex and empty sleep for sleep time or random time say terminating Here are the msdn links for using semaphores and mutex objects: