#include
#include
#include
#include
#include
int main(void)
{
int pchan[2], pid, rval, cvar;
char buf[20];
if (pipe(pchan) == -1){
perror("pipe");
exit(2);
}
switch (pid = fork() ){
case -1: perror("fork");
exit(2);
case 0:
close(0);
if (dup(pchan[0] != 0)){
perror("dup");
exit(3);
}
execlp("grep", "grep", "123", NULL);
perror("exec");
exit(4);
default:
for(cvar = 0; cvar < 200; cvar++){
rval = rand();
sprintf(buf, "%d\n", rval);
write(pchan[1], buf, strlen(buf));
}
if (close(pchan[0]) == -1 || close(pchan[1]) == -1){
perror("close");
exit(4);
}
wait(NULL);
return(0);
}
}