catchsignal


 
Thread Tools Search this Thread
Top Forums Programming catchsignal
# 1  
Old 12-06-2006
catchsignal

how does the catchsignal know what signal it needs to ignore? thanks
# 2  
Old 12-06-2006
catchsignal isn't part of POSIX UNIX, it sounds like a function inside a program - one somebody wrote.

Can you tell us what you are doing, and what you are trying to answer.
# 3  
Old 12-07-2006
hey jim. my prof gave me this program...
Code:
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void catchsignal(int signo) {
   char handmsg[] = "You cannot \"Ctrl-C\" me!\n";
   int msglen = sizeof(handmsg);

   write(STDERR_FILENO, handmsg, msglen);
}


int main() {

   struct sigaction act;
   act.sa_handler = catchsignal;
   act.sa_flags = 0;
   if ((sigemptyset(&act.sa_mask) == -1) ||
      (sigaction(SIGINT, &act, NULL) == -1)) {
      printf("Failed to set SIGINT to handle Ctrl-C");
   }

   while(1);
}

that program catches the signal Ctrl C which if I'm not mistaken is one of the kill commands in linux. i can't remember what specific kill command is that. my question is how does the catchsignal function learn which signal to ignore? thanks.

Last edited by blowtorch; 12-11-2006 at 05:22 AM..
# 4  
Old 12-07-2006
Quote:
my question is how does the catchsignal function learn which signal to ignore?
Only signal(s) you 'intercept' will be handled by catchsignal.
So you cant kill that program with ctrl-c but try delivering it signal like TERM
Code:
kill -TERM pid

and you'll see ...
# 5  
Old 12-11-2006
Quote:
Originally Posted by andryk
Only signal(s) you 'intercept' will be handled by catchsignal.
So you cant kill that program with ctrl-c but try delivering it signal like TERM
Code:
kill -TERM pid

and you'll see ...
yup, i know it wouldn't be able to ignore the sigterm...i just want to know how it intercept the ctrl-c...?
# 6  
Old 12-11-2006
Quote:
Originally Posted by kelogs1347
yup, i know it wouldn't be able to ignore the sigterm...i just want to know how it intercept the ctrl-c...?
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.
# 7  
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.
thank you very much.
Login or Register to Ask a Question

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