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);
sleep(2); //make process orphan
exit(100); //exit to init
}
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
{
sleep(1); //suspend parent
exit(0); //be a bad parent ;)
}
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
but i m not happy...i m unnecessarily wasting away 2 secs for emulating this...
and i personally dont feel it as an elegant way to emulate exec()....
please help me grasp the internals of exec() call... and explain to me how it works and tell me if there is an alternative to make a function-call never return...
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
...and i have same complaint with execv*() style exec()...
thank you.