C语言 父进程创建子进程并且父进程和子进程运行相同程序不同代码的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16077528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:04:24  来源:igfitidea点击:

A program where parent process creates a child process and both parent and child run same program different code

cunixprocessfork

提问by Ashutosh Singh

//same program different code
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
    int pid;
    pid=fork();
    if(pid<0)
    {
        printf("\n Error ");
        exit(1);
    }
    else if(pid==0)
    {
        printf("\n Hello I am the child process ");
        printf("\n My pid is %d ",getpid());
        exit(0);
    }
    else
    {
        printf("\n Hello I am the parent process ");
        printf("\n My actual pid is %d \n ",getpid());
        exit(1);
    }

}

I tried this , I hope its correct .
But I am not satisfied with the output .

我试过了,我希望它是正确的。
但我对输出不满意。

The output is :

输出是:

 Hello I am the parent process 
 My actual pid is 4287 
 ashu@ashu-VirtualWorld:~/Desktop/4thSemester/testprep$ 
 Hello I am the child process 
 My pid is 4288

Please help me I cant understand its output , I want the child process to occur first and then parent process . Also , when the execution ends the control is transferred to the program , so to return to terminal I have to use ctrl+c , I want that after the execution of the program ends the control transfers to the terminal .

请帮帮我,我无法理解它的输出,我希望子进程先发生,然后是父进程。另外,当执行结束时,控制权转移到程序,所以要返回终端我必须使用 ctrl+c ,我希望在程序执行结束后控制权转移到终端。

回答by unwind

The thing with parallel processes is that they don't map well to the idea of "happening first"; they're running in parallel.

并行进程的问题是它们不能很好地映射到“先发生”的想法;他们并行运行。

Of course, you can change the odds by having a delay in the code for the parent, before the print-outs.

当然,您可以通过在打印输出之前延迟父代码的代码来更改赔率。

Not at all sure what you mean by "the control is transferred to the program"; since both processes will hit exit()very quickly after having printed their messages, there should be no program left to transfer control to.

完全不确定您所说的“控制权已转移到程序”是什么意思;由于两个进程exit()在打印它们的消息后都会很快命中,因此应该没有程序可以将控制转移到。

Note that you don't need to use exit()from main(), it should end with a plain old returnsince its return type is int.

请注意,您不需要使用exit()from main(),它应该以普通的 old 结尾,return因为它的返回类型是int.

回答by loganaayahee

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
int main()
{
    int status;
    int pid;
    pid=fork();
    if(pid<0)
    {
        printf("\n Error ");
        exit(1);
    }
    else if(pid==0)
    {
        printf("\n Hello I am the child process ");
        printf("\n My pid is %d ",getpid());
        exit(0);
    }
    else
    {
       wait(&status);
        printf("\n Hello I am the parent process ");
        printf("\n My actual pid is %d \n ",getpid());
        exit(1);
    }

}

In this program the if (pid == 0) means child and pid > 0 means parent so the parent and child are running in the same time access the same resource so the problem of the race condition occur. The which one is first access the resource its executed first and another one is executed at later time.

在这个程序中,if (pid == 0) 表示子进程,pid > 0 表示父进程,因此父进程和子进程同时运行访问相同的资源,因此会出现竞争条件的问题。哪个首先访问它首先执行的资源,另一个在稍后执行。

The wait function avoid the race condition and when the child execution complete until the parent wait and after executed the parent

等待函数避免了竞争条件,当子执行完成时,直到父等待和执行父之后

The Default vfork avoid the race condition

默认 vfork 避免竞争条件

        pid=vfork();

Because the vfork use the parent wait for until the child complete. 

Also if you want to get the process ID of parent process. Use int ppid = getppid() function.

此外,如果您想获取父进程的进程 ID。使用 int ppid = getppid() 函数。

The output of the program is:

程序的输出是:

     Hello I am the child process 
     My pid is 7483 
     Hello I am the parent process 
     My actual pid is 7482