Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the return code of a spawned program

Tags:

c++

linux

process

This will run a program and give me the return code.

int returnCode;

returnCode = system(program);

How can I do the same thing with execv?

like image 586
node ninja Avatar asked Jul 15 '26 18:07

node ninja


1 Answers

The exec family of calls trash the current process and launch the new program within the same process space. If you want to execute another program within your current program, you need to spawn a new process using fork You then call exec in the child process and get the parent to wait until the child process completes. The waitpid function will then provide the child process's return code.

See here for an example.

like image 55
doron Avatar answered Jul 18 '26 08:07

doron