Linux可靠/不可靠訊號編程實踐

來源:互聯網
上載者:User

標籤:linux   可靠   不可靠訊號   編程實踐   非同步   

綜合案例

    1) 建立子進程與父進程;

    2) 註冊SIGINT非即時訊號與SIGRTMIN即時訊號,並將這兩種訊號添加到進程屏蔽訊號組中;

    3) 註冊使用者自訂訊號;

    4) 子進程發送5次非即時訊號,發5次即時訊號;

    5) 然後子進程發送SIGUSR1解除進程對SIGINT,SIGTRMIN訊號的阻塞

    6) 觀察即時訊號與非即時訊號的區別


//程式樣本void onSigAction(int signalNumber, siginfo_t *sigInfoStruct, void *){    //擷取接收到的資料    int receiveNumber = sigInfoStruct->si_int;    //如果收到的是SIGUSR1訊號,則解除對SIGINT,SIGRTMIN的屏蔽    if (signalNumber == SIGUSR1)    {        sigset_t unblockSet;        sigemptyset(&unblockSet);        sigaddset(&unblockSet,SIGINT);        sigaddset(&unblockSet,SIGRTMIN);;        sigprocmask(SIG_UNBLOCK,&unblockSet,NULL);        //Value值會是亂碼!        //cout << "Receive SIGUSR1, Value = " << receiveNumber << endl;        cout << "Unblock, Receive SIGUSR1" << endl;    }    else if (signalNumber == SIGINT)    {        cout << "Receive SIGINT, Value = " << receiveNumber << endl;    }    else if (signalNumber == SIGRTMIN)    {        cout << "Receive SIGRTMIN, Value = " << receiveNumber << endl;    }}//錯誤退出函數inline void err_exit(string str);int main(){    struct sigaction act;    //如果需要使得訊號處理常式接收額外資料,    //則必須將sa_flags位置為SA_SIGINFO    act.sa_flags = SA_SIGINFO;    sigemptyset(&act.sa_mask);    act.sa_sigaction = onSigAction;    //註冊訊號處理函數    if (sigaction(SIGINT,&act,NULL) < 0)        err_exit("sigaction SIGINT");    if (sigaction(SIGRTMIN,&act,NULL) < 0)        err_exit("sigaction SIGRTMIN");    if (sigaction(SIGUSR1,&act,NULL) < 0)        err_exit("sigaction SIGUSR1");    //將訊號SIGINT,SIGRTMIN訊號阻塞    sigset_t blockSet;    sigemptyset(&blockSet);    sigaddset(&blockSet,SIGINT);    sigaddset(&blockSet,SIGRTMIN);    if (sigprocmask(SIG_BLOCK,&blockSet,NULL) < 0)        err_exit("sigprocmask error");    pid_t pid = fork();    if (pid == -1)        err_exit("fork error");    else if (pid == 0)    {        union sigval Value;        Value.sival_int = 200;        //給父進程發送五次帶額外資料的非可靠訊號(其實最終只接受到了一次)        for (int i = 0; i < 5; ++i)        {            ++ Value.sival_int;            if (sigqueue(getppid(),SIGINT,Value) != 0)                err_exit("sigqueue error");        }        //給父進程發送五次帶額外資料的可靠訊號(最終接收到了五次!!!)        Value.sival_int = 0;        for (int i = 0; i < 5; ++i)        {            ++ Value.sival_int;            if (sigqueue(getppid(),SIGRTMIN,Value) != 0)                err_exit("sigqueue error");        }        //給父進程發送SIGUSR1訊號,解除對SIGINT,SIGRTMIN訊號的阻塞        kill(getppid(),SIGUSR1);    }    while (true)    {        pause();    }}void err_exit(string str){    perror(str.c_str());    exit(EXIT_FAILURE);}

運行結果:

 

 

其實只是收到了一份非可靠訊號SIGINT!

 

附-查看系統限制命令:ulimit

[email protected]:~$ ulimit -acore file size          (blocks, -c) unlimiteddata seg size           (kbytes, -d) unlimitedscheduling priority             (-e) 0file size               (blocks, -f) unlimitedpending signals                 (-i) 47131max locked memory       (kbytes, -l) 64max memory size         (kbytes, -m) unlimitedopen files                      (-n) 1024pipe size            (512 bytes, -p) 8POSIX message queues     (bytes, -q) 819200real-time priority              (-r) 0stack size              (kbytes, -s) 8192cpu time               (seconds, -t) unlimitedmax user processes              (-u) 47131virtual memory          (kbytes, -v) unlimitedfile locks                      (-x) unlimited

Linux可靠/不可靠訊號編程實踐

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.