Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to the child process, when the parent process calls an exec command

Tags:

c

linux

unix

Suppose we have a parent process and if it calls an exec function, after calling a fork to create child process.

Now what happens to the child process: will it act like the original parent process, such that the user won't spot the difference that the parent process got replaced with some other binary ?

I think this question differs from the following question what happens to child process?.

if ( (pid == fork ()) != 0 )
{
    if (strcmp(cmd,"mypwd")==0)
    {
        execlp (“mypwd”,0);
    }

    ...
    ...

    else if (strcmp(cmd,"myexit")==0)
        exit(1);
}
like image 571
Amrith Krishna Avatar asked Jul 29 '15 07:07

Amrith Krishna


People also ask

What happens when exec is called in a child process?

The Execve() Function It reads a line of text from stdin, parses it, and calls fork() to create a new process. The child then calls execve() to load a file and execute the command just entered. execve() overwrites the calling process's code, data, and SSs.

Does exec creates a child process?

exec does not create a new process; it just changes the program file that an existing process is running.

Does a call to exec ever return?

There is no return from a successful exec, because the calling process image is overlaid by the new process image.

Does the parent process execute before the child?

The original process is called the parent process and the second process is called the child process. The child process is an almost exact copy of the parent process. Both processes continue executing from the point where the fork( ) calls returns execution to the main program.


1 Answers

The pid of the parent process will remain the same after the exec, so the process hierarchy won't be affected.

The problem with this approach is that the newly replaced parent process generally won't be aware that it has previously spawned a child and will not call wait or waitpid on it. This will cause the child process to become a zombie when it exits.

like image 103
Blagovest Buyukliev Avatar answered Oct 05 '22 23:10

Blagovest Buyukliev