After I fork I appear to lose my variable the first time I try to execute a program. It will run the 2nd, 3rd,.... time but not the first. Below is my code.
This is part of the shell code I am writing, so everything is in a while loop. it appears to lose the execCommand variable. For instance with I run a ps command the first time and print execCommand after the for it comes out as a ?. Every other time though it comes out ok.
thank you.
Code:
childPid = fork();
if (childPid < 0)
{
perror ("fork");
return 0;
}
if (childPid == 0)
{
if (execlp (execCommand, execCommand,(char*)0) == -1)
perror("exec");
}
/* Have the parent wait for child to complete */
if (wait (&status) < 0)
perror ("wait");