Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <pthread.h>
  9. #include <semaphore.h>
  10. #include <fcntl.h>
  11.  
  12.  
  13. int pipefd[2];
  14. char buf1[100];
  15. char buf2[100];
  16. sem_t p1,p2;
  17. int threadResult = 0;
  18. int threadState = 1;
  19. extern void left_child(void), right_child(void);
  20.  
  21. /* main --- fork children, wait for them to finish */
  22.  
  23. int main(int argc, char **argv)
  24. {
  25. pid_t left_pid, right_pid;
  26. pid_t ret;
  27. int status;
  28.  
  29. if (pipe(pipefd) < 0) { /* pipe */
  30. perror("pipe");
  31. exit(1);
  32. }
  33.  
  34. if ((left_pid = fork()) < 0) { /* procc 1 */
  35. perror("fork");
  36. exit(1);
  37. } else if (left_pid == 0)
  38. left_child();
  39.  
  40. if ((right_pid = fork()) < 0) { /* procc 2 */
  41. perror("fork");
  42. exit(1);
  43. } else if (right_pid == 0)
  44. right_child();
  45.  
  46. close(pipefd[0]);
  47. close(pipefd[1]);
  48.  
  49. while ((ret = wait(& status)) > 0) { /* fin du traitment */
  50. if (ret == left_pid)
  51. printf("proccessus E/S term: %x\n", status);
  52. else if (ret == right_pid)
  53. printf("processus traitment term: %d\n", status);
  54. else
  55. printf("p %d -- %x\n",ret, status);
  56. }
  57.  
  58. return 0;
  59. }
  60.  
  61. /* processus E/S */
  62.  
  63. void left_child(void)
  64. {
  65.  
  66.  
  67. //close(1);
  68. dup(pipefd[1]);
  69. //dup(pipefd[0]);
  70.  
  71. printf("Fichier 1 : ");
  72. char x[50];
  73. scanf("%s",x);
  74. while(access( x, R_OK ) == -1){
  75. printf("Fichier 1 : ");
  76. scanf("%s",x);
  77.  
  78. }
  79.  
  80. write(pipefd[1], x, strlen(x)+1);
  81. char x2[50];
  82. printf("Fichier 2 : ");
  83. scanf("%s",x2);
  84. while(access( x, R_OK ) == -1){
  85. printf("Fichier 2 : ");
  86. scanf("%s",x2);
  87.  
  88. }
  89. write(pipefd[1], x2, strlen(x2)+1);
  90.  
  91. read(pipefd[0], x2, 50);
  92. printf("Resultat = %s",x2);
  93.  
  94. close(pipefd[1]);
  95. close(pipefd[0]);
  96. _exit(errno == ENOENT ? 127 : 126);
  97. }
  98.  
  99. /* les threads */
  100. void * threadProd1(void *arg)
  101. {
  102. FILE* f = (FILE*)arg;
  103. while(threadState){
  104.  
  105. if(!fscanf(f,"%s",buf1)){
  106. //buf1 = "-p1";
  107. strcpy(buf1, "\EOF");
  108. }
  109. sem_wait(&p1); // -1
  110.  
  111. }
  112. }
  113.  
  114. void * threadProd2(void *arg)
  115. {
  116. FILE* f = (FILE*)arg;
  117. while(threadState){
  118.  
  119. if(!fscanf(f,"%s",buf2)){
  120. //buf2 = "-p2";
  121. strcpy(buf2, "\EOF");
  122. }
  123. sem_wait(&p2); // -1
  124.  
  125. }
  126. }
  127. void * threadCons(void *arg)
  128. {
  129. while(threadState){
  130. int v1,v2;
  131. sem_getvalue(&p1, &v1);
  132. sem_getvalue(&p2, &v2);
  133. while(v1!=0 || v2!=0){
  134. sem_getvalue(&p1, &v1);
  135. sem_getvalue(&p2, &v2);
  136. }
  137. // traitement
  138.  
  139. printf("%s -- %s\n",buf1,buf2);
  140. if((buf1 == "\EOF")&&(buf2 == "\EOF")){
  141. if(threadResult == 0){
  142. threadResult = 2;
  143. threadState = 0;
  144. }
  145. }
  146. if(buf1 != buf2){
  147. threadResult = -1;
  148. threadState = 0;
  149. }
  150. // -------
  151. sem_post(&p1);
  152. sem_post(&p2);
  153. }
  154. }
  155. /* processus traitment */
  156. void right_child(void)
  157. {
  158. static char *right_argv[] = { "sed", "s/hi/hello/g", NULL };
  159.  
  160. //close(pipefd[1]);
  161. //close(0);
  162. dup(pipefd[0]);
  163.  
  164. int resultat = 1;
  165. char f1[50];
  166. char f2[50];
  167. read(pipefd[0], f1, 50);
  168. FILE* fo1 = fopen(f1,"r");
  169. read(pipefd[0], f2, 50);
  170. FILE* fo2 = fopen(f2,"r");
  171. if((fo1 == NULL) || (fo2 == NULL)){
  172. exit(-2);
  173. }
  174.  
  175.  
  176. pthread_t producteur1;
  177. pthread_t producteur2;
  178. pthread_t cons;
  179. //sem_t p1,p2;
  180.  
  181. sem_init(&p1,0,1);
  182. sem_init(&p2,0,1);
  183.  
  184.  
  185.  
  186. if( pthread_create(&producteur1, NULL, threadProd1, fo1) != 0 )
  187. printf("erreur creation du thread 1 \n");
  188. if( pthread_create(&producteur2, NULL, threadProd2, fo2) != 0 )
  189. printf("erreur creation du thread 2\n");
  190. if( pthread_create(&cons, NULL, threadCons, NULL) != 0 )
  191. printf("erreur creation du thread 3\n");
  192.  
  193. pthread_join(cons, NULL);
  194. if(threadResult == 2 ){
  195. write(pipefd[1], "2\0", 2);
  196. }
  197. if(threadResult == -1 ){
  198. write(pipefd[1], "1\0", 2);
  199. }
  200.  
  201.  
  202.  
  203. close(pipefd[0]);
  204. _exit(errno == ENOENT ? 127 : 126);
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement