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
|
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
#include <pthread.h>
void * child(void * pfds) {
int * pfd = pfds;
//close(pfd[1]); /* close the unused write side */
dup2(pfd[0], 0); /* connect the read side with stdin */
//close(pfd[0]); /* close the read side */
/* execute the process (wc command) */
execlp("wc", "wc", (char *) 0);
printf("wc failed"); /* if execlp returns, it's an error */
}
void * parent(void * pfds) {
int * pfd = pfds;
//close(pfd[0]); /* close the unused read side */
dup2(pfd[1], 1); /* connect the write side with stdout */
//close(pfd[1]); /* close the write side */
/* execute the process (ls command) */
execlp("ls", "ls", (char *)0);
printf("ls failed"); /* if execlp returns, it's an error */
}
int main( int argc, char ** argv )
{
/* create the pipe */
int pfd[2];
if (pipe(pfd) == -1)
{
printf("pipe failed\n");
return 1;
}
pthread_t t1, t2;
pthread_create(&t1, NULL, child, pfd);
pthread_create(&t2, NULL, parent, pfd);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
} |
Partager