저는 C 프로그래밍의 놈입니다. 나는 사용자가 CRTL + C를 3 초 동안 세 번 누르는 경우 등록하는 프로그램을 만들려고합니다. 내 문제는 터미널은 항상 다음과 같이 말합니다 : 'sa'의 저장 크기는 알려져 있지 않습니다. 웹에서 많은 예제를 검색했지만 항상 같은 문제로 나타납니다. 어쩌면 모든 코드를 게시 할 수 있으므로 포함 섹션에 문제가있을 수 있습니다.sigaction 및 명령어 SIGINT를 사용하는 신호 처리기. struct 변수로 실패합니다 : 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 %d\n", signum);
if (signum == SIGUSR1){
printf("received SIGUSR1\n");
signal(SIGUSR1, SIGUSR1handler);
counter++;
}else if (signum == SIGKILL){
printf("received SIGKILL\n");
counter++;
}else if (signum == SIGSTOP){
printf("received SIGSTOP\n");
counter++;
}else if(counter>=3){
printf("shut program");
}else if (signum == SIGINT){// sorry sigint can't be handled
printf("received SIGint\n");
counter++;
}else if(signum== SIGALRM){
printf("recived SIGALRM\n");
signal(SIGALRM, ALARMhandler);
counter++;
}else if(signum== SIGHUP){
printf("recived SIGHUP\n");
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, 0, sizeof(sa));
sa.sa_handler = signalHandler;
//sigemptyset(&sa.sa_mask);
//sa.sa_flags = SA_RESTART;
counter++;
while(counter2<3){
printf("please press CRTL+C\n");
timeStart=clock();
//sigaction(SIGINT, &sa, NULL);
pause();
timeEnd=clock();
if (timeEnd - timeStart < 3){
counter++;
}else if(timeEnd - timeStart ==3){
printf("shutting down\n");
exit(1);
}//end esle if
counter2++;
}
printf("shutting down\n");
return 0;
}
문제는이 선언에있을 것 같다 struct sigaction sa;
터미널은 말한다 : no storage size of 'sa' is known
.
구조체의 매개 변수를 선언해야하지만이 선언이없는 그물에서 많은 예제를 보았습니다. 내가 뭘 잘못하고있어?
많은 else/if 문에 비해 switch 문을 사용할 것을 강력히 권합니다. –