Operating system fork如何返回Pid?

Operating system fork如何返回Pid?,operating-system,fork,pid,systems-programming,Operating System,Fork,Pid,Systems Programming,我试图理解fork如何返回子进程id,因为子方法没有返回,也没有通过其他机制将其id发送给父进程。在最低级别上,我不理解是否让我们假设子进程是一个长时间运行的循环: //parent code ...some code... Pid=fork([see below]) ...some code... //some file containing the executable code of the child process void childProcessRunn

我试图理解
fork
如何返回子进程id,因为子方法没有返回,也没有通过其他机制将其id发送给父进程。在最低级别上,我不理解是否让我们假设子进程是一个长时间运行的循环:

//parent code 
    ...some code...
    Pid=fork([see below])
    ...some code...

//some file containing the executable code of the child process
void childProcessRunningMethod()
{
    while(true);
}
谁负责将
Pid
分配给新流程以及何时发生。分配子流程的
Pid
的人如何工作

子方法是否会被覆盖为以下内容:

void childProcessRunningMethod(string parentPipeAddress)
{
    var somePipe=new Pipe(parentPipeAddress);
    somePipe.Open();
    somePipe.Send([ Pid]); //somehow generates its own Pid
    somePipe.Close();
    
    while(true);
}

发动机罩下的叉状物如下所示:

int fork() {
 1. generate a new PID for child                        // executed only by parent process
 2. do million more things required to create a process // executed only by parent
 /* now we have a new process in the system, which can be scheduled on CPU */
 3. finally return value of a specific CPU register     // executed by both parent and child
 // Note that at this point we have two processes, 
 // in case of child process the CPU register contains 0 (fork returns 0 to child)    
 // in case of parent process register contains PID of child
}
因此,正如您在父进程中看到的
fork
不必等待子进程返回子进程的
PID
,因为父进程已经可以使用它