Introduction to signal programming in Linux-Unreliable Signals

Source: Internet
Author: User

Introduction to signal programming in Linux-Unreliable Signals
(Author: mikespook | Release Date: | views: 135)

Keywords: Linux, signal, signal ()
Preface:
This article is only intended to provide guidance to many cainiao like me. If you are a master or have no interest in programming. We recommend that you do not waste time here.

What is the signal? In fact, this is a very interesting thing. For example, when a car is driving on the road, it is a signal, and when it encounters a red light, it must be stopped.

Signal. In the operating system, if the process is an automobile, a signal can be sent between processes. If the operating system is a highway system, the operating system

You can also send signals to processes.
In Linux, CITIC numbers are classified into unreliable (or insecure) signals and reliable signals. I will explain why.
First, we will briefly introduce the general signals in Linux:
Sighup terminal line to suspend. This refers to the suspension of the modem line when the carrier signal is lost. However, when it is disabled using the logout function

Any terminal device.
The SIGINT terminal line has received the interrupt character.
The sigquit terminal line has received the exit character.
SIGUSR1 User-Defined signal 1.
Sigusr2 User-Defined Signal 2.
The process in which sigterm is being terminated.
A subprocess of sigchld has been terminated.
Write operation on the sigpipe semi-closed pipeline.
When the sigalrm alarm function is recorded.
Let's take a look at the signal () function to be used (). The signal () function is defined in the header file signal. h. The original form of its function is sig_t signal (INT Sig,

Sig_t func); the first parameter is the signal to be registered, the second parameter is the pointer to the signal processing function, or the default operation. The system provides two default operations:

Sig_del and sig_ign are default signal operations and ignore signals respectively.

Well, I 've made a lot of boring things, and I'm a little dizzy. Go straight to a program. Remember the previous multi-process program? I made

Some modifications. So that after the sub-process finishes running, it does not have to wait until the parent process runs to the wait () function sequentially, but directly transfers it to the signal processing function. Alas, the complexity is unclear. From

Check it out!

/* -------------------------- Signal_fork.c ------------------------------*/
/* Mikespook */
/* Exercise function signal ()*/
/* 2002.7.18 */

# Include
# Include
# Include
# Include
# Define fac_n 65535

/* The sub-process calls the function. Here I use a loop to simulate a large background operation. */
Void big_loop (int n );
/* The function called by the parent process is actually not put into the function, but it is better to put the program structure in the function */
Void input_information ();
/* Signal processing function. When the parent process receives the sigchld signal, it is transferred to this function for operation */
Void handler ();

Int main ()
{
/* Process Number */
Pid_t PID;
/* Want to use a signal? Register the signal processing function first */
Signal (sigchld, Handler );
/* The program "Forks" here, and a new process is created */
PID = fork ();
/* Determine whether it is a parent process or a child process through the return value of Fork */
Switch (PID ){
/* Return-1. Unfortunately, the process fails to be created. There may be insufficient memory space or too many processes have been started. */
Case-1:
Perror ("fork/N ");
Break;
/* Returns 0 and runs in the sub-process. Then, the sub-process's operation function is called. */
Case 0:
/* A 65535 running cycle. If your machine runs too fast and you cannot see the effect of the two processes running at the same time, you need to increase the number of cycles. Or use

Sleep () function */
Big_loop (fac_n );
/* Obtain the PID of the child process. You can see that the PID of the child process is different from that of the parent process (the PID of the child process is larger than that of the parent process because it is run in the parent process.

). */
Printf ("PID: % d/N", getpid ());
/* After the sub-process is executed, the signal sigchld is sent. As this signal was previously registered, it will be transferred to the signal processing operation. */
Break;
/* Haha, the returned result is neither an error nor a sub-process, that is, the parent process. */
Default:
/* Here, the user is allowed to enter 4 numbers */
Input_information ();
/* Obtain the PID of the sub-process. */
Printf ("PID: % d/N", getpid ());
Break;
}
Exit (0 );
}

/* Big_loop: simple. It can be understood at a glance. */
Void big_loop (int n)
{
Int I;
For (I = 0; I <n; I ++ ){
Switch (I % 4 ){
Case 0:
Putchar ('-');
Break;
Case 1:
Putchar ('/');
Break;
Case 2:
Putchar ('| ');
Break;
Case 3:
Putchar ('//');
Break;
}
Putchar ('/B ');
}
}

/* Input_information: easy to understand at first glance. */
Void input_information ()
{
Int n_table [4], I;

For (I = 0; I <4; I ++ ){
Printf ("Number % d:/T", I );
Scanf ("% d", & n_table [I]);
}

Printf ("number1/tnumber2/tnumber3/tnumber4/N ");
Printf ("% d/T % d/N", n_table [0], n_table [1], n_table [2], n_table [3]);
}

/* The signal processing function sends a sigchld signal to the parent process when the child process ends. At this time, the program transfers the signal to this function for processing */
Void handler ()
{
/* Confirm the parent process and the child process exits */
Wait ();
}
/* -------------------------- Signal_fork.c ------------------------------*/

As you can see, the signal usage is still very simple. However, this is an unreliable signal usage method. Why? The principle is simple. When you run the program, you receive

A signal to be processed, your program will be transferred to the signal processing function to process the signal, and then continue to execute the subsequent program after processing. In this process, if

When a function such as malloc () is executed, the program will not continue to execute because malloc () is not a reentrant function after signal processing.

Malloc (), so that the space may not be allocated, resulting in errors. Therefore, we also need reliable signal processing methods, that is, when performing some operations that cannot be interrupted

Shielding the signal processing. Wait until these key operations are completed and then process the signal. In the future, I will explain in detail how to operate reliable signals.

Reference: http://blog.csdn.net/zhoujunyi/archive/2007/04/13/1563180.aspx

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.