catchsignal


 
Thread Tools Search this Thread
Top Forums Programming catchsignal
# 8  
Old 12-11-2006
Quote:
Originally Posted by blowtorch
Code:
struct sigaction act;
   act.sa_handler = catchsignal;
   act.sa_flags = 0;
   if ((sigemptyset(&act.sa_mask) == -1) ||
      (sigaction(SIGINT, &act, NULL) == -1))

This bunch of statements is where the signal handler is being setup. The "act.sa_handler=catchsignal;" statement spells out the function that will be used (catchsignal). And the statement signaction(SIGINT,&act,NULL) specifies that values from the act structure are to be associated with the reception of SIGINT.
one more question...how could i get out of the if loop in the statement above. say i want to have another catchsignal...?
# 9  
Old 12-11-2006
Here's another way of writing the code:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void catchsignal(int signo) {
        fprintf(stdout,"Ha! Caught signal %d\n",signo);
}


int main() {

        struct sigaction act;
        act.sa_handler = catchsignal;
        act.sa_flags = 0;
        if(sigemptyset(&act.sa_mask)==-1) {
                perror("error in sigemptyset!!\n");
        }
        if(sigaction(SIGINT,&act,NULL)==-1) {
                perror("Failed to set SIGINT to handle Ctrl-C\n");
        }
        if(sigaction(SIGQUIT,&act,NULL)==-1) {
                perror("Failed to set SIGQUIT to handle Ctrl-\\\n");
        }

while(1);
}

You can just keep adding signals to that list.
# 10  
Old 12-13-2006
As a side note - you might be better off with using write() in a signal handler.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question