how to force a function to never return...like exec() does it

This is a discussion on how to force a function to never return...like exec() does it within the C Programming forums, part of the General Programming Boards category; how does exec() do it? on successful call of exec() family of functions, they never return...
how to i emulate ...

please help me grasp the internals of exec() call... and explain to me how it works and tell me if there is an alternative to make a function-call never return...

The fact that exec() never returns is as inevitable as death... If you replace the currently executing program with something else, obviously the old program no longer exists. Something can't return if it doesn't exist.

but why in execl*() a set of arguments is preferred to be passed as in

Code:

execl ("/bin/ls", "ls", "-l", (char *)0);

when it could have been implemented as

Code:

execl ("/bin/ls", "ls -l", (char *)0);

instead

There are a few reasons. One, exec() is a system call, which means the kernel does it. The single string will have to be broken back into individual arguments to pass in the argv[] array of the executed process anyway. It would be pointless to force the kernel to do that.

Two, if you are dynamically building a command line, you would have to use sprintf() to pack the arguments into a single string -- again, why bother when you already have them separately.

Three, what happens if you have a program that has a space in its name? There would be no way to distinguish "space as argument separator" from "space just because there happened to be a space in the name."

thanks but...where can i find the defination of the exec() family of funtions...

unistd.h has the following...and they seem to be more like declarations than definations...

exec*() are System Calls. That means they are built into the Kernel.
If you want to see how they work, download the Linux source code and take a look.
Although there's no way to do the same thing as exec*() without going through the Kernel, since the OS won't let you do it.