Download presentation
Presentation is loading. Please wait.
Published byClare Gibbs Modified over 8 years ago
1
4330/6310 THIRD ASSIGNMENT Spring 2015
2
The problem (I) A post office with a single queue and several nameless clerks: Clerk 0 Customers waiting Clerk 1 …
3
The problem (II) Can represent the n clerks by a single semaphore post_office Its initial value will be the total number of clerks helping customers Its current value will indicate the number of idle clerks
4
Your program (I) Main program will Read a line with a customer's name, arrival_delay, and service_time Sleep for arrival_delay seconds Fork a child Wait until all children have terminated Print statistics
5
Your program (II) Your child processes will Print a message Do a P() on the post_office semaphore Print a message Sleep for service_time seconds Print a message Do a V() on the post_office semaphore Terminate by doing _exit(0);
6
Collecting statistics Before terminating, your program should display: The total number of customers that got serviced Can be maintained by the main program The number of customers that did not have to wait Will require some extra coding The number of customers that had to wait Trivial
7
Knowing if a customer had to wait A customer process will wait if all clerks were busy when it arrived at the post office Easiest way is to check the value of the semaphore post office sem_getvalue(mysem,&value);
8
Customer processes revisited (I) Your child processes will Print a message Get the current value of the post_office If all clerks are busy increment the number of customers who had to wait Do a P() on the post_office semaphore Print a message Sleep for service_time seconds Print a message Do a V() on the post office semaphore Terminate by doing _exit(0);
9
Using a shared variable The number of customers that have to wait is A global variable shared by all your processes Must be stored in a shared memory segment Must be accessed in mutual exclusion Add a second semaphore ("mutex") to your program
10
Customer processes revisited (II) Your child processes will Print a message Get the current value of the post_office semaphore If all clerks are busy then Do a P() on a mutex Increment the number of customers who had to wait Do a V() on a mutex Do a P() on the post_office semaphore Print a message Sleep for service_time seconds Print a message Do a V() on the post office semaphore Terminate by doing _exit(0);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.