书上说,4.2BSD引入某些被中断系统调用的自动重启,自动重启动的系统调用包括ioctl、read、readv、write、writev、wait和waitpid。而且Linux2.4.22默认方式是重启动由信号中断的系统调用。
但是我在centOS5上执行以下代码并没有发现有重启动,代码如下:
-
#include <signal.h>
-
#include <string.h>
-
#include <stdio.h>
-
#include <errno.h>
-
#include <stdarg.h>
-
#include <stdlib.h>
-
-
-
static void sig_usr(int);
-
-
int main(void)
-
{
-
int status,retValue;
-
pid_t pid;
-
pid = fork();
-
if(pid == 0)
-
{
-
printf("child pid = %d\n",getpid());
-
sleep(10000000);
-
exit(0);
-
}
-
retValue = wait(&status);
-
-
if(retValue == -1)
-
perror("wait error");
-
else
-
printf("retValue = %d\n",retValue);
-
printf("OVER\n");
-
}
一个终端执行:
[huenyifei@localhost workspace]$ ./signal
child pid = 32355
另一个终端执行:
[huenyifei@localhost workspace]$ ps -ae|grep signal
32354 pts/2 00:00:00 signal
32355 pts/2 00:00:00 signal
[huenyifei@localhost workspace]$ kill -USR1 32354
回到第一个终端:
[huenyifei@localhost workspace]$ ./signal
child pid = 32355
User defined signal 1
[huenyifei@localhost workspace]$
并没有像书上说的那样自动重启了,而是直接退出了,并且也没执行后续代码
但是当我将代码改为如下,则是可重入的:
-
#include <signal.h>
-
#include <string.h>
-
#include <stdio.h>
-
#include <errno.h>
-
#include <stdarg.h>
-
#include <stdlib.h>
-
-
-
static void sig_usr(int);
-
-
int main(void)
-
{
-
signal(SIGUSR1,sig_usr);
-
int status,retValue;
-
pid_t pid;
-
pid = fork();
-
if(pid == 0)
-
{
-
printf("child pid = %d\n",getpid());
-
sleep(10000000);
-
exit(0);
-
}
-
retValue = wait(&status);
-
-
if(retValue == -1)
-
perror("wait error");
-
else
-
printf("retValue = %d\n",retValue);
-
printf("OVER\n");
-
}
-
-
static void sig_usr(int signo)
-
{
-
printf("received SIGUSR1\n");
-
}
Linux帮助上关于wait函数是这么说的,
If a child has already changed state, then these calls return immediately. Otherwise they block until either a child changes state or a signal handler
interrupts the call (assuming that system calls are not automatically restarted using the SA_RESTART flag of sigaction(2)).
后来我想了一下,linux默认确实是自动重启的,第一段代码之所以退出,是因为 SIGUSR1 的默认处理是终止程序
阅读(450) | 评论(0) | 转发(0) |