ITPub博客

首页 > 应用开发 > IT综合 > 【原创】编写守护进程程序中对信号进行安装在不同平台上的差异

【原创】编写守护进程程序中对信号进行安装在不同平台上的差异

原创 IT综合 作者:oract 时间:2007-10-25 18:55:18 0 删除 编辑
现代的AIX或者Linux系统上的守护进程中如果在主函数中定义了信号安装之后,是否仍存在以往那种一旦信号执行程序被触及那么信号必须马上再次被安装否则进程对于该信号的处理方式将被进程设置为默认处理的问题呢?
[@more@]

也就是说:

main()
{
...

signal(SIG****, handle );

...

}

handle ()

{
signal(SIG****, handle ); ----------- 这一行是否还有必要再写??

....
}

我们先在FreeBSD上做实验:

#include
#include
#include
#include
#include

int main()
{
void my_alarm(int);
struct passwd *ptr;

signal(SIGALRM,my_alarm);
alarm(1);
for(;;)
{
if((ptr=getpwnam("root"))==NULL)
{
printf("nError in main getpwnam!n");
fflush(stdout);
return 1;
}

if(strcmp(ptr->pw_name,"root")!=0)
printf("nReturn value corrupted! pw_name=%sn",ptr->pw_name);
}
}

void my_alarm(int signo)
{
struct passwd *ptr1;

/*signal(SIGALRM,my_alarm); */
printf("n In signal handler..........n");
if((ptr1=getpwnam("ftpup"))==NULL)
{
printf("n Error in my_alarm getpwnam!n");
fflush(stdout);
exit(1);
}
alarm(1);
}

$
$
$ gcc ./chongru.c -o ./chongru
$
$
$ ./chongru

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........
chongru in free(): warning: page is already free
chongru in free(): warning: chunk is already free
chongru in free(): warning: chunk is already free
chongru in free(): warning: chunk is already free
chongru in free(): warning: chunk is already free
chongru in free(): warning: chunk is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: chunk is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: chunk is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: chunk is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: chunk is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: chunk is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: chunk is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: page is already free
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: modified (chunk-) pointer
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): warning: junk pointer, too high to make sense
chongru in free(): error: freelist is destroyed
Abort trap (core dumped)

可见,在FreeBSD上是不用重新安装的。

现在在AIX 4.3 上做实验:

(fund)/fund/c# oslevel -r
4330-11
(fund)/fund/c# cat ./chongru.c
#include
#include
#include
#include
#include

int main()
{
void my_alarm(int);
struct passwd *ptr;

signal(SIGALRM,my_alarm);
alarm(1);
for(;;)
{
if((ptr=getpwnam("root"))==NULL)
{
printf("nError in main getpwnam!n");
fflush(stdout);
return 1;
}

if(strcmp(ptr->pw_name,"root")!=0)
printf("nReturn value corrupted! pw_name=%sn",ptr->pw_name);
}
}

void my_alarm(int signo)
{
struct passwd *ptr1;

/* signal(SIGALRM,my_alarm); */
printf("n In signal handler..........n");
if((ptr1=getpwnam("fund"))==NULL)
{
printf("n Error in my_alarm getpwnam!n");
fflush(stdout);
exit(1);
}
alarm(1);
}
(fund)/fund/c# xlc ./chongru.c -o ./chongru
(fund)/fund/c# ./chongru

In signal handler..........
Alarm clock
(fund)/fund/c#


可见,进程被默认中止。

重新安装信号后,再编译、执行:
(fund)/fund/c# xlc ./chongru.c -o ./chongru
(fund)/fund/c# ./chongru

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........
^C(fund)/fund/c#
(fund)/fund/c#
(fund)/fund/c#
(fund)/fund/c# ./chongru

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

Return value corrupted! pw_name=fund

In signal handler..........

Return value corrupted! pw_name=fund

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

In signal handler..........

Return value corrupted! pw_name=fund

In signal handler..........

可见AIX 4.3 上的确要重新安装!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7437037/viewspace-978548/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-23

  • 博文量
    37
  • 访问量
    216490