Guest User

Untitled

a guest
Jan 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5.  
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/sem.h>
  9.  
  10. #include <sys/stat.h>
  11.  
  12. #define SEM_ACCESS 0
  13. #define SEM_EMPTY 1
  14. #define SEM_FILLED 2
  15.  
  16. #define SIZE 3
  17. #define OP_DOWN -1
  18. #define OP_UP 1
  19. #define nb_prod 4
  20. #define nb_cons 4
  21.  
  22. void die(const char *s)
  23. {
  24. perror(s);
  25. exit(EXIT_FAILURE);
  26. }
  27.  
  28. void produce(int semid)
  29. {
  30.  
  31. struct sembuf operations[1];
  32. operations[0].sem_flg=0;
  33.  
  34.  
  35. operations[0].sem_num=SEM_EMPTY;
  36. operations[0].sem_op=OP_DOWN;
  37. if (semop(semid,operations,1) == -1)
  38. {
  39. perror("MAIN.c op_up a foiré dans sem_access");
  40. return EXIT_FAILURE;
  41. }
  42.  
  43. operations[0].sem_num=SEM_ACCESS;
  44. operations[0].sem_op=OP_DOWN;
  45. if (semop(semid,operations,1) == -1)
  46. {
  47. perror("MAIN.c op_up a foiré dans sem_access");
  48. return EXIT_FAILURE;
  49. }
  50.  
  51. struct storage {
  52. int pos_prod, pos_cons;
  53. int store[SIZE];
  54. };
  55.  
  56. struct storage storage;
  57.  
  58. FILE* stream=fopen("entrepot.txt","r");
  59. fscanf(stream, "%d %d", &storage.pos_prod, &storage.pos_cons);
  60. int loop_i;
  61. for(loop_i = 0; loop_i < SIZE; loop_i++)
  62. {
  63. fscanf(stream, "%d", &storage.store[loop_i]);
  64. }
  65. fclose(stream);
  66.  
  67. storage.store[storage.pos_prod] = getpid();
  68. storage.pos_prod++;
  69. stream=fopen("entrepot.txt","w");
  70. if(stream == NULL)
  71. {
  72. die("produce:fopen");
  73. }
  74.  
  75. fprintf(stream, "%d %d\n", storage.pos_prod, storage.pos_cons);
  76. for(loop_i=0;loop_i<SIZE;loop_i++)
  77. {
  78. fprintf(stream, "%d ",storage.store[loop_i]);
  79. }
  80.  
  81. operations[0].sem_num=SEM_ACCESS;
  82. operations[0].sem_op=OP_UP;
  83. if (semop(semid,operations,1) == -1)
  84. {
  85. perror("MAIN.c op_up a foiré dans sem_access");
  86. return EXIT_FAILURE;
  87. }
  88.  
  89. operations[0].sem_num=SEM_FILLED;
  90. operations[0].sem_op=OP_UP;
  91. if (semop(semid,operations,1) == -1)
  92. {
  93. perror("MAIN.c op_up a foiré dans sem_filled");
  94. return EXIT_FAILURE;
  95. }
  96.  
  97. }
  98.  
  99. void consume(int semid)
  100. {
  101.  
  102. }
  103.  
  104. int main() {
  105.  
  106. // synchronisation
  107. int semid = 0;
  108. if((semid=semget(IPC_PRIVATE,3 /*nb de sempahore*/ ,IPC_CREAT
  109. |S_IRUSR/*user read*/
  110. |S_IWUSR/*user write*/))==-1)
  111. {
  112. die("ERROR creation semaphore");
  113. }
  114.  
  115. if(semctl(semid,SEM_ACCESS,SETVAL,1) == -1)
  116. {
  117. die("ERROR control semaphore access");
  118. } //binaire ou mutux ---> un seul processus
  119.  
  120.  
  121. if(semctl(semid,SEM_EMPTY,SETVAL,SIZE) == -1)
  122. {
  123. die("ERROR control semaphore empty");
  124. }
  125.  
  126. if(semctl(semid,SEM_FILLED,SETVAL,0) == -1)
  127. {
  128. die("ERROR control semaphore filled");
  129. }
  130.  
  131. FILE* stream=fopen("entrepot.txt","w+");
  132. fprintf(stream, "0 0\n");
  133.  
  134. int loop_i = 0;
  135. for(loop_i = 0; loop_i < SIZE; loop_i++)
  136. {
  137. fprintf(stream, "0 ");
  138. }
  139.  
  140.  
  141. // Processus management
  142. char father = 1;
  143.  
  144. pid_t PIDs[nb_cons + nb_prod];
  145. for(loop_i = 0; loop_i < nb_cons; loop_i++)
  146. {
  147. PIDs[loop_i] = fork();
  148. if(PIDs[loop_i] == -1 )
  149. {
  150. die("ERROR Creation");
  151. }
  152.  
  153. if(PIDs[loop_i] == 0)
  154. {
  155. father = 0;
  156. consume(semid);
  157. break;
  158. }
  159. }
  160.  
  161. if(father)
  162. {
  163. for(loop_i = 0; loop_i < nb_cons; loop_i++)
  164. {
  165. PIDs[loop_i] = fork();
  166. if(PIDs[loop_i] == -1 )
  167. {
  168. die("ERROR Creation processus");
  169. }
  170.  
  171. if(PIDs[loop_i] == 0)
  172. {
  173. father = 0;
  174. produce(semid);
  175. break;
  176. }
  177. }
  178.  
  179. if(father)
  180. {
  181. for(loop_i = 0; loop_i < nb_cons; loop_i++)
  182. {
  183. if(waitpid(PIDs[loop_i], NULL, 0) == -1)
  184. {
  185. die("ERROR attente processus");
  186. }
  187. }
  188.  
  189. if (semctl(semid, SEM_ACCESS, IPC_RMID) == -1)
  190. {
  191. die("ERROR semaphore détruite");
  192. }
  193. }
  194. }
  195.  
  196. return EXIT_SUCCESS;
  197. }
Add Comment
Please, Sign In to add comment