IPC Pipe in Linux C -
it's simple code makes 2 child processes communicate: first 1 execute "ls" , pass output myfd[1]; second 1 receives output myfd[0], , execute "sort"(and shows result). parent process waits these 2 processes.
but code doesn't work. gets stuck @ second child process. possible reason why? did use right "close" , "dump" in proper place?
my code:
#include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> int main(void) { int pid; int wpid; int status = 0; int myfd[2]; printf("parent's pid: %d\n",getpid()); pipe(myfd); pid = fork(); if(pid == 0) // child 1 - execute "ls" { printf("child1's pid: %d\n",getpid()); close(1); dup(myfd[1]); close(0); close(myfd[0]); execlp("ls","child_process1",null); } else { pid = fork(); if(pid == 0) // child 2 - execute "sort" { printf("child2's pid: %d\n",getpid()); close(myfd[1]); close(0); dup(myfd[0]); execlp("sort","child_process2",null); } } // parent while((wpid = wait(&status)) > 0) { // wait until 2 child processes finish } printf("done!\n"); }
Comments
Post a Comment