fork()如何知道它是否在子进程和父进程中? [英] How does the fork() know whether it is in child process and in parent process?

查看:478
本文介绍了fork()如何知道它是否在子进程和父进程中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当执行fork()系统调用时,处理器进入内核模式. 因此,在派生调用结束时,产生了一个新的进程,其中包含堆栈,用户数据和用户程序的副本. 因此,fork现在如何决定在子进程中它是否返回0,并且如果它是主父进程,那么它必须返回子进程的PID.

When a fork() system call gets executed , the processor turns into kernel mode. Thus at the end of the fork call a new process has spawned with copy of stack , user data and user programs. So how does fork decides at this time if it in the child process that it returns 0 and if it is the main parent process then it has to return PID of child process.

fork()被调用两次了吗? 请解释. 我很困惑!!!

Is fork() called twice ?? Please explain. I am confused !!!

推荐答案

调用fork()时,进程将生成具有共享或重复进程段的子进程.这意味着在程序中,在调用fork()之前,只有一个进程或一个执行单元.返回fork()后,将同时运行两个进程.由于这两个进程此时都具有相同的调用堆栈,因此每个进程看起来就像刚刚调用了fork()一样.在父进程中,返回值fork()是子进程的PID.在子进程中,fork()的返回值为0.

When fork() is called, a process spawns a child process with shared or duplicated process segments. What that means is that in a program, before fork() is called, there is only one process, or one unit of execution. After fork() returns, there are two processes running concurrently. Since both processes have the same call stack at this point, it looks to each processes as if it had just called fork(). In the parent process, the return value of fork() is the PID of the child process. In the child process, the return value of fork() is 0.

您可以通过一个非常简单的演示看到这一点:

You can see this with a really simple demonstration:

#include <unistd.h>
#include <stdio.h>

int main(){
    pid_t parent = getpid();
    pid_t child = fork();

    if(child != 0){
        printf("I am the parent process. My PID is %d\n", parent);
        printf("Parent process terminating\n");
    }
    else{
        printf("I am the child process. My PID is %d\n", getpid());
        printf("    My parent's PID is %d\n", parent);
        printf("Child process terminating\n");
    }
    return 0;
}

以下是在笔记本电脑上运行的示例:

Here's a sample run on my laptop:

$ gcc -o fork fork.c
$ ./fork
I am the parent process. My PID is 16048
Parent process terminating
I am the child process. My PID is 16049
    My parent's PID is 16048
Child process terminating
$

请注意,当您运行此命令时,PID将有所不同.另外,输出受竞争条件的影响,因此有时,父进程会在子进程完成打印之前返回到外壳程序,因此它可能像这样:

Note that when you run this, the PIDs will be different. Also, the output is subject to a race condition, so sometimes, the parent process will return to the shell before the child process finished printing, so it might look like this:

$ ./fork
I am the parent process. My PID is 16265
Parent process terminating
$ I am the child process. My PID is 16266
    My parent's PID is 16265
Child process terminating

要理解的重要一点是,fork()会导致单个执行过程分裂为两个独立的单元.由于每个进程仍从同一程序(或源代码)产生,因此两个进程的行为是相同的.它们的输出不同的原因仅是因为fork()为父或子进程返回不同的值.

What's important to understand is that fork() causes the single process of execution to split into two independent units. Since each process is still spawned from the same program (or source code), the behaviour is the same for both processes. The reason their output differs is only due to the fact that fork() returns different values for parent or children processes.

这篇关于fork()如何知道它是否在子进程和父进程中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆