Thread: flush printf() to a pipe

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    Question flush printf() to a pipe

    i constructed the following paradiam:

    parent child
    --------------------- --------------------
    | | | |
    ------>| stdin fd[1] | --------> | stdin (fd[0]) |
    | | | |
    <------| stdout fd[0] | <-------- | stdout (fd[1]) |
    | | | |
    --------------------- ---------------------

    the parent looks like

    int main ()
    {
    ... ...
    setvbuf (stdout, NULL, _IONBF, 0);
    ... ...
    switch (fork ())
    {
    ... ...
    default:
    flags = fcntl (fd[0], F_GETFL, 0)))
    ... ...
    flags |= O_NONBLOCK;
    flags = fcntl (file_desc[0], F_SETFL, flags)))
    ... ...
    num_byte = read (fd[0], buf, BUF_SIZE);
    ... ...
    write (fileno(stdout), buf, num_byte);
    ... ...
    }
    ... ...

    return 0;
    }

    the child looks like

    int main ()
    {
    ... ...
    printf ("hello, world\n");
    fflush (stdout);
    ... ...
    sleep (30);
    return 0;
    }

    if i leave out fflush() in the child, "hello, world" never shows up on the console. but if i run the child as a stand-alone app, "hello, world" always shows up with or without fflush(), before the child returns.

    why is this?
    Last edited by xingzhao; 11-12-2004 at 06:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM