I'm a noob with C programming. I'm just trying to make a program that registers if the user taps three times CRTL+C in three seconds. My problem is that the terminal says always that: no storage size of 'sa' is known. I searched for many examples in the web but it appears always the same problem. Maybe it's a problem withe the include section so I post all my code.
#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;
}
The problem seems to be in this declaration: struct sigaction sa;
The terminal says: no storage size of 'sa' is known.
I know that the parameters of struct must be declared but I've seen many examples on the net that doesn't have this declaration. What I'm doing wrong?
I'm guessing that you're compiling under C99. If you are, you need to #define the macro _XOPEN_SOURCE for sigaction and friends to be available. If you compile with the option -D_XOPEN_SOURCE it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With