Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use `sigsetjmp` instead of `setjmp` function in C?

Tags:

c

signals

Why might somebody choose to use sigsetjmp instead of setjmp in C?

I read in a book that there are some disadvantages in using setjmp with signal-related code, so the sigsetjmp function was implemented.

Can anybody explain this with an example?

Thanks in Advance.

like image 910
Chandru Avatar asked Dec 24 '13 05:12

Chandru


People also ask

What is Sigsetjmp?

The sigsetjmp() function saves the current stack environment and, optionally, the current signal mask. The stack environment and signal mask saved by sigsetjmp() can subsequently be restored by siglongjmp(). sigsetjmp() is similar to setjmp(), except for the optional capability of saving the signal mask.

How does sigsetjmp work?

The return value from sigsetjmp() tells you whether it is being called for the first time (return value 0), or whether it is returning as the result of a siglongjmp() (return value non-zero). The behaviour of sigjmp() and longjmp() is the same.

What is Siglongjmp?

The siglongjmp() function restores the stack environment previously saved in env by sigsetjmp(). siglongjmp() also provides the option to restore the signal mask, depending on whether the signal mask was saved by sigsetjmp().


1 Answers

See section 10.15 in “Advanced Programming in the UNIX® Environment”:

the setjmp and longjmp functions, which can be used for nonlocal branching. The longjmp function is often called from a signal handler to return to the main loop of a program, instead of returning from the handler. We saw this in Figures 10.8 and 10.11.

There is a problem in calling longjmp, however. When a signal is caught, the signal-catching function is entered with the current signal automatically being added to the signal mask of the process. This prevents subsequent occurrences of that signal from interrupting the signal handler. If we longjmp out of the signal handler, what happens to the signal mask for the process?

Under FreeBSD 5.2.1 and Mac OS X 10.3, setjmp and longjmp save and restore the signal mask. Linux 2.4.22 and Solaris 9, however, do not do this. FreeBSD and Mac OS X provide the functions _setjmp and _longjmp, which do not save and restore the signal mask.

To allow either form of behavior, POSIX.1 does not specify the effect of setjmp and longjmp on signal masks. Instead, two new functions, sigsetjmp and siglongjmp, are defined by POSIX.1. These two functions should always be used when branching from a signal handler.

...

The only difference between these functions and the setjmp and longjmp functions is that sigsetjmp has an additional argument. If savemask is nonzero, then sigsetjmp also saves the current signal mask of the process in env. When siglongjmp is called, if the env argument was saved by a call to sigsetjmp with a nonzero savemask, then siglongjmp restores the saved signal mask.

like image 114
paulsm4 Avatar answered Oct 13 '22 20:10

paulsm4