PDA

View Full Version : Use system() in Solaris



y0gus
02-02-2009, 12:02 PM
Hello,

I am trying to get result status from system(my_exe) on Solaris. I want to detect if `my_exe` gives exception/core dump...So I created dummy exe which has zero division operation.

And then I run it with system().
The problem is that I can't detect the exception. My code is like:

int status = system("./my_exe");
printf("WIFEXITED=%d\n", WIFEXITED(status) );

the macro WIFEXITED always gives 1 !

On Linux it becomes 0, and I can WTERMSIG() then. But I cant achieve this on Solaris.
I tried different ways to kill/stop the executed process - WIFEXITED is always 1.

thanks for any help

Salem
02-02-2009, 12:16 PM
The return result of system() is implementation-defined for anything other than system(NULL).
Which in short means you can't use the result to tell you anything useful.

For example, what I suspect you're getting is the exit status of the shell, not the exit status of your process. Depending (or perhaps not) on the complexity of the string you pass, you may find that your "cmd" is translated into "/bin/sh -c cmd" instead. Result, you see what sh exited with.

If you want that much control over the child process, and to be able to reliably get the exit status, then you must use fork() and exec() to run the child process.

Codeplug
02-02-2009, 12:19 PM
The return value of system() is not the same as what wait() or waitpid() returns. And even if it were, you would be getting the status of the shell process that executed the command.

You'll have to fork(), exec() the command, and call waitpid() yourself if you want to use WIFEXITED and related macros.

gg

regurgitation of Salem's post

y0gus
02-02-2009, 02:52 PM
Thanks a lot guys. Very helpful answers!