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


 
Thread Tools Search this Thread
Top Forums Programming help me understand exec() family of calls...
# 1  
Old 03-27-2009
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,

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

now this would never return.

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

Code:
#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)
Code:
[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)
Code:
[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
Code:
execl ("/bin/ls", "ls", "-l", (char *)0);

when it could have been implemented as
Code:
execl ("/bin/ls", "ls -l", (char *)0);

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

thank you.

Last edited by c_d; 03-27-2009 at 03:54 PM..
# 2  
Old 03-27-2009
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.
# 3  
Old 03-28-2009
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...

Code:
/* 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...

Last edited by c_d; 03-28-2009 at 07:48 AM..
# 4  
Old 03-28-2009
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.

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

5. 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.
# 5  
Old 03-29-2009
thanks a lot

thanks a lot...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Simple shell running with exec family

# Erroneous question, so can be removed. (0 Replies)
Discussion started by: beginnerboy
0 Replies

2. UNIX for Dummies Questions & Answers

Working of exec family of functions

Hi, I am studying exec family of functions.Its man page says,it replaces the current process image with a new process image. If it replaces the binary,then after returning back,how does it get the previous parameters of the process which called exec?As replacing process image means replacing... (5 Replies)
Discussion started by: Radha.Krishna
5 Replies

3. Programming

Working of exec family of functions

Hi, I am studying exec family of functions.Its man page says,it replaces the current process image with a new process image. If it replaces the binary,then after returning back,how does it get the previous parameters of the process which called exec?As replacing process image means replacing all... (1 Reply)
Discussion started by: Radha.Krishna
1 Replies

4. Shell Programming and Scripting

Not able to understand use exec command

Hi i am in learning phase of unix. i was going through exec in a unix book. below is the command exec n>file exec n>>file however when i used the exec command like below , where ex is the file name exec 2>>exand then do ls -lrt then again when i do the ls -lrt to see the size of the file... (3 Replies)
Discussion started by: scriptor
3 Replies

5. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

6. UNIX for Dummies Questions & Answers

Family tree illustrated

hello is there a family tree, or words that would illustrate the family tree of, Unix -> Linux As i would understand Unix, it is a OS. And Linux is a ?, is Linux a OS or a sub structure inside of the Unix OS ?. Have you ever seen one of those family tree`s where ma and pa are shown at... (3 Replies)
Discussion started by: cowLips
3 Replies

7. Shell Programming and Scripting

bash shell: 'exec', 'eval', 'source' - looking for help to understand

Hi, experts. Whould anybody clear explay me difference and usage of these 3 commands (particulary in bash) : exec eval source I've tryed to read the manual pages but did not get much. Also could not get something useful from Google search - just so much and so not exactly, that is... (3 Replies)
Discussion started by: alex_5161
3 Replies

8. OS X (Apple)

Address family not supported by protocol family

Hi, I compiled with no error a C program, than I tryed to execute it and than I get this error: connessione al server fallita: Address family not supported by protocol family What does it mean? Why I get this error only on Mac os x while on Ubuntu the program works? The code is:... (3 Replies)
Discussion started by: DNAx86
3 Replies

9. IP Networking

Identification of data calls & voice calls

Is there any facility to filter/identify the data calls and voice calls coming throug modem? OR Can we get the data or voice calls information through a script(preferably C Kermit)? (0 Replies)
Discussion started by: pcsaji
0 Replies
Login or Register to Ask a Question