pankajtiwary
asked on
exec() for Linux
Well I tried the following code:-
#include <stdio.h>
int main ( ) {
int i;
printf("Before calling execl()\n");
i = execl("/bin/sh","sh","shpg .sh",NULL) ;
if(i<0) {
perror("execl()");
exit(0);
}
printf("After calling execl()\n");
return 0;
}
My shpg.sh is:-
echo "Some Text"
I expect the output to be:-
Before calling execl()
Some Text
After calling execl()
But the output is:-
Before calling execl()
Some Text
Can somebody tell me why the code after the exec is not being executed? Thanks in advance.
#include <stdio.h>
int main ( ) {
int i;
printf("Before calling execl()\n");
i = execl("/bin/sh","sh","shpg
if(i<0) {
perror("execl()");
exit(0);
}
printf("After calling execl()\n");
return 0;
}
My shpg.sh is:-
echo "Some Text"
I expect the output to be:-
Before calling execl()
Some Text
After calling execl()
But the output is:-
Before calling execl()
Some Text
Can somebody tell me why the code after the exec is not being executed? Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanx Grue,
I appreciate your help in clarifying my confusion. Yeah, You got exactly what I wanted. I got your point. Actually I was thinking when completing the request execl returns and the rest of the code executes. But it's not like that. I case of success execl() never returns. Thanx again.
I appreciate your help in clarifying my confusion. Yeah, You got exactly what I wanted. I got your point. Actually I was thinking when completing the request execl returns and the rest of the code executes. But it's not like that. I case of success execl() never returns. Thanx again.
WEXITSTATUS(i);
but I think it happens to "work" in this case because you're just looking for a zero result and the program you run doesn't generate any signals or anything funky like that... but you really shouldn't be looking at the value directly; use the macro.
see the man page for waitpid for more details.