如何以某种顺序运行分叉进程?

时间:2022-05-04 13:50:57

Given the scenario, I am asked to paint the boxes which have various color tags which indicate the color that the boxes will be painted with and those boxes arrive at a painting unit which has a box capacity of 2, which means it can only paint 2 boxes concurrently. The unit can not paint different colors at the same time. It has to wait for a color of boxes to finish before changing the color and paint other boxes.

鉴于这种情况,我被要求绘制具有各种颜色标签的盒子,这些颜色标签表示盒子将被涂上的颜色,并且那些盒子到达一个容器容量为2的涂装单元,这意味着它只能涂2盒子同时。本机不能同时绘制不同的颜色。在更改颜色和绘制其他盒子之前,必须等待盒子的颜色完成。

Processes and threads will be used in the process. The problem is, I am asked to paint the boxes in a FIFO manner. Consider the example below:

进程中将使用进程和线程。问题是,我被要求以FIFO方式绘制框。考虑以下示例:

Box #1    Box #2    Box #3    Box #4    Box #5
  R         B         G         R         B

I have to paint in the given order:

我必须按照给定的顺序进行绘画:

Box #1, Box #4, Box #2, Box #5, Box #3
  R        R       B       B        G

I can arrange the color sequence which the unit will use to paint boxes(RBG in this case) by the help of a pointer, but I cannot guarantee that the boxes will be painted in the given order, i.e. the order might be 4 1 5 2 3 instead of 1 4 2 5 3, thus I will violate the FIFO rule since there will be #ofBoxes processes will be running(not concurrently, in total) and the order is not determined. The only method I could think of was (somehow) considering the order of the forked processes (each box) and somehow prioritize the painting operation but I don't think this is even a solution. This could be solved easily by a sequential approach (sorting then painting), but considering IPC and concurrent processes, sorting will not help at all, at least won't guarantee any ordering.

我可以通过指针安排单位用于绘制框的颜色序列(在这种情况下为RBG),但我不能保证框将按给定的顺序绘制,即顺序可能是4 1 5 2 3而不是1 4 2 5 3,因此我将违反FIFO规则,因为将会运行#ofBoxes进程(不同时,总共)并且未确定顺序。我能想到的唯一方法是(某种程度上)考虑分叉进程(每个框)的顺序,并以某种方式优先考虑绘制操作,但我认为这甚至不是解决方案。这可以通过顺序方法(排序然后绘制)轻松解决,但考虑到IPC和并发过程,排序根本没有帮助,至少不能保证任何排序。

I am kind of stuck here and I am only asking a hint to solve the problem, not the exact solution. Please be as abstract as you can. Thanks in advance.

我有点被困在这里,我只是要求提示解决问题,而不是确切的解决方案。请尽量抽象。提前致谢。

EDIT

Excuse me for not mentioning some necessary details. I am forced to model each box as a separate process and not to use signals for synchronization.

对不起,我没有提到一些必要的细节。我*将每个盒子建模为一个单独的过程,而不是使用信号进行同步。

1 个解决方案

#1


1  

You can always start the processes one-by-one, and wait for the new process to finish before you start the next one.

您始终可以逐个启动流程,并在开始下一个流程之前等待新流程完成。

Or you can use pipe to chain all processes together, making the stdout of the first process connect to stdout of the second, and so on. Then in the child process you don't continue until you get some input from the previous process to continue.

或者您可以使用管道将所有进程链接在一起,使第一个进程的stdout连接到第二个进程的stdout,依此类推。然后在子进程中,您不会继续,直到您从上一个进程获得一些输入才能继续。

Or use something like named semaphores to signal each other.

或者使用命名信号量之类的信号来互相发出信号。

#1


1  

You can always start the processes one-by-one, and wait for the new process to finish before you start the next one.

您始终可以逐个启动流程,并在开始下一个流程之前等待新流程完成。

Or you can use pipe to chain all processes together, making the stdout of the first process connect to stdout of the second, and so on. Then in the child process you don't continue until you get some input from the previous process to continue.

或者您可以使用管道将所有进程链接在一起,使第一个进程的stdout连接到第二个进程的stdout,依此类推。然后在子进程中,您不会继续,直到您从上一个进程获得一些输入才能继续。

Or use something like named semaphores to signal each other.

或者使用命名信号量之类的信号来互相发出信号。