linux信號學習02

  1. 未決信號集與阻塞信號集(信號屏蔽字)
    阻塞信號集: 將某些信號加入集合,對他們設置屏蔽,當屏蔽x信號後,再收到該信號,該信號的處理將推後(解除屏蔽後)
    未決信號集:
    a. 信號產生,未決信號集中描述該信號的位立刻翻轉爲1,表信號處於未決狀態。當信號被處理對應位翻轉回爲0。這一時刻往往非常短暫。
    b. 信號產生後由於某些原因(主要是阻塞)不能抵達。這類信號的集合稱之爲未決信號集。在屏蔽解除前,信號一直處於未決狀態。
    相當於 阻塞信號集在未決信號集前面設置了一堵牆
    在這裏插入圖片描述

  2. 系統api產生信號
    kill函數

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

參數介紹:
pid >0,要發送的進程ID
pid =0,代表當前進程組內所有進程
pid =-1, 代表有權限發送的所有進程
pid <-1, 代表 -pid對應組內所有進程
sig 對應的信號

kill函數代碼示例

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() 函數 給調用者(自己)發送 一個 SIGALRM 信號,在 seconds秒後。
如果 seconds是0, 取消之前的 設置的 alarm。

return返回值
返回之前設置的 alarm剩餘的秒數,如果之前沒有設置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函數,週期性發送信號
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);

參數介紹:
which 三種選擇

真實時間
 ITIMER_REAL    decrements in real time, and delivers SIGALRM upon expi‐
                ration.

計算進程執行時間
 ITIMER_VIRTUAL decrements  only  when  the  process  is  executing, and
                delivers SIGVTALRM upon expiration.

計算進程執行 + 調度時間
ITIMER_PROF    decrements both when the process executes and  when  the
               system  is  executing on behalf of the process.  Coupled
                with 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 */  週期定時器間隔
    struct timeval it_value;    /* Time until next expiration */ 下次執行時間
};

struct timeval {
    time_t      tv_sec;         /* seconds */ 秒數
    suseconds_t tv_usec;        /* microseconds */ 微秒數
};

new_value 要設置的鬧鐘時間
old_value 原鬧鐘時間

return: 成功返回0, 失敗-1,並設置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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章