请教 。关于alarm.

kpman 2009-06-15 03:46:22
#include <signal.h>

void alarm_test();
main()
{

signal( SIGALRM, alarm_test); //我就是想每10运行alarm_test()一次。可是等十秒后程序退出了。何解?


signal(SIGCLD, SIG_IGN);
printf("kill function()\n" );
kill(getpid(), SIGALRM);


while(1)
{
printf("before pause ....\n");
pause();
}

printf("end the program \n" );
}

void alarm_test()
{
printf("this is alarm_test() function\n" );
alarm(10);
}


运行结果如下:
kill function()
this is alarm_test() function
before pause ....
Alarm call(运行完后就直接退出了。这个 Alarm call 是哪个涵数发出的呢 ?
...全文
172 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2009-06-16
  • 打赏
  • 举报
回复
Unix下写程序,手边要准备3件宝贝:
1. ISO C99 标准,
http://cpp.ga-la.com/html/8/9/0510/54.htm
2. The Single UNIX® Specification, Version 2 (Unix 98)
http://www.opengroup.org/onlinepubs/7990989775/toc.htm
3. Advanced Programming in the UNIX® Environment: Second Edition
By W. Richard Stevens, Stephen A. Rago
http://download.csdn.net/source/161963
kpman 2009-06-16
  • 打赏
  • 举报
回复
oh..man thanks a lot.
mymtom 2009-06-16
  • 打赏
  • 举报
回复
From APUE 10.4. Unreliable Signals
[code=BatchFile]
In earlier versions of the UNIX System (such as Version 7), signals were unreliable.
By this we mean that signals could get lost: a signal could occur and the process
would never know about it. Also, a process had little control over a signal: a process
could catch the signal or ignore it. Sometimes, we would like to tell the kernel to
block a signal: don't ignore it, just remember if it occurs, and tell us later when
we're ready.

Changes were made with 4.2BSD to provide what are called reliable signals. A different
set of changes was then made in SVR3 to provide reliable signals under System V. POSIX.1
chose the BSD model to standardize.

One problem with these early versions is that the action for a signal was reset to its
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
default each time the signal occurred. (In the previous example, when we ran the program
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

in Figure 10.2, we avoided this detail by catching each signal only once.) The classic
example from programming books that described these earlier systems concerns how to handle
the interrupt signal. The code that was described usually looked like.
[/code]
注意这句话:
[code=BatchFile]
action for a signal was reset to its default each time the signal occurred
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[/code]
简单来说就是有很多操作系统(包括HP-UP, AIX),在收到信号后,系统会重置进程的信号处理动作为缺省值
(SIGALRM的缺省动作就是结束进程)
所以信号处理函数的第一个调用就是重建信号处理函数,(虽然有些操作系统,比如Linux不会重置信号信号处理方式)
但是这里有个时间窗,就是在信号处理函数内调用signal之前又收到信号的话。。。

现在一般推荐的函数是sigaction, 可以提供可靠的信号处理。
kpman 2009-06-16
  • 打赏
  • 举报
回复
楼上的对呀 就加一句。。为什么会这样。。?不明白。
mymtom 2009-06-16
  • 打赏
  • 举报
回复
有错,编译不过?

#include <signal.h>
#include <unistd.h>
void alarm_test(int sig);
int main()
{

signal( SIGALRM, alarm_test);
signal(SIGCLD, SIG_IGN);

printf("kill function()\n" );
kill(getpid(), SIGALRM);

while(1)
{
printf("before pause ....\n");
pause();
}

printf("end the program \n" );
return 0;
}

void alarm_test(int sig)
{
signal( SIGALRM, alarm_test); /* TODO: 加上这句应该就可以了 */
printf("this is alarm_test() function\n" );
alarm(10);
}

mymtom 2009-06-16
  • 打赏
  • 举报
回复

#include <signal.h>
#include <unistd.h>
void alarm_test(int);
int main()
{

signal( SIGALRM, alarm_test);
signal(SIGCLD, SIG_IGN);

printf("kill function()\n" );
kill(getpid(), SIGALRM);

while(1)
{
printf("before pause ....\n");
pause();
}

printf("end the program \n" );
return 0;
}

void alarm_test(int)
{
signal( SIGALRM, alarm_test); /* TODO: 加上这句应该就可以了 */
printf("this is alarm_test() function\n" );
alarm(10);
}

kpman 2009-06-16
  • 打赏
  • 举报
回复
cc -o t t.c
Treazy 2009-06-16
  • 打赏
  • 举报
回复
能把你的编译指令帖一下吗?并注明编译环境,编译器版本等
事实上,我运行很正常,并没发现你说的那个问题!

kpman 2009-06-16
  • 打赏
  • 举报
回复
???
kpman 2009-06-15
  • 打赏
  • 举报
回复
是的 头文件全包含进去了。。可以正常编译。
Treazy 2009-06-15
  • 打赏
  • 举报
回复
事实上你的程序应该包含头文件unistd.h
就象我写的那样

因为HP-UX上,unistd由HP开发
  • 打赏
  • 举报
回复
Alarm call

这么搞笑的?那里有这句?你去掉pause,就while(1)看看,看正常不
kpman 2009-06-15
  • 打赏
  • 举报
回复
HP UINX
Treazy 2009-06-15
  • 打赏
  • 举报
回复

#include <signal.h>
#include <unistd.h>
void alarm_test(int);
int main()
{

signal( SIGALRM, alarm_test);
signal(SIGCLD, SIG_IGN);

printf("kill function()\n" );
kill(getpid(), SIGALRM);

while(1)
{
printf("before pause ....\n");
pause();
}

printf("end the program \n" );
return 0;
}

void alarm_test(int)
{
printf("this is alarm_test() function\n" );
alarm(10);
}

确认一下你的编译环境是?你的运行环境是?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧