Running multiple C programs from a C program under Linux

懵懂的女人 提交于 2021-02-07 19:22:09

问题


I'm trying to learn a bit or 2 about process communication under Linux, so I wrote 2 simple C programs that communicate with each other.

However, it's a bit annoying to have to run them manually every single time, so I'd like to know is there a way to make a program that will run them both, something like this:

./runner program1 program2

I'm using latest Ubuntu and Bash shell.


回答1:


run.sh script

#!/bin/sh
./program1 & 
./program2 &

run command:

$sh run.sh



回答2:


This line will do (in Bash):

program1 & program2 &

If you want to record the output:

program1 >output1.txt & program2 >output.txt &

If you want to run the commands in two separate terminals:

xterm -e program1 & xterm -e program2 &



回答3:


Why not use this:

./program1;./program2

or

./program1 &;./program2 &

I don't know why somebody thinks it's not useful,but it really works.

Surely you can write a script,but what's the content of the script?Still the same thing.

And you can change it at once with no need to open the script first.




回答4:


Just write a shell script to do what you want -- you don't need to use a C program to run a C program.




回答5:


Do do exactly what you asked, first created a file called runner which will be the shell script.

#!/bin/bash

for arg in $@
do
$arg & 
done

$@ in bash is an array of all the arguments passed to the script, this makes the script no restricted to only launching 2 programs. Note any programs your launch with this scripts need to be on the $PATH or passed to the script as ./program1.

./runner ./program1 program2 

In the example program1 is not on the $PATH but program2 is.



来源:https://stackoverflow.com/questions/9521114/running-multiple-c-programs-from-a-c-program-under-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!