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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux信号学习02

發(fā)布時(shí)間:2023/11/30 linux 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux信号学习02 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • 未決信號集與阻塞信號集(信號屏蔽字)
    阻塞信號集: 將某些信號加入集合,對他們設(shè)置屏蔽,當(dāng)屏蔽x信號后,再收到該信號,該信號的處理將推后(解除屏蔽后)
    未決信號集:
    a. 信號產(chǎn)生,未決信號集中描述該信號的位立刻翻轉(zhuǎn)為1,表信號處于未決狀態(tài)。當(dāng)信號被處理對應(yīng)位翻轉(zhuǎn)回為0。這一時(shí)刻往往非常短暫。
    b. 信號產(chǎn)生后由于某些原因(主要是阻塞)不能抵達(dá)。這類信號的集合稱之為未決信號集。在屏蔽解除前,信號一直處于未決狀態(tài)。
    相當(dāng)于 阻塞信號集在未決信號集前面設(shè)置了一堵墻

  • 系統(tǒng)api產(chǎn)生信號
    kill函數(shù)

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

    參數(shù)介紹:
    pid >0,要發(fā)送的進(jìn)程ID
    pid =0,代表當(dāng)前進(jìn)程組內(nèi)所有進(jìn)程
    pid =-1, 代表有權(quán)限發(fā)送的所有進(jìn)程
    pid <-1, 代表 -pid對應(yīng)組內(nèi)所有進(jìn)程
    sig 對應(yīng)的信號

    kill函數(shù)代碼示例

    de <sys/types.h> #include <signal.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h>int main() {int i=0;pid_t pid;for(; i<5; ++i) {pid = fork();if(pid==0){break;}}if(i=2) {printf("son---%d will kill fatherProgress\n",i);sleep(5);kill(getppid(), SIGKILL);while (1) {sleep(1);}} if(i=5) {while(1) {printf("father ----\n");sleep(1);}}return 0; }

    –分割線–
    alarm
    man alarm

    #include <unistd.h>unsigned int alarm(unsigned int seconds);

    alarm() arranges for a SIGALRM signal to be delivered to the calling
    process in seconds seconds.
    If seconds is zero, any pending alarm is canceled.
    In any event any previously set alarm() is canceled.
    alarm() 函數(shù) 給調(diào)用者(自己)發(fā)送 一個 SIGALRM 信號,在 seconds秒后。
    如果 seconds是0, 取消之前的 設(shè)置的 alarm。

    return返回值
    返回之前設(shè)置的 alarm剩余的秒數(shù),如果之前沒有設(shè)置alarm,就返回0.

    alarm代碼示例:

    #include <stdio.h> #include <signal.h> #include <unistd.h>int main() {int ret_1 = alarm(5);printf("%d\n", ret_1);sleep(2);int ret_2 = alarm(4);int num = 4;printf("%d\n", ret_2);while (1) {printf(" will ararm fater %d\n", num--);sleep(1);}return 0; }

    –分割線–
    setitimer函數(shù),周期性發(fā)送信號
    man setitimer

    #include <sys/time.h> int getitimer(int which, struct itimerval *curr_value); int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);

    參數(shù)介紹:
    which 三種選擇

    真實(shí)時(shí)間ITIMER_REAL decrements in real time, and delivers SIGALRM upon expi‐ration.計(jì)算進(jìn)程執(zhí)行時(shí)間ITIMER_VIRTUAL decrements only when the process is executing, anddelivers SIGVTALRM upon expiration.計(jì)算進(jìn)程執(zhí)行 + 調(diào)度時(shí)間 ITIMER_PROF decrements both when the process executes and when thesystem is executing on behalf of the process. Coupledwith ITIMER_VIRTUAL, this timer is usually used to pro‐file the time spent by the application in user and ker‐nel space. SIGPROF is delivered upon expiration. struct itimerval {struct timeval it_interval; /* Interval for periodic timer */ 周期定時(shí)器間隔struct timeval it_value; /* Time until next expiration */ 下次執(zhí)行時(shí)間 };struct timeval {time_t tv_sec; /* seconds */ 秒數(shù)suseconds_t tv_usec; /* microseconds */ 微秒數(shù) };

    new_value 要設(shè)置的鬧鐘時(shí)間
    old_value 原鬧鐘時(shí)間

    return: 成功返回0, 失敗-1,并設(shè)置errno
    setitimer 代碼示例:

    #include <signal.h> #include <stdio.h> #include <unistd.h> #include <sys/time.h>void catch(int sign) {if(sign == SIGALRM) {printf("catch signal is %d\n", sign);} } int main() {signal(SIGALRM, catch);struct itimerval new_value = {{3, 0},{1, 0}};setitimer(ITIMER_REAL, &new_value, NULL);while (1){printf(" setitimer test\n");sleep(1);}return 0; }

    總結(jié)

    以上是生活随笔為你收集整理的linux信号学习02的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。