Sleep function error (& quot; Advanced Programming in the UNIX Environment & quot; Third Edition No. 374), programmingno.374

Source: Internet
Author: User
Tags sigalarm sleep function

Sleep function error ("Advanced Programming in the UNIX Environment" Third Edition No. 374), programmingno.374

Test proof code:

#include <unistd.h>#include <fcntl.h>#include <time.h>#include "apue.h"static void sig_alrm(int signo){    /* nothing to do, just returning wakes up sigsuspend */}static void sig_usr1(int signo){}//static void sig_alrm_test(int signo)//{//    err_msg("left an alrm");//}unsigned int sleep(unsigned int seconds){    struct sigaction newact, oldact;    sigset_t newmask, oldmask, suspmask;    unsigned int unslept;    timespec ts;            // ++    ts.tv_sec = 25;         // ++    ts.tv_nsec = 0;         // ++    /* set our handler, save previous information */    newact.sa_handler = sig_alrm;    sigemptyset(&newact.sa_mask);    newact.sa_flags = 0;    sigaction(SIGALRM, &newact, &oldact);    /* block SIGALRM and save current signal mask */    sigemptyset(&newmask);    sigaddset(&newmask, SIGALRM);    sigprocmask(SIG_BLOCK, &newmask, &oldmask);    alarm(seconds);    suspmask = oldmask;    /* make sure SIGALRM isn't block */    sigdelset(&suspmask, SIGALRM);    /* wait for any signal to be caught */    sigsuspend(&suspmask);    nanosleep(&ts, NULL);                   // ++    /* some signal has been caught, SIGALRM is now blocked */    unslept = alarm(0);    /* reset previous action */    sigaction(SIGALRM, &oldact, NULL);    /* reset signal mask, which unblocks SIGALRM */    sigprocmask(SIG_SETMASK, &oldmask, NULL);    return unslept;}int main(int argc, char *argv[]){    struct sigaction useract;    struct sigaction alrmact;    useract.sa_handler = sig_usr1;    sigemptyset(&useract.sa_mask);    useract.sa_flags = 0;    if (sigaction(SIGUSR1, &useract, NULL) < 0)        err_sys("SIG SUR1 error");//    alrmact.sa_handler = sig_alrm_test;//    sigemptyset(&alrmact.sa_mask);//    alrmact.sa_flags = 0;//    if (sigaction(SIGALRM, &alrmact, NULL) < 0)//        err_sys("SIG ALRM error");    sleep(20);    return 0;}

Operation instructions: Here I will explain how to communicate with the author, so I use English, but my English level is limited. I hope to understand:

I shoshould give you my manipulation. you shoshould send a SIGUSR1 to the process in less than 20 seconds. so that when alarm (0) tries to cancel the alarm, it was not expired. you see, I didn't send the process SIGALARM, but the SIGALARM handler was fired.

The following is the running result (in English for the author to see clearly ):

Below is the author's reply (there are some twists and turns in the middle, and the last part of the reply is provided ):

Yes, there is a very small window where if the sigsuspend () is interrupted by a different signal and the SIGALRM is posted before calling alarm (0) to cancel it, that the process will be terminated if the default disposition of SIGALRM is in effect. you have artificially widened that window by adding the call to nanosleep ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.