EmbLogic's Blog

Understanding Linux InterProcess Communication-III: Duplicate Process

The Exec family calls

To create a new process, which initially is a near duplicate of its parent process . Often, the new process immediately executes a new program. The act of creating a new process is called forking, and this functionality is provided by the fork( ) system call. Two acts—first a fork, to create a new process, and then an exec, to load a new image into that process are thus required to execute a new program image in a new process.

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.

 There is no single exec function; instead, there is a family of exec functions built on a single system call. List of exec() family system calls are:

  • int execl(const char *path, const char *arg, …);
  • int execlp(const char *file, const char *arg, …);
  • int execle(const char *path, const char *arg,…, char * const envp[]);
  • int execv(const char *path, char *const argv[]);
  • int execvp(const char *file, char *const argv[]);
  • int execvpe(const char *file, char *const argv[],char *const envp[]);

 When a C-language program is executed as a result of this call, it shall be entered as a C-language function call as follows:

 int main (int argc, char *argv[]);

where argc is the argument count and argv is an array of character pointers to the arguments themselves.

The argv array are each terminated by a null pointer. The null pointer terminating the argv array is not counted in argc. The arguments specified by a program with one of the exec functions shall be passed on to the new process image in the corresponding main() arguments. The argument path points to a pathname that identifies the new process image file.

The argument file is used to construct a pathname that identifies the new process image file. If the file argument contains a slash character, the file argument shall be used as the pathname for this file. Otherwise, the path prefix for this file is obtained by a search of the directories passed as the environment variable PATH

Signals set to the default action (SIG_DFL) in the calling process image shall be set to the default action in the new process image. Except for SIGCHLD, signals set to be ignored (SIG_IGN) by the calling process image shall be set to be ignored by the new process image. Signals set to be caught by the calling process image shall be set to the default action in the new process image

  • After a successful call to any of the exec functions, any functions previously registered by atexit() are no longer registered.
  • Any shared memory segments attached to the calling process image shall not be attached to the new process image
  • Any named semaphores open in the calling process shall be closed as if by appropriate calls to sem_close().
  • Any blocks of typed memory that were mapped in the calling process are unmapped,
  • Memory locks established by the calling process via calls to mlockall() or mlock() shall be removed
  • All open message queue descriptors in the calling process shall be closed, as described in mq_close().
  • The new process image shall inherit the CPU-time clock of the calling process image.

The new process shall inherit at least the following attributes from the calling process image:

  • Read and write descriptor created via pipes.
  • Nice value
  • Process ID
  • Parent process ID
  • Process group ID
  • Session membership
  • Real user ID
  • Real group ID
  • Supplementary group IDs
  • Time left until an alarm clock signal (see alarm())
  • Current working directory
  • Root directory
  • File mode creation mask
  • File size limit
  • Process signal mask
  • Pending signal
  • Resource limits
  • Controlling terminal
  • Interval timers

Upon successful completion, the exec functions shall mark for update the st_atime field of the file. If an exec function failed but was able to locate the process image file, whether the st_atime field is marked for update is unspecified.

Lets see a the some examples of  exec() family calls.

 Using execl()

The following example executes the ls command, specifying the pathname of the executable ( /bin/ls) and using arguments supplied directly to the command to produce single-column output.

#include <unistd.h>
int ret;
ret = execl ("/bin/ls", "ls", "-1", (char *)0);
Using execle()

The following example is similar to Using execl(). In addition, it specifies the environment for the new process image using the env argument.

#include <unistd.h>

int ret;
char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 };
ret = execle ("/bin/ls", "ls", "-l", (char *)0, env);
Using execlp()

The following example searches for the location of the ls command among the directories specified by the PATH environment variable.

#include <unistd.h>
int ret;
ret = execlp ("ls", "ls", "-l", (char *)0);
Using execv()

The following example passes arguments to the ls command in the cmd array.

#include <unistd.h>

int ret;
char *cmd[] = { "ls", "-l", (char *)0 };
ret = execv ("/bin/ls", cmd);
Using execve()

The following example passes arguments to the ls command in the cmd array, and specifies the environment for the new process image using the env argument.

#include <unistd.h>

int ret;
char *cmd[] = { "ls", "-l", (char *)0 };
char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 };
ret = execve ("/bin/ls", cmd, env);
Using execvp()

The following example searches for the location of the ls command among the directories specified by the PATH environment variable, and passes arguments to the ls command in the cmd array.

#include <unistd.h>
int ret;
char *cmd[] = { "ls", "-l", (char *)0 };
ret = execvp ("ls", cmd);

 Return Value:

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.(Refer man pages)

In our next article we will cover some more system calls like exit(),kill() and using fork and exec() family functions.

6 Responses to Understanding Linux InterProcess Communication-III: Duplicate Process

  1. samta says:

    Implemented:::
    #All the IPC classroom questions on processes..

    **Issues
    void main()
    {
    pid_t pid;
    pid=fork();
    pid=fork();
    if(pid==0)
    printf(“\nIn child process PID=%d and PPID=%d”,getpid(),getppid());
    else if(pid>0)
    printf(“\nIn parent process PID=%d and PPID=%d”,getpid(),getppid());
    else
    printf(“\nHere is an error!!!”);
    }

    The similar code which I saw in the lecture today did run properly and the shell didnt hang while in my code both the parent processes are terminating first and then the orphan processes are hanging the shell…

    • amritpreet singh says:

      happened with me too………added a loop before return statement to stop the prior termination of the parent process.
      if(pid)
      { for(i=0;i<20;i++)
      printf("i m the parent process");
      }
      but dont know the reason

  2. amritpreet singh says:

    studied and implemented
    #all assignment questions of first session(i.e. system(),fork(),wait())
    #all assignment questions of second session.(all exec commands )
    #performed passing file-descriptor from one process to another process & displayed its file contents.

  3. samta says:

    **Resolved the issue faced above in earlier assignment by assuming any process can function first child or parent

    Implemented
    #All the 4 exec functions
    #using the file descriptor frm 1 process into another replaced process
    #called fork() function in the child process and kept the parent process and the new replaced process printg their data

    **Issues faced
    1)All the printf commands above fork() or exec() were nt printg the data in the output
    Resolved this by putting \n at the end of every printf statement. as without \n at the end it was taking data in the stdout buffer but was nt printg it.

    2)execlp and execvp were nt executing
    as in it one has to put the name of the file twice and nt once

  4. samta says:

    Implemented
    #Todays assignment questions on signal system calls and sigaction
    #Obtained the signal info using signal structure

  5. amritpreet singh says:

    studied and implemented
    # all questions on signal handlers using signal().
    # signal handler using sigaction().
    # retrieval of the siginfo.

Leave a Reply to samta Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>