fork和exec在bash [英] fork and exec in bash

查看:223
本文介绍了fork和exec在bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现fork和exec在bash?

How do I implement fork and exec in bash?

让我们假设脚本

echo "Script starts"

function_to_fork(){
sleep 5
echo "Hello"
}

echo "Script ends"

基本上我想该功能被称为C一样,我们使用fork和exec调用新的进程。

Basically I want that function to be called as new process like in C we use fork and exec calls..

从脚本预期母体脚本将结束,然后在5秒钟后,你好被打印。

From the script it is expected that the parent script will end and then after 5 seconds, "Hello" is printed.

推荐答案

使用的符号,就像你从shell会。

Use the ampersand just like you would from the shell.

#!/usr/bin/bash
function_to_fork() {
   ...
}

function_to_fork &
# ... execution continues in parent process ...

这篇关于fork和exec在bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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