Question

In: Computer Science

Write a Pthreads program 'Dining-Philosophers.c' to implement the classical 'Dining-Philosophers'problem by using Pthreads mutex locks and...

Write a Pthreads program 'Dining-Philosophers.c' to implement the classical 'Dining-Philosophers'problem by using Pthreads mutex locks and condition variables. To implement the program, please followthe hints below.

(a) The detailed 'Dining-Philosophers' problem statement: Refer to Page 71 ~ Page 72 in 'Lecture-08.pptx'

   (b) The introduction to Pthreads mutex locks and condition variables: Refer to Page 17 ~ Page 39 in'Lecture-08.pptx'

   (c) Creating five philosophers, each of which will run as a separate thread. Philosophers alternatebetween thinking and eating:

  • The activities of thinking and eating can be simulated by having the thread sleep for a random periodbetween one and three seconds.

  • When a philosopher wishes to eat, he/she invokes the function: Pickup_Forks (int  Philosopher_ID)

  • When a philosopher finishes eating, he/she invokes the function: Return_Forks (int Philosopher_ID)

There are many possible output sequences. Here is one possible output sequence.

Note: I place the output sequence into two columns here, actually the five on the left column should be placed before the rest five on the right column in the same column.

./Dining-Philosophers

Philosopher 0 is eating Philosopher 1 is eating
Philosopher 2 is eating Philosopher 1 is thinking
Philosopher 2 is thinking Philosopher 3 is thinking
Philosopher 3 is eating Philosopher 4 is eating
Philosopher 0 is thinking Philosopher 4 is thinking

Solutions

Expert Solution

code:

output:


Related Solutions

How could we use Pthreads to find the minimum value in an array without mutex locks?...
How could we use Pthreads to find the minimum value in an array without mutex locks? Would this version be slower? If so, why?
Use any C program without using PTHREADS calculate time taken to execute program. Write same program...
Use any C program without using PTHREADS calculate time taken to execute program. Write same program of question 1 using PTHREADS. Calculate time taken to execute program.(1 mark) Identify data races in above program and explain why this situation occurred with an example (1 mark) Rewrite the code to avoid data races should use any of the THREE techniques.(1.5 marks) please I need the c code.. critical section mutex solution semaphore functions Barriers Read-Write Locks Run program using 1, 2,...
3. Add mutex locks to tprog2.c to achieve synchronization, and screenshot the output tprog2.c #include <stdio.h>...
3. Add mutex locks to tprog2.c to achieve synchronization, and screenshot the output tprog2.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> void* print_i(void *ptr) { printf("1: I am \n"); sleep(1); printf("in i\n"); } void* print_j(void *ptr) { printf("2: I am \n"); sleep(1); printf("in j\n"); } int main() { pthread_t t1,t2; int rc1 = pthread_create(&t1, NULL, print_i, NULL); int rc2 = pthread_create(&t2, NULL, print_j, NULL); exit(0); }
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6,...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of the class is to get the data, store it into a file, perform some operations and display data. For the purpose mentioned above, you should write a template class Directory for storing the data empID(template), empFirstName(string), empLastName(string), empContactNumber(string) and empAddress(string) of each Employee in a file EmployeeDirectory.txt. 1. Write a function Add to write the above mentioned contact details of the employee into EmployeeDirectory.txt....
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT