sort
is waiting, but what? I tried execlp("head", "head", "-n", "3", NULL);
instead sort
and it works fine.
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <assert.h>
int main()
{
int p[2], cat_pid, sort_pid;
if (pipe(p) < 0) { assert(0 && "pipe fail"); }
if ((cat_pid = fork()) == 0) { dup2(p[1], 1); execlp("cat", "cat", "text", NULL); assert(0 && "cat fail"); }
if ((sort_pid = fork()) == 0) { dup2(p[0], 0); execlp("sort", "sort", NULL); assert(0 && "sort fail"); }
waitpid(sort_pid, NULL, 0);
}
input text
is:
hello
world
foo
bar