signal

SIGNAL(2)                  Linux Programmer's Manual                 SIGNAL(2)



åå
       signal - ANSI C ã·ã°ãã«æä½

æ¸å¼
       #include <signal.h>

       typedef void (*sighandler_t)(int);

       sighandler_t signal(int signum, sighandler_t sighandler);

説æ
       signal()  ã®åä½ã¯ UNIX ã®ãã¼ã¸ã§ã³ã«ããç°ãªãã ã¾ããæ´å²çã«è¦ã¦ Linux
       ã®ãã¼ã¸ã§ã³ã«ãã£ã¦ãç°ãªã£ã¦ããã ãã®ã·ã¹ãã ã³ã¼ã«ã®ä½¿ç¨ã¯é¿ãã 代ããã«
       sigaction(2)  ã使ç¨ãããã¨ã ä¸è¨ã®ã移æ¤æ§ããåç§ã

       signal()  ã¯ã·ã°ãã« signum ã®å¦çæ¹æ³ã handler ã«è¨å®ããã handler ã«ã¯ã
       SIG_IGNã SIG_DFLã ããã°ã©ããå®ç¾©ããé¢æ° (ãã·ã°ãã«ã»ãã³ãã©ã) ã®ã¢ãã¬ã¹ã®
       ãããããæå®ããã

       ã·ã°ãã« signum ãããã»ã¹ã«ééãããã¨ã以ä¸ã®ãããããçºçããã

       *  å¦çæ¹æ³ã SIG_IGN ã«è¨å®ããã¦ããå ´åããã®ã·ã°ãã«ã¯ç¡è¦ãããã

       *  å¦çæ¹æ³ã SIG_DFL ã«è¨å®ããã¦ããå ´åãã·ã°ãã«ã«é¢é£ã¥ãããã
          ããã©ã«ãã®åä½ãè¡ããã (signal(7)  åç§)ã

       *  å¦çæ¹æ³ã¨ãã¦é¢æ°ãè¨å®ããã¦ããå ´åã ã¾ãæåã«å¦çæ¹æ³ã SIG_DFL
          ã«ãªã»ãããããããã®ã·ã°ãã«ã®ãããã¯ãå®è¡ãããå¾ã signum ãå¼ãæ°ã¨ã㦠handler
          ãå¼ã³åºãããã ãã³ãã©ãèµ·åãããéã«ã·ã°ãã«ããããã¯ãããå ´åã
          ãã³ãã©ãè¿ãéã«ãã®ã·ã°ãã«ã®ãããã¯ã解é¤ãããã

       ã·ã°ãã« SIGKILL 㨠SIGSTOP ã¯ææã§ãããç¡è¦ãããã¨ãã§ããªãã

è¿ãå¤
       signal()  ã¯ãä»ã¾ã§ã®ã·ã°ãã«ã»ãã³ãã©ã®å¤ãè¿ããã¨ã©ã¼ã®å ´å㯠SIG_ERR
       ãè¿ãã

ã¨ã©ã¼
       EINVAL signum ãä¸æ£ã§ããã

æºæ
       C89, C99, POSIX.1-2001.

注æ
       ãã«ãã¹ã¬ããããã»ã¹ã«ããã signal()  ã®çµæã¯ãæå®ããã¦ããªãã

       POSIX ã§ã¯ã kill(2)  ã raise(3)  ã§çæã§ããªãã·ã°ãã« SIGFPE, SIGILL,
       SIGSEGV ãç¡è¦ (ignore) ããå ´åããã®å¾ã®åä½ã¯æªå®ç¾©ã§ããã ã¼ã‐
       ã«ããæ´æ°å²ãç®ã®çµæã¯æªå®ç¾©ã¨ãªãã ã¢ã¼ããã¯ãã£ã¼ã«ãã£ã¦ã¯ããã®ã¨ã SIGFPE
       ã·ã°ãã«ãçæãããã (åæ§ã«è² ã®æ大æ´æ°ã -1 ã§å²ã㨠SIGFPE ãçæããããããããªã)
       ãã®ã·ã°ãã«ãç¡è¦ããã¨ç¡éã«ã¼ãã«é¥ããããããªãã

       SIGCHLD ã®åä½ã¨ã㦠SIG_IGN ãè¨å®ããå ´åã®è©³ç´°ãªåä½ã«ã¤ãã¦ã¯ã
       sigaction(2)  ãåç§ãããã¨ã

       ã·ã°ãã«ã»ãã³ãã©åããå®å¨ã«å¼ã³åºããã¨ãã§ããã async-signal-safe functions
       (éåæã·ã³ã°ã«ã§å®å¨ãªé¢æ°) ã® ãªã¹ãã«ã¤ãã¦ã¯ signal(7)  ãåç§ã

       sighandler_t ã®ä½¿ç¨ã¯ GNU æ¡å¼µã§ããã _GNU_SOURCE ãå®ç¾©ããã
       å ´åã«å¬éããããglibc ã§ã¯ _BSD_SOURCE ãå®ç¾©ãããå ´åã«ã¯ (BSD ç±æ¥ã®)
       sig_t ãå®ç¾©ãããããã®ãããªåã使ç¨ããªãã¨ã signal() ã®å®£è¨ã¯èª‐
       ã¿ã«ãããã®ã¨ãªãã

           void ( *signal(int signum, void (*handler)(int)) ) (int);

   移æ¤æ§
       移æ¤æ§ã®ãã signal()  ã®ä½¿ãæ¹ã¯ãã·ã°ãã«ã®å¦çæ¹æ³ã SIG_DFL ã SIG_IGN
       ã«è¨å®ããæ¹æ³ã ãã§ããã ã·ã°ãã«ã»ãã³ãã©ãè¨å®ããã®ã« signal()
       ã使ã£ãã¨ãã®åä½ã¯ã·ã¹ãã ã«ããç°ãªã (POSIX.1 ã¯æ示çã«ãã®éããèªãã¦ãã)ã
       移æ¤æ§ãå¿è¦ãªã¨ãã¯ãã®ã·ã¹ãã ã³ã¼ã«ã使ç¨ããªããã¨ã

       POSIX.1 ã¯ã sigaction(2)  ãè¦å®ãããã¨ã§ç§»æ¤æ§ã«é¢ããæ··ä¹±ã解決ããã
       sigaction(2) ã¯ã·ã°ãã«ã»ãã³ãã©ãèµ·åãããéã®æåãæ示çã«å¶å¾¡ã§ããã signal()
       ã®ä»£ããã«ãã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã使ããã¨ã

       ãªãªã¸ãã«ã® UNIX ã·ã¹ãã ã§ã¯ã signal()  ã使ã£ã¦è¨‐
       å®ããããã³ãã©ãã·ã°ãã«ã®ééã«ããèµ·åãããã¨ã ãã®ã·ã°ãã«ã®å¦çæ¹æ³ã¯ SIG_DFL
       ã«ãªã»ãããããã·ã¹ãã ã¯åãã·ã°ãã«ãããã«çæããã¦ã ã·ã°ãã«ã®ééããã‐
       ãã¯ããªãã£ããããã¯ã以ä¸ã®ãã©ã°ã§ sigaction(2) ãå¼ã³åºãã®ã¨ç価ã§ããã

           sa.sa_flags = SA_RESETHAND | SA_NODEFER;

       System V ã§ãã signal()  ã«å¯¾ãã¦ãããã®æåãè¦å®ãã¦ããã
       ããããæåã¯ã¾ããããã³ãã©ããã³ãã©èªèº«ãåè¨å®ããæ©ä¼ã
       æ¥ãããåã«ãåãã·ã°ãã«ãã¾ãééãããå¯è½æ§ãããã
       ããã«ãåãã·ã°ãã«ãç«ã¦ç¶ãã«ééãããã¨ãåãã·ã°ãã«ã
       ãã³ãã©ãç¹°ãè¿ãèµ·åããããã¨ã«ãªãã

       BSD improved on this situation, but unfortunately also changed the
       semantics of the existing signal()  interface while doing so.  On BSD,
       when a signal handler is invoked, the signal disposition is not reset,
       and further instances of the signal are blocked from being delivered
       while the handler is executing.  Furthermore, certain blocking system
       calls are automatically restarted if interrupted by a signal handler
       (see signal(7)).  The BSD semantics are equivalent to calling
       sigaction(2)  with the following flags:

           sa.sa_flags = SA_RESTART;

       Linux ã§ã®ç¶æ³ã¯ä»¥ä¸ã®éãã§ããã

       * ã«ã¼ãã«ã® signal()  ã·ã¹ãã ã³ã¼ã«ã¯ System V æ¹å¼ãæä¾ãã¦ããã

       * ããã©ã«ãã§ã¯ãglibc 2 以éã§ã¯ã signal()
         ã©ããã¼é¢æ°ã¯ã«ã¼ãã«ã®ã·ã¹ãã ã³ã¼ã«ãèµ·åããªãã 代ããã«ãã©ããã¼é¢æ°ã¯ BSD
         æ¹å¼ã示ããã©ã°ã使ã£ã¦ sigaction(2)  ãå¼ã³åºãã æ©è½æ¤æ»ãã¯ã
         _BSD_SOURCE ãå®ç¾©ãã¦ããã°ããã®ããã©ã«ãã®åä½ã¨ãªãã ããã©ã«ãã§ã¯ã
         _BSD_SOURCE ãå®ç¾©ãããã _BSD_SOURCE 㯠_GNU_SOURCE
         ãå®ç¾©ãããå ´åã«ã¯æé»ã®ãã¡ã«å®ç¾©ããã ãã¡ããæ示çã«å®ç¾©ãããã¨ãã§ããã

         glibc 2 以éã§ã¯ãæ©è½æ¤æ»ãã¯ã _BSD_SOURCE ãå®ç¾©ããã¦ããªããã°ã signal()
         㯠System V æ¹å¼ã¨ãªãã (gcc(1)  ãæ¨æºæå®ã¢ã¼ã (-std=xxx or -ansi)
         ã§èµ·åãããå ´åãããã㯠_POSIX_SOURCE, _XOPEN_SOURCE, _SVID_SOURCE
         ã¨ãã£ãä»ã®æ§ããªæ©è½æ¤æ»ãã¯ããå®ç¾©ãããå ´åã ããã©ã«ãã® _BSD_SOURCE
         ã®æé»ã®å®ç¾©ã¯è¡ãããªãã feature_test_macros(7)  ãåç§ã®ãã¨ã)

       * Linux ã® libc4 㨠libc5 ã® signal()  é¢æ°ã¯ System V æ¹å¼ã§ããã libc5
         ã·ã¹ãã ã«ãã㦠<signal.h> ã®ãããã« <bsd/signal.h> ãã¤ã³ã¯ã«ã¼ãããã¨ã
         signal()  㯠__bsd_signal()  ã«åå®ç¾©ããã signal()  㯠BSD æ¹å¼ã¨ãªãã

é¢é£é ç®
       kill(1), alarm(2), kill(2), killpg(2), pause(2), sigaction(2),
       signalfd(2), sigpending(2), sigprocmask(2), sigsuspend(2),
       bsd_signal(3), raise(3), siginterrupt(3), sigqueue(3), sigsetops(3),
       sigvec(3), sysv_signal(3), signal(7)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2013-02-09                         SIGNAL(2)