0% found this document useful (0 votes)
4 views1 page

lab_synchronization (1)

lab

Uploaded by

ajbull04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

lab_synchronization (1)

lab

Uploaded by

ajbull04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Lab assignment: Synchronization between threads

Part 1: The dinining philosophers problem


Implement two solutions to the dining philosophers problem, the first one using mutexes and the second one relying on
semaphores. The implementation with each mechanism should be included in two separate C files –philosophers_mtx.c
and philosophers_sem.c–, each with its own main() function. Note that a philosophers.c file is provided as supplemen-
tary material. This file must be used as a starting point for both solutions.
In this problem, each philosopher is emulated by means of a separate thread. Hence, the program will feature 5 threads,
created with pthread_create(). For simplicity in the implementation, a number (0..4) is assigned to each fork and
philosopher.
Each philosopher repeatedly thinks and eats. Specifically, thinking is simulated via the sleep() or usleep()
function (check out the associated man page for details) with a random time period. The philosopher must grab its left
and right fork (not necessarily in that order to avoid deadlocks. . . ). After eating, the philosopher will put the forks back
on the table, and will then sleep for some time before thinking again. In implementing the program, grabbing a fork will
be done by acquiring a mutex or invoking the wait() operation on a semaphore. Similarly, putting a fork back on the
table comes down to releasing a mutex or invoking a semaphore’s signal() operation.

Part 2: The hungry students


Write a concurrent program that simulates the behavior of a set of hungry students eating sandwiches served in a
Faculty’s cafeteria. The solution must make use of a mutex, a number of condition variables and other shared variables
to impose the necessary synchronization between the students and a cook, which are represented as separate threads of
the concurrent program.
Sandwiches of the following types are served in the cafeteria: (0) ham and (1) omelette. When a student wants to have a
sandwich of a certain type, he/she must go and pick it up at the bar. If at that moment sandwiches of the chosen type are
available, one will be served to the student. Otherwise, the student must warn the cook, who remains asleep (blocked)
unless otherwise noted. When awakened by the student, the cook will check what kind of sandwiches ran out, and will
prepare N sandwiches of each missing type so that they become available again; then the cook will go to sleep. After
that, the student will have a chance to get the sandwich and eat.
An arbitrary number of students and the cook behave as follows:
void Student(int sandwichType){
while (true) {
getSandwich(sandwichType);
eat();
}
}

void Cook(){
while (true) {
serveSandwiches();
}
}

A sandwiches.c file is provided as supplementary material to be used as a starting point for the implementation. Pay
attention to the comments found inside that file.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy