1

I want to handle the signal SIGTSTP (18) on Linux. This is my code:

void handler(int signum){ printf("Signal %d has tried to stop me.", signum); } int main(void){ struct sigaction act; sigset_t mask; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&mask); sigaddset(&mask, SIGTSTP); sigprocmask(SIG_SETMASK, &mask, NULL); sigaction(SIGTSTP, &act, NULL); pause(); } 

When I send a signal from another terminal this way:

$ kill -18 PID 

the handler() function does not run. I've tried replacing SIGTSTP with 18 in the call to sigaction(). It does not work.

Any ideas? Thanks for your time.

2 Answers 2

2

You are deliberately blocking delivery of SIGTSTP to your process by doing

sigaddset(&mask, SIGTSTP); sigprocmask(SIG_SETMASK, &mask, NULL); 

sigprocmask with SIG_SETMASK sets a mask to block signals. Remove the call to sigprocmask, and the signal should be delivered.

One other thing to do is to zero act before filling it in:

memset(&act, 0, sizeof(act)); 
Sign up to request clarification or add additional context in comments.

Comments

0

The most likely reason is that you did not set your struct sigaction act to zero. It will be full of random stack values.

Fix it by using struct sigaction act = {}; or memset(&act, 0, sizeof(act))

Double check that I got the memset argument order right. I sometimes get it mixed up.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.