How to fork a Linux process in a Stopped state?

拈花ヽ惹草 提交于 2021-01-28 14:41:37

问题


I am starting a new task using a clone(2) call.

There used to be CLONE_STOPPED flag, but it is no longer present in current kernel.

Is there any trick to start a task in a Stopped state (waiting for SIGCONT to actually run)?


回答1:


You can't, there's no way to do that in recent kernels, not unless you write a kernel module to do that.

You can see how kernel v2.6.32 used to do it in kernel/fork.c (L1449):

if (unlikely(clone_flags & CLONE_STOPPED)) {
    /*
     * We'll start up with an immediate SIGSTOP.
     */
     sigaddset(&p->pending.signal, SIGSTOP);
     set_tsk_thread_flag(p, TIF_SIGPENDING);
    __set_task_state(p, TASK_STOPPED);
} else {
    wake_up_new_task(p, clone_flags);
}

It should be possible (but arguably not trivial?) to write a wrapper function in kernel space to do something similar.




回答2:


The child can send itself SIGSTOP at the beginning of the executed fn



来源:https://stackoverflow.com/questions/32959016/how-to-fork-a-linux-process-in-a-stopped-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!