The execlp()
or exec()
family of functions replaces the current process image with a new process image so when execlp()
exits then child terminates but fork()
returns some non-zero value to parent process.
The main point is after doing fork()
there are two independent processes
1st : main process called parent
2nd : child process
And you can't control their execution order it's unspecified. so put wait(NULL)
in you parent code just after the child code finishes.
So that parent will wait till child terminates. otherwise both independent processes will run in this way. Sometimes you find that only parent is running (that's infinite loop)
but sometimes you see that child is running too.