-1

So I know that anything that comes after exec* functions will not get executed (if the exec* call is successful of course).

I want to understand why is this so ? So I developed this tiny little program

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

    printf("A program made to understand execvp\n");
    char *cmd[4] = {"ls", "-l","file",NULL};
    execvp(cmd[0],cmd);
    printf("This will not be printed!!\n");

    return 0;
}

What I do understand is that executable commands like those found in the bin directory are actually executable programs and so when basically call another program from our program.

And I did read somewhere that

if successful, the exec system calls do not return to the invoking program as the calling image is lost.

But what does this really means, and why do they not return to the invoking program ? How do they return then ?

Pro
  • 173
  • 4
  • 15
  • quote: `The exec family of functions **REPLACES** the current process image with a new process image`. In other words, working exactly as written. – Marc B Feb 09 '16 at 15:07
  • The first line in [man exec](http://linux.die.net/man/3/exec) explains this: *"The exec() family of functions replaces the current process image with a new process image."*. – Kenney Feb 09 '16 at 15:07
  • Because the `exec` family of function *replaces* your code with the one it loads. – Some programmer dude Feb 09 '16 at 15:07
  • return to what? The code that called `exec()` is **GONE**. it's not in memory anymore, so there's nothing to return TO. – Marc B Feb 09 '16 at 15:08
  • So was it you who asked the same question: http://stackoverflow.com/questions/32899582/why-the-exec-family-of-functions-doesnt-execute-the-code-after-exec last October? – Steve Summit Feb 09 '16 at 15:43
  • @SteveSummit Nope !! that's not me :D – Pro Feb 09 '16 at 16:13

1 Answers1

5

You said "basically call another program from our program", but that's not quite right.

If you want to call another program from within your program, you can use system().

But if you use the exec family, what they do is replace your program with another program. Clobber your program with another program. Overwrite your program with another program.

Once exec successfully overwrites your program with that other program, there's no trace of your program left to return to.

This is all because of the concept of a process. When you call system(), it creates a new process for that other program to run in, and when the other process exits, it can return to your original program in the original process.

But exec does not create a new process -- it reuses the original one.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • So how does my program terminates then, or is it in this case that when exec terminates , it just terminate my program too ? – Pro Feb 09 '16 at 15:13
  • @Pro: Imagine you are the computer, and your program is a book you're reading. Calling `exec` is like throwing your book in the fireplace, but immediately replacing it with a different book which you start reading from the beginning. – Steve Summit Feb 09 '16 at 15:24
  • Calling `exec` effectively terminates your program -- although the process that was holding your program is still there. The new program runs in the old process. When the new program exits, the process terminates. – Steve Summit Feb 09 '16 at 15:25
  • For those familiar with BASIC, `exec` is to `system` like `GOTO` is to `GOSUB`. – chepner Feb 09 '16 at 17:28
  • @SteveSummit But the file descriptor table is preserved right? – 2501 Feb 09 '16 at 18:05
  • 1
    @2501: Right, there's a bunch of miscellaneous process state that is preserved across `exec`: open files, ignored signals, uid/gid, 'nice' level, certain capabilities/privileges, etc. – Steve Summit Feb 09 '16 at 18:33