Fork and Execlp

I am trying a program with fork and execlp where the parent address space is replaced with the ls command.

#include<stdio.h> main() { int pid,j=10,fd; pid=fork(); if(pid==0) { printf("\nI am the child\n"); execlp("/bin/ls","ls",NULL); printf("\nStill I am the child\n"); } else if (pid > 0) { printf("\n I am the parent\n"); wait(); } } 

When I run the program, the last line of the child

 printf("\nStill I am the child\n"); 

not printed. Why?

+6
source share
5 answers

exec Family functions are not returned on success.

http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html

The exec family of functions should replace the current process image with a new process example. A new image must be created from a regular executable file called a new process image file. There should be no return from a successful exec, because the image of the calling process is superimposed with a new sample process.

If one of the exec functions returns to the image of the calling process, an error has occurred; the return value should be -1, and errno should be set to indicate an error.

+16
source

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); //make sure you kill your process, it won't disappear by itself. } 
+4
source

The reason is simple: exec () functions are only returned if an error occurs. For the same man pages for exec () functions.

What exactly happens when exec () functions are called:

execl () does not create a new process - it modifies the VADS and its related contents - in addition, the execution context also changes.

  • the old execution context is no longer used - a new execution context is created.
    • a new fresh context is created for the recently loaded application and the control is transferred to the scheduler, the same child resumes the process with the new execution context available - using this, the jump is made to the entry point of the new application, in user space - the new application starts execution in the same child process.
      • the system stack is overwritten with a new hw context to resume the main () new program in user space.
    • execution context and code / data / heap / stack of the old application in the child process is completely destroyed - is no longer available.
    • only the execve () or execl () time will return to the same application / code of the current process when execve () or execl () does not load a new application in the current process - that is, the only time execv () / execvl () or the call family will return when there is an error in terminating the execv () / execl () / call family.

Note: you must check the return value of the exec () system call APIs for errors / error codes - based on the error / error codes, you can terminate the current process or take some other action.

+1
source

after the execlp () function is not executed according to the execlp documentation so your printf () operator "Still I am a child" is not executed ... !!

0
source
  • You accept the process id as int , but in fact, to keep the process id you should use pid_t
  • When you use the exec family function, the entire address space of the called process replaces the calling process. So, now the last printf statement does not exist in the new process, in fact, even the process ID of the process also does not change.
-3
source

Source: https://habr.com/ru/post/895648/


All Articles