linux_unix编程手册-信号概述signal函数
關(guān)于linux_unix編程手冊(cè)的代碼見(jiàn)
https://github.com/zzu-andrew/linux_unix.git
上面由編譯過(guò)得代碼可以直接使用或者自己clone之后再使用
改變信號(hào)量的處置
在linux手冊(cè)中對(duì)函數(shù)signal的解釋是:
可以看出man手冊(cè)對(duì)signal的書(shū)寫(xiě)更加的形象,signal返回的是一個(gè) void (*sighandler_t)(int)類(lèi)型的函數(shù)
返回的函數(shù)就是之前的信號(hào)處理函數(shù),一般使用signal的過(guò)程如下:
程序測(cè)試過(guò)程:
說(shuō)明,程序中信號(hào)處理函數(shù)使用printf()函數(shù)顯示具體信息,但是在現(xiàn)實(shí)的編程環(huán)境中是絕對(duì)不允許在信號(hào)處理函數(shù)中使用stdio函數(shù),這里只是將其作為調(diào)試的一種手段
為兩個(gè)不同的信號(hào)建立同樣的信號(hào)處理程序:
/*************************************************************************\ * Copyright (C) Michael Kerrisk, 2018. * * * * This program is free software. You may use, modify, and redistribute it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation, either version 3 or (at your option) any * * later version. This program is distributed without any warranty. See * * the file COPYING.gpl-v3 for details. * \*************************************************************************//* Listing 20-2 *//* intquit.cCatch the SIGINT and SIGQUIT signals, which are normally generatedby the control-C (^C) and control-\ (^\) keys respectively.Note that although we use signal() to establish signal handlers in thisprogram, the use of sigaction() is always preferable for this task. */ #include <signal.h> #include "tlpi_hdr.h"static void sigHandler(int sig) {static int count = 0;/* UNSAFE: This handler uses non-async-signal-safe functions(printf(), exit(); see Section 21.1.2) */if (sig == SIGINT) {count++;printf("Caught SIGINT (%d)\n", count);return; /* Resume execution at point of interruption */}/* Must be SIGQUIT - print a message and terminate the process */printf("Caught SIGQUIT - that's all folks!\n");exit(EXIT_SUCCESS); }int main(int argc, char *argv[]) {/* Establish same handler for SIGINT and SIGQUIT. Here we use thesimpler signal() API to establish a signal handler, but for thereasons described in Section 22.7 of TLPI, sigaction() is the(strongly) preferred API for this task. */if (signal(SIGINT, sigHandler) == SIG_ERR)errExit("signal");if (signal(SIGQUIT, sigHandler) == SIG_ERR)errExit("signal");for (;;) /* Loop forever, waiting for signals */pause(); /* Block until a signal is caught */ }發(fā)送信號(hào),使用kill一個(gè)進(jìn)程可以向另一個(gè)進(jìn)程發(fā)送信號(hào)(之所以使用kill作為術(shù)語(yǔ),是因?yàn)樵缙诘腢NIX實(shí)現(xiàn)中大多數(shù)信號(hào)的默認(rèn)行為是終止進(jìn)程)
#include <signal.h>int kill(pid_t pid, int sig);pid參數(shù)用于標(biāo)示一個(gè)或者多個(gè)目標(biāo)進(jìn)程,而sig則指向要發(fā)送的信號(hào)
kill()系統(tǒng)調(diào)用還有另一重功用,將參數(shù)sig指定為0(即所謂的空信號(hào)),則無(wú)信號(hào)發(fā)送,相反kill()僅會(huì)檢查錯(cuò)誤檢驗(yàn),查看是否可以向目標(biāo)進(jìn)程發(fā)送信號(hào),從另一個(gè)角度這意味著,可以使用控信號(hào)檢測(cè)具有特定進(jìn)程ID的進(jìn)程是否存在。若是發(fā)送信號(hào)失敗,且errno為ESRCH,則表明目標(biāo)進(jìn)程不存在,若果調(diào)用失敗且errno為EPERM(表示進(jìn)程存在但是無(wú)權(quán)向目標(biāo)進(jìn)程發(fā)送信號(hào))或者調(diào)用成功(有權(quán)向目標(biāo)進(jìn)程發(fā)送信號(hào)),那么就表示進(jìn)程存在。
改變信號(hào)處置:sigaction()函數(shù)
除去signal()之外,sigaction()系統(tǒng)調(diào)用是設(shè)置信號(hào)好處置的另一個(gè)選擇;
等待信號(hào):pause()
調(diào)用pause()將暫停進(jìn)程的執(zhí)行,直至信號(hào)處理器函數(shù)中斷該調(diào)用為止(或者直至一個(gè)為處理的信號(hào)終止進(jìn)程為止)。
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的linux_unix编程手册-信号概述signal函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 作者:覃海焕(1978-),女,博士,
- 下一篇: linux_unix编程手册--信号处理