c - 如何知道进程是父进程还是子进程

标签 c linux unix pid systems-programming

如何使用其 pid 识别一个进程是否是另一个进程的子/孙进程?

最佳答案

进程 ID:子进程和父进程

所有运行的程序都有一个唯一的进程 ID。这 process ID , 一个 非负整数,是进程的唯一标识符 始终独一无二。但是,进程 ID 会被重复使用。

当一个进程终止时,它的 ID 变得可以重用。肯定 系统延迟重用,这样新创建的进程就不会混淆 与旧的。

某些 ID 是“保留的”,因为它们正在被使用 系统进程,例如 scheduler 进程。另一个例子是 始终占用 PID 1 的 init 进程。取决于系统 ID 可能会被主动保留。

运行命令

> ps -eaf | head -n 5
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:49 ?        00:00:02 /sbin/init splash
root         2     0  0 11:49 ?        00:00:00 [kthreadd]
root         3     2  0 11:49 ?        00:00:00 [ksoftirqd/0]
root         5     2  0 11:49 ?        00:00:00 [kworker/0:0H]

> pidof init
1

将允许您独立验证这一点。1

在C中我们可以使用下面的函数来获取进程ID 调用进程和调用进程的父进程ID,

#include <unistd.h>

pid_t getpid(void);
pid_t getppid(void);

一个进程可以创建其他进程。创建的进程称为 “子进程”,我们将创建它们的进程称为 “父进程”。

使用 fork() 创建一个新进程

要创建子进程,我们使用系统调用 fork()

#include <unistd.h>

pid_t fork(void);

该函数被父进程调用一次,但它返回 两次。子进程中返回值为0,返回 父进程中的值为新子进程的进程 ID。1

一个进程可以有多个子进程但是没有系统 要求一个进程获取其所有子进程的进程 ID,因此 父进程观察子进程的返回值并可以使用 这些标识符来管理它们。

一个进程只能有一个父进程,它总是 可通过调用 getppid 获得。

child 是 parent 的副本,它得到 parent 的副本 数据空间,堆和栈。他们不分享这些部分 内存! 2

我们将编译并执行以下代码片段,看看如何 这行得通,

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>

int main(void) {
    int var = 42; // This variable is created on the stack
    pid_t pid;

    // Two processes are created here
    //                 v~~~~~~~~~~|
    if ((pid = fork()) < 0) {
        perror("Fork failed");
    } else if (pid == 0) { // <- Both processes continue executing here
        // This variable gets copied
        var++; 

        printf("This is the child process:\n"
               "\t my pid=%d\n"
               "\t parent pid=%d\n"
               "\t var=%d\n", getpid(), getppid(), var);

    } else {
        printf("This is the parent process:\n"
               "\t my pid=%d\n"
               "\t child pid=%d\n"
               "\t var=%d\n", getpid(), pid, var);

    }


    return 0;
}

我们将在执行程序时看到没有任何保证 至于哪个进程首先执行。他们甚至可能经营 同时,有效地交织他们的输出。 3

$ # Standard compilation
$ gcc -std=c99 -Wall fork_example1.c -o fork_example1
$ # Sometimes the child executes in its entirety first
$ ./fork_example1
This is the child process:
     my pid=26485
     parent pid=26484
     var=43
This is the parent process:
     my pid=26484
     child pid=26485
     var=42
$ # and sometimes the parent executes in its entirety first
$ ./fork_example1
This is the parent process:
     my pid=26461
     child pid=26462
     var=42
This is the child process:
     my pid=26462
     parent pid=26461
     var=43
$ # At times the two might interleave
$ ./fork_example1
This is the parent process:
     my pid=26455
This is the child process:
     my pid=26456
     parent pid=26455
     var=43
     child pid=26456
     var=42

1 PID代表Process IDPPID代表 父进程 ID

2 进程ID 0是保留给内核使用的,所以 0 不可能是子进程的 ID。

3 许多系统不执行这些的完整复制 内存段,而不是仅在任一进程时创建副本 执行写入。最初,共享区域由 内核为“只读”,每当进程试图修改这些 内核授予每个进程自己的内存副本的区域。

4 标准输出是缓冲的,所以这不是一个完美的例子。

关于c - 如何知道进程是父进程还是子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40451305/

相关文章:

linux - Ubuntu 中的串行输入,USB 到串行转换器(Pl2303 - Prolific)在 ubuntu 18.04 中不起作用

c - 如何将文件路径放入剪贴板,以便将其作为文件粘贴到文件管理器中?

unix - AIX 与 Unix 命令

c - 我如何理解这个编译器错误: "multiple definition of ..."

linux - 嵌套目录中的 Shell 脚本

linux - 替换文件中的值

c - 为什么需要在 Visual Studio 2010 中将 FILE 指针声明为 main()?

c - 如何在没有段错误的情况下增加 C 中的字符指针?

c++ - 有什么不同?指向数组与常规数组的指针

c - 为什么 Xlib 规范不使用指向 struct Window 的指针?