1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
#include <pthread.h>
typedef struct {
int * p1;
int * p2;
} pipes;
void * ecrire(void * pfds) {
int * pfd = pfds;
write(pfd[1], "coucou\n", strlen("coucou\n"));
}
void * transfert(void * pfds) {
pipes * pfd = pfds;
dup2(pfd->p1[0], 0);
dup2(pfd->p2[1], 1);
char saisie[20];
fgets (saisie, sizeof saisie, stdin);
strcat(saisie, " retour");
printf("%s", saisie);
}
void * lire(void * pfds) {
int * pfd = pfds;
char * buffer = malloc(sizeof(char)*14);
read(pfd[0], buffer, sizeof(buffer));
printf("%s",buffer);
free(buffer);
}
int main( int argc, char ** argv )
{
int pfd1[2];
int pfd2[2];
if (pipe(pfd1) == -1)
{
printf("pipe failed\n");
return 1;
}
if (pipe(pfd2) == -1)
{
printf("pipe failed\n");
return 1;
}
pthread_t t1, t2, t3;
pthread_create(&t1, NULL, ecrire, pfd1);
pipes * pfd12 = malloc(sizeof(pipes));
pfd12->p1 = pfd1;
pfd12->p2 = pfd2;
pthread_create(&t2, NULL, transfert, pfd12);
pthread_create(&t3, NULL, lire, pfd2);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
return 0;
} |
Partager