How come _exit(0) (exiting by syscall) prevents me from receiving any stdout content?

前端 未结 3 1031
你的背包
你的背包 2021-01-19 03:12

I have a Linux x86-32 GAS assembly program terminating like this:

movl $1, %eax
movl $0, %ebx # argument for _exit
int $0x80

When I exit li

相关标签:
3条回答
  • 2021-01-19 03:29

    when you pipe stdout to wc, stdout becomes fully buffered.

    _exit terminates the process immediately and does not run atexit() and other cleanup handlers. The runtime will register such handlers to be run on exit that flushes open FILE*, such as stdout. When those handlers does not get executed on exit, buffered data will be lost.

    You should see output if you call fflush(stdout) after the printf call, or if you just run the program in a consol without piping the output to another program - in which case stdout will normally be line buffered, so stdout is flushed whenever you write a \n

    0 讨论(0)
  • 2021-01-19 03:42

    According to the man page for _exit(2)

    Whether it flushes standard I/O buffers and removes temporary files created with tmpfile(3) is implementation-dependent. On the other hand, _exit() does close open file descriptors, and this may cause an unknown delay, waiting for pending output to finish. If the delay is undesired, it may be useful to call functions like tcflush(3) before calling _exit(). Whether any pending I/O is canceled, and which pending I/O may be canceled upon _exit(), is implementation-dependent.

    So unless you've flushed stdout, it may get discarded.

    0 讨论(0)
  • 2021-01-19 03:45

    Output sent to standard out is usually buffered. If you call fflush(stdout) before you call _exit you should get your output.

    The reason exit works is because that function is guaranteed to close and flush any open streams (such as stdout) before calling _exit itself to actually terminate the program.

    0 讨论(0)
提交回复
热议问题