Lab06 Instructions
User Manual:
Open the PDF directly: View PDF .
Page Count: 4
Download | |
Open PDF In Browser | View PDF |
2/26/2019 Quiz: Lab 06 Lab 06 Started: Feb 26 at 12:35pm Quiz Instruc ons Question 1 100 pts Task Descrip on Implement a solution to the Dining Philosophers problem using the pthread library's mutex and conditional variable functions. Recall the pseudocode from the lecture notes: monitor DiningPhilisophers { enum { THINKING; HUNGRY; EATING} state[5]; condition self[5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait(); } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); } void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal(); } } initialization_code() https://cilearn.csuci.edu/courses/6978/quizzes/15478/take/questions/239479 1/4 2/26/2019 Quiz: Lab 06 { for (int i = 0; i < 5; i++) state[i] = THINKING; } } Implementa on The content of the archive file philoBase.zip is a naive implementation of the dining philosophers paradigm. Analyze the code. Do you see any problems with the code? Compile and run the code a number of times. At some point the program will hang. If it does not, then experiment with some larger values for the number of seats and the number of rounds. Add the following line between the locking requests for chopsticks: usleep(DELAY*2); Add a similar line between the calls to return the chopsticks: usleep(DELAY*4); Experiment with the length of the delay. Can you explain why the program gets stuck? One way to fix the problem is to utilize pthread_mutex_trylock() with spinning. That implies using the approach in which picking both chopsticks is attempted, but if only one is available, then the other must be returned; no deadlocks will occur occurs. Try it out. A better approach is to use monitors. C does not provide monitors, so you will implement them. A monitor synchronizes execution of its member functions, so only one function can be executing at any given time. The same effect can be achieved with pthread mutexes. The following is a pseudocode that adds a C function to a virtual monitor defined by a mutex: function() { wait(monitor_mutex); ... // body of the function ... signal(monitor_mutex); https://cilearn.csuci.edu/courses/6978/quizzes/15478/take/questions/239479 2/4 2/26/2019 Quiz: Lab 06 } You will need just one monitor for this assignment, so just one mutex is sufficient for all functions. Note that a conditional variable must be associated with a mutex. It will be an error to call wait or signal on a conditional variable from outside of a critical section protected by the associated mutex. Also, you can allocate a mutex statically in the following way: pthread_mutex_t monitor_mutex = PTHREAD_MUTEX_INITIALIZER; Note furthermore, that trying to lock the same lock twice blocks the thread. With the pickup() and putdown() functions synchronized with a monitor, each philosopher is a thread using the following template: ... pickup(i); // EAT putdown(i); ... Generate a random number and use it in a delay in lieu of the eating time. NOTE: Your implementation MUST follow this guideline. Submission You must submit the following: the signed source code, the MakeLists.txt file with which you built your application, and multiple scripts of tests that confirm that your program does not lock under any condition. Upload Choose a File https://cilearn.csuci.edu/courses/6978/quizzes/15478/take/questions/239479 3/4 2/26/2019 Quiz: Lab 06 Previous Not saved https://cilearn.csuci.edu/courses/6978/quizzes/15478/take/questions/239479 Submit Quiz 4/4
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.4 Linearized : No Page Count : 4 Creator : Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36 Producer : Skia/PDF m72 Create Date : 2019:02:27 02:46:41+00:00 Modify Date : 2019:02:27 02:46:41+00:00EXIF Metadata provided by EXIF.tools