kill 0 判断进程存活

通过kill 0,可以判断进程是否还存活着:

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

static int checkproc(const pid_t proc)
{    
    return kill(proc, 0) == 0;
}

int main()
{
	char file[20] = "client-00000501";
	pid_t pid,pid2;
	pid = (pid_t) strtoul(file + sizeof "client-", NULL, 10);
	if((pid2=fork()) == 0)
		printf("1=====%d\n",checkproc(pid));
	if(pid2>0)
		printf("2=====%d\n",checkproc(pid));
	return 0;
}

你可能感兴趣的:(kill 0 判断进程存活)