Signal kill and raise Functions

Source: Internet
Author: User

The kill function sends signals to processes or process groups. The raise function allows a process to send signals to itself.

#include <signal.h> kill(pid_t pid,  raise(-

Call

raise(signo);

It is equivalent to calling

kill(getpid(), signo);

Kill has four different pid parameters:

Pid> 0 sends the signal to the process whose ID is pid.

Pid = 0 send the signal to all processes in the same process group of the sending process (the process group ID of these processes is equal to the Process Group ID of the sending process ), the sending process has the permission to send signals to these processes. Note that the term "all processes" here does not include the system process set that implements the definition. For most UNIX systems, the system process set includes kernel processes and init (pid 1 ).

Pid <0 sends signals to all processes in the process group whose process group ID is equal to the absolute value of pid, And the sending process has the permission to send signals to it. As mentioned above, "all processes" do not include certain system processes.

Pid =-1 send the signal to all processes on the system where the sender has the permission to send the signal to them. As mentioned above, "all processes" do not include certain system processes.

The process requires the following permissions to send signals to other processes:

Super Users can send signals to any process.

For non-superusers, the basic rule is that the actual or valid user ID of the sender must be equal to the actual or valid user ID of the recipient. If _ POSIX_SAVED_IDS is supported, check the Set User ID (rather than the valid user ID) Saved by the receiver ). When testing permissions, there is also a special case: if the signal sent is SIGCONT, the process can send it to any other process that belongs to the same session.

POSIX.1 define a signal numbered 0 as an empty signal. If the signo parameter is 0, kill still performs a normal error check without sending a signal. This is often used to determine whether a specific process still exists. If an empty signal is sent to an existing process, kill returns-1 and errno is set to ESRCH. However, it should be noted that the UNIX system will re-use the process ID after a period of time, so an existing process with the given process ID is not necessarily the process you want.

It should also be understood that testing whether a process exists is not an atomic operation. When kill returns the test result to the caller, the existing tested process may have been terminated at this time, so this test has little value.

If kill is called to generate a signal for the calling process and the signal is not blockedBefore kill returnsThen, the signo or another outstanding non-blocking signal is sent to the process. (There are some additional conditions for threads ).

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

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.