加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用sigaction和指令SIGINT的信号处理程序.使用struct变量失败:

发布时间:2020-12-16 09:49:22 所属栏目:百科 来源:网络整理
导读:我是C编程的菜鸟.我只是想制作一个程序,如果用户在三秒钟内点击三次CRTL C就会注册.我的问题是终端总是这样说:没有’sa’的存储大小.我在网上搜索了很多例子,但似乎总是出现同样的问题.也许这是包含部分的问题,所以我发布了所有代码. #include stdio.h#incl
我是C编程的菜鸟.我只是想制作一个程序,如果用户在三秒钟内点击三次CRTL C就会注册.我的问题是终端总是这样说:没有’sa’的存储大小.我在网上搜索了很多例子,但似乎总是出现同样的问题.也许这是包含部分的问题,所以我发布了所有代码.

#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include  <stdlib.h>


int counter=0;
int counter2=0;
//time timestart;
//time timeend;
void signalHandler(int signum);
void  ALARMhandler(int sig);
void SIGHUPhandler();
void SIGUSR1handler();


void signalHandler(int signum){ 
alarm(3);
//printf("the signal caught %dn",signum);
if (signum == SIGUSR1){
        printf("received SIGUSR1n");
    signal(SIGUSR1,SIGUSR1handler);
    counter++;
    }else if (signum == SIGKILL){
        printf("received SIGKILLn");
    counter++;
    }else if (signum == SIGSTOP){
        printf("received SIGSTOPn");
    counter++;
}else if(counter>=3){
    printf("shut program");

}else if (signum == SIGINT){// sorry sigint can't be handled
        printf("received SIGintn");
    counter++;
}else if(signum== SIGALRM ){
    printf("recived SIGALRMn");
    signal(SIGALRM,ALARMhandler);
    counter++;
}else if(signum== SIGHUP){
    printf("recived SIGHUPn");
    signal(SIGHUP,SIGHUPhandler);
}
//exit(signum);
  }


  int main( void )
  {
/* Place your handler somewhere around here */
clock_t timeStart,timeEnd;
printf( "Hello World!n" );
//for ( ;; )
//{
    /* infinite loop */
//}

struct sigaction sa;
memset (&sa,sizeof(sa));
sa.sa_handler = signalHandler;
//sigemptyset(&sa.sa_mask);
//sa.sa_flags = SA_RESTART;


counter++;

while(counter2<3){
    printf("please press CRTL+Cn");
    timeStart=clock();
    //sigaction(SIGINT,&sa,NULL);
    pause();
    timeEnd=clock();
    if (timeEnd - timeStart < 3){
        counter++;
    }else if(timeEnd - timeStart ==3){
        printf("shutting downn");
        exit(1);
    }//end esle if


    counter2++;

}
printf("shutting downn");
return 0;

}

问题似乎出现在这个声明中:struct sigaction sa;
终端说:没有’sa’的存储大小.

我知道必须声明struct的参数,但我在网上看到了许多没有这个声明的例子.我做错了什么?

解决方法

我猜你在C99下编译.如果你是,你需要#define宏_XOPEN_SOURCE进行sigaction和朋友可用.如果使用选项-D_XOPEN_SOURCE进行编译,它应该可以正常工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读