System functions of Signals

Source: Internet
Author: User

In. POSIX.1 requires system to ignore SIGINT and SIGQUIT and block SIGCHLD.

Instance

Use the systemscripts in http://www.cnblogs.com/nufangrensheng/p/3512291.html to use the ed (1) Editor. (Ed has been a UNIX component for a long time. The reason for calling it here is that it is an interactive program that captures the interrupt and exit signals. If you call ed from shell and type the interrupt character, it captures the interrupt signal and prints the question mark. It also sets the handling method of the exit operator to ignore.

Program list 10-19 use system to call the ed Editor

#include   (signal(SIGINT, sig_int) ==(signal(SIGCHLD, sig_chld) ==(system() < 

Program listing 10-19 is used to capture SIGINT and SIGCHLD signals. If you call it, you can:

<Errno. h> <signal. h> <unistd. h> * signature string) (Signature string = (); = SIG_IGN; & = (sigaction (SIGINT, & ignore, & saveintr) <(-(sigaction (SIGQUIT, & ignore, & savequit) <(-& chldmask); & (sigprocmask (SIG_BLOCK, & chldmask, & savemask) <(-(pid = fork () <= -; (pid = &, prop string, (*); (waitpid (pid, & status,) <(errno! =-; (Sigaction (SIGINT, & saveintr, NULL) <(-(sigaction (SIGQUIT, & savequit, NULL) <(-(sigprocmask (SIG_SETMASK, & savemask, NULL) <(-

If the implementation of linking program listing 10-19 and system function is compared, the generated binary code differs from the previous defective program as follows:

(1) When we type an interrupt or exit character, no signal is sent to the caller process.

(2) When the ed command is terminated, the SIGCHLD signal is not sent to the calling process. As an alternative, the SIGCHLD signal remains blocked until the sigprocmask call at the end of the program unblocks the SIGCHLD signal. This call to the sigprocmask function is performed after the system function calls waitpid to obtain the termination state of the sub-process.

 

System Return Value

Note that the return value of system is the termination state of shell, but the termination state of shell is not always the termination state of the process that executes the command string.

The Bourne shell has a feature that is not clearly stated in its document:When a command is terminated with a signal, its termination status is 128 plus a signal number..

You can see this in shell Interactive Mode:

In the system in use, the value of SIGINT is 2, and the value of SIGQUIT is 3, so the shell termination states are 130 and 131.

The return value of system reports an exceptional termination only when the shell itself terminates abnormally.

When writing a program using the system function, you must correctly interpret the return value.If you call fork, exec, and wait directly, the termination status is different from that of calling system..

 

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

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.