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.
LVL 4
pankajtiwaryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grueCommented:
From execl man page:

       The exec family of functions replaces the current  process
       image  with  a new process image.  The functions described
       in this  manual  page  are  front-ends  for  the  function
       execve(2).   (See  the manual page for execve for detailed
       information about the replacement of the current process.)

When you call successfully call execl, the current process is REPLACED with the new process.  If successful, execl never returns.

You probably want something like this:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main (int argc, char **argv)
{
  int i;
  printf("Before calling execl()\n");

  pid_t child_pid;
  if (0 == (child_pid = fork())) // fork a new copy of this process
    {
      i = execl("/bin/sh","sh","shpg.sh", NULL); // replace the new copy with sh shpg.sh
    }
  else
    {
      waitpid(child_pid, &i, 0);  // the original copy waits for child to finish
    }

  if(i<0)
    {
    printf("execl() or child error");
    exit(0);
    }

  printf("After calling execl()\n");
  return 0;
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
grueCommented:
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.
0
pankajtiwaryAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.