I am currently working on a program that will launch another program multiple times though I have hit a road block. I am attempting to launch a new process within a loop by forking and then calling exec with the child process. This seems to work fine though as soon as I place exec within a loop it all fails. I was hoping someone would know why and suggest a possible solution to allow me to launch a new process within a loop multiple times.
Here is the code.
int main()
{
const char *PROG_DIR = "/home/me/Desktop/SandBox/Dummy";
const char *PROG_NAME = "Dummy";
char *arg[] = {(char*)PROG_NAME};
cout << "START" << endl;
int status;
for (unsigned int i = 0; i < 5; i++)
{
int pid = vfork();
if (pid == 0) // this is the child proccess
{
execv(PROG_DIR, arg);
// only reached if execv() fails
cerr << "FAIL" << endl; exit(EXIT_SUCCESS);
}
wait(&status); // forces the parent to wait until the child is finished
cout << status << endl;
}
if (status == 0)
{
cout << "END" << endl;
}
}
So I have narrowed the problem down to execv() not actually being executed though I do not know why.
This is the "Dummy" code.
int main(int argc, char* argv[])
{
// kill time
usleep(1000000);
cout << "RUN" << endl;
exit(0);
return 1;
}
Both of these pieces of code compile properly (I have the include statements needed).
The output for the program is :
START
FAIL
0
FAIL
0
FAIL
0
FAIL
0
FAIL
0
END
though I would expect and like it to be :
START
RUN
0
RUN
0
RUN
0
RUN
0
RUN
0
END
Which would indicate execv() is being called and run properly. I have checked the paths and names and everything is correct so upon several hours of beating my head off the wall I have narrowed the problem down to the execv() call.
Thank you for any and all help with this problem.


Sign In
Create Account


Back to top









