为什么在派生子进程时出现时序问题 [英] Why is there timing problem while to fork child processes

查看:57
本文介绍了为什么在派生子进程时出现时序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在

When I took a look at the reference of 'Launching-Jobs' in gnu.org, I didn't get this part.

shell还应该调用setpgid,以将其每个子进程放入新的进程组中.这是因为存在潜在的定时问题:每个子进程必须在开始执行新程序之前放入进程组中,并且外壳程序依赖于所有子进程.组中的子进程继续执行之前.如果子进程和外壳程序都调用setpgid,则可以确保无论先进入哪个进程,都可以进行正确的操作.

The shell should also call setpgid to put each of its child processes into the new process group. This is because there is a potential timing problem: each child process must be put in the process group before it begins executing a new program, and the shell depends on having all the child processes in the group before it continues executing. If both the child processes and the shell call setpgid, this ensures that the right things happen no matter which process gets to it first.

链接页面上有两种方法,launch_job ()launch_process (). 他们都呼叫setpgid以防止定时问题.

There is two method on the link page, launch_job () and launch_process (). They both call the setpgid in order to prevent the timing problem.

但是我不明白为什么会有这样的问题.

But I didn't get why is there such a problem.

我猜新程序表示launch_process()execvp (p->argv[0], p->argv);的结果.并且在运行execvp之前,总是执行setpgid (pid, pgid);,而launch_job ()上没有相同的功能.

I guess new program means result of execvp (p->argv[0], p->argv); in launch_process(). And before run execvp, setpgid (pid, pgid); is always executed, without same function on launch_job ().

那么,为什么会有这样的问题呢? (为什么还要在launch_job ()上调用setpgid ();?)

So again, why is there such a problem? (why we have to call setpgid (); on launch_job () either?)

推荐答案

问题是shell希望进程位于正确的进程组中.如果外壳程序未在其子进程上调用setpgid(),则在一段时间内该子进程不属于进程组,而外壳程序将继续执行. (通过调用setpgid(),shell可以保证子进程在该调用之后成为进程组的一部分.)

The problem is that the shell wants the process to be in the right process group. If the shell doesn't call setpgid() on its child process, there is a window of time during which the child process is not part of the process group, while the shell execution continues. (By calling setpgid() the shell can guarantee that the child process is part of the process group after that call).

还有另一个问题,子进程可能在正确设置了进程组ID之前(即,在父进程调用setpgid()之前)执行新程序(通过exec).这就是为什么子进程也应该调用setpgid()(在调用exec()之前).

There is another problem, which is that the child process may execute the new program (via exec) before its process group id has been properly set (i.e. before the parent calls setpgid()). That is why the child process should also call setpgid() (before calling exec()).

该描述确实很糟糕.这里不仅解决了一个问题,还解决了一个问题.这实际上是两个独立的问题.一个-父(即外壳)希望将子进程放入正确的进程组中.二-只有在将新程序的进程放入正确的进程组后,新程序才应开始执行.

The description is admittedly pretty bad. There isn't just one problem being solved here; it's really two separate problems. One - the parent (i.e. the shell) wants to have the child process in the right process group. Two - the new program should begin execution only once its process has already been put into the right process group.

这篇关于为什么在派生子进程时出现时序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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