Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 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,1);
  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. exit(0);
  82. }
  83. }
  84.  
  85. parent();
  86.  
  87. for (int m=0; m<N;m++){
  88. waitpid(pids[m],NULL,0);
  89. }
  90.  
  91. printf("This is final k %d\n",shared->k);
  92.  
  93. if (munmap(shared, sizeof(struct shared_memory)) == -1) {
  94. printf("Function munmap() failed\n");
  95. exit(-1);
  96. }
  97. if ((shm_unlink("/test")) == -1){
  98. printf("Function shm_unlink() failed\n");
  99. exit(-1);
  100. }
  101. return EXIT_SUCCESS;
  102. }
  103.  
  104. void philosopher(int arga) {
  105. //int count =0;
  106.  
  107. //while(count<5){
  108. while(1){
  109. //sleep(1);
  110. //think(i);
  111. take_forks(arga);
  112. sleep(2);
  113. //eat(i);
  114. //sleep(1);
  115. put_forks(arga);
  116. //count++;
  117. }
  118. shared->state[arga] = FINISH;
  119. }
  120.  
  121. void take_forks(int arga) /* i: philosopher number, from 0 to Nāˆ’1 */
  122. {
  123. //printf("arga take_fork: %d\n", arga);
  124.  
  125. sem_wait(&shared->mutex); /* enter critical region, down mutex */
  126. shared->state[arga] = HUNGRY; /* record fact that philosopher i is hungry */
  127. //while (shared->state[i] == HUNGRY){
  128. test(arga); /* try to acquire 2 forks */
  129. sem_post(&shared->mutex); /* exit critical region, up mutex */
  130.  
  131. //sem_wait(&shared->s[arga]); /* block if for ks were not acquired, down philosopher*/
  132.  
  133. //}
  134.  
  135.  
  136. //sleep(1);
  137. }
  138. void put_forks(int arga) /* i: philosopher number, from 0 to Nāˆ’1 */
  139. {
  140. //printf("arga put_fork: %d\n", arga);
  141. sem_wait(&shared->mutex); /* enter critical region, down mutes */
  142. //test(LEFT); /* see if left neighbor can now eat */
  143. //test(RIGHT); /* see if right neighbor can now eat */
  144. shared->state[arga] = THINKING; /* philosopher has finished eating */
  145. sem_post(&shared->mutex); /* exit critical region, up mutex */
  146.  
  147.  
  148. }
  149. void test(int arga) /* i: philosopher number, from 0 to Nāˆ’1 */
  150. {
  151. sem_wait(&shared->s[LEFT]);
  152. sem_wait(&shared->s[RIGHT]);
  153.  
  154. shared->state[arga]= EATING;
  155. sleep(1);
  156.  
  157. sem_post(&shared->s[RIGHT]);
  158. sem_post(&shared->s[LEFT]);
  159. //if (shared->state[arga] == HUNGRY && shared->state[LEFT] != EATING && shared->state[RIGHT] != EATING)
  160. //{
  161. //printf("Philosopher: %d (i+N-1)%N - %d", i, ((i+N-1)%N));
  162. //shared->state[arga] = EATING;
  163. //sleep(2);
  164. //sem_post(&shared->s[arga]); //Up philosopher
  165. //}
  166.  
  167. }
  168.  
  169. void think(int arga){
  170. //printf("I am philosopher %d i am thinking\n", i);
  171. sleep(2);
  172. }
  173.  
  174. void eat(int arga){
  175. //printf("I am philosopher %d i am eating\n", i);
  176. sleep(3);
  177. }
  178.  
  179. int parent(void){
  180. puts("This is the Parent Process");
  181. int philosopherState;
  182. char* philprint;
  183. int count=0;
  184. int c;
  185.  
  186. while (1){
  187. sleep(1);
  188. //sem_wait(&shared->mutex);
  189. for(int o = 0; o<N;o++ ){
  190.  
  191. /*philosopherState = shared->state[o];
  192.  
  193. if (philosopherState == 0) {
  194. philprint = "Thinking";
  195. }else if(philosopherState == 1){
  196. philprint = "Eating";
  197. }else if(philosopherState == 2){
  198. philprint = "Hungry";
  199. }else if(philosopherState == 3){
  200. philprint = "Gone";
  201. count++;
  202. }*/
  203. sem_getvalue(&shared->s[o], &c);
  204. //printf("P%d %s \t",o, philprint);
  205.  
  206. if (c==0){
  207. printf("P%d Thinking\t",o);
  208. }
  209. else if(c==1){
  210. printf("P%d Eating\t",o);
  211. }
  212. else if (c==2){
  213. printf("P%d Hungry\t",o);
  214. }
  215. else if (c==3){
  216. printf("P%d Gone\t",o);
  217. }
  218.  
  219. if (count >4){
  220. break;
  221. }
  222. }
  223. //sem_post(&shared->mutex);
  224. printf("\n");
  225. }
  226. return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement