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?
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c that accepts a list of numbers from the user. There are two threads: Main thread waits for the numbers to be entered by the user and for the second thread to finish sorting. It then asks user whether there are more lists or not. It will do this until the user indicates there are no more lists to be entered, at which point it...
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,...
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. Identify data races in above program and explain why this situation occurred with an example Rewrite the code to avoid data races should use any of the THREE techniques. critical section mutex solution semaphore functions Barriers Read-Write Locks Run program using 1, 2, 4, and 8 threads Comparison study Number of threads Implementation...
Write a C or C++ program that uses pthreads to compute the number of values that...
Write a C or C++ program that uses pthreads to compute the number of values that are evenly divisible by 97 between a specified range of values (INCLUSIVE). The program should accept 3 command-line arguments: low value high value number of threads to create to perform the computation -Specifics for program order of input: 0 9000000000 2 this means 0 to 9 Billion (INCLUSIVE); 2 threads alarm(90); this should be the first executable line of the program to make sure...
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); }
Write a program to implement a distributed chat server using TCP sockets in ‘C’.
Write a program to implement a distributed chat server using TCP sockets in ‘C’.
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT