1

In APUE(Advanced programming in the UNIX environment), in second paragraph of section 10.4, mentioned the following

One problem with these early versions was that the action for a signal was reset to its default each time the signal occurred

I write the following code:

#include <unistd.h>
#include <stdio.h>
#include <signal.h>

void sh(int signo) {
        printf ("signo=%d\n\n", signo);
}

int main (int argc, char *argv[]) {
        signal(SIGUSR1, sh);

        while (1)
                pause();
        return 0;
}

But I see that everytime I send SIGUSR1 (or SIGTERM) signal to the process, signal handler does not changed.

I am using Ubuntu 22.04

tahzibi
  • 77
  • 6
  • 2
    the book is > 20y old. Many Linux programmers are younger that it, Do not learn from such old sources – 0___________ Aug 06 '23 at 13:36
  • 4
    Re “*One problem with these early versions*… I am using Ubuntu 22.04”: Ubuntu 22.04 is not an early version. What is your question? There is no actual question in your post. – Eric Postpischil Aug 06 '23 at 13:36
  • 6
    The semantics of `signal` is different. On BSD-like systems it behaves one way, on System V-like system it behaves another. Always use `sigaction` for consistent behavior. IIRC this is exactly what the book should tell you to do (long time since I read it). – Some programmer dude Aug 06 '23 at 13:36
  • It will if you use `sigaction()` with SA_RESETHAND flag. – dimich Aug 06 '23 at 13:39
  • 2
    And you [can't safely call `printf()` from within a signal handler](https://port70.net/~nsz/c/c11/n1570.html#note188): "Thus, a signal handler cannot, in general, call standard library functions." [POSIX has the concept of "async-signal-safe" functions](https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04), which **can** be safely called from a signal handler. `printf()` is not one of those functions. – Andrew Henle Aug 06 '23 at 13:39
  • 1
    I know that now we should use sigaction, but my question is: "Does action for a '**signal** system call' was reset to its **default** each time the signal occurred?" – tahzibi Aug 06 '23 at 13:40
  • @tahzibi, that question has already been addressed in these comments: it depends. – John Bollinger Aug 06 '23 at 13:41
  • @tahzibi, in any case, if it is your intention to clarify the question, then please do so by *editing* it. – John Bollinger Aug 06 '23 at 13:42
  • https://man7.org/linux/man-pages/man7/signal.7.html – 0___________ Aug 06 '23 at 13:43
  • 3
    @0___________ Don't you be doing APUE and W. Richard Stevens dirty like that. The book's the Bible of unix programming. – Shawn Aug 06 '23 at 13:50
  • 2
    The third edition of APUE is from 2013 — W Richard Stevens, Stephen A Rago [Advanced Programming in the Unix Environment, 3rd Edn](https://www.amazon.com/dp/0321637739). – Jonathan Leffler Aug 06 '23 at 14:31
  • 3
    You're not using one of those "early versions". You're using a recent version. The behaviour described was what happened on 7th Edition Unix, and System V Unix. – Jonathan Leffler Aug 06 '23 at 14:32

0 Answers0