Joey
Joey

Reputation: 809

Why does execv exit a function?

Here is the unix v6 code: http://v6shell.org/history/if.c

doex(earg) {
...
execv(ncom, nargv, np);
return(1);
}

So if execv is successfully executed, we will not reach the next line and return 1. Instead execv will return something (0?) and exit the function doex. But why?

I would except you would have to write this:

if ( execv(ncom, nargv, np) ) return (0);
return (1);

unix v6 exec - man page: http://man.cat-v.org/unix-6th/2/exec

Upvotes: 1

Views: 1568

Answers (1)

Michael Homer
Michael Homer

Reputation: 1117

From the start of the exec man page you linked:

Exec overlays the calling process with the named file, then transfers to the beginning of the core image of the file. There can be no return from the file; the calling core image is lost.

Just like in today's exec functions, the execv call fully replaces the calling process with a new one. If execv failed for some reason, control will pass to the next line and the function will return 1. Otherwise, the exit code of the subprocess will be used as the exit code of this process, and no further code from this process will execute.

Upvotes: 4

Related Questions