fork n进程和重新订购“ ls”每个孩子过程的命令
我有一个int数组,该数组的第一个元素是定义要分叉的过程数量。代表每个进程执行命令的优先级的其他下一个元素。例如,如果该数组的第二个元素为1,则意味着第一个叉进程应首先运行命令,并在完成具有“ 2”优先级的下一个工作过程后应运行命令。
for(int i=0;i<pr_array[0];i++)
{
if(fork() == 0)
{
pr_pid_array[i]=getpid();
execlp("/bin/ls","ls",NULL);
exit(0);
}
}
我不知道如何保持这种同步。我不想使用信号量。
I have an int array that first element of that array is defining number of process that I want to fork. Other next elements representing the priority of executing a command by each process. e.g. if second element of that array is 1, it means that the first fork process should first run the command and after finishing the work next process which has "2" priority should run the command.
for(int i=0;i<pr_array[0];i++)
{
if(fork() == 0)
{
pr_pid_array[i]=getpid();
execlp("/bin/ls","ls",NULL);
exit(0);
}
}
I don't know how to keep this synchronization. I don't want to use semaphore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,信号量似乎是最好的解决方案,无论如何你也可以使用其他同步方法。
您可以使用保存数组的共享内存,当第 i 个工作完成时,您将用一个失效值替换
sharedArray[i]
,这意味着工作已经完成。这样每个进程就知道第i个工作已经完成,第i+1个工作可以开始了。
如果您使用的是 Unix 系统,您可以使用 shm 文档
Semaphores seems to be the best solution in my opinion, anyway you can also use other synchronization methods.
You could use a shared memory that holds your array, when i-th work is completed you will replace
sharedArray[i]
with a defunct value that means the work has finished.In this way each process knows that i-th work is finished and i+1 can start.
If you are in a Unix system you can use shm documentation