日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux_unix编程手册-信号概述signal函数

發布時間:2025/3/15 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux_unix编程手册-信号概述signal函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關于linux_unix編程手冊的代碼見
https://github.com/zzu-andrew/linux_unix.git
上面由編譯過得代碼可以直接使用或者自己clone之后再使用


改變信號量的處置



在linux手冊中對函數signal的解釋是:

#include <signal.h>typedef void (*sighandler_t)(int);sighandler_t signal(int signum, sighandler_t handler);

可以看出man手冊對signal的書寫更加的形象,signal返回的是一個 void (*sighandler_t)(int)類型的函數
返回的函數就是之前的信號處理函數,一般使用signal的過程如下:

/*************************************************************************\ * 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-1 *//* ouch.cCatch the SIGINT signal, generated by typing control-C (^C).Note that although we use signal() to establish the signal handler in thisprogram, the use of sigaction() is always preferable for this task. */ #include <signal.h> #include "tlpi_hdr.h"static void sigHandler(int sig) {printf("Ouch!\n"); /* UNSAFE (see Section 21.1.2) */ }int main(int argc, char *argv[]) {int j;/* Establish handler for SIGINT. Here we use the simpler signal()API to establish a signal handler, but for the reasons described inSection 22.7 of TLPI, sigaction() is the (strongly) preferred APIfor this task. */if (signal(SIGINT, sigHandler) == SIG_ERR)errExit("signal");/* Loop continuously waiting for signals to be delivered */for (j = 0; ; j++) {printf("%d\n", j);//加上,不然程序不會自己退出,并且使用  ^C ^D他也不能使程序退出if(j == 20){exit(0);}sleep(3); /* Loop slowly... */} }

程序測試過程:

說明,程序中信號處理函數使用printf()函數顯示具體信息,但是在現實的編程環境中是絕對不允許在信號處理函數中使用stdio函數,這里只是將其作為調試的一種手段

為兩個不同的信號建立同樣的信號處理程序:

/*************************************************************************\ * 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 */ }

發送信號,使用kill一個進程可以向另一個進程發送信號(之所以使用kill作為術語,是因為早期的UNIX實現中大多數信號的默認行為是終止進程)

#include <signal.h>int kill(pid_t pid, int sig);

pid參數用于標示一個或者多個目標進程,而sig則指向要發送的信號

kill()系統調用還有另一重功用,將參數sig指定為0(即所謂的空信號),則無信號發送,相反kill()僅會檢查錯誤檢驗,查看是否可以向目標進程發送信號,從另一個角度這意味著,可以使用控信號檢測具有特定進程ID的進程是否存在。若是發送信號失敗,且errno為ESRCH,則表明目標進程不存在,若果調用失敗且errno為EPERM(表示進程存在但是無權向目標進程發送信號)或者調用成功(有權向目標進程發送信號),那么就表示進程存在。

改變信號處置:sigaction()函數

除去signal()之外,sigaction()系統調用是設置信號好處置的另一個選擇;

等待信號:pause()

調用pause()將暫停進程的執行,直至信號處理器函數中斷該調用為止(或者直至一個為處理的信號終止進程為止)。

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的linux_unix编程手册-信号概述signal函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 中文字幕日韩一区二区三区 | 综合久久伊人 | 粗了大了 整进去好爽视频 日本女优中文字幕 | 舐め犯し波多野结衣在线观看 | 黄视频网站免费看 | 国产一区不卡视频 | www性| 99r热 | 青娱乐国产视频 | 亚洲日本欧美 | 99伊人网| 日韩电影在线观看一区 | 国产啊v在线 | 桃色91| 欧美1区2区3区 | 性欧美色图| 一区二区激情视频 | 中国美女一级看片 | 伊人中文字幕在线观看 | 久久夜夜操妹子 | 欧美激情视频一区二区三区在线播放 | 夜夜操av| 欧美一区二区三区久久综合 | 青青导航 | 色偷偷av男人的天堂 | 精品欧美一区二区三区 | 亚洲av无码久久精品狠狠爱浪潮 | 亚洲天堂一区在线 | 久久99久久99精品蜜柚传媒 | 欧美一区二区最爽乱淫视频免费看 | 日本免费一区二区三区四区 | 激情综合网站 | 国产精品色片 | 丰满人妻中伦妇伦精品app | 国产精品一区二区三区高潮 | 欧美精品成人一区二区在线观看 | 野外一级片 | 午夜看看 | a天堂资源 | 玉女心经是什么意思 | av网址在线看 | 亚欧美精品 | 青青草原亚洲 | 人人妻人人爽一区二区三区 | 亚洲天堂av网 | 日韩在线视频免费播放 | 欧美乱欲视频 | 蜜桃av噜噜一区二区三区网址 | 三级全黄视频 | 成人乱人乱一区二区三区 | 永久免费精品视频 | 黄色av网站免费看 | 91国产在线看| 中文字幕97 | 精品久久久久久一区二区里番 | 欧美日韩视频在线观看一区 | 国产乱码久久久久久 | www.69视频 | 国产视频福利在线观看 | 顶级尤物极品女神福利视频 | 初音未来打屁股 | av色综合 | 成人在线免费看片 | 国产成人精品一区二区色戒 | 欧美午夜精品久久久久久蜜 | 亚洲欧美天堂 | 欧亚成人av | 亚洲综合激情小说 | 视频一区二区在线 | 神马午夜电影一区二区三区在线观看 | 久久亚洲av午夜福利精品一区 | www.日本在线 | 躁躁躁日日躁 | 调教撅屁股啪调教打臀缝av | 锦绣未央在线观看 | 69精品久久久久久 | 丰满大爆乳波霸奶 | 亚洲国产黄 | 日韩草逼视频 | 黄网站免费入口 | 杨贵妃颤抖双乳呻吟求欢小说 | 久久精久久 | 性――交――性――乱a | 免费看的黄色录像 | 一区二区三区四区影院 | 国产第十页 | 亚洲精品国产精品乱码桃花 | 女人扒开腿让男人捅爽 | 天码人妻一区二区三区在线看 | 偷拍亚洲欧美 | 亚洲一区二区三区高清视频 | 久久久视频在线 | 全部毛片永久免费看 | 涩涩视频在线 | 久草久| 亚洲骚| 国产精品视频麻豆 | 日本不卡一区二区在线观看 | 国产一级二级三级精品 |