2012-09-25 26 views
1

我有信號處理器sa_sigaction參數

struct sigaction pipe_act; 
pipe_act.sa_flags = SA_SIGINFO; 
pipe_act.sa_sigaction = sigpipehandler 
sigaction(SIGPIPE, &pipe_act, NULL); 

當我嘗試寫sigpipeHandler,GCC告訴我,它需要三個參數。第二個參數是好的,它是siginfo_t結構,它包含有關信號的信息,第一個和第三個(一個int和一個void變量),它們是什麼?

回答

5

第一個參數是信號編號。這是必要的,因爲你可以使用相同的處理程序來處理幾個不同的信號

第三個參數包含接收信號時正在使用的上下文。可以通過makecontext()/setcontext()/getcontext()/swapcontext()函數使用上下文來實現用戶空間線程。上下文結構還包含非便攜式體系結構特定信息,例如,處理器在收到信號時寄存器的值。與處理器狀態混淆需要您自擔風險。

+0

好吧,所以只是宣佈假兩個參數。我爲每個信號使用一個處理程序,並且我不需要知道特定代碼情況下的上下文。謝謝。 – giozh

7

sigaction的人說:

The sigaction structure is defined as something like: 

     struct sigaction { 
      void  (*sa_handler)(int); 
      void  (*sa_sigaction)(int, siginfo_t *, void *); 
      sigset_t sa_mask; 
      int  sa_flags; 
      void  (*sa_restorer)(void); 
     }; 

    sa_handler specifies the action to be associated with signum and may be 
    SIG_DFL for the default action, SIG_IGN to ignore this signal, or a 
    pointer to a signal handling function. This function receives the sig‐ 
    nal number as its only argument. 

    If SA_SIGINFO is specified in sa_flags, then sa_sigaction (instead of 
    sa_handler) specifies the signal-handling function for signum. This 
    function receives the signal number as its first argument, a pointer to 
    a siginfo_t as its second argument and a pointer to a ucontext_t (cast 
    to void *) as its third argument. 


    .... 
The siginfo_t argument to sa_sigaction is a struct with the following 
    elements: 
 siginfo_t { 
      int  si_signo; /* Signal number */ 
      int  si_errno; /* An errno value */ 
      int  si_code;  /* Signal code */ 
      int  si_trapno; /* Trap number that caused 
            hardware-generated signal 
            (unused on most architectures) */ 
      pid_t si_pid;  /* Sending process ID */ 
      uid_t si_uid;  /* Real user ID of sending process */ 
      int  si_status; /* Exit value or signal */ 
      clock_t si_utime; /* User time consumed */ 
      clock_t si_stime; /* System time consumed */ 
      sigval_t si_value; /* Signal value */ 
      int  si_int;  /* POSIX.1b signal */ 
      void *si_ptr;  /* POSIX.1b signal */ 
      int  si_overrun; /* Timer overrun count; POSIX.1b timers */ 
      int  si_timerid; /* Timer ID; POSIX.1b timers */ 
      void *si_addr;  /* Memory location which caused fault */ 
      long  si_band;  /* Band event (was int in 
            glibc 2.3.2 and earlier) */ 
      int  si_fd;  /* File descriptor */ 
      short si_addr_lsb; /* Least significant bit of address 
            (since kernel 2.6.32) */ 
     } 
+0

第一個arg的必要性是什麼?如果我收到(在這種情況下)sigpipe,並且sigpipeHandler它被執行,我知道信號編號是sigpipe ...並且第三個參數是什麼?並用於什麼? – giozh

+1

閱讀這個維基百科頁面http://en.wikipedia.org/wiki/Setcontext後,我認爲ucontext_t用於保存用戶上下文。 – Dimitri