The signaling mechanism in the Linux kernel--A simple example of "go"

Source: Internet
Author: User
Tags sigint signal signal handler

This article was reproduced from: http://blog.csdn.net/ce123_zhouwei/article/details/8562958

The signaling mechanism in the Linux kernel--a simple example

Author:ce123 (http://blog.csdn.NET/ce123)

Signal mechanism is one of the important inter-process communication methods in Unix-like systems. We often use signals to send a short message to a process. For example: Suppose we start a process to read the network packets sent by the remote host through the socket, at this time due to network factors the current host has not received the corresponding data, the current process is set to an interruptible wait state (task_interruptible), at this time we have lost patience, To end this process prematurely, you can send a kill signal to the process through the KILL command, the kernel wakes up the process, executes its signal handler, and the kill signal's default processing is to exit the process. Of course, it is not certain that the process is in the task_interruptible state to handle the signal.

In addition, the application can set a default handler for a signal through functions such as signal (). For example, when the user presses CTRL + C, the shell emits a SIGINT signal, and the default handler for SIGINT is the exit code that executes the process, but the following example sets the SIGINT response function to Int_handler.

[Plain]View PlainCopyprint?
  1. #include <signal.h>
  2. #include <stdio.h>
  3. void Int_handler (int signum)
  4. {
  5. printf ("\nsigint signal handler.\n");
  6. printf ("exit.\n");
  7. Exit (-1);
  8. }
  9. int main ()
  10. {
  11. Signal (SIGINT, Int_handler);
  12. printf ("Int_handler set for sigint\n");
  13. while (1)
  14. {
  15. printf ("Go to sleep.\n");
  16. Sleep (60);
  17. }
  18. return 0;
  19. }

When executing the above code, first execute the main function, set the SIGINT handler function, and go to sleep, the process enters the interruptible wait state:

When CTRL + C is pressed, the process is awakened to execute SIGINT's handler function, Int_handler (), and the process exits.

· Signal distribution and processing is carried out in the kernel state, when from the above example can read, the signal processing function may be in the user state, in this case, the kernel needs to build a temporary user-state environment, and then call the user-state signal processing function.

The signaling mechanism in the Linux kernel--A simple example of "go"

Related Article

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.