linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Lee <live4thee@gmail.com>
To: Ardhan Madras <nightdecoder@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Help on pipe
Date: Fri, 10 Jul 2009 15:27:07 +0800	[thread overview]
Message-ID: <8f62e1cd0907100027h6579e99ckf2db4226776c9868@mail.gmail.com> (raw)
In-Reply-To: <34e1241d0907092322h5aea016cj437168a43ebfe50e@mail.gmail.com>

On Fri, Jul 10, 2009 at 2:22 PM, Ardhan Madras<nightdecoder@gmail.com> 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@vger.kernel.org
> 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@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2009-07-10  7:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-10  6:22 Help on pipe Ardhan Madras
2009-07-10  7:06 ` Michał Nazarewicz
2009-07-10  7:34   ` Ardhan Madras
2009-07-10 17:40   ` Glynn Clements
2009-07-11  3:41     ` David Lee
2009-07-13  6:45     ` Michał Nazarewicz
2009-07-10  7:27 ` David Lee [this message]
  -- strict thread matches above, loose matches on Subject: below --
2009-07-10 18:21 Ardhan Madras

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8f62e1cd0907100027h6579e99ckf2db4226776c9868@mail.gmail.com \
    --to=live4thee@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=nightdecoder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).