LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 540|回复: 2

一段引起段错误的程序

[复制链接]
发表于 2005-3-27 22:50:53 | 显示全部楼层 |阅读模式
  1. #include<signal.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<sys/types.h>
  5. #include<unistd.h>

  6. static void child_sigfun(int signo){
  7.   if( signo == SIGALRM){
  8.    puts("catch SIGALRM\n");
  9.   }
  10. }

  11. static void parent_sigfun(int signo){
  12.   if(signo == SIGUSR1){
  13.     puts("catch SIGUSR1");
  14.   }
  15. }

  16. int main(){
  17.   pid_t pid,ppid;
  18.   sigset_t sigs,psigs;
  19.   struct sigaction psa_new;
  20.   struct sigaction psa_old;
  21.   struct sigaction sa_new;
  22.   struct sigaction sa_old;
  23.   psa_new.sa_handler = parent_sigfun;
  24.   sigemptyset(&psa_new.sa_mask);
  25.   sigaddset(&psa_new.sa_mask,SIGALRM);
  26.   psa_new.sa_flags = 0;
  27.   sigaction(SIGUSR1,&psa_new,&psa_old);
  28.   sigfillset(&sigs);
  29.   sigfillset(&psigs);
  30.   sigdelset(&psigs,SIGUSR1);
  31.   sigdelset(&sigs,SIGALRM);

  32.   sa_new.sa_handler = child_sigfun;
  33.   sigemptyset(&sa_new.sa_mask);
  34.   sigaddset(&sa_new.sa_mask,SIGUSR1);
  35.   sa_new.sa_flags = 0;
  36.   sigaction(SIGALRM,&sa_new,&sa_old);


  37.   if((pid = fork()) == 0){
  38.     puts("child process\n");
  39.     alarm(3);
  40.     sigsuspend(&sigs);
  41.     kill(getppid(),SIGUSR1);
  42.   }else{
  43.     puts("parent process\n");
  44.     sigsuspend(&psigs);
  45.   }

  46.   return 0;
  47. }
复制代码


[root@localhost linux24]# gcc signalset.c
[root@localhost linux24]# ./a.out
parent process

child process

catch SIGALRM
Segmentation fault
找不出原因,请指教,谢谢!
 楼主| 发表于 2005-3-27 23:15:06 | 显示全部楼层
犯了小错误。已解决,请斑主删掉吧。
好像不能删自己的贴子啊。
回复 支持 反对

使用道具 举报

发表于 2005-3-28 09:29:28 | 显示全部楼层
我倒,有错误嘛,要大家分享下啊,大家以后就不会再犯了啊!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表