分叉流程的执行顺序 [英] Forked processes order of execution

查看:97
本文介绍了分叉流程的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道还有一个同名线程,但这实际上是一个不同的问题.

I know there's another thread with the same name, but this is actually a different question.

当一个进程分叉多次时,父进程在子进程之前完成执行吗?反之亦然?同时?

When a process forks multiple times, does the parent finish executing before the children? Vice versa? Concurrently?

这是一个例子.可以说我有一个for循环,它将1个父进程派生到4个子进程中.在for循环的最后,我希望父进程通过管道将一些数据馈送到子进程.数据将写入每个子进程的各自标准输入.

Here's an example. Lets say I have a for loop that forks 1 parent process into 4 children. At the end of that for loop, I want the parent process to feed some data to the children via pipes. The data is written to each child process' respective stdin.

在任何子级执行代码之前,父级会先发送数据吗?这很重要,因为我们不希望它从无效的stdin开始工作.

Will the parent send the data first, before any of the children execute their code? This is important, because we don't want it to start working from an invalid stdin.

推荐答案

执行的顺序由特定的OS调度策略确定,而没有任何保证.为了使过程同步,有专门用于过程间通信(IPC)的特殊设施.提到的管道就是一个例子.它们使读取过程实际上等待等待其他过程来写入它,从而创建了(单向)同步点.其他示例是FIFO和套接字.对于更简单的任务, wait()函数家族或

The order of the execution is determined by the specific OS scheduling policy and not guaranteed by anything. In order to synchronize the processes there are special facilities for the inter-process communication (IPC) which are designed for this purpose. The mentioned pipes are one example. They make the reading process to actually wait for the other process to write it, creating a (one-way) synchronization point. The other examples would be FIFOs and sockets. For simpler tasks the wait() family of functions or signals can be used.

这篇关于分叉流程的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆