Functions of the Linux sigsuspend Function

Source: Internet
Author: User

Functions of the Linux sigsuspend Function
2010-07-08 19:36:30

Category: Linux

UNIX provides a system call to wait for a signal, and sigsuspend is one of them. In Cu (www.chinaunix.net), we have discussed a problem about the system call. Here we will also explain it.

The core issue discussed by Cu users is whether sigsuspend returns first or signal handler returns first. In the book "Advanced Programming for UNIX environments", this question is answered by "if a signal is caught and if the signal handler returns, then sigsuspend returns and the signal mask of the process is set to its value before the call to sigsuspend. ", because sigsuspend is an atomic operation, it gives people the feeling that they call signal first.
Handler first returns, then sigsuspend returns. However, the first example cannot be used. Let's look at the following code:
The example in which the problem was discussed on Cu:
Int main (void ){
Sigset_t newmask, oldmask, zeromask;

If (signal (SIGINT, sig_int) = sig_err)
Err_sys ("signal (SIGINT) error ");

Sigemptyset (& zeromask );

Sigemptyset (& newmask );
Sigaddset (& newmask, SIGINT );
/* Block SIGINT and save current signal mask */
If (sigprocmask (sig_block, & newmask, & oldmask) <0)
Err_sys ("sig_block error ");

/* Critical region of code */
Pr_mask ("in critical region :");

/* Allow all signals and pause */
If (sigsuspend (& zeromask )! =-1)
Err_sys ("sigsuspend error ");
Pr_mask ("after return from sigsuspend :");

/* Reset signal mask which unblocks SIGINT */
If (sigprocmask (sig_setmask, & oldmask, null) <0)
Err_sys ("sig_setmask error ");

/* And continue processing ...*/
Exit (0 );
}

Static void sig_int (INT signo ){
Pr_mask ("\ Nin sig_int :");
Return;
}
 
Result:
$ A. Out
In critical region: SIGINT
^ C
In sig_int: SIGINT
After return from sigsuspend: SIGINT

If the return is based on sig_handler, The SIGINT should not be printed, because at that time the blocked word has not been restored, and all signals are not blocked. So is it Steven s wrong? Of course not. But what did Steven do in sigsuspend's atomic operation?
The entire atomic operation process of sigsuspend is:
(1) set a new mask to block the current process;
(2) receive the signal and restore the original mask;
(3) Call the signal processing function set by this process;
(4) After the signal processing function returns, sigsuspend returns.
This process is basically the process above. Oh, the original signal handler is part of the atomic operation and is executed after the blocked word is restored. Therefore, the above example is correct, that's right for Steven. Because Linux and UNIX are closely related, the semantics of most system calls on the two platforms are consistent. The above sigsuspend atomic operations were also obtained from the book "deep understanding of Linux kernel. The description is as follows:
The sigsuspend () System Call puts the process in the task_interruptible state, after having blocked the standard signals specified by a bit mask array to which the mask parameter points. the process will wake up only when a nonignored, nonblocked signal is
Sent to it. The corresponding sys_sigsuspend () service routine executes these statements:

Mask & = ~ (Sigmask (sigkill) | sigmask (sigstop ));
Spin_lock_irq (effect-> sigmask_lock );
Saveset = Current-> blocked;
Siginitset (blocks T-> blocked, mask );
Recalc_sigpending (current );
Spin_unlock_irq (effect-> sigmask_lock );
Regs-> eax =-eintr;
While (1 ){
Current-> state = task_interruptible;
Schedule ();
If (do_signal (regs, & saveset ))
Return-eintr;
}
The last do_signal function call is the guy who calls the user signal handler. I think the problem on the Cu should be clarified.

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.