Linux Learning Notes (10)-Signals

Source: Internet
Author: User

The so-called signal (Singal), in my understanding, in fact, and microcontroller development in the same as the interruption, but it is not provided by the system hardware, but the software operating system support a reminder mechanism.

The processing method after receiving the signal, generally consists of three kinds:

(1) The first is similar to the interrupt handler function, and for the signal to be processed, the process specifies a handler function.

(2) The second is to ignore a signal and not do any processing.

(3) The third Way is to use the system default processing, such as CTRL + C to terminate the current process.

  

There are more than 30 types of signals commonly used in Linux, each of which starts with the keyword sig, such as an abnormally terminated signal called SIGABRT.

In the header file "Singal.h", they are all defined as integers.

Here are some commonly used semaphores:

SIGALRM: The signal generated when the timing time arrives after using the Timer function alarm ()

SIGINT: The signal generated when the user uses CTRL + C at the terminal (if no other redefinition is made to the signal, then it is meant to terminate the current process)

SIGKILL: This signal is one of two signals that cannot be captured and cannot be ignored, and his role is to kill a process

SIGSTOP: This signal is one of two signals that cannot be captured and cannot be ignored, and his role is to stop a process

......

Use the kill-l command to produce a view of the current system's semaphore usage.

  

Signal if used in code, of course there are corresponding functions, such as functions: sigaction () This function is used to redefine, when the signal is generated when the processing method ...

In the microcontroller programming, a bit like the interrupt callback function of the login!

The prototype function Sigaction function is as follows:

  sigaction (int signum, const struct sigaction *act, struct sigaction *oldaction)

Now the parameters are explained:

 int Signum : Specifies the signal that you want to modify the processing, such as SIGINT, when the user outputs Ctrl + C in the terminal, we can change its processing, that is, without terminating the current process

As mentioned above, there are two signal processing can not be changed by the user, is Sigkill and sigstop These two signals, kill also can't change

  Act and oldact are the same type of struct, which specifies new processing, which is the original processing method.

The prototype of the sigaction structure is as follows:

  

void (*sa_handler) (int) is a function pointer used to specify the handler function after the signal has occurred.

void (*sa_sigaction) (int,siginfo_t*,void*) is also used to specify the processing function after the signal has occurred ...

As to which of the above two functions to use after the signal has occurred, it depends on the member sa_flags. If the value of Sa_flags contains Sa_siginfo, the second function is used, otherwise the first function is used.

The Sa_mask member is used to specify a signal that needs to be masked during the execution of the signal function.

The Sa_flags member is used to specify the behavior of the signal processing, which is the bitwise OR combination of the values below.

Sa_restart: A process restart that is interrupted by a signal

Sa_nocldstop: The parent process does not receive a sigchid signal when the child process pauses or continues to run.

Explain:

SIGCLD is a very important signal in the multi-process programming of Linux.  When a child process exits, it does not immediately release the resource it occupies, but instead notifies its parent process of subsequent work by the parent process.  In this process, the following events are generated sequentially.  1) sends the SIGCLD signal to the parent process, and the child process enters the zombie (zombie) state. 2) The parent process receives the SIGCLD signal for processing.

However, I still do not understand this signal!

Sa_nocldwait: The parent process does not receive a sigchid signal when the child process exits. This way, the process will not form a zombie process even if it exits.

Sa_nodefer: The shielding of the signal is not valid, even in the Interrupt Service function processing, still can send this signal

Sa_resethand: After the signal has been processed once, the original default processing of the system is restored

Sa_siginfo: Specifies whether the first function or the second function is used after a signal has occurred

——————————————————————————————————————————————————————

Now start to write code, the requirements are as follows, when the user presses CTRL + C, the system does not terminate the current process, but to promise a word, and then press once, then the process will be terminated.

  

#include <signal.h>#include<stdio.h>#include<stdlib.h>voidHandlerintSIG) {printf ("Catch a CTRL + C signal:%d.\n", SIG); return;}intMainvoid){    structSigaction Act; Act.sa_handler= handler;//Specify the signal service function to useAct.sa_flags = Sa_resethand;//setting the signal reversion modeSigemptyset (&act.sa_mask);//emptying the masked signal setsigaction (sigint,handler,null);//Redefining Signal Processing     while(1)    {        //wait for the signal to happenprintf"wait for the signal to happen! \ n"); Sleep (1); }    return 0;}

The code is written, and now it's done makefile

Okay, this is done!

Now execute the compile command!

A lot of warnings, eh! How to do it!!!

  

Sigaction (Sigint,&act,null); //I wrote it wrong here, the function directly as a structure

Modified after compiling ... Safe Pass, execute code.

The results are as follows:

  

Now I'll press CTRL + C to see what happens.

  

The process did not terminate, but, according to our request, a word was output. What's the meaning of that 2 rep? I don't know now ... If you know, I hope you can tell me.

Then I press CTRL + C again and the result is as follows:

  

The process stopped ...

Of course, if you modify the code, the system default processing Reset statement screen out, then this process will never stop!

Act.sa_flags   = sa_resethand;//Set signal reversion mode

............

Now use another signal, alarm clock signal to achieve a function, both, the system after hibernation, every 3 seconds to print out the current time.

  

#include <unistd.h>#include<stdio.h>#include<signal.h>#include<stdlib.h>#include<time.h>voidHandlerintSIG)    {time_t curtime; Time (&curtime); printf ("Current time:%s\n", CTime (&curtime)); Alarm (3);}intMainvoid) {Alarm (3);    Signal (Sigalrm,handler); printf ("Go to sleep! \ n");  while(1) {Sleep (1); }    return 0;}

The code is written and the results are as follows!

  

The execution result is correct.

Of course, this is just two simple signals, there are other signals to try again another day, now almost 11 o'clock ... I have to go to work tomorrow!

Linux Learning Notes (10)-Signals

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.