开发者

executing c program using system call

开发者 https://www.devze.com 2023-02-02 05:52 出处:网络
I want to write a program in C which can read another program and give it to processor for execution.is it possible to use system call to do it w开发者_JAVA技巧ithout using shell(linux)

I want to write a program in C which can read another program and give it to processor for execution.is it possible to use system call to do it w开发者_JAVA技巧ithout using shell(linux)

Thanks


The system call you're looking for is execve. It has related functions execl, execlp, execle, execv, execvp, depending on how you want to store and pass the command line arguments and/or environment variables. But the catch is, all of those functions will stop the program that called it from doing anything else, since the new program replaces the existing one.

If you want to run a program, wait for it to finish, and then resume the original program, try the library function system instead. (Though system does require /bin/sh.)

If you need something fancier, you'll probably have to combine fork and waitpid with one of the exec* functions. There are plenty of examples in man pages and around the Web about how to combine those functions.


if you want a simple way of doing this use :

 system("./Program_To_Run.o");

However this is not the best way. It is just the fastest way.

If you use the ampersand you can the process to the background:

system("./Program_To_Run.o &");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号