| liupch 回复于:2003-02-14 09:22:17
|
sleep会被阻塞住。程序执行到sleep就会停止等待,sleep下面的代码就不能马上执行。而信号方式则不会
|
| unicorns 回复于:2003-02-14 09:28:50
|
我看的这个程序是顺序执行的
而且调用pause进程就被挂起了
在alarm产生信号之前也是什么事情都不能做的吧
|
| liupch 回复于:2003-02-14 09:32:45
|
你的程序采用什么结构也可能和程序员的喜好有关吧,我刚才说的只是sleep和alarm的区别
|
| tomken 回复于:2003-02-14 09:38:30
|
如果要是有一天哪么长的时间,你可以把函数做成一个程序
查查 crontab 命令吧,这样比较好
|
| unicorns 回复于:2003-02-14 09:50:53
|
谢谢各位
刚才我说的不够详细,我可以把简化的代码写一下
用signal的
alarm(somesec);
while(1) pause();
alarmHandler(){
appFunc();
alarm(somesec);
}
用sleep的
while(1){
appFunc();
sleep(somesec);
}
|
| 无双 回复于:2003-02-14 12:55:33
|
不同的地方是
一个是在信号处理程序中运行
这样一收到alarm信号就会执行
其它信号不执行
你可以向它发alram信号使它执行
另一个sleep在收到其它信号时也会醒过来
|
| unicorns 回复于:2003-02-14 13:04:31
|
哦!
我知道了,thx
那如果在sleep前把我不需要的信号都屏蔽掉
能不能做到用sleep来实现而不出问题呢
再谢:)
|
| gadfly 回复于:2003-02-14 15:36:08
|
不过有些信号也是没法屏蔽的,例如:SIGBUS,SIGSEGV
|
| tulobear 回复于:2003-02-14 14:03:02
|
sighandler_t signal (int signum, sighandler_t action)
The signal function establishes action as the action for the signal signum.
The second argument, action, specifies the action to use for the signal signum. This can be one of the following:
SIG_DFL
SIG_DFL specifies the default action for the particular signal. The default actions for various kinds of signals are stated in Standard Signals.
SIG_IGN
SIG_IGN specifies that the signal should be ignored.
比如忽略SIGCHLD信号
signal(SIGCHLD, SIG_IGN);
|
| 无双 回复于:2003-02-14 15:22:13
|
有一些信号不应该屏蔽掉
因为这些信号是程序出错的提示
如内存访问越界
所以屏蔽不是最好的办法
|
| unicorns 回复于:2003-02-14 15:48:59
|
多谢,多谢
可能是我惰性太重,老是想用最省事的办法
|