The signal of Linux system programming (VI.) race condition and sigsuspend function

Source: Internet
Author: User
Tags error code sleep sleep function

Using pause and alarm functions to realize the sleep function

#include <unistd.h>

int pause (void);

The pause function suspends the calling process until a signal is reached. If the signal is processed to terminate the process, the process terminates, the pause function does not have a chance to return, and if the signal processing action is ignored, the process continues to suspend, the pause does not return, and if the signal processing action is captured, then pause returns 1 after the signal processing function is invoked. Errno is set to Eintr, so pause only has an error return value. Error code EINTR indicates "interrupted by signal".

Alarm function can refer to here: http://blog.csdn.net/simba888888/article/details/8944647

The following uses pause and alarm to implement the sleep (3) function, called the Mysleep:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
    
void sig_alrm (int signo)
{
    /* Nothing to do
*
    
    
/} unsigned int mysleep (unsigned int nsecs)
{
    struct sigaction newact, Olda CT;
    unsigned int unslept;
    
    Newact.sa_handler = SIG_ALRM;
    Sigemptyset (&newact.sa_mask);
    newact.sa_flags = 0;
    Sigaction (SIGALRM, &newact, &oldact);
    
    Alarm (nsecs);
    Pause ();
    
    unslept = alarm (0);
    Sigaction (SIGALRM, &oldact, NULL);
    
    return unslept;
}
    
int main (void)
{while
    (1)
    {
        mysleep (2);
        printf ("Two seconds passed\n");
    }
    return 0;
}

1. The main function calls the Mysleep function, which calls sigaction to register the processing function of the SIGALRM signal sig_alrm.

2. Call Alarm (NSECS) to set the alarm.

3. Call pause wait, the kernel switches to another process to run.

4. Nsecs seconds later, the alarm clock timed out, the kernel sent sigalrm to the process.

5. Before returning the user state of this process from the kernel state to process the pending signal, the SIGALRM signal is found, and its processing function is SIG_ALRM.

6. Switch to the user state to perform the SIG_ALRM function, enter the SIG_ALRM function when the SIGALRM signal is automatically shielded, from the SIG_ALRM function return SIGALRM signal automatically lifted shielding. It then automates the system call Sigreturn into the kernel again and returns the main control flow of the user state to continue executing the process (the Mysleep function called by the main function).

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.