0

So I'm currently studying POSIX threads and signals programming in C. My lecturer uses the

sigset(int sigNumber, void* signalHandlerFUnction)

Because his notes are not the best in the world, I had to do some research on my own and came across a good article that explains signals, but the author is using signal() method, that accepts the same type of parameters, the sigNumber and a pointer to the signal handler function.

From what I can see both methods accept same parameters and basically produce the same result. I read that signal() is old and not portable, so sigaction() is better. But what's the actual difference?

gEdringer
  • 16,284
  • 3
  • 14
  • 27
  • 2
    Read [signal(7)](http://man7.org/linux/man-pages/man7/signal.7.html) very carefully. Indeed, [sigaction(2)](http://man7.org/linux/man-pages/man2/sigaction.2.html) is preferable. – Basile Starynkevitch Nov 05 '14 at 17:20
  • 1
    Googling produced this answer at http://compgroups.net/comp.unix.programmer/difference-bet-sigset-sigaction-and-sign/313151 and there are plenty of references to follow up. Be prepared to do your own research as a student and not have everything handed on a plate. – Weather Vane Nov 05 '14 at 17:22
  • 4
    The [`sigset()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigset.html) family of functions are marked obsolescent by POSIX. You should use [`sigaction()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html) for many reasons, which are outlined (at least) in [What is the difference between `sigaction()` and `signal()`](http://stackoverflow.com/questions/231912/what-is-the-difference-between-sigaction-and-signal/232711#232711) – Jonathan Leffler Nov 05 '14 at 17:24
  • @WeatherVane thank you for the link :D But the down vote wasn't necessary :/ – gEdringer Nov 05 '14 at 17:25
  • 2
    Sorry about that, it was your remark *I had to do some research on my own* as though you didn't think you should have to. – Weather Vane Nov 05 '14 at 17:48

2 Answers2

7

The functions signal, sigset (and its relatives), and sigaction all establish or remove signal handlers.

signal is the only one of these functions specified by ISO C, and therefore the most portable, but it is also the most limited, and crucial aspects of its behavior are underspecified (e.g. whether or not it establishes signals that interrupt blocking system calls).

The sigset family was part of POSIX but is now marked obsolescent. These functions were never reliably available cross-platform, and didn't fix all of the underspecification problems with signal. Don't use them.

sigaction is in POSIX.1, offers more functionality than any other option, and its behavior is well-specified including all of the corner cases. It should be used if at all possible. Since it's not in ISO C, but is in POSIX.1-2001, to first order you can use it everywhere with the critical exception of Windows. However, signal handlers shouldn't be used at all on Windows, because they are not system primitives there and do not work reliably; it is my understanding that one should use structured exception handling and asynchronous procedure calls instead, depending on what one is trying to do.

Your lecturer is teaching specifically POSIX system programming, so they should update their sample code and lectures to refer only to sigaction. They really ought to cover sigprocmask, sigtimedwait, and pselect as well.

zwol
  • 135,547
  • 38
  • 252
  • 361
1

signal is used to install a signal handler or ignore a signal. sigaction is a Posix function that can ignore signals or install a signal handler. sigaction takes a signal mask so it can operate on more than one signal, whereas signal is designed to install a signal handler for one signal.

See this previous post: What is the difference between sigaction and signal?

sigset is used by Unix operating systems based on System V.

Community
  • 1
  • 1
Nick Wilkerson
  • 353
  • 2
  • 17