Professional Web Applications Themes

_exit and redirection problem - UNIX Programming

Consider the following program: ------------------------------------------------------- int main(void) { char buf[] ="exit program"; write(1, buf, sizeof(buf)); printf("This is the output..\n"); printf("Next output is\n"); #ifdef _EXIT _exit(0); #endif #ifdef EXIT exit(0); #endif } ---------------------------------------------- compile it with -D_EXIT option running a.out gives ------------------------------ exit programThis is the output.. ------------------------------- but a.out >tmp.out; cat tmp.out give only --------------------- exit program ------------------- I am unable to understand the reason why it behaves differently when redirected. since whatever is thrown out on stdout should go into tmp.out file Any logical explanations ??? Thanks...

  1. #1

    Default _exit and redirection problem

    Consider the following program:
    -------------------------------------------------------
    int
    main(void)
    {
    char buf[] ="exit program";

    write(1, buf, sizeof(buf));

    printf("This is the output..\n");
    printf("Next output is\n");

    #ifdef _EXIT
    _exit(0);
    #endif
    #ifdef EXIT
    exit(0);
    #endif
    }
    ----------------------------------------------
    compile it with -D_EXIT option
    running a.out gives
    ------------------------------
    exit programThis is the output..
    -------------------------------
    but a.out >tmp.out; cat tmp.out give only
    ---------------------
    exit program
    -------------------
    I am unable to understand the reason why it behaves differently when
    redirected. since whatever is thrown out on stdout should go into
    tmp.out file

    Any logical explanations ???

    Thanks
    Ash Guest

  2. #2

    Default Re: _exit and redirection problem

    Le Thu, 12 Feb 2004 04:29:26 -0800, Ash a écrit:

    The problem is that your are mixing two kinds of I/O :
    Native unix I/o and standard C I/O.
    And when you use the _exit call, flushing standard I/O buffers is
    implementation dependant while closing open file descriptors is mandatory.

    only exit system call flushes standard C I/O

    check _exit man page:
    .... snip ...
    The function _exit is like exit(), but does not call any functions reg-
    istered with the ANSI C atexit function, nor any registered signal han-
    dlers. Whether it flushes standard I/O buffers and removes temporary
    files created with tmpfile(3) is implementation-dependent.
    .... snip ...

    regards,
    Paulo
    --
    ---------------------------------------
    Paulo PEREIRA
    fr

    Paulo Guest

  3. #3

    Default Re: _exit and redirection problem

    On Thu, 12 Feb 2004, Ash wrote:
     

    Yes. By default, standard output is line buffered, so the "\n"s
    in your printfs cause the output to be flushed. When you redirect
    the output, standard output is no longer associated with a terminal,
    so it becomes fully buffered. Hence, the \ns don't cause it to be
    flushed. exit() causes standard I/O buffers to be flushed, but
    _exit() doesn't, which is why your output goes "missing". (exit()
    or preferably return should be used in your example instead.)

    It's also why _exit should always be used after fork if exec fails
    or isn't called, rather than exit.

    HTH,

    --
    Rich Teer, SCNA, SCSA

    President,
    Rite Online Inc.

    Voice: +1 (250) 979-1638
    URL: http://www.rite-online.net
    Rich Guest

  4. #4

    Default Re: _exit and redirection problem

    Ash <com> wrote: 

    A simple example: your program as written something to stdout and you
    don't know if that already made it past the buffers when you start a
    child process that never does I/O. If the child now would exit using
    exit() instead of _exit() it would flush the buffers. But the same
    stuff is still in the buffers of the parent process and thus it would
    get written out twice instead of once. So having _exit() _not_ do the
    standard I/O cleanup that exit() does can be necessary.

    Another example: Your program installs an exit handler with atexit()
    where shared memory segments and temporary files get deleted and file
    locks are released etc. If this exit handler function would also be
    run by child processes things could go badly wrong. And again _exit()
    comes to the rescue because when you exit with _exit() these handlers
    aren't run.

    So you will see the use of _exit() nearly exclusively from within
    child processes where it makes sense to skip all the normal cleanup
    exit() does for you.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ fu-berlin.de
    \__________________________ http://www.physik.fu-berlin.de/~toerring
    Jens.Toerring@physik.fu-berlin.de Guest

Similar Threads

  1. Redirection Problem
    By Topspin in forum PHP Programming
    Replies: 4
    Last Post: February 2nd, 07:16 AM
  2. Problem in redirection
    By sachin in forum PERL Modules
    Replies: 5
    Last Post: September 5th, 01:45 PM
  3. Problem with header redirection but only with IE
    By Ian in forum PHP Development
    Replies: 2
    Last Post: January 14th, 11:38 AM
  4. Passport Redirection Problem.
    By Munna in forum ASP.NET Security
    Replies: 2
    Last Post: July 14th, 10:06 AM
  5. Problem with redirection in a Form...
    By Calvin in forum ASP
    Replies: 2
    Last Post: July 15th, 01:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139