如何启动多个子进程,它会自动接收信号发送给家长 [英] How to launch multiple child processes that will automatically receive signals sent to parent

查看:206
本文介绍了如何启动多个子进程,它会自动接收信号发送给家长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Bash脚本来启动并行的子进程。有没有办法做到这一点,使孩子的脚本仍然收到发送到父进程?信号

I want to create a Bash script to launch parallel child processes. Is there a way to do this so that the child scripts still receive signals that are sent to the parent process?

下面是大致我多么想推出的子进程,但是这不符合我的标准的信令

Here's roughly how I want to launch child processes, but this doesn't meet my signaling criteria.

for (( i=0; i<9; i++ ))
   {
   { echo $i start ; sleep 5s ; echo $i complete ; } &
   }
wait

由于这在C程序自动工作(使用叉/ EXEC ),我认为它应该是可能的,而无需使用的陷阱基于信号转发 - 该信号被转发之前本身可以打断

Because this works automatically in a C-program (that uses fork/exec), I believe that it should be possible without the use of trap-based signal forwarding -- which itself could be interrupted before the signals are forwarded.

这个问题的一个解决方法是使用 GNU平行。我不知道它的机制,但它解决了这个问题 - 只要你愿意调整你的循环到的xargs 风格的语法。信号灯选项用于 - GNU平行并的的如解决问题。

One workaround for this is to use GNU-parallel. I don't know what it's mechanism is, but it solves this problem -- as long as you are willing to restructure your loops into xargs style syntax. GNU-parallel does not solve the problem if the --semaphore option is used.

我想答案就在这里,但我不知道怎么翻译它来砸:信号发送到两个小孩和父母过程

I think the answer is here, but I don't know how to translate it to Bash: Signal sent to both child and parent process.

推荐答案

这听起来就好像你很细使用GNU并行 - 只是不xargs的风格

It sounds as if you are fine with using GNU Parallel - just not the xargs style.

这将是确定使用的功能?

Will it be OK to use functions?

doit() {
    # Trap 2 signals to show they are being given to the function
    trap 'echo you hit Ctrl-C/Ctrl-\, now exiting..; exit' SIGINT SIGQUIT
    echo $1 start
    sleep 5s
    echo $1 complete
}
export -f doit
seq 10 | parallel -u doit

或避免管

parallel -u doit ::: {1..10}

这篇关于如何启动多个子进程,它会自动接收信号发送给家长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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