user567879
user567879

Reputation: 5339

Fork and Execlp

I trying a program with fork and execlp where parent address space is replaced with "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 execute the program the last line of child

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

is not printed. Why?

Upvotes: 7

Views: 34433

Answers (5)

user2670535
user2670535

Reputation: 139

  1. You are taking process id as int type but actually , to store process id you should use pid_t
  2. When you use exec family function the entire address space of called process replaces the calling process. So,now the last printf statement is not there in the new process , actually even the process id of the process is also not changed

Upvotes: -3

Prashanth Reddy D
Prashanth Reddy D

Reputation: 21

The reason is simple : The exec() functions only return if an error has have occurred. For the same refer man pages of exec() functions.

What exactly is happening when exec() functions are called :

execl() does not create a new process - it modifies the VADS and associated contents - in addition, execution context is also modified.

  • old execution context is no longer used - a new execution context is created.
    • a new, fresh context is created for the newly loaded application and control is passed to the scheduler- scheduler resumes the same child process with the newly available execution context - using this, a jump is executed to the entry point of the new application, in user-space - the new application starts executing in the same child process.
      • system stack is overwritten with new hw context for resuming the main() of the new program in user-space.
    • execution context and code/data/heap/stack of old application in the child process are completely destroyed - no longer available.
    • only time execve() or execl() will return to the same application/code of the current process is when execve() or execl() fails to load a new application in the current process - meaning, the only time execv()/execvl() or family of calls will return is when there is error in completing execv()/execl()/family of calls.

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

Upvotes: 2

Subbu
Subbu

Reputation: 2101

after the function execlp() does not get executed as per the documentation of execlp hence your printf() statement "Still I'm the child" does not get executed ...!!

Upvotes: 0

rahmu
rahmu

Reputation: 5878

exec functions will not merely execute your command. They will actually replace the execution context of the process by your selected executable (in your case /bin/ls).

In other words, since the ls function ends by terminating its process (thorugh 'exit' or returning the main function or whatever), your child process will be killed at the end of the execution of ls.

You can actually use this printf call to print some errors, for instance:

 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. 
    }

Upvotes: 4

hari
hari

Reputation: 9733

exec family functions do not return when successful.

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

The exec family of functions shall replace the current process image with a new process image. The new image shall be constructed from a regular, executable file called the new process image file. There shall be no return from a successful exec, because the calling process image is overlaid by the new process image.

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

Upvotes: 16

Related Questions