On Fri, Jul 10, 2009 at 2:22 PM, Ardhan Madras<nightdecoder@xxxxxxxxx> wrote:
> 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.
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
The sort program is waiting for an EOF (^D).
--- sort.c.orig 2009-07-10 15:54:36.000000000 +0800
+++ sort.c 2009-07-10 15:53:35.000000000 +0800
@@ -49,6 +49,7 @@
fprintf(stream, "means\n");
fprintf(stream, "war\n");
fflush(stream);
+ fclose(stream);
waitpid(pid, &ret, 0);
close(fds[1]);
}
--
Thanks,
Li Qun
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
[Linux Assembler]
[Git]
[Kernel List]
[Fedora Development]
[Fedora Announce]
[Autoconf]
[Yosemite Campsites]
[Yosemite News]
[GCC Help]