简体   繁体   中英

executing c program using system call

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 without 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 &");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM