Page 1 of 1

force timeout for system call

Posted: Fri Jan 16, 2009 3:10 pm
by georgewr3
How can I call the PHP system call and force it to timeout if the native executable hangs indefinitely?

Re: force timeout for system call

Posted: Fri Jan 16, 2009 3:29 pm
by jaoudestudios
In the php.ini file you can set timeouts etc. But try not to code such things that go into infinite loops :P

Re: force timeout for system call

Posted: Fri Jan 16, 2009 4:09 pm
by georgewr3
Not my (native) code. How can I do it besides a global setting?

Re: force timeout for system call

Posted: Fri Jan 16, 2009 5:02 pm
by Burrito
I don't understand the OP's original question. But if it's something you can change in php.ini but don't want to change it globally, you can make a per instance change with ini_set()

Re: force timeout for system call

Posted: Fri Jan 16, 2009 5:20 pm
by Eran
use set_time_limit() to control the script max execution time. http://www.php.net/set_time_limit

Re: force timeout for system call

Posted: Mon Jan 19, 2009 8:38 am
by georgewr3
The problem is that set_time_limit doesn't include time spent in the system call and if the code called by system hangs, my php will hang, I believe. That native code is written by someone else and I want to recover from the hang after some time period which set_time_limit does not address.

I think my only solution is to write some sort of native wrapper like this where my php calls this executable and that executable calls the 3rd party code. The php code will definitely terminate and the return code will be zero for success or not zero for failure. It's just kind of involved and I was hoping to find a simpler solution.


void ChildProcess(void);
void ParentProcess(int, char*);
void SigHandle(int);

int main(int, char** argv) {
pid_t pid;
pid = fork();
if (pid == 0) {
ChildProcess();
kill(getppid(), SIGALRM);
} else {
signal(SIGALRM, SigHandle);
ParentProcess(pid, argv[1]);
exit(-1);
}
return 0;
}

void ChildProcess(void) {
Do3rdPartyStuff();
}

void ParentProcess(int pid, char* timeout) {
sleep(atoi(timeout));
kill(pid, SIGTERM);
}

void SigHandle(int sig_num) {
exit(0);
}