help me understand exec() family of calls...

how does exec() do it? on successful call of exec() family of functions, they never return...

how to i emulate that.

assume the standard example,

execl("/bin/ls","ls",NULL);

now this would never return.

i m trying to emulate exec()'s never to return feature...

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

void myexec(const char* path,const char* argv)
{
    chdir(path);
    system(argv);
    exit(100);   
}


int main(int argc,char *argv[])
{
    if(argc!=3)
    {
        printf("USAGE:%s <path> <command>\n",argv[0]);
        exit(1);
    }   
    if( fork() == 0)
    {
        myexec(argv[1],argv[2]);
    } 
    else
    { 
          exit(0);      
     }
    printf("this should never be executed");    
}

i m not trying anything fancy...only to emulate the "no return" feature once a command is executed.

so when i run this program
(trial 1)

[c_d@localhost cfiles]$ gcc 1.c
[c_d@localhost cfiles]$ ./a.out /bin/ls ls
1.c      a.out         fcopy.c~  frevread.c~    msg~     prgm1.c~  prgm2.c~  prgm3.c~  prgm4.c~  prgm5.c~  prgm7.c     sth~
1.c~      cfiles.tar.7z  file1~    key.c    prgm1     prgm2       prgm3     prgm4     prgm5     prgm6.c   prgm7.c~
alpha.c~  f1~         fread.c~  key.c~    prgm1.c  prgm2.c   prgm3.c   prgm4.c   prgm5.c     prgm6.c~  printip.c~
[c_d@localhost cfiles]$ 

(trial 2)

[c_d@localhost cfiles]$ ./a.out /home/c_d/workspace/unix/shellscripts/ ./leapyear_prgm07.sh
2009 is not a leap year
[c_d@localhost cfiles]$ 

i get what i want...the line "this should never be executed" never gets printed

please help me grasp the internals of exec() call... and explain to me how it works

also please explain to me, why in execl*() a set of arguments is preferred to be passed as in

execl ("/bin/ls", "ls", "-l", (char *)0);

when it could have been implemented as

execl ("/bin/ls", "ls -l", (char *)0);

instead
i have same complaint with execv*() style exec()...

thank you.

By calling exit within a function as you have done and there is no need for sleeping. My take on why execl ("/bin/ls", "ls -l", (char *)0) is not implemented that way is maybe it is best to separate the command from its options and arguments to make it more flexible.

thanks but...where can i find the defination of the exec() family of funtions...

unistd.h has the following...and they seem to be more like declarations than definations...

/* Replace the current process, executing PATH with arguments ARGV and
   environment ENVP.  ARGV and ENVP are terminated by NULL pointers.  */
extern int execve (__const char *__path, char *__const __argv[],
		   char *__const __envp[]) __THROW __nonnull ((1));

#ifdef __USE_GNU
/* Execute the file FD refers to, overlaying the running program image.
   ARGV and ENVP are passed to the new program, as for `execve'.  */
extern int fexecve (int __fd, char *__const __argv[], char *__const __envp[])
     __THROW;
#endif


/* Execute PATH with arguments ARGV and environment from `environ'.  */
extern int execv (__const char *__path, char *__const __argv[])
     __THROW __nonnull ((1));

/* Execute PATH with all arguments after PATH until a NULL pointer,
   and the argument after that for environment.  */
extern int execle (__const char *__path, __const char *__arg, ...)
     __THROW __nonnull ((1));

/* Execute PATH with all arguments after PATH until
   a NULL pointer and environment from `environ'.  */
extern int execl (__const char *__path, __const char *__arg, ...)
     __THROW __nonnull ((1));

/* Execute FILE, searching in the `PATH' environment variable if it contains
   no slashes, with arguments ARGV and environment from `environ'.  */
extern int execvp (__const char *__file, char *__const __argv[])
     __THROW __nonnull ((1));

/* Execute FILE, searching in the `PATH' environment variable if
   it contains no slashes, with all arguments after FILE until a
   NULL pointer and environment from `environ'.  */
extern int execlp (__const char *__file, __const char *__arg, ...)
     __THROW __nonnull ((1));

thanks...

The arguments absolutely must be separated because each argument will be a separate element in the argv array. Things like shells and the system() subroutine accept arguments separated by spaces, but they must isolate each argument prior to calling exec().

exec() tells the kernel to replace the current existing program with a new program. The kernel roughly:

  1. saves a copy of the new argv array and a copy of the environment
  2. frees any private memory area
  3. disconnects the process from any shared memory area including a shared text segment and any shared libraries.

Note: At this point, any code following the exec call no longer exists in the process. The same is true of any code prior to the exec call and even the exec call itself. It's all gone.

  1. reads in the exec'ed program and reconstructs a working program. The environment and the arguments are copied into the new program.

  2. transfers control to the newly created program.

Note that this all happens in one process. The process id does not change. The program that the process is running changes. After an exec() a process might be smaller (use less memory) than it did prior to the exec().

You can't emulate stuff like this in userland. Only the kernel can do this sort of thing.

thanks a lot...:slight_smile: