Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. Name : lab4b.c
  4. Author :
  5. Version :
  6. Copyright : Your copyright notice
  7. Description : Hello World in C, Ansi-style
  8. ============================================================================
  9. */
  10.  
  11. #include <stdio.h> // needed for printf, sprintf, ...
  12. #include <stdlib.h> // needed for exit()
  13. #include <unistd.h> // needed for sleep, fork, waitpid, _exit, ftruncate
  14. #include <sys/wait.h> // needed for waitpid
  15. #include <fcntl.h> // For O_* constants
  16. #include <sys/mman.h> // needed for mmap, munmap and associated constants
  17. #include <semaphore.h>
  18.  
  19. void philosopher(int i);
  20. int parent(void);
  21. void take_forks(int i);
  22. void put_forks(int i);
  23. void test (int i);
  24. void think(int i);
  25. void eat(int i);
  26.  
  27. #define N 5 /* number of philosophers */
  28. #define THINKING 0 /* philosopher is thinking */
  29. #define HUNGRY 1 /* philosopher is trying to get for ks */
  30. #define EATING 2 /* philosopher is eating */
  31. #define FINISH 3
  32. #define LEFT (arga+4)%N
  33. #define RIGHT (arga+1)%N
  34.  
  35. struct shared_memory {
  36. int k;
  37. int state[N];
  38. sem_t s[N];
  39. sem_t mutex;
  40. };
  41.  
  42. struct shared_memory *shared;
  43. int fd_shm, fd_log;
  44.  
  45. int main(void) {
  46.  
  47. if ((fd_shm = shm_open ("/test", O_RDWR | O_CREAT, 0660)) == -1)
  48. printf("error");
  49. if (ftruncate (fd_shm, sizeof (struct shared_memory)) == -1)
  50. printf("error");
  51. if ((shared = mmap (NULL, sizeof (struct shared_memory), PROT_READ | PROT_WRITE, MAP_SHARED,fd_shm, 0)) == MAP_FAILED)
  52. printf("error");
  53.  
  54. // Initialize the shared memory
  55. shared -> k = 0;
  56. for (int i=0; i<N;i++){
  57. shared -> state[i] = 0;
  58. sem_init(&shared->s[i],0,0);
  59. }
  60.  
  61. sem_init(&shared->mutex,1,1);
  62.  
  63. pid_t pids[N];
  64.  
  65. // At the beginning every philosopher is thinking
  66. for (int l = 0; l < N; l++){
  67. shared ->state[l]=THINKING;
  68. }
  69.  
  70. printf("Start");
  71.  
  72. for(int j=0; j< N; j++){
  73.  
  74. if((pids[j] = fork()) < 0){
  75. printf("failed");
  76. perror("fork");
  77. }else if (pids[j] == 0 ){
  78. int pid = getpid();
  79. printf("pids[%d] = %d\n",j,pid);
  80. philosopher(j);
  81. }
  82. }
  83.  
  84. parent();
  85.  
  86. for (int m=0; m<N;m++){
  87. waitpid(pids[m],NULL,0);
  88. }
  89. printf("This is final k %d\n",shared->k);
  90.  
  91. if (munmap(shared, sizeof(struct shared_memory)) == -1) {
  92. printf("Function munmap() failed\n");
  93. exit(-1);
  94. }
  95. if ((shm_unlink("/test")) == -1){
  96. printf("Function shm_unlink() failed\n");
  97. exit(-1);
  98. }
  99. return EXIT_SUCCESS;
  100. }
  101.  
  102. void philosopher(int arga) {
  103. //int count =0;
  104.  
  105. //while(count<5){
  106. while(1){
  107. sleep(1);
  108. //think(i);
  109. take_forks(arga);
  110. sleep(2);
  111. //eat(i);
  112. sleep(1);
  113. put_forks(arga);
  114. //count++;
  115. }
  116. shared->state[arga] = FINISH;
  117. exit(0);
  118. }
  119.  
  120. void take_forks(int arga) /* i: philosopher number, from 0 to N−1 */
  121. {
  122. printf("arga take_fork: %d\n", arga);
  123. sem_wait(&shared->mutex); /* enter critical region, down mutex */
  124. shared->state[arga] = HUNGRY; /* record fact that philosopher i is hungry */
  125. //while (shared->state[i] == HUNGRY){
  126. test(arga); /* try to acquire 2 forks */
  127. sem_post(&shared->mutex); /* exit critical region, up mutex */
  128. sem_wait(&shared->s[arga]); /* block if for ks were not acquired, down philosopher*/
  129. //}
  130.  
  131.  
  132. //sleep(1);
  133. }
  134. void put_forks(int arga) /* i: philosopher number, from 0 to N−1 */
  135. {
  136. printf("arga put_fork: %d\n", arga);
  137. sem_wait(&shared->mutex); /* enter critical region, down mutes */
  138. shared->state[arga] = THINKING; /* philosopher has finished eating */
  139. test(LEFT); /* see if left neighbor can now eat */
  140. test(RIGHT); /* see if right neighbor can now eat */
  141. sem_post(&shared->mutex); /* exit critical region, up mutex */
  142.  
  143.  
  144. }
  145. void test(int arga) /* i: philosopher number, from 0 to N−1 */
  146. {
  147. if (shared->state[arga] == HUNGRY && shared->state[LEFT] != EATING && shared->state[RIGHT] != EATING)
  148. {
  149. //printf("Philosopher: %d (i+N-1)%N - %d", i, ((i+N-1)%N));
  150. shared->state[arga] = EATING;
  151. //sleep(2);
  152. sem_post(&shared->s[arga]); //Up philosopher
  153. }
  154.  
  155. }
  156.  
  157. void think(int arga){
  158. //printf("I am philosopher %d i am thinking\n", i);
  159. sleep(2);
  160. }
  161.  
  162. void eat(int arga){
  163. //printf("I am philosopher %d i am eating\n", i);
  164. sleep(3);
  165. }
  166.  
  167. int parent(void){
  168. puts("This is the Parent Process");
  169. int philosopherState;
  170. char* philprint;
  171. int count=0;
  172.  
  173. while (1){
  174. sleep(1);
  175. for(int o = 0; o<N;o++ ){
  176. philosopherState = shared->state[o];
  177.  
  178. if (philosopherState == 0) {
  179. philprint = "Thinking";
  180. }else if(philosopherState == 1){
  181. philprint = "Eating";
  182. }else if(philosopherState == 2){
  183. philprint = "Hungry";
  184. }else if(philosopherState == 3){
  185. philprint = "Gone";
  186. count++;
  187. }
  188.  
  189. printf("P%d %s \t",o, philprint);
  190.  
  191. if (count >4){
  192. break;
  193. }
  194. }
  195. printf("\n");
  196. }
  197. return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement