execlp -- Overlay Calling Process and Run New Program


 #include <unistd.h>

 int execlp(const char *path, const char *arg0, ..., NULL);


Like all of the exec functions, execlp replaces the calling process image with a new process image. This has the effect of running a new program with the process ID of the calling process. Note that a new process is not started; the new process image simply overlays the original process image. The execlp function is most commonly used to overlay a process image that has been created by a call to the fork function.
identifies the location of the new process image within the hierarchical file system (HFS). If the path argument contains a slash (/), it is assumed that either an absolute or a relative pathname has been specified. If the path argument does not contain a slash, the directories specified by the PATH environment variable are searched in an attempt to locate the file.
arg0, ..., NULL
is a variable length list of arguments that are passed to the new process image. Each argument is specified as a null-terminated string, and the list must end with a NULL pointer. The first argument, arg0, is required and must contain the name of the executable file for the new process image. If the new process image is a normal SAS/C main program, the list of arguments will be passed to argv as a pointer to an array of strings. The number of strings in the array is passed to the main() function as argc.

ARG_MAX specifies the maximum number of bytes, including the NULL terminator at the end of the string, that can be passed as arguments to the new process image. The value of ARG_MAX is obtained by calling the sysconf function with the _SC_ARG_MAX symbol.


A successful call to execlp does not have a return value because the new process image overlays the calling process image. However, a -1 is returned if the call to execlp is unsuccessful.


The following example illustrates creating a new process and executing an HFS file called newShell. The path /u/userid/bin is added at the end of the PATH environment variable before calling execlp.

Note: You must specify the posix option when compiling this example.

  #include <unistd.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>

     pid_t pid;
     char *pathvar;
     char newpath[1000];

     pathvar = getenv("PATH");
     strcpy(newpath, pathvar);
     strcat(newpath, ":u/userid/bin");
     setenv("PATH", newpath);

     if ((pid = fork()) == -1)
        perror("fork error");
     else if (pid == 0) {
        execlp("newShell", "newShell", NULL);
        printf("Return not expected. Must be an execlp error.n");



execl, execvp

Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.