force timeout for system call

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
georgewr3
Forum Newbie
Posts: 9
Joined: Fri Jan 16, 2009 12:57 pm

force timeout for system call

Post by georgewr3 »

How can I call the PHP system call and force it to timeout if the native executable hangs indefinitely?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: force timeout for system call

Post 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
georgewr3
Forum Newbie
Posts: 9
Joined: Fri Jan 16, 2009 12:57 pm

Re: force timeout for system call

Post by georgewr3 »

Not my (native) code. How can I do it besides a global setting?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: force timeout for system call

Post 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()
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: force timeout for system call

Post by Eran »

use set_time_limit() to control the script max execution time. http://www.php.net/set_time_limit
georgewr3
Forum Newbie
Posts: 9
Joined: Fri Jan 16, 2009 12:57 pm

Re: force timeout for system call

Post 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);
}
Post Reply