Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/wait.h>
  3. #include <sys/mman.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include <assert.h>
  10. #include <semaphore.h>
  11. #include <time.h>
  12.  
  13. #define N 5
  14. #define LEFT(x) (((x) - 1 + N)%N)
  15. #define RIGHT(x) (((x) + 1)%N)
  16. #define THINKING 0
  17. #define HUNGRY 1
  18. #define EATING 2
  19.  
  20. /* Die Struktur der im SHM abgelegten Daten */
  21. struct shared_state {
  22. int state[N];
  23. sem_t mutex;
  24. sem_t s[N];
  25. };
  26.  
  27. static struct shared_state *shared;
  28. int error;
  29.  
  30. void msleep(unsigned int ms)
  31. {
  32. struct timespec ts;
  33.  
  34. assert(ms > 0);
  35. assert(ms <= 10000);
  36.  
  37. ts.tv_sec = ms / 1000;
  38. ts.tv_nsec = (ms * 1000000) % 1000000000;
  39. nanosleep(&ts, NULL);
  40. }
  41.  
  42. void think(){
  43. msleep(100);
  44. }
  45.  
  46. void eat(){
  47. msleep(100);
  48. }
  49.  
  50. void test(int i){
  51. if(shared->state[i] == HUNGRY && shared->state[LEFT(i)]!=EATING && shared->state[RIGHT(i)]!=EATING){
  52. shared->state[i] = EATING;
  53. error = sem_post(&shared->s[i]);
  54. if(error != 0){
  55. perror("sem_post\n");
  56. exit(EXIT_FAILURE);
  57. }
  58. }
  59. }
  60.  
  61. void take_forks(int i){
  62. error = sem_wait(&shared->mutex);
  63. if(error != 0){
  64. perror("sem_wait\n");
  65. exit(EXIT_FAILURE);
  66. }
  67.  
  68. shared->state[i] = HUNGRY;
  69. test(i);
  70. error = sem_post(&shared->mutex);
  71. if(error != 0){
  72. perror("sem_post\n");
  73. exit(EXIT_FAILURE);
  74. }
  75. error = sem_wait(&shared->s[i]);
  76. if(error != 0){
  77. perror("sem_wait\n");
  78. exit(EXIT_FAILURE);
  79. }
  80. }
  81.  
  82. void put_forks(int i){
  83. error = sem_wait(&shared->mutex);
  84. if(error != 0){
  85. perror("sem_wait\n");
  86. exit(EXIT_FAILURE);
  87. }
  88. shared->state[i] = THINKING;
  89. test(LEFT(i));
  90. test(RIGHT(i));
  91. error = sem_post(&shared->mutex);
  92. if(error != 0){
  93. perror("sem_post\n");
  94. exit(EXIT_FAILURE);
  95. }
  96. }
  97.  
  98. void philosopher(int i){
  99. for (size_t j = 0; j < 10; j++) {
  100. printf("Philosoph %d denken\n",i);
  101. think();
  102. printf("Philosoph %d take forks\n",i);
  103. take_forks(i);
  104. printf("Philosoph %d essen\n",i);
  105. eat();
  106. printf("Philosoph %d put forks\n",i);
  107. put_forks(i);
  108. }
  109. }
  110.  
  111. int main(int argc, char *argv[])
  112. {
  113. printf("Dinierende Philosophen mit Prozessen ...\n\n");
  114.  
  115. pid_t pids[N];
  116.  
  117. shared = mmap(NULL,sizeof(*shared),PROT_READ | PROT_WRITE,MAP_ANONYMOUS | MAP_SHARED,-1,0);
  118. if(shared == MAP_FAILED){
  119. perror("mmap\n");
  120. exit(EXIT_FAILURE);
  121. }
  122. printf("mmap\n");
  123.  
  124. error = sem_init(&shared->mutex,0,1);
  125. if(error != 0){
  126. perror("sem_init\n");
  127. exit(EXIT_FAILURE);
  128. }
  129.  
  130. for (size_t i = 0; i < N; i++) {
  131. error = sem_init(&shared->s[i],0,1);
  132. if(error != 0){
  133. perror("sem_init\n");
  134. exit(EXIT_FAILURE);
  135. }
  136. }
  137.  
  138. for (size_t i = 0; i < N; i++) {
  139. printf("fork\n");
  140. pids[i] = fork();
  141. if(pids[i] < 0){
  142. perror("fork\n");
  143. exit(EXIT_FAILURE);
  144. } else if(pids[i] == 0){
  145. philosopher(i);
  146. exit(EXIT_SUCCESS);
  147. }
  148. }
  149.  
  150. int status;
  151. pid_t terminated_process;
  152. int n = N;
  153.  
  154. while(n > 0){
  155. terminated_process = wait(&status);
  156. printf("%d\n", terminated_process);
  157. --n;
  158. }
  159. printf("fertig\n");
  160. return EXIT_SUCCESS;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement