Linux uses kill 0 to detect if a process exists

Source: Internet
Author: User

Previously encountered Kill (PID, 0) situation, due to the usual did not notice the KILL function of the shape parameter 0, do not know its role. A description of the next kill function is viewed with the Man 2 Kill command, and is found to detect the presence of the process.

Man 2 Kill View a description of parameter 0

1 Description of Kill 0 1

From the text in the description area above, you can see that the parameter sig in the Kill function is 0, so no signal is sent to the PID process, but the error continues to be detected (process ID or process group ID exists). The return value of the Kill function and the specific error message are as follows:

Return VALUE
On success (in least one signal was sent), the zero is returned. On Error,-1
is returned, and errno is set appropriately.

Einval an invalid signal is specified.

Eperm the process does not have permission to send the signal to any of the
Target processes.

Esrch the PID or process group does not exist. Note this an existing
Process might be a zombie, a process which already committed Termina?
tion, but has not yet been wait (2) Ed for.

2 Simple test procedure

#include <stdio.h>
#include <signal.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>

#define GO_FREE (P) \
Free (p); \
p = NULL; \
printf ("exit.\n");

int main (int argc, char **argv)
    if (argc!= 2)
   ;      printf ("Usage:%s%s\n", Argv[0], "pid");
        Exit (-1);
    Char *p = (char *) malloc (sizeof (char) * 20);
    memcpy (P, argv[1], strlen (argv[1));

* No signal is sent, but error checking is still performed;
* This can is used to check for the existence of a process ID or process group ID.
if (Kill (Atoi (p), 0) = = 1)
Perror ("error");
printf ("signal[%s] exist.\n", p);
Go_free (P);
return 0;

3 Test Execution

Use the makefile file in the article "Common Makefile for generating elf, dynamic/static library files in Linux C + + project" To compile the program, of course, you can also compile GCC using the command chk_signal_exist.c-o Chk_signal_ Exist

First Test the process does not exist: Randomly enter a process ID (23232) as parameter 1

Then test the existence of the process: first use the PS command to view the existing process Daemon_sleep1 (20608), and then pass as parameter 1 to the

4 Other Notes

There is information on the Internet that 0 represents the signal 0, but the use of command kill-l did not show, so here personally feel should not be called signal 0. It is only a normal parameter in the KILL function, when greater than 0 sends the corresponding signal to a process, equal to 0 when the detection of a process exists

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: 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.