Code:
void die()
{
perror("fatal");
_exit(112);
}
int main()
{
int pfds[2];
int pid;
int stat;
char *args[5];
if (pipe(pfds) == -1) die();
switch (pid = fork()) {
case -1:
die();
break;
case 0:
if (close(0) == -1) _exit(112);
if (dup2(pfds[0], 0) == -1) _exit(112);
args[0] = "/usr/bin/sort";
args[1] = 0;
if (execve(args[0], args, 0) == -1) _exit(112);
break;
default:
if (write(pfds[1], "a\nb\nc\n", 6) == -1) die();
if (waitpid(pid, &stat, 0) == -1) die();
break;
}
return 0;
}
The write() call is sucessful, but no output is seen from
/usr/bin/sort and the program hangs. I'm guessing that
sort waits for input on stdin but for some reason never
receives it. The parent process sits at the waitpid()
call forever.
What did I do wrong?