So I'm trying to create a child using fork and have that child run a program using execlp, only problem is that it isnt running... kind of. Heres my simple program
else if (pid == 0)
{
fprintf(stderr,"Child process is accessing memory %d.\n", rand_num);
execlp("helloworld", "helloworld", (char*)0);
printf("hi\n");
exit(0);
}
I've done some research and read that if the execution is successful, the printf("hi\n)
wont run, and thats what happened, it wont print out hi so that means it is accessing the program right? But my program test is a simple Hello World output
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
and it isn't running, any ideas why?
I think that your program is actually working perfectly. And also, "test" is a terrible name for a command.
When you are calling execlp("test",...)
, the kernel looks for a program named test
along your PATH
environment variable (that's the the p
means in execlp
). It will find one in /bin
:
$ ls -l /bin/test
-rwxr-xr-x. 1 root root 37368 Oct 15 04:31 /bin/test
It is highly likely that /bin
is in your $PATH
before your current directory. In fact, it's likely that your current directory isn't even in your $PATH
.
The /bin/test
program is (well, mostly was) used to implement conditional statements in the shell. E.g., when you right:
if [ -f /my/file ]; then ...
The [
is actually just another name for /bin/test
.