Linux 通过 fork() 创建子进程 shell 脚本中的父进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17290872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:18:11  来源:igfitidea点击:

create a child process by fork() a parent process in shell script

linuxbashshell

提问by Sam

It seems some answers on the site but those seem not to resolve my question.

网站上似乎有一些答案,但那些似乎无法解决我的问题。

Say,

说,

#/bin/sh
fpfunction(){
n=1
while (($n<20))
do

        echo "Hello World-- $n times"
        sleep 2
        echo "Hello World2-- $n times"
        n=$(( n+1 ))
done
}

fork(){
    count=0
    while (($count<=10))
    do
      fpfunction &
      count=$(( count+1 ))
    done
}

fork


I am expecting to "fork" this parent process to the number of 10 child processes.

我期待将这个父进程“分叉”到 10 个子进程的数量。

The way I check that those child processes are actually created is to type "ps -l" command.

我检查这些子进程是否实际创建的方法是键入“ps -l”命令。

I am a newbie to the shell world and please let me know how to archive this.

我是 shell 世界的新手,请让我知道如何存档。

Thanks in advance.

提前致谢。

采纳答案by Ignacio Vazquez-Abrams

The ampersand (&) after a command will run it in a forked subshell.

&命令后面的与号( ) 将在分叉的子 shell 中运行它。

fpfunction &