Simple use of signals under Linux

Source: Internet
Author: User

1, 1 Main, contains 2 while,

Don't be fooled by sleep in two while, where only main () is running, and the program executes in a top-down order.

When I encounter sleep in a 1th while loop, this thread pauses execution and gives it to other threads, but because the process has only one thread, it only has a quiet sleep to the specified time (1s), and then resumes the loop at the point of pause.

However, because the condition of the 1th while loop is still true, the 1th while loop body is still being executed.

I originally thought the result is i=1, will and the i=1000 cross to print, actually this kind of thought is wrong.

When the program presses CTRL + C, it exits directly.

#include <stdio.h>#include<stdlib.h>#include<time.h>#include<iostream>#include<signal.h>using namespacestd;intMain () {inti =1;  while(i) {printf ("i=%d\n", i++); Sleep (1); }        intj = +;  while(j) {printf ("j=%d\n", J + +); Sleep (1); }        return 0;}

Output Result:

[[email protected] test]#./a . Out I=1i=2i=3I =4i=5I=6i=7i=8 ^c

2, while example with signal processing function

There are still 2 while loops in this example, and the breakpoint that handles signal is placed in the 1th while loop body.

Because the signal processing function is added, the program responds with CTRL + C, and the following analysis:

The program begins execution of the 1th while loop body, printing

If you encounter Ctrl + C, the response is interrupted (print catch statement and G_sigid), and then back to the loop body, g_sigid condition, exit the 1th while loop.

Then execute a 2nd while loop body, print 1000,1001,

At this time, such as the encounter CTRL + C, still can not block the 2nd cycle to continue the footsteps, because this while there is no set blocking conditions, unlike the 1th while, in the loop in the body of the g_sigid to make a decision as a blocking condition.

And we customize the operation of the CTRL + C signal, so the program does not exit,

The program forcibly exits until you encounter Ctrl + Z.

#include <stdio.h>#include<stdlib.h>#include<time.h>#include<iostream>#include<signal.h>using namespacestd;intG_sigid;voidHandle_sigint (int) {printf ("[Singal]: catched ctrl+c\n"); G_sigid= -;}intMain () {//signal (SIGINT, handle_sigint);        structsigaction SIGHDL; Sighdl.sa_handler=Handle_sigint; Sigemptyset (&sighdl.sa_mask); Sighdl.sa_flags=0; Sigaction (SIGINT,&SIGHDL, NULL); inti =1;  while(i) {printf ("i=%d\n", i++); if(g_sigid== -)                {                         Break; } Sleep (1); }        intj = +;  while(j) {printf ("j=%d\n", J + +); Sleep (1); }        return 0;}

Output Result:

[[email protected] test]#./A. outI=1I=2I=3I=4^c[singal]: catched CTRL +ci=5J= +J=1001J=1002^c[singal]: catched CTRL +CJ=1003J=1004^z[3]+ Stopped./A. out

3, while example with signal processing function

The difference between this example and the 2nd example is that the block condition is set in the two while loop,

So when you print the contents of a 1th while loop body,

If a CTRL + C is encountered at this point, the 1th while loop exits,

Then the 2nd loop body is executed, and the 2nd loop body's blocking condition is also set, so the program exits the 2nd loop body,

Finally, the subsequent code is executed sequentially.

#include <stdio.h>#include<stdlib.h>#include<time.h>#include<iostream>#include<signal.h>using namespacestd;intG_sigid;voidHandle_sigint (int) {printf ("[Singal]: catched ctrl+c\n"); G_sigid= -;}intMain () {//signal (SIGINT, handle_sigint);        structsigaction SIGHDL; Sighdl.sa_handler=Handle_sigint; Sigemptyset (&sighdl.sa_mask); Sighdl.sa_flags=0; Sigaction (SIGINT,&SIGHDL, NULL); inti =1;  while(i) {printf ("i=%d\n", i++); if(g_sigid== -) Break; Sleep (1); }        intj = +;  while(j) {printf ("j=%d\n", J + +); if(g_sigid== -) Break; Sleep (1); } printf ("Mainapp exit\n"); return 0;}

[[email protected] test]#./a . Out I=1i=2i=3^c[ Singal]: catched Ctrl +ci=4J=Mainapp exit

Simple use of signals under Linux

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.