The same and difference between the Linux C signal pause and Sigsuspend

Source: Internet
Author: User
Tags sigint signal

    • Pause function:

function: Let the process pause until the signal appears

#include <unistd.h>

Intpause ();

Function Description: Pause () pauses the current process (goes to sleep) until the signal (signal) is interrupted.

Return value: Returns only-1.

#include <stdio.h>#include<unistd.h>void  deal () {   printf ("signal interference! \ n ");} void Main () {     printf ("Process Execution! \ n ");    Signal (sigint,deal);    Pause ();    printf ("The process is over!" \ n ");}

Description

When the program is running, pause causes the current process to hang (into sleep) until we send the SIGINT interrupt signal to the process, the process is awakened, the signal is processed, the pause function returns after processing the signal, and the program continues to run.

Note: Any signal can cause pause to wake up.

    • Sigsuspend function:

#include <signal.h>

int sigsuspend (const sigset_t *sigmask);

return value:-1, and set errno to Eintr

The Sigsuspend function, like the pause function, can be a process hang (go to sleep) until a signal occurs.

The parameter of the Sigsuspend function is a signal set, which is used to mask the signal, and the signal is centrally stored to be shielded.

If the signal set is empty, Sigsuspend does not block any signals, and any signal can cause the process to wake up from the suspended state, just like the pause function.

#include <stdio.h>#include<unistd.h>#includevoid  deal () {   printf (" Signal Interference! \ n ");} void Main () {    sigset_tsigmask;    Sigemptyset (&sigmask);    printf ("Process Execution! \ n ");    Signal (sigint,deal);    Sigsuspend (&sigmask);    printf ("The process is over!" \ n ");}

Description

This example is the same as the example of the pause function above.

    • The difference between sigsuspend and pause:

The Sigsuspend function is an enhanced version of the pause function. When the parameter signal set of the Sigsuspend function is an empty signal set, the Sigsuspend function is the same as the pause function and can accept any interruption of the signal. However, the Sigsuspend function can mask the signal and accept the specified signal interruption.

Sigsuspend function =pause function + Specify shielding signal

Note: The signal is interrupted by the sigsuspend and pause functions, not the program code.

Does sigsuspend affect the signal of sigprocmask shielding?

Impact! sigsuspend So that the original shielding signal all failed, when the sigsuspend return, restore the original shielding signal.

    • When does sigsuspend use?

When our program does not want to be interrupted by some signals while doing some business processing, we can first block these signals, at the end of this business process we can use the Sigsuspend function to process the signal in the queue (in this process can also specify the shielding signal), after processing is completed, The signal is masked before recovery and the next business processing is processed.

#include <stdio.h>#include<unistd.h>#include<signal.h>voiddeal () {printf ("in signal processing! \ n ");}voidMain () {sigset_t sigs,sigmask; inti;    Signal (sigint,deal); Sigemptyset (&sigs);//Sigsuspend Signal set is set to NULL, indicating that the sigsuspend will be in place//if you want to block some of the signals that are queued//Signal , you can add the corresponding signal to the signal setSigemptyset (&sigmask); Sigaddset (SIGINT,&sigs); Sigprocmask (Sig_block,&sigs,0);//shielding the SIGINT signal so that it can no longer process the business//the interference process.      for(i=0; i<5; i++{printf ("process business ... \ n"); //Analog Business ProcessingSleep5) printf ("Process business end \ n"); Sigsuspend (&sigmask);//handle the signal that is being queued, and when the signal is finished,//the Sigsuspend function returns and executes the next business processingprintf ("Process business ... \ n");//Analog Business ProcessingSleep5) printf ("Process business end \ n"); }    }                                    

Note: When the Sigsuspend function is called, the process hangs (goes to sleep) waits for the signal to be interrupted, and if no signal occurs, the process will hang until a signal occurs, but the signal is not the masked signal set in the signal set of the Sigsuspend function. The sigsuspend processes the signal, and when the signal processing is complete, the Sigsuspend function returns and executes the next code.

The same and difference between the Linux C signal pause and Sigsuspend

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.