Yes, I do have a function earlier which setenv to a PATH. So I should use execvp(busywait) instead and it would go to the PATH to search for the correct path.
I thought when using execvp, busywait program would take up the child's PID to run since it replaces everything that follows? Why would I need to fork out again and relink to that 'grandchild' PID?
I've tried to execvp in the current child, and do the readlink in the parent after the waitpid. busywait program ran, but it didn't run for the expected result of 1 seconds. Also, the readlink didn't work, saying that it couldn't get the directory.
Code:
int main(){
int pid, child_pid;
char buf[50];
char temp[50];
int len;
int execStatus;
int status;
char *parameter[10];
setenv("PATH", "home/QuietBoi/Desktop/TestSysCommand", 1);
pid = fork();
if(pid > 0) {
printf("Parent\n");
waitpid(-1, &status, 0);
sprintf(temp, "/proc/%d/exe", child_pid);
len = readlink(temp, buf, sizeof(buf) - 1);
if (len == -1) {
// The call failed.
if (errno == EINVAL) {
// It's not a symbolic link; report that.
fprintf (stderr, "%s is not a symbolic link\n", temp);
}
else {
// Some other problem occurred; print the generic message.
perror("readlink error");
return 1;
}
}
else {
buf[len] = '\0';
printf ("%s\n", buf);
}
printf("\nexit status: %d\n", status);
}
else if(pid == 0) {
printf("Child\n");
child_pid = getpid();
parameter[0] = "busywait";
parameter[1] = '\0';
execStatus = execvp("busywait", parameter);
if (execStatus < 0) {
perror("Error in executing the process");
}
printf("\nChild completed\n");
}
else {
perror("oops why did fork fail?");
exit(1);
}
return 0;
}
Result I get:
Code:
Child
Parent
busywait program finished in 0 secs
readlink error: No such file or directory
Maybe you roughly sketch out your idea, how the code could look like?
Thanks a lot. =)