Re: [PHP] pcntl functions for task manager - comments?

2002-06-02 Thread Clay Loveless

After playing around with this further, here is more functional pseudo-code
... Primary change in the placement and method of the waidpid routine.

I'm still open to comments on this ... Otherwise, I hope this snippet is
useful to someone! : )

// run forever if necessary
set_time_limit(0);

// detatch from the controlling terminal
if (!posix_setsid()) {
die('could not detach from terminal');
}

// setup signal handlers
pcntl_signal(SIGTERM, sig_handler);
pcntl_signal(SIGHUP, sig_handler);

// loop forever waiting on jobs
while(1) {

// check queue for pending jobs
// (db lookup omitted, let's assume jobs are waiting)
$jobs_waiting = true;

if($jobs_waiting) {
$pid = pcntl_fork();
if($pid == -1) {
die('could not fork');
} else if ($pid) {
// parent
} else {
// child
// perform task on jobs waiting
// when job(s) complete, quit
exit();
}
}

// call waitpid to collect children that
// have already terminated
$tpid = pcntl_waitpid(-1,$status,WNOHANG);
//verbose output
//if($tpid  0) echo Parent collected child $tpid\n;

// wait two seconds before checking queue again
sleep(2);
unset($jobs_waiting);
}

function sig_handler($signo) {
// blah blah
}


-Clay


 From: Clay Loveless [EMAIL PROTECTED]
 Date: Sat, 01 Jun 2002 12:38:18 -0700
 To: PHP-General [EMAIL PROTECTED]
 Subject: [PHP] pcntl functions for task manager - comments?
 
 I'm experimenting with PHP's pcntl_* functions using the PHP cgi ... I've
 never written a daemon before, and there doesn't seem to be a lot of
 information out there about how to do this with the pcntl functions.
 
 So, I've read what I can find on the subject as it deals with UNIX
 programming. The goal is a script that will run forever, checking a job
 queue ... If jobs are waiting, use pcntl_fork() to handle the jobs.
 
 To this end, I've come up with this pseudo-code ...  Before going WAY off in
 this direction, I'd like to submit this for comments by those who've had
 more experience with this sort of thing.
 
 --
 // run forever if necessary
 set_time_limit(0);
 
 // detatch from the controlling terminal
 if (!posix_setsid()) {
   die('could not detach from terminal');
 }
 
 // setup signal handlers
 pcntl_signal(SIGTERM, sig_handler);
 pcntl_signal(SIGHUP, sig_handler);
 
 // loop forever waiting on jobs
 while(1) {
 
   // check queue for pending jobs
   // (db lookup omitted, let's assume jobs are waiting)
   $jobs_waiting = true;
   
   if($jobs_waiting) {
   
   $pid = pcntl_fork();
   if($pid == -1) {
   die('could not fork');
   } else if ($pid) {
   
   // parent
   // call waitpid to reap children that
   // have already terminated
   do {
   $tpid = pcntl_waitpid(-1,$status,WNOHANG);
   if($tpid == -1)
   die('error occurred while waiting for child');
   } while (!$tpid);
   
   } else {
   // child
   // perform task on jobs waiting
   
   // when job(s) complete, quit
   exit();
   }
 
   }
   // wait two seconds before checking queue again
   sleep(2);
   unset($jobs_waiting);
 }
   
 function sig_handler($signo) {
   // blah blah
 }
 
 
 
 I am open to suggestions or comments on this approach! Please let me know if
 you think this looks nuts.
 
 Thanks,
 Clay
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] pcntl functions for task manager - comments?

2002-06-01 Thread Clay Loveless

I'm experimenting with PHP's pcntl_* functions using the PHP cgi ... I've
never written a daemon before, and there doesn't seem to be a lot of
information out there about how to do this with the pcntl functions.

So, I've read what I can find on the subject as it deals with UNIX
programming. The goal is a script that will run forever, checking a job
queue ... If jobs are waiting, use pcntl_fork() to handle the jobs.

To this end, I've come up with this pseudo-code ...  Before going WAY off in
this direction, I'd like to submit this for comments by those who've had
more experience with this sort of thing.

--
// run forever if necessary
set_time_limit(0);

// detatch from the controlling terminal
if (!posix_setsid()) {
die('could not detach from terminal');
}

// setup signal handlers
pcntl_signal(SIGTERM, sig_handler);
pcntl_signal(SIGHUP, sig_handler);

// loop forever waiting on jobs
while(1) {

// check queue for pending jobs
// (db lookup omitted, let's assume jobs are waiting)
$jobs_waiting = true;

if($jobs_waiting) {

$pid = pcntl_fork();
if($pid == -1) {
die('could not fork');
} else if ($pid) {

// parent
// call waitpid to reap children that
// have already terminated
do {
$tpid = pcntl_waitpid(-1,$status,WNOHANG);
if($tpid == -1)
die('error occurred while waiting for child');
} while (!$tpid);

} else {
// child
// perform task on jobs waiting

// when job(s) complete, quit
exit();
}

}
// wait two seconds before checking queue again
sleep(2);
unset($jobs_waiting);
}

function sig_handler($signo) {
// blah blah
}



I am open to suggestions or comments on this approach! Please let me know if
you think this looks nuts.

Thanks,
Clay


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php