

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Note: Assignment shall be submitted as a word document with the code and outputs for the corresponding questions. The document shall contain your Roll No and Name on the top.
Now write a C program, using POSIX threads and POSIX mutexes and condition variables, that creates N threads, each of which is supposed to model a Linux user in the above scenario. Your program should accept the two input parameters N and C. N is the number of Linux users and C is described above. You can assume that N is divisible by C. The Linux users are numbered with 1 up to N. Each thread representing a Linux user should call the following functions, which you should also implement, in the specified order:
static void enter_restaurant(...) { // print on stdout that Linux user i is entering the restaurant }
static void eat(...) { // sleep(n); where n is a pseudo-random number between 1 and 3 // print on stdout that Linux user i is starting to eat // sleep(n); where n is a pseudo-random number between 2 and 6 // print on stdout that Linux user i finished eating }
static void exit_restaurant(...) { // print on stdout that Linux user i is leaving the restaurant
Note: Make sure all error situations are handled appropriately. Hint: When testing your program, you can use additional sleep () calls before/after printing on stdout.
Tunbridge. Farmers in the two villages use this bridge to deliver their produce to the neighboring town. The bridge can become deadlocked if both a northbound and a southbound farmer get on the bridge at the same time (Vermont farmers are stubborn and are unable to back up.) Using semaphores, design an algorithm that prevents deadlock. Initially, do not be concerned about starvation (the situation in which northbound farmers prevent southbound farmers from using the bridge, or vice versa).