Check if process exists given its pid

后端 未结 6 1281
孤独总比滥情好
孤独总比滥情好 2020-12-01 02:06

Given the pid of a Linux process, I want to check, from a C program, if the process is still running.

6条回答
  •  醉话见心
    2020-12-01 02:27

    Use procfs.

    #include 
    [...]
    struct stat sts;
    if (stat("/proc/", &sts) == -1 && errno == ENOENT) {
      // process doesn't exist
    }
    

    Easily portable to

    • Solaris
    • IRIX
    • Tru64 UNIX
    • BSD
    • Linux
    • IBM AIX
    • QNX
    • Plan 9 from Bell Labs

提交回复
热议问题