The answer to your question depends on whether you want to execute additional statements in your parent program after executing the second (or child) program.
If you want to execute additional statements in your parent program after executing your second (or child) program, I would use system()
.
If you do NOT need to execute additional statements from the parent program after executing your second (or child) program, I would just use execv()
. A second process will NOT be created. The parent program will terminate and the second program will run with the same process ID as the parent program.
P.S. In this scenario, you could also use system()
. But if you use system()
, two processes with different process IDs will be created - one process ID for the main program and a second process ID for the second program.
In your example, you can use either the system()
function or the execv()
function.
Here is a simple self-explanatory example using system()
:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello from the parent process\n");
if ( system("echo 'Hello from the child process' > test.txt") != 0 )
{
printf("ERROR executing system() command\n");
}
if ( system("cat test.txt") != 0 )
{
printf("ERROR executing system() command\n");
}
printf("Executing from the parent process again!\n");
}
Here is the output:
Grinchs-MBP:Downloads rob$ ./parent3
Hello from the parent process
Hello from the child process
Executing from the parent process again!
Remember to include the stdlib.h
header file, if you intend to use system()
.
If you do NOT need to execute additional statements in your parent program after the child or second program has finished, I would just use execv()
in the parent program.
In this case, you do not need to create a fork using fork()
. You just need to use execv()
. If you want to use execv()
, remember to include the unistd.h
header file.
When execv()
is called, the process from which it was called is terminated and replaced by the new process in the allocated portion of memory and with the same ID.
If the execv()
executes successfully, it does not return. If it returns, it is because an error occurs. In other words, execv()
should be the last statement you intend to execute from your parent program since execv()
will only return to your parent program in case of an error.
Again -- if you want to execute another program from your parent program and want to execute additional statements in the parent program after the second (or child) program has finished, then I would use system()
instead.
Here is a simple example using execv()
:
Parent Process Code
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main ()
{
printf ("I am the parent process\n\n");
char *arg_Ptr[4];
arg_Ptr[0] = "child.c";
arg_Ptr[1] = "Hello from the child process";
arg_Ptr[2] = "Good-Bye from the child process!";
arg_Ptr[3] = NULL;
execv("/Users/rob/Downloads/child.bin", arg_Ptr);
printf ( "Error: %i\n", errno);
}
Child Process Code (Saved at /Users/rob/Downloads/child.bin).
#include <stdio.h>
int main(int argc, char *argv[])
{
printf ("I am the child process\n");
printf ("Argument 1: %s\n", argv[1]);
printf ("Argument 2: %s\n", argv[2]);
printf ("Argument 3: %s\n", argv[3]);
}
Here is the Output
Grinchs-MBP:Downloads rob$ ./parent
I am the parent process
I am the child process
Argument 1: Hello from the child process
Argument 2: Good-Bye from the child process!
Argument 3: (null)