Strange return codes
Posted: Fri Jan 16, 2009 1:06 pm
I'm using system to call a C++ executable. The executable calls the fork command. The child does some processing in a finite loop. The parent goes to sleep for a time. If the parent wakes up and the child is not done, the parent kills the child and sets the return code to -1. Otherwise the child finishes before the parent and kills the parent and sets the return code to zero.
When I run the C++ executable it does what I expect and finishes with a result of either -1 or 0. When I use the PHP system call, the value of the supplied return code is either 15 or 255. Why?
(PHP 5.2.0, Suse 10.2)
Here's the code:
---------------
C++
---------------
[c]#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <signal.h>using namespace std; int ChildProcess(char*, char*);void ParentProcess(int, char*); int res; int main(int argc, char** argv){ if (argc != 4) { cerr << endl; cerr << "usage: ./test <reps> <sleep-time> <child-wait-time>" << endl << endl; exit(-1); } res = 0; // assume success pid_t pid, parent_pid; parent_pid = getpid(); pid = fork(); if (pid == 0) { ChildProcess(argv[1], argv[2]); kill(parent_pid, SIGTERM); } else { ParentProcess(pid, argv[3]); res = -1; } printf(" *** res == %d ***\n", res); return res;} int ChildProcess(char* reps, char* sleeptime){ int i; for (i = 1; i <= atoi(reps); i++) { sleep(atoi(sleeptime)); printf(" This line is from child, value = %d\n", i); } printf(" *** Child process is done ***\n"); return 0;} void ParentProcess(int pid, char* timeout){ sleep(atoi(timeout)); kill(pid, SIGTERM);}[/c]
-------------
PHP
-------------
When I run the C++ executable it does what I expect and finishes with a result of either -1 or 0. When I use the PHP system call, the value of the supplied return code is either 15 or 255. Why?
(PHP 5.2.0, Suse 10.2)
Here's the code:
---------------
C++
---------------
[c]#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <signal.h>using namespace std; int ChildProcess(char*, char*);void ParentProcess(int, char*); int res; int main(int argc, char** argv){ if (argc != 4) { cerr << endl; cerr << "usage: ./test <reps> <sleep-time> <child-wait-time>" << endl << endl; exit(-1); } res = 0; // assume success pid_t pid, parent_pid; parent_pid = getpid(); pid = fork(); if (pid == 0) { ChildProcess(argv[1], argv[2]); kill(parent_pid, SIGTERM); } else { ParentProcess(pid, argv[3]); res = -1; } printf(" *** res == %d ***\n", res); return res;} int ChildProcess(char* reps, char* sleeptime){ int i; for (i = 1; i <= atoi(reps); i++) { sleep(atoi(sleeptime)); printf(" This line is from child, value = %d\n", i); } printf(" *** Child process is done ***\n"); return 0;} void ParentProcess(int pid, char* timeout){ sleep(atoi(timeout)); kill(pid, SIGTERM);}[/c]
-------------
PHP
-------------
Code: Select all
#!/usr/bin/php -q
<?php
$retval = 0;
system('/badger/cspans/test 5 5 30', $retval);
print("\n$retval\n");
?>