views:

110

answers:

3

I want to return a unique status code to a waiting parent process from a child process through exit(), based on the execution of child's code. If execvp fails, then the exit() is used. I assume that if execvp is successful, the command executed will send its status code.

pid=fork();
if(pid==0)
{
    if(execvp(cmdName,cmdArgs)==-1)
    {
         printf("Exec failed!\n");
         exit(K);   //K?
    }
}
waitpid(pid,&status,0);

Suppose the command passed to execvp() is "ls", the man page says that it may return 0(success), 1 or 2(failure).

What safe unique value K can I use to indicate the return status of a child process, which won't clash with any value returned by the command executed by execvp()?

+7  A: 

For obvious reasons, there cannot be such a value of K that will never clash with the return status of any other program.

Proof: Suppose there was such a K, and you make your program call itself...

ammoQ
You can safely assume the program won't call itself, atleast in the context in which I am using this code.
Kedar Soparkar
Sure you won't, but the consideration shows that there can be no safe value. It depends on the program you are calling which values are safe.
ammoQ
+1  A: 

There is no safe unique value as every program chooses its return values of which there are only a limited number.

You have to document your program and say what it returns and also provide some form of log to give more details.

Mark
A: 

I believe anything above 127 (or negative, if you use signed byte) is reserved for OS (on Unix) for reporting segfaults, parity errors and such (any exit due to signal handler and some other things besides). All other exit codes you can use.

Update: found a link for Linux: http://www.tldp.org/LDP/abs/html/exitcodes.html

Arkadiy
@Arkadiy, are all negatives reserved for OS?
Kedar Soparkar