【问题标题】:Forcing the parent process to run first强制父进程先运行
【发布时间】:2017-03-05 03:50:31
【问题描述】:

默认情况下,在我的系统(Opensuse)上,子进程总是在 fork 之后首先执行。还有一些方法可以强制子进程先运行。我想知道有没有办法强制父进程先运行?

【问题讨论】:

  • 取决于操作系统的类型和上下文切换时间。你唯一能做的就是在子进程中写入 sleep(time) 以便它在父进程之后获得 cpu
  • 是的,这会发生,但即便如此,Child 必须运行一次才能执行 sleep/sigsuspend。
  • 您可以通过ptrace 安排在SIGSTOPed 状态下创建孩子。

标签: c linux-kernel operating-system fork


【解决方案1】:

你可以用这个方法

pid_t pid = fork();
if (pid == -1)
    abort();
else if (pid == 0) 
{
    raise(SIGSTOP); // stop the child
} 
else 
{
    waitpid(pid, NULL, WUNTRACED); // wait until the child is stopped
    kill(pid, SIGCONT); // resume the child
}

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 1970-01-01
    • 2018-01-02
    • 2019-05-19
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    相关资源
    最近更新 更多