之前在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

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

被折叠的 条评论
为什么被折叠?



