?
Solved

exec() for Linux

Posted on 2003-02-25
3
Medium Priority
?
1,831 Views
Last Modified: 2013-11-18
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.
0
Comment
Question by:pankajtiwary
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
grue earned 200 total points
ID: 8015282
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
 
LVL 2

Expert Comment

by:grue
ID: 8015313
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
 
LVL 4

Author Comment

by:pankajtiwary
ID: 8024178
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question