当前位置: 技术问答>linux和unix
关于atexit的问题
来源: 互联网 发布时间:2016-03-05
本文导语: 下面是实验程序,测试atexit功能,但仍不是很明白 #include #include #include #include #include #include int quitt(int no) { printf("number is %dn",getpid()); puts("byen"); } int main() { pid_t pc, pr; { if( atexit( (void *)quitt ) == ...
下面是实验程序,测试atexit功能,但仍不是很明白
#include
#include
#include
#include
#include
#include
int quitt(int no)
{
printf("number is %dn",getpid());
puts("byen");
}
int main()
{
pid_t pc, pr;
{
if( atexit( (void *)quitt ) == 0 )
printf("reload successn");
else
exit(0);
}
pc = fork();
if ( pc 0 ) /*子进程正常返回*/
printf("I catched a child process with pid of %dn", pr);
else /*出错*/
printf("error: %sn.n", strerror(errno));
}
exit(0);
}
运行结果如下:
reload success
I am child process with pid 4179
Now in parent process, pid = 4178
I am waiting child process to exit.
number is 4179
bye
I catched a child process with pid of 4179
number is 4178
bye
书上所说exit(0)执行后,会向父进程发送一个SIGCHLD信号,atexit可为exit(0)设置一个回调函数,从而在exit()执行时,
回调函数将被调用
我的问题是,当我在程序中改用signal(SIGCHLD, quitt)为父子进程均重设了回调函数时,exit后,只有父进程的会被执行,我认为这是正确的,因为只有父进程收到了这个信号,但是用atexit函数注册的回调函数并不是对应于SIGCHLD信号,那么它对应的是那个信号?并且这个信号是不是执法送给该进程本身而不发给父进程?
敬期高手解答!
#include
#include
#include
#include
#include
#include
int quitt(int no)
{
printf("number is %dn",getpid());
puts("byen");
}
int main()
{
pid_t pc, pr;
{
if( atexit( (void *)quitt ) == 0 )
printf("reload successn");
else
exit(0);
}
pc = fork();
if ( pc 0 ) /*子进程正常返回*/
printf("I catched a child process with pid of %dn", pr);
else /*出错*/
printf("error: %sn.n", strerror(errno));
}
exit(0);
}
运行结果如下:
reload success
I am child process with pid 4179
Now in parent process, pid = 4178
I am waiting child process to exit.
number is 4179
bye
I catched a child process with pid of 4179
number is 4178
bye
书上所说exit(0)执行后,会向父进程发送一个SIGCHLD信号,atexit可为exit(0)设置一个回调函数,从而在exit()执行时,
回调函数将被调用
我的问题是,当我在程序中改用signal(SIGCHLD, quitt)为父子进程均重设了回调函数时,exit后,只有父进程的会被执行,我认为这是正确的,因为只有父进程收到了这个信号,但是用atexit函数注册的回调函数并不是对应于SIGCHLD信号,那么它对应的是那个信号?并且这个信号是不是执法送给该进程本身而不发给父进程?
敬期高手解答!
|
C标准只规定了atexit要实现的功能, 不会涉及实现方式, 所以关于"书上所说exit(0)执行后,会向父进程发送一个SIGCHLD信号,atexit可为exit(0)设置一个回调函数,从而在exit()执行时回调函数将被调用"的说法是不可信的.
不同的系统实现方式是不同的, 只要符合C标准都是可以的.
楼主可以看看BSD系统的实现方法:
不同的系统实现方式是不同的, 只要符合C标准都是可以的.
楼主可以看看BSD系统的实现方法:
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94";
#endif /* LIBC_SCCS and not lint */
#include
__FBSDID("$FreeBSD: src/lib/libc/stdlib/atexit.c,v 1.8 2007/01/09 00:28:09 imp Exp $");
#include "namespace.h"
#include
#include
#include
#include
#include "atexit.h"
#include "un-namespace.h"
#include "libc_private.h"
#define ATEXIT_FN_EMPTY 0
#define ATEXIT_FN_STD 1
#define ATEXIT_FN_CXA 2
static pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER;
#define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
#define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
struct atexit {
struct atexit *next; /* next in list */
int ind; /* next index in this table */
struct atexit_fn {
int fn_type; /* ATEXIT_? from above */
union {
void (*std_func)(void);
void (*cxa_func)(void *);
} fn_ptr; /* function pointer */
void *fn_arg; /* argument for CXA callback */
void *fn_dso; /* shared module handle */
} fns[ATEXIT_SIZE]; /* the table itself */
};
static struct atexit *__atexit; /* points to head of LIFO stack */
/*
* Register the function described by 'fptr' to be called at application
* exit or owning shared object unload time. This is a helper function
* for atexit and __cxa_atexit.
*/
static int
atexit_register(struct atexit_fn *fptr)
{
static struct atexit __atexit0; /* one guaranteed table */
struct atexit *p;
_MUTEX_LOCK(&atexit_mutex);
if ((p = __atexit) == NULL)
__atexit = p = &__atexit0;
else while (p->ind >= ATEXIT_SIZE) {
struct atexit *old__atexit;
old__atexit = __atexit;
_MUTEX_UNLOCK(&atexit_mutex);
if ((p = (struct atexit *)malloc(sizeof(*p))) == NULL)
return (-1);
_MUTEX_LOCK(&atexit_mutex);
if (old__atexit != __atexit) {
/* Lost race, retry operation */
_MUTEX_UNLOCK(&atexit_mutex);
free(p);
_MUTEX_LOCK(&atexit_mutex);
p = __atexit;
continue;
}
p->ind = 0;
p->next = __atexit;
__atexit = p;
}
p->fns[p->ind++] = *fptr;
_MUTEX_UNLOCK(&atexit_mutex);
return 0;
}
/*
* Register a function to be performed at exit.
*/
int
atexit(void (*func)(void))
{
struct atexit_fn fn;
int error;
fn.fn_type = ATEXIT_FN_STD;
fn.fn_ptr.std_func = func;;
fn.fn_arg = NULL;
fn.fn_dso = NULL;
error = atexit_register(&fn);
return (error);
}
/*
* Register a function to be performed at exit or when an shared object
* with given dso handle is unloaded dynamically.
*/
int
__cxa_atexit(void (*func)(void *), void *arg, void *dso)
{
struct atexit_fn fn;
int error;
fn.fn_type = ATEXIT_FN_CXA;
fn.fn_ptr.cxa_func = func;;
fn.fn_arg = arg;
fn.fn_dso = dso;
error = atexit_register(&fn);
return (error);
}
/*
* Call all handlers registered with __cxa_atexit for the shared
* object owning 'dso'. Note: if 'dso' is NULL, then all remaining
* handlers are called.
*/
void
__cxa_finalize(void *dso)
{
struct atexit *p;
struct atexit_fn fn;
int n;
_MUTEX_LOCK(&atexit_mutex);
for (p = __atexit; p; p = p->next) {
for (n = p->ind; --n >= 0;) {
if (p->fns[n].fn_type == ATEXIT_FN_EMPTY)
continue; /* already been called */
if (dso != NULL && dso != p->fns[n].fn_dso)
continue; /* wrong DSO */
fn = p->fns[n];
/*
Mark entry to indicate that this particular handler
has already been called.
*/
p->fns[n].fn_type = ATEXIT_FN_EMPTY;
_MUTEX_UNLOCK(&atexit_mutex);
/* Call the function of correct type. */
if (fn.fn_type == ATEXIT_FN_CXA)
fn.fn_ptr.cxa_func(fn.fn_arg);
else if (fn.fn_type == ATEXIT_FN_STD)
fn.fn_ptr.std_func();
_MUTEX_LOCK(&atexit_mutex);
}
}
_MUTEX_UNLOCK(&atexit_mutex);
}
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)exit.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include
__FBSDID("$FreeBSD: src/lib/libc/stdlib/exit.c,v 1.9 2007/01/09 00:28:09 imp Exp $");
#include "namespace.h"
#include
#include
#include "un-namespace.h"
#include "atexit.h"
#include "libc_private.h"
void (*__cleanup)(void);
/*
* This variable is zero until a process has created a thread.
* It is used to avoid calling locking functions in libc when they
* are not required. By default, libc is intended to be(come)
* thread-safe, but without a (significant) penalty to non-threaded
* processes.
*/
int __isthreaded = 0;
/*
* Exit, flushing stdio buffers if necessary.
*/
void
exit(status)
int status;
{
/* Ensure that the auto-initialization routine is linked in: */
extern int _thread_autoinit_dummy_decl;
_thread_autoinit_dummy_decl = 1;
__cxa_finalize(NULL);
if (__cleanup)
(*__cleanup)();
_exit(status);
}
|
结果是正确的
你可以理解,atexit是进程退出后执行的东西.
不管是子进程父进程.
你可以理解,atexit是进程退出后执行的东西.
不管是子进程父进程.
|
atexit是为进程注册一个退出时要执行的函数,创建子进程是复制父进程的整个进程空间,当然也会复制这个注册函数
|
不需要对应信号。
只要注册了,推出时就会调用。
只要注册了,推出时就会调用。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。