Reputation: 83
I am getting a -1 return value, even after running a command successfully via system() call. Please see the following C code for more information.
system("ping -w 3 -c 1 -q -I 63.254.203.24 63.254.203.26 >/dev/null");
and then I am checking the return value of this system() call, even though it is pingable but I found -1 as a return value.
Upvotes: 0
Views: 992
Reputation: 1667
to see the actual return value use WEXITSTATUS(n)
and the reason why system
doesn't return the actual value is because (and this is only valid for linux) it is implemented using fork
, exec
and wait
the later returns the status of the process which contains whether the process ended normally or because of a signal plus the actual return value , and to access those different values you'll have to use macro's such as WEXITSTATUS(n)
these macros are mentioned in man 3 wait
The section RETURN VALUE of man system
:
The value returned is -1 on error (e.g., fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be
WEXITSTATUS(status)
. In case /bin/sh could not be executed, the exit status will be that of a command that doesexit(127)
.
Upvotes: 7
Reputation: 83
Fortunately, I got the answer. it was fork failed due to memory failure. It is not a good idea to use system() call.
Thanks for the support and answer.
Upvotes: 1