Thread: signal vs sigaction

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    Smile signal vs sigaction

    hi, all

    what is difference between signal and sigaction?
    if sigaction is better then signal then why?
    is it portability issue in both?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    signal() is standard C, sigaction() is not.

    If you're able to use either (that is, you're on a POSIX system), then use sigaction(); it's unspecified whether signal() resets the handler, meaning that to be portable you have to call signal() again inside the handler. What's worse is that there's a race: if you get two signals in quick succession, and the second is delivered before you reinstall the handler, you'll have the default action, which is probably going to be to kill your process.

    sigaction(), on the other hand, is guaranteed to use “reliable” signal semantics. You need not reinstall the handler, because it will never be reset. With SA_RESTART, you can also get some system calls to automatically restart (so you don't have to manually check for EINTR).

    sigaction() has more options and is reliable, so its use is encouraged.

    Psst... don't tell anyone I told you this, but POSIX currently has a function bsd_signal() which acts like signal() but gives BSD semantics, which means it's reliable. Its main use is for porting old applications that assumed reliable signals, and POSIX does not recommend using it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-07-2009, 10:05 AM
  2. Wrapping sigaction() in loop until != EINTR;
    By heras in forum C Programming
    Replies: 5
    Last Post: 04-05-2008, 05:39 AM
  3. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  4. keyboard capturing
    By xlnk in forum Linux Programming
    Replies: 4
    Last Post: 01-31-2003, 01:02 PM
  5. signal handling
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 02:52 AM