"Signals" in Linux (to be continued)

Source: Internet
Author: User

How the signal is generated:

1. Keyboard input

If CTRL + C means a SIGINT signal is generated

2. Abnormal signal generation

This is the case when the program executes to 2/0, the read segment of the pipe is closed and the write end still writes data to the pipeline ...

3. Send a signal to the specified process by command

How to handle the signal:

1. Ignore

2. Perform default processing (usually terminating program)

3. Perform a custom action (capture of the signal)

Let me give you an example.

The following procedure, starting from 0, outputs an increasing number every second.

650) this.width=650; "title=" 1.png "alt=" wkiol1edvahscrvwaaajpb8fhbw498.png-wh_50 "src=" Http://s4.51cto.com/wyfs02 /m01/84/07/wkiol1edvahscrvwaaajpb8fhbw498.png-wh_500x0-wm_3-wmp_4-s_3877545669.png "/>

Perform:

650) this.width=650; "title=" 2.png "alt=" Wkiom1edvdfzmcxraaasficfx6a911.png "src=" http://s2.51cto.com/wyfs02/M01/ 84/08/wkiom1edvdfzmcxraaasficfx6a911.png "/>

Apparently it's a dead loop, and it's going to go on.

Interrupt it with a signal, you can use CTRL + C directly

The kernel then sends a SIGINT signal to the process to end the current process:

650) this.width=650; "title=" 4.png "alt=" Wkiom1edv0nskpajaaaa94hs8sm704.png "src=" http://s4.51cto.com/wyfs02/M01/ 84/08/wkiom1edv0nskpajaaaa94hs8sm704.png "/>

Additionally, after executing the program, open a different terminal

(In fact, the execution of the command after the statement with & can let the current process in the background to run, there is no need to open the interrupt, this is to be more intuitive)

Find the PID of the current process with the PS aux command First

Then use the Killl-l command to view all signals:

650) this.width=650; "title=" 5.png "alt=" Wkiol1edwa_axhhmaabxqddf1ts983.png "src=" http://s2.51cto.com/wyfs02/M02/ 84/07/wkiol1edwa_axhhmaabxqddf1ts983.png "/>

Here can be found that the CTRL + C shortcut to send the SIGINT signal corresponding to 2,

And the PID of my current process is: 20262

Then, execute the command: Kill-2 20262 or Kill-sigint 20262

The process of discovering the dead loop is terminated.

Next, we introduce several signal set operation functions:

int Sigemptyset (sigset_t *set); int Sigfillset (sigset_t *set); int Sigaddset (sigset_t *set, int signo); int Sigdelset ( sigset_t *set, int signo); int sigismember (const sigset_t *set, int signo); int sigprocmask (int how, const sigset_t *set, sigset_t *oset); int sigpending (sigset_t *set);

First function:

The Sigemptyset is used to initialize a set-directed signal set so that all signals corresponding to the bit are zeroed, indicating that the signal set does not contain any valid signals

Successfully returned 0, error returned-1

A second function:

Sigfillset is also used for initialization, but in contrast to the above Sigemptyset function, which indicates that the signal set includes all the signals supported by the system.

Successfully returned 0, error returned-1

Before you use the sigset_t type variable, you must call either of the two initialization states above

A third function:

The function of Sigaddset is to add a valid signal to a specified set of signals.

Successfully returned 0, error returned-1

A fourth function:

The function of the sigdelset is to remove the active signal in the specified set of signals.

Successfully returned 0, error returned-1

A fifth function:

The Sigismember is used to determine whether the specified signal is in the specified signal set

If present, returns 1 if not present, returns 0 if an error occurs, returns 1

A sixth function:

Sigprocmask can read or change the signal screen word of a process
int sigprocmask (int how, const sigset_t *set, sigset_t *oset);

Successful return 0, failure return-1

Where the first parameter, how, is handled in the following three ways:

· Sig_block:

The set contains the signal we want to add to the current signal screen Word//equivalent to mask = Mask | Set

· Sig_unblock:

Set contains the signal we want to unblock from the current signal mask//equivalent to mask = Mask | ~set

· Sig_setmask:

Sets the value that the current signal screen word is set to point to//equivalent to mask = set

A seventh function:

Sigpending reads the outstanding signal set of the current process, and passes the set parameter out

Successful return 0, failure return-1

"Signals" in Linux (to be continued)

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.