Chinaunix首页 | 论坛 | 博客
  • 博客访问: 39289
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 285
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-08 13:03
个人简介

海纳百川有容乃大,壁立千仞无欲则刚。

文章分类
文章存档

2015年(17)

2014年(12)

我的朋友

分类: LINUX

2015-01-19 13:56:21

书上说,4.2BSD引入某些被中断系统调用的自动重启,自动重启动的系统调用包括ioctl、read、readv、write、writev、wait和waitpid。而且Linux2.4.22默认方式是重启动由信号中断的系统调用。
但是我在centOS5上执行以下代码并没有发现有重启动,代码如下:

  1. #include <signal.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <stdarg.h>
  6. #include <stdlib.h>


  7. static void sig_usr(int);

  8. int main(void)
  9. {
  10.         int status,retValue;
  11.         pid_t pid;
  12.         pid = fork();
  13.         if(pid == 0)
  14.         {
  15.                 printf("child pid = %d\n",getpid());
  16.                 sleep(10000000);
  17.                 exit(0);
  18.         }
  19.         retValue = wait(&status);

  20.         if(retValue == -1)
  21.                 perror("wait error");
  22.         else
  23.                 printf("retValue = %d\n",retValue);
  24.         printf("OVER\n");
  25. }

一个终端执行:
[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]$

并没有像书上说的那样自动重启了,而是直接退出了,并且也没执行后续代码

但是当我将代码改为如下,则是可重入的:

  1. #include <signal.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <stdarg.h>
  6. #include <stdlib.h>


  7. static void sig_usr(int);

  8. int main(void)
  9. {
  10.         signal(SIGUSR1,sig_usr);
  11.         int status,retValue;
  12.         pid_t pid;
  13.         pid = fork();
  14.         if(pid == 0)
  15.         {
  16.                 printf("child pid = %d\n",getpid());
  17.                 sleep(10000000);
  18.                 exit(0);
  19.         }
  20.         retValue = wait(&status);

  21.         if(retValue == -1)
  22.                 perror("wait error");
  23.         else
  24.                 printf("retValue = %d\n",retValue);
  25.         printf("OVER\n");
  26. }

  27. static void sig_usr(int signo)
  28. {
  29.         printf("received SIGUSR1\n");
  30. }
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) |
给主人留下些什么吧!~~