Link to home
Start Free TrialLog in
Avatar of pankajtiwary
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.
ASKER CERTIFIED SOLUTION
Avatar of grue
grue

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grue
grue

Err, instead of just directly looking at the content of waitpid's status (as I did in my example), there are some macros you should be using...  Something like:

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.
Avatar of pankajtiwary

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.