Help on pipe
Ardhan Madras <[email protected]>
| Newsgroups | org.kernel.vger.linux-c-programming |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I'm writing pipe demo:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
pid_t pid;
int fds[2], ret;
const char *sort = "/usr/bin/sort";
char *argv[2] = { "sort", NULL };
ret = pipe(fds);
if (ret == -1) {
perror("pipe");
return -1;
}
pid = fork();
if (pid == -1) {
perror("fork");
return -1;
}
if (pid == 0) {
int ret;
close(fds[1]);
dup2(fds[0], STDIN_FILENO);
ret = execve(sort, argv, NULL);
if (ret == -1) {
perror("execve");
}
close(fds[0]);
_exit(0);
}
else {
FILE *stream;
close(fds[0]);
stream = fdopen(fds[1], "w");
if (!stream) {
perror("fdopen");
return -1;
}
fprintf(stream, "this\n");
fprintf(stream, "means\n");
fprintf(stream, "war\n");
fflush(stream);
waitpid(pid, &ret, 0);
close(fds[1]);
}
return 0;
}
I want to send parent's data to the child as "sort" input, i don't
know why sort doesn't receive the data. Did i made mistake here?
Thanks before.