fork download
  1. #include <stdio.h>
  2. #include <unistd.h> // For sleep function
  3.  
  4. void philosopher_activity(int id) {
  5. printf("Philosopher %d has entered the room\n", id);
  6. printf("Philosopher %d is eating\n", id);
  7. sleep(1); // Simulates eating
  8. printf("Philosopher %d has left the room\n", id);
  9. }
  10.  
  11. int main() {
  12. int i;
  13.  
  14. // Philosopher 0 enters, eats, and leaves first
  15. philosopher_activity(0);
  16.  
  17. // Other philosophers enter but do not eat immediately
  18. for (i = 1; i <= 3; i++) {
  19. printf("Philosopher %d has entered the room\n", i);
  20. }
  21.  
  22. // Philosopher 1 eats and leaves
  23. philosopher_activity(1);
  24.  
  25. // Philosopher 2 eats and leaves
  26. philosopher_activity(2);
  27.  
  28. // Philosopher 3 eats and leaves
  29. philosopher_activity(3);
  30.  
  31. // Philosopher 4 enters, eats, and leaves
  32. philosopher_activity(4);
  33.  
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Philosopher 0 has entered the room
Philosopher 0 is eating
Philosopher 0 has left the room
Philosopher 1 has entered the room
Philosopher 2 has entered the room
Philosopher 3 has entered the room
Philosopher 1 has entered the room
Philosopher 1 is eating
Philosopher 1 has left the room
Philosopher 2 has entered the room
Philosopher 2 is eating
Philosopher 2 has left the room
Philosopher 3 has entered the room
Philosopher 3 is eating
Philosopher 3 has left the room
Philosopher 4 has entered the room
Philosopher 4 is eating
Philosopher 4 has left the room