flush printf() to a pipe

This is a discussion on flush printf() to a pipe within the C Programming forums, part of the General Programming Boards category; i constructed the following paradiam: parent child --------------------- -------------------- | | | | ------>| stdin fd[1] | --------> | stdin ...

  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 05:11 PM.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Use code tags please

    what exactly is your problem by the way?
    what did you expect to happen?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    How about posting a small real program (one we can compile and run for ourselves) rather than one which you've "nicely" edited with ... ...
    Who knows what critical detail you've omitted (or typos you've introduced in your editing - yes, I can see one of those).

  4. #4
    Registered User PanzTec's Avatar
    Join Date
    Sep 2004
    Posts
    24
    Y do you think people do that?
    do you think its cause they dont want there code stolen or what... i edit mine cause i know the is alot of spelling mistakes(as in the text out parts) and my code lay out sucks
    The Matrix Will Live Again!

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    Quote Originally Posted by PanzTec
    Y do you think people do that?
    do you think its cause they dont want there code stolen or what... i edit mine cause i know the is alot of spelling mistakes(as in the text out parts) and my code lay out sucks
    The program in your signature appears to be missing something as well
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    hi, All,

    thx a lot for ur comments. i really appreciate them.

    the issue was just resolved. to recap, apps use tty. most likely, only 1 tty available for each system. if a process needs to take the tty, it has to put other (child) processes on pipes or other msg passing ipcs. in order to make the pipes "really" like std i/o, one needs to use setvbuf() AFTER exec() not before.

    let me know if u have any questions. or, i'll close the thread in a day or 2.

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, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05: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, 08:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21