如何使用Fork()只创建两个子进程?

时间:2022-12-07 22:12:27

I'm starting to learn some C and while studying the fork, wait functions I got to a unexpected output. At least for me.

我开始学习一些C语言,在学习fork函数的时候,我得到了一个意想不到的输出。至少对我来说是这样。

Is there any way to create only 2 child processes from the parent?

是否有方法从父进程中创建两个子进程?

Here my code:

这里我的代码:

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

int main ()
{
    /* Create the pipe */
    int fd [2];
    pipe(fd);

    pid_t pid;
    pid_t pidb;


    pid = fork ();
    pidb = fork ();

    if (pid < 0)
    {
        printf ("Fork Failed\n");
        return -1;
    }
    else if (pid == 0)
    {
        //printf("I'm the child\n");
    }
    else 
    {
        //printf("I'm the parent\n");
    }

    printf("I'm pid %d\n",getpid());

    return 0;
}

And Here is my output:

这是我的输出:

I'm pid 6763
I'm pid 6765
I'm pid 6764
I'm pid 6766

Please, ignore the pipe part, I haven't gotten that far yet. I'm just trying to create only 2 child processes so I expect 3 "I'm pid ..." outputs only 1 for the parent which I will make wait and 2 child processes that will communicate through a pipe.

拜托,忽略管道部分,我还没到那一步。我只是尝试创建两个子进程,所以我希望3“我是pid…”只输出1个父进程,我将让等待和2个子进程通过管道进行通信。

Let me know if you see where my error is.

如果你知道我的错误在哪里,请告诉我。

5 个解决方案

#1


31  

pid = fork (); #1
pidb = fork (); #2

Let us assume the parent process id is 100, the first fork creates another process 101. Now both 100 & 101 continue execution after #1, so they execute second fork. pid 100 reaches #2 creating another process 102. pid 101 reaches #2 creating another process 103. So we end up with 4 processes.

让我们假设父进程id为100,第一个fork创建另一个进程101。现在100和101在#1之后继续执行,所以它们执行第二个fork。pid 100到达2,创建另一个进程102。pid 101到#2创建另一个进程103。最后我们得到4个过程。

What you should do is something like this.

你应该做的是这样的。

if(fork()) # parent
    if(fork()) #parent
    else # child2
else #child1

#2


13  

After you create process , you should check the return value. if you don't , the seconde fork() will be executed by both the parent process and the child process, so you have four processes.

创建过程之后,应该检查返回值。如果不这样做,第二个fork()将由父进程和子进程执行,因此您有四个进程。

if you want to create 2 child processes , just :

如果要创建两个子进程,只需:

if (pid = fork()) {
    if (pid = fork()) {
        ;
    } 
} 

You can create n child processes like this:

可以像这样创建n个子进程:

for (i = 0; i < n; ++i) {
    pid = fork();
    if (pid) {
        continue;
    } else if (pid == 0) {
        break;
    } else {
        printf("fork error\n");
        exit(1);
    }
}

#3


3  

When a fork statement is executed by the parent, a child process is created as you'd expect. You could say that the child process also executes the fork statement but returns a 0, the parent, however, returns the pid. All code after the fork statement is executed by both, the parent and the child.

当父进程执行fork语句时,将按预期创建子进程。您可以说子进程也执行fork语句,但是返回一个0,但是父进程返回pid。fork语句之后的所有代码都由父类和子类执行。

In your case what was happening was that the first fork statement created a child process. So presently there's one parent, P1, and one child, C1.

在您的例子中,第一个fork语句创建了一个子进程。现在有一个家长,P1和一个孩子,C1。

Now both P1 and C1 encounter the second fork statement. The parent creates another child (c2) as you'd expect, but even the child, c1 creates a child process (c3). So in effect you have P1, C1, C2 and C3, which is why you got 4 print statement outputs.

现在P1和C1都遇到了第二个fork语句。父进程按您的期望创建另一个子进程(c2),但是即使是子进程,c1也创建一个子进程(c3)。实际上有P1 C1 C2 C3,这就是为什么有4个print语句输出。

A good way to think about this is using trees, with each node representing a process, and the root node is the topmost parent.

考虑这个问题的一个好方法是使用树,每个节点表示一个进程,根节点是最上面的父节点。

#4


0  

you can check the value as if ( pid < 0 ) process creation unsuccessful this tells if the child process creation was unsuccessful.. fork returns the process id of the child process if getpid() is used from parent process..

您可以检查该值,就好像(pid < 0)进程创建不成功一样。如果从父进程使用getpid(), fork将返回子进程的进程id。

#5


0  

You can create a child process within a child process. This way you can have 2 copies of the original parent process.

您可以在子进程中创建子进程。这样您就可以拥有两个原始父进程的副本。

int main (void) {
    pid_t pid, pid2;
    int status;

    pid = fork();

    if (pid == 0) { //child process
        pid2 = fork();
        int status2;

        if (pid2 == 0) { //child of child process
            printf("friends!\n");
        }
        else {
            printf("my ");
            fflush(stdout);
            wait(&status2);
        }
    }
    else { //parent process
        printf("Hello ");
        fflush(stdout);
        wait(&status);
    }

    return 0;
}

This prints the following:

这个打印以下:

Hello my friends!

#1


31  

pid = fork (); #1
pidb = fork (); #2

Let us assume the parent process id is 100, the first fork creates another process 101. Now both 100 & 101 continue execution after #1, so they execute second fork. pid 100 reaches #2 creating another process 102. pid 101 reaches #2 creating another process 103. So we end up with 4 processes.

让我们假设父进程id为100,第一个fork创建另一个进程101。现在100和101在#1之后继续执行,所以它们执行第二个fork。pid 100到达2,创建另一个进程102。pid 101到#2创建另一个进程103。最后我们得到4个过程。

What you should do is something like this.

你应该做的是这样的。

if(fork()) # parent
    if(fork()) #parent
    else # child2
else #child1

#2


13  

After you create process , you should check the return value. if you don't , the seconde fork() will be executed by both the parent process and the child process, so you have four processes.

创建过程之后,应该检查返回值。如果不这样做,第二个fork()将由父进程和子进程执行,因此您有四个进程。

if you want to create 2 child processes , just :

如果要创建两个子进程,只需:

if (pid = fork()) {
    if (pid = fork()) {
        ;
    } 
} 

You can create n child processes like this:

可以像这样创建n个子进程:

for (i = 0; i < n; ++i) {
    pid = fork();
    if (pid) {
        continue;
    } else if (pid == 0) {
        break;
    } else {
        printf("fork error\n");
        exit(1);
    }
}

#3


3  

When a fork statement is executed by the parent, a child process is created as you'd expect. You could say that the child process also executes the fork statement but returns a 0, the parent, however, returns the pid. All code after the fork statement is executed by both, the parent and the child.

当父进程执行fork语句时,将按预期创建子进程。您可以说子进程也执行fork语句,但是返回一个0,但是父进程返回pid。fork语句之后的所有代码都由父类和子类执行。

In your case what was happening was that the first fork statement created a child process. So presently there's one parent, P1, and one child, C1.

在您的例子中,第一个fork语句创建了一个子进程。现在有一个家长,P1和一个孩子,C1。

Now both P1 and C1 encounter the second fork statement. The parent creates another child (c2) as you'd expect, but even the child, c1 creates a child process (c3). So in effect you have P1, C1, C2 and C3, which is why you got 4 print statement outputs.

现在P1和C1都遇到了第二个fork语句。父进程按您的期望创建另一个子进程(c2),但是即使是子进程,c1也创建一个子进程(c3)。实际上有P1 C1 C2 C3,这就是为什么有4个print语句输出。

A good way to think about this is using trees, with each node representing a process, and the root node is the topmost parent.

考虑这个问题的一个好方法是使用树,每个节点表示一个进程,根节点是最上面的父节点。

#4


0  

you can check the value as if ( pid < 0 ) process creation unsuccessful this tells if the child process creation was unsuccessful.. fork returns the process id of the child process if getpid() is used from parent process..

您可以检查该值,就好像(pid < 0)进程创建不成功一样。如果从父进程使用getpid(), fork将返回子进程的进程id。

#5


0  

You can create a child process within a child process. This way you can have 2 copies of the original parent process.

您可以在子进程中创建子进程。这样您就可以拥有两个原始父进程的副本。

int main (void) {
    pid_t pid, pid2;
    int status;

    pid = fork();

    if (pid == 0) { //child process
        pid2 = fork();
        int status2;

        if (pid2 == 0) { //child of child process
            printf("friends!\n");
        }
        else {
            printf("my ");
            fflush(stdout);
            wait(&status2);
        }
    }
    else { //parent process
        printf("Hello ");
        fflush(stdout);
        wait(&status);
    }

    return 0;
}

This prints the following:

这个打印以下:

Hello my friends!