測試**1:
# include # include # include # include # include typedef void sigfunc(int);
void sig_usr(int signo)
int main(int argc, char *ar**)
$ gcc unix.c -o unixsig_usr處理使用者定義的訊號sigusr1和sigusr2,kill指令向程序傳送訊號,由於程序沒有處理終止訊號,所以收到終止訊號sigterm時程式預設被終止(**注釋部分就是處理終止訊號的)。./unix &
[1] 4339
$ kill -sigusr1 4339
sigusr1 received.
$ kill -sigusr2 4339
sigusr2 received.
$ kill 4339
$[1]+ 已終止 ./unix
測試**2:進行訊號處理函式時能否捕捉其他訊號?
# include # include # include # include # include # include typedef void sigfunc(int);
void handler()
void handler2()
int main(int argc, char *ar**)
$ ./unix &結果顯示為可以(另外經過測試在執行訊號a的處理函式時是會阻塞訊號a的到來的)。[1] 3548
$ kill -sigint 3548
begin sigint
$ kill -sigusr1 3548
begin sigusr1
end sigusr1
$ end sigint
done : interrupted system call
測試**3:sigaction函式代替signal函式
# include # include # include # include # include # include typedef void sigfunc(int);
void handler()
sigfunc *signal(int signo, sigfunc *func)
int main(int argc, char *ar**);
read(0, s, sizeof(s));
printf("%s\n",s);
perror("done");
return 0;
}
$ ./unixsigaction比signal提供了更多功能,包括阻塞訊號sa_mask和狀態位sa_flags,後者可以設定被訊號中斷的函式是否自動重啟(比如低速系統呼叫read、write)。上面**如果是alarm引起的中斷不重啟,其他重啟,也就是8秒內read要返回,否則就跳過了。另外老版的signal函式只能生效一次,而sigaction函式能永久生效。^chandling singo.
^chandling singo.
^chandling singo.
^chandling singo.
handling singo.
done: interrupted system call
Signal訊號處理
在goahead的linux模組main.c 檔案中,main函式中出現了以下三個語句 signal sigpipe,sig ign signal sigint,siginthandler signal sigterm,siginthandler 這三條語句與網路訊號有關。在這裡做咯記錄。1.訊號概...
signal 捕捉訊號
file demo6.c synopsis inclue typeof void sighandler t int sighandler t signal int signum,sighandler t handler author mrclimb version 1.1.0 date 2012 0...
signal訊號總結
signaldescription sigabrt 由呼叫abort函式產生,程序非正常退出 sigalrm 用alarm函式設定的timer超時或setitimer函式設定的interval timer超時 sigbus 某種特定的硬體異常,通常由記憶體訪問引起 sigcancel 由solari...