如何在一个shell脚本中开启多个应用程序?

本文介绍了一种在Bash中让多个程序并行执行的方法,通过使用fork和ampersand符号实现进程并发运行,避免了程序串行执行带来的延迟,并提供了一个简单的测试程序来演示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前在csdn上搜索,提示用gnome-terminal指令,但是发现怎么都不好使。于是找到一种解决方案。

#!/bin/bash
echo "the first one"
./hello fork &
sleep 10
echo "the second one"
./hello   

只要在可执行程序后添加"fork &"即可,测试两个程序同时执行。
测试程序

#include<iostream>
#include <unistd.h>
using namespace std;
int main(){
    cout<<"hello"<<endl;
    sleep(10);
}

结果:

the first one
the second one
hello
hello

fork 后不加 &,会等待第一个程序执行结束再执行第二个程序。
测试结果

the first one
hello
the second one
hello

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值