Re: atexit doubt

From: Michael Kerrisk (michael.kerrisk.at.gmx.net_at_nospam.com)
Date: 11/30/04


Date: Tue, 30 Nov 2004 10:05:35 +0100

On 29 Nov 2004 23:01:06 -0800, "Kelvin Moss" <km_jr_usenet@yahoo.com>
wrote:

>Hi all,
>
>I have an application where I read input through telnet. Something like
>-
>
>telnet localhost 10999
>a
>b
>c
>
>I have registered a cleanup function with atexit. It gets invoked when
>I press ctrl]q. I looked at strace to find which system call invokes
>atexit. I don't see exit being called...is it shutdown that invokes
>exit or something else ?
>
>write(1, "telnet> ", 8telnet> ) = 8
>read(0, q
>"q\n", 1024) = 2
>shutdown(3, 2 /* send and receive */) = 0
>write(1, "Connection closed.\r\n", 20Connection closed.
>) = 20
>close(3) = 0
>rt_sigaction(SIGTSTP, {0x804f290, [TSTP], SA_RESTART|0x4000000},
>{SIG_DFL}, 8) = 0
>ioctl(0, SNDCTL_TMR_STOP, {B38400 opost isig icanon echo ...}) = 0
>ioctl(0, 0x5421, [1]) = 0
>ioctl(1, 0x5421, [1]) = 0
>select(2, NULL, [1], NULL, NULL) = 1 (out [1])
>rt_sigaction(SIGTSTP, {SIG_DFL}, {0x804f290, [TSTP],
>SA_RESTART|0x4000000}, 8) = 0
>rt_sigprocmask(SIG_BLOCK, [], [], 8) = 0
>rt_sigprocmask(SIG_SETMASK, [], [], 8) = 0
>ioctl(0, SNDCTL_TMR_STOP, {B38400 opost isig icanon echo ...}) = 0
>ioctl(0, 0x5421, [0]) = 0
>ioctl(1, 0x5421, [0]) = 0
>rt_sigaction(SIGTSTP, {0x804f290, [TSTP], SA_RESTART|0x4000000},
>{SIG_DFL}, 8) = 0
>ioctl(0, SNDCTL_TMR_STOP, {B38400 opost isig icanon echo ...}) = 0
>ioctl(0, 0x5421, [1]) = 0
>ioctl(1, 0x5421, [1]) = 0
>select(2, NULL, [1], NULL, NULL) = 1 (out [1])
>rt_sigaction(SIGTSTP, {SIG_DFL}, {0x804f290, [TSTP],
>SA_RESTART|0x4000000}, 8) = 0
>rt_sigprocmask(SIG_BLOCK, [], [], 8) = 0
>rt_sigprocmask(SIG_SETMASK, [], [], 8) = 0
>ioctl(0, SNDCTL_TMR_STOP, {B38400 opost isig icanon echo ...}) = 0
>ioctl(0, 0x5421, [0]) = 0
>ioctl(1, 0x5421, [0]) = 0
>munmap(0x40014000, 4096) = 0
>_exit(0)
>
>
>Thanks.

Hello Kelvin,

Your question isn't very clear... Anyway, process termination
functions registered by atexit() are called when a process terminates
by calling exit(). exit() does the following:

1. Calls functions registered using atexit().

2. Flushes and closes stdio streams.

3. Calls the _exit() system call to terminate the process.

NOTE: process terminate functions are NOT called if:

1. The program bypasses exit() and calls _exit() directly, or

2. The process is killed by a signal (unless the process has a
   handler for that signal which causes the program to call exit()).

Cheers,

Michael



Relevant Pages

  • Re: nss_ldap broken
    ... >> exit() calls the finalization of atexit() to run down the exit ... >> handlers. ... > atexit_mutex is not held while calling the actual handler. ...
    (freebsd-current)
  • Re: Fork, Pipe, Dup2 etc.
    ... You MUST use _exit() in the child of a fork/vfork, ... Every process is the result of a fork or vfork! ... forkcopies stdio buffers and atexit calls. ...
    (comp.unix.programmer)
  • Re: zombie process questions
    ... with atexit() nor any registered signal handlers. ... new _Exit() function from C99 was merged in. ... POSIX required that _exitnot flush open streams, ... registered with atexitnor any registered signal handlers. ...
    (comp.unix.programmer)
  • Re: Monitor Thread In DLL
    ... Runtime calls atexitfor the EXE. ... Runtime calls atexit() for the DLL during the detach phase. ... > finally wait for the thread to exit using WaitForSingleObject.. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: dlopen(), atexit() crash on FreeBSD (testcase included)
    ... > expect themselves to live until actual exit() happens. ... Firebird folks to remove their atexit() calls from the client ... contrary to its design as you say. ...
    (freebsd-hackers)