exec functions will not just execute your command. They will actually replace the process execution context with your selected executable (in your case /bin/ls ).
In other words, since the ls function ends with the completion of its process ("exit" or return of the main function or something else), your child process will be killed at the end of ls .
In fact, you can use this printf call to print some errors, for example:
if(pid==0) { printf("\nI am the child\n"); execlp("/bin/ls","ls",NULL); printf("\nError: Could not execute function %s\n", "/bin/ls"); _exit(0);
source share