Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <fcntl.h>
- int pipefd[2];
- char buf1[100];
- char buf2[100];
- sem_t p1,p2;
- int threadResult = 0;
- int threadState = 1;
- extern void left_child(void), right_child(void);
- /* main --- fork children, wait for them to finish */
- int main(int argc, char **argv)
- {
- pid_t left_pid, right_pid;
- pid_t ret;
- int status;
- if (pipe(pipefd) < 0) { /* pipe */
- perror("pipe");
- exit(1);
- }
- if ((left_pid = fork()) < 0) { /* procc 1 */
- perror("fork");
- exit(1);
- } else if (left_pid == 0)
- left_child();
- if ((right_pid = fork()) < 0) { /* procc 2 */
- perror("fork");
- exit(1);
- } else if (right_pid == 0)
- right_child();
- close(pipefd[0]);
- close(pipefd[1]);
- while ((ret = wait(& status)) > 0) { /* fin du traitment */
- if (ret == left_pid)
- printf("proccessus E/S term: %x\n", status);
- else if (ret == right_pid)
- printf("processus traitment term: %d\n", status);
- else
- printf("p %d -- %x\n",ret, status);
- }
- return 0;
- }
- /* processus E/S */
- void left_child(void)
- {
- //close(1);
- dup(pipefd[1]);
- //dup(pipefd[0]);
- printf("Fichier 1 : ");
- char x[50];
- scanf("%s",x);
- while(access( x, R_OK ) == -1){
- printf("Fichier 1 : ");
- scanf("%s",x);
- }
- write(pipefd[1], x, strlen(x)+1);
- char x2[50];
- printf("Fichier 2 : ");
- scanf("%s",x2);
- while(access( x, R_OK ) == -1){
- printf("Fichier 2 : ");
- scanf("%s",x2);
- }
- write(pipefd[1], x2, strlen(x2)+1);
- read(pipefd[0], x2, 50);
- printf("Resultat = %s",x2);
- close(pipefd[1]);
- close(pipefd[0]);
- _exit(errno == ENOENT ? 127 : 126);
- }
- /* les threads */
- void * threadProd1(void *arg)
- {
- FILE* f = (FILE*)arg;
- while(threadState){
- if(!fscanf(f,"%s",buf1)){
- //buf1 = "-p1";
- strcpy(buf1, "\EOF");
- }
- sem_wait(&p1); // -1
- }
- }
- void * threadProd2(void *arg)
- {
- FILE* f = (FILE*)arg;
- while(threadState){
- if(!fscanf(f,"%s",buf2)){
- //buf2 = "-p2";
- strcpy(buf2, "\EOF");
- }
- sem_wait(&p2); // -1
- }
- }
- void * threadCons(void *arg)
- {
- while(threadState){
- int v1,v2;
- sem_getvalue(&p1, &v1);
- sem_getvalue(&p2, &v2);
- while(v1!=0 || v2!=0){
- sem_getvalue(&p1, &v1);
- sem_getvalue(&p2, &v2);
- }
- // traitement
- printf("%s -- %s\n",buf1,buf2);
- if((buf1 == "\EOF")&&(buf2 == "\EOF")){
- if(threadResult == 0){
- threadResult = 2;
- threadState = 0;
- }
- }
- if(buf1 != buf2){
- threadResult = -1;
- threadState = 0;
- }
- // -------
- sem_post(&p1);
- sem_post(&p2);
- }
- }
- /* processus traitment */
- void right_child(void)
- {
- static char *right_argv[] = { "sed", "s/hi/hello/g", NULL };
- //close(pipefd[1]);
- //close(0);
- dup(pipefd[0]);
- int resultat = 1;
- char f1[50];
- char f2[50];
- read(pipefd[0], f1, 50);
- FILE* fo1 = fopen(f1,"r");
- read(pipefd[0], f2, 50);
- FILE* fo2 = fopen(f2,"r");
- if((fo1 == NULL) || (fo2 == NULL)){
- exit(-2);
- }
- pthread_t producteur1;
- pthread_t producteur2;
- pthread_t cons;
- //sem_t p1,p2;
- sem_init(&p1,0,1);
- sem_init(&p2,0,1);
- if( pthread_create(&producteur1, NULL, threadProd1, fo1) != 0 )
- printf("erreur creation du thread 1 \n");
- if( pthread_create(&producteur2, NULL, threadProd2, fo2) != 0 )
- printf("erreur creation du thread 2\n");
- if( pthread_create(&cons, NULL, threadCons, NULL) != 0 )
- printf("erreur creation du thread 3\n");
- pthread_join(cons, NULL);
- if(threadResult == 2 ){
- write(pipefd[1], "2\0", 2);
- }
- if(threadResult == -1 ){
- write(pipefd[1], "1\0", 2);
- }
- close(pipefd[0]);
- _exit(errno == ENOENT ? 127 : 126);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement