开发者

How to send SIGSTOP to all processes that launched from a shell script

I have a shell script that launches 4 other binaries. I am sending SIGSTOP to the shell script. Does this stop all other 4 processes also? If not, what should I do to forward the SIGSTOP to these processes? Simil开发者_如何学Pythonar is the case with SIGCONT.

I have the C source code for all the 4 binaries.


You can call setpgid() in the forked child process that will execute the shell script. That will give any spawned processes from that shell script the same group ID as the child process. You can then use killpg() to send a signal to the entire group that all processes in that group will receive.

For instance, if inside the child process you called setpgid(0, 0), this would setup a special instance where the child-process' group ID will be set to the same value as the child's PID. Then any processes overlaid on the child process using one of the exec family of functions will have the same group-ID value that the child had. In addition, any processes that the newly overlaid process may spawn will also have the same group ID (i.e., your shell-script). You can then, using killpg(), send a signal to any processes sharing a group ID value using just the child's PID value that fork() returned since the group ID of the child process is the same value as the child's PID after the setpgid(0, 0) call.

If you are using fork(), depending on how quickly you need to send signals to the group from the parent process may create some synchronization issues ... for example, you want to immediately send a signal to the process group right after forking the child process. There are two work-arounds for this: Either 1) use vfork() instead of fork, so that the parent is suspended until the child has changed it's group-ID and successfully called exec, or 2) call setpgid() in the parent process as well as in the child-process, but in the parent, rather than using setgpid(0, 0) like you would in the child, you can use setpgid(CHILD_PID, CHILD_PID). Then it won't matter which call was successful (one of them will be successful, and the other will fail with a EACCES), and any successive signals sent from the parent wil now go to a valid group ID.


If your processes form a group, you can use standard kill(1). man kill has the following info:

pid... 
    Specify the list of processes that kill should signal. Each pid can be one of five things: 
n 
    where n is larger than 0. The process with pid n will be signaled. 
    All processes in the current process group are signaled.
-1 
    All processes with pid larger than 1 will be signaled. 
-n 
     where n is larger than 1. All processes in process group n are signaled. When an argument of the form '-n' is given, and it is meant to denote a process group, either the signal must be specified first, or the argument must be preceded by a '--' option, otherwise it will be taken as the signal to send. 
commandname

It seems to me that the '-n' specification might help you

kill -STOP -- "-$(pgrep myparentproc)"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜