Question: Task 5: Implement semaphores (30 marks) Copy your code from Task 4 into a new directory named task5. Stage 1 Replace the use of busy

Task 5: Implement semaphores (30 marks) Copy your code from Task 4 into a new directory named task5. Stage 1 Replace the use of busy waiting with semaphores to achieve mutual exclusion and synchronise message passing between the producer and consumer. As with Task 1, you will need to research the Linux system calls necessary to accomplish this. The semget function is a good starting point.

Your producer should now initially sleep for 1 second (1000 ms) between writing messages. Continue adjusting the sleep interval based on the message content as described in Task 4. Sleep intervals less than 800 ms are now valid. A value of 0 ms indicates the producer should not sleep between writing messages to the shared memory segment.

You will need to consider how the consumer can be congured to only read a message when it is ready and how the producer can be congured to not overwrite a message before the consumer has had chance to read it.

Stage 2 As before, use top to observe the utilisation of the producer and consumer for a variety of producer sleep intervals (as controlled by the message content).

Does the implementation of semaphores make a dierence to utilisation? Discuss your observations in your report and compare with previous tasks results.

Hint: Think about how many semaphores you would need to achieve mutual exclusion and synchronisation between the producer and consumer. Refer back to the concurrency lecture material from week 4 if you need to refresh your memory on these concepts.

#task 4 code

#define _REENTRANT #include #include #include #include #include // The maximum number of customer threads. #define MAX_CUSTOMERS 25 // Function prototypes... void *customer(void *num); void *barber(void *); void randwait(int secs); // Define the semaphores. // waitingRoom Limits the # of customers allowed // to enter the waiting room at one time. sem_t waitingRoom; // barberChair ensures mutually exclusive access to // the barber chair. sem_t barberChair; // barberPillow is used to allow the barber to sleep // until a customer arrives. sem_t barberPillow; // seatBelt is used to make the customer to wait until // the barber is done cutting his/her hair. sem_t seatBelt; // Flag to stop the barber thread when all customers // have been serviced. int allDone = 0; int main(int argc, char *argv[]) { pthread_t btid; pthread_t tid[MAX_CUSTOMERS]; long RandSeed; int i, numCustomers, numChairs; int Number[MAX_CUSTOMERS]; // Check to make sure there are the right number of // command line arguments. if (argc != 4) { printf("Use: SleepBarber "); exit(-1); } // Get the command line arguments and convert them // into integers. numCustomers = atoi(argv[1]); numChairs = atoi(argv[2]); RandSeed = atol(argv[3]); // Make sure the number of threads is less than the number of // customers we can support. if (numCustomers > MAX_CUSTOMERS) { printf("The maximum number of Customers is %d. ", MAX_CUSTOMERS); exit(-1); } printf(" SleepBarber.c "); printf("A solution to the sleeping barber problem using semaphores. "); // Initialize the random number generator with a new seed. srand48(RandSeed); // Initialize the numbers array. for (i=0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!