Linux0.11 signal mechanism

Source: Internet
Author: User
Linux0.11 signal mechanism this article briefly describes the implementation of the linux0.11 signal mechanism www.2cto.com 1: When the process receives a signal, the process calls the signal processing function according to the relevant settings. There are three types of signal processing methods: default processing method, ignore signal method, and execute user settings... linux0.11 signal mechanism

This article briefly describes the implementation of the linux0.11 signal mechanism
Www.2cto.com
I. related signals
When a process receives a signal, the process calls the signal processing function according to the relevant settings.

There are three types of signal processing methods: the default processing method, the ignore signal method, and the execution of user-defined signal processing functions.

Method of sending signals: Press the corresponding key (such as CTRL + C), and use the kill command or function to send signals to the specified process.

Typedef void sig_func (int );
Sig_func * signal (int signr, sig_func * handler );

After signal (signo, handler) is called in the process,
If the process receives the signal signo, the process executes the function pointed to by handler.

Suppose there are process A and process B. When process A sends A signal to process B, when will the signal processing function of Process B be executed?
To execute the signal processing function of Process B, Process B must be in the execution state. That is to say, only when the scheduler is scheduled to process B,
It is possible to execute the signal processing function of Process B. Otherwise, Process B is not in the executable status, and it is useless to receive a signal.

If process B calls the kill function and sends a signal to itself, we will assume that the signal processing function will be executed immediately.
Therefore, after the system function kill is executed, the signal of Process B is processed immediately.


From these two points, we can realize that the kernel needs to process the signal of the current process after clock interruption and system call.
The schedule function is called when the clock is interrupted because it is a time-sharing system,
If process A sends A signal to process B and is now scheduled to process B, the signal processing function of Process B must be executed.


II. signal mechanism of linux0.11
Take the kill function as an example to briefly describe the general process. The following describes the do_signal function in the kernel in detail.

After sending a signal to the current process using the kill function.
Because this is a system function, int 0x80 will be executed to enter the system_call entry point.
_ System_call:
Cmpl $ nr_system_calls-1, % eax # % eax saves the call number of the kill function
Ja bad_sys_call # invalid system call
Push % ds
Push % es
Push % fs
Push % edx
Push % ecx
Push % ebx # Related data into the stack
.....................
Call _ sys_call_table (, % eax, 4) # run the system call. here is the sys_kill function.
Pushl % eax # The return value of the system call is written into the stack, that is, the return value of sys_kill.
......................
 
Ret_from_sys_call:
.....
Pushl % ecx # % ecx stores the signal value.
Call _ do_signal # process the signal
Popl % eax # exit the signal value from the stack
Popl % eax # store the return value of the system call to the stack, that is, the return value of sys_kill, in the % eax register.
Popl % ebx
Popl % ecx
Popl % edx
Pop % fs
Pop % es
Pop % ds
Iret


It can be seen that after each system call, ret_from_sys_call may be executed to process the signal.

In addition to the _ system_call method, the ret_from_sys_call method is also called under some interruptions. clock interruption is one of them.
Now we know when the kernel processes the signal.


3. do_signal function.
Do_signal mainly sets the kernel stack and application user stack. after the stack is set,
When the following iret command of ret_from_sys_call is executed, the signal processing function of the process is automatically executed. After the signal processing function is executed, the next instruction of the process is executed.

It is written in the book Linux0.11 kernel full comment, which shows the stack changes before and after calling do_signal.

The left side is the kernel state stack, which is the stack content before the call _ do_signal is executed.

Do_signal:
1: save the eip value in the stack to old_eip, and the old_eip points to the code to be executed in the user program.
2: execute the eip signal processing function. In this way, when the iret in ret_from_sys_call is executed, the code directed to the cs: eip is executed, that is, the signal processing function.
3: move the esp value of the user State stack down 7 or 8 long words (32-bit)
4: then put the sa_resotrer and signr values into the stack, as shown in the user stack on the right.

After the preceding operations are completed, do_signal is executed and returned to ret_from_sys_call,
Ret_from_sys_call: execute the pop T command after executing some pop operations, and then jump to the signal processing function to execute.
After the signal processing function is executed, the ret operation is executed (ret is used for function return and iret is used for interrupted return). then the sa_restorer is stored in the eip,
Therefore, the sa_restorer

Sa_restorer restores the user stack.
_ Sig_restore:
Addl $4, % esp
Popl % eax # save the system call return value to eax
Popl % ecx
Popl % edx
Popfl
Ret
After popfl is executed, it is obvious that there is only old_eip in the user stack. Therefore, when ret is executed, the program will jump to cs: old_eip for execution, that is, the next user command called by the system.
So far, the signal processing function has been executed, and the system call has been returned. the user program can proceed without any worries.
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.