ActiveState Code

Recipe 576794: process exist? by reading /proc


判断一个进程还存不存在

C
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/**
 *@brief 判断一个进程还存不存在
 *
 */
int process_exist(pid_t pid){
    char buf[32] = {0};
    struct stat file_info;

    sprintf(buf, "/proc/%d",pid);

    errno = 0;
    if(-1 == stat(buf, &file_info) ){
        printf("\t\t\t %s : %s", buf, strerror(errno));
        return 0;
    }
    else{
        return 1;
    }
}

Sign in to comment