如何生成fork()创建的子进程,然后其他子进程有机会运行?

时间:2022-08-21 20:56:05

In my program, I create 3 child processes and then assign them to do a same thing which is decreasing a number. the program stops when the number=0. I use 2 pipes to communicate between parent and child process.

在我的程序中,我创建了3个子进程,然后指定它们执行相同的操作,即减少数字。数字= 0时程序停止。我使用2个管道在父进程和子进程之间进行通信。

int  a;
int main(void)
{

a=10;
//declare and create 2 pipes
int p1[2], p2[2];
pipe(p1);
pipe(p2);
    int ra;
for(int i=0;i<3;i++)
{   
    pid=fork();
    if(pid==0) 
    {
        close(p1[1]);
        close(p2[0]);
        read(p1[0],&ra,3);

        while(ra>0)
        {

            ra-=1;
            printf("%i a are available, reported by process %i\n",ra,getpid());
            close(p1[0]);
            write(p2[1],&ra,3);

            close(p2[1]);

        }
        break;

    }
    else
    if(pid>0)
    {


    }else
    {
        wait(NULL);

    }

}
  }
 if(pid>0)
{
        close(p1[0]);
        close(p2[1]);
        if(a>0)
        {
            write(p1[1],&a,3);
            close(p1[1]);
        }
        else
            exit(0);
        read(p2[0],&ra,3);
        a=ra;
        close(p2[0]);


}

My problem is there only one child process running and decreasing a value until a=0. other processes dont have a chance. How can I fix that? thank in advance

我的问题是只有一个子进程在运行并减少一个值,直到a = 0。其他过程没有机会。我该如何解决这个问题?预先感谢

1 个解决方案

#1


1  

You want to have each child block on a read from the parent on each iteration of the loop. For example, the child code should be something like:

您希望在循环的每次迭代中从父级读取每个子块。例如,子代码应该类似于:

while( read( p1[0], &a, sizeof a ) == sizeof a ) {
  a -= 1;
  write( p2[1], &a, sizeof a );
}

and the parent code should look like:

并且父代码应如下所示:

do {
    write( p1[1], &a, sizeof a );
} while( read( p2[0], &a, sizeof a ) == sizeof a && a > 0 );
close( p1[1] );

This makes each child block on a read after decrementing the counter, thus going to sleep until the parent is scheduled. After the parent writes a value into the appropriate pipe, only one of the children that is blocking on a read will be awakened to decrement the counter.

这使得每个子块在递减计数器后都处于读取状态,从而进入睡眠状态直到调度父进程。在父项将值写入适当的管道之后,只有一个在读取时阻塞的子项将被唤醒以递减计数器。

#1


1  

You want to have each child block on a read from the parent on each iteration of the loop. For example, the child code should be something like:

您希望在循环的每次迭代中从父级读取每个子块。例如,子代码应该类似于:

while( read( p1[0], &a, sizeof a ) == sizeof a ) {
  a -= 1;
  write( p2[1], &a, sizeof a );
}

and the parent code should look like:

并且父代码应如下所示:

do {
    write( p1[1], &a, sizeof a );
} while( read( p2[0], &a, sizeof a ) == sizeof a && a > 0 );
close( p1[1] );

This makes each child block on a read after decrementing the counter, thus going to sleep until the parent is scheduled. After the parent writes a value into the appropriate pipe, only one of the children that is blocking on a read will be awakened to decrement the counter.

这使得每个子块在递减计数器后都处于读取状态,从而进入睡眠状态直到调度父进程。在父项将值写入适当的管道之后,只有一个在读取时阻塞的子项将被唤醒以递减计数器。