note 74500 added to function.popen

From: Date: Sat, 14 Apr 2007 04:42:40 +0000
Subject: note 74500 added to function.popen
Groups: php.notes 
Request: Send a blank email to php-notes+get-125375@lists.php.net to get a copy of this message
Care needs to be taken in the case of long running child processes. Say you want to run tail -f /var/log/messages or in my case burn dvds. If you have a busy wait, Apache2 can sit towards 100%cpu and steadily grow memory. In my case I crashed the server after about an hour and 90% of the dvd burned. During that time apache had consumed a gig of swap. Offending code - don't copy: $ThisCommand = sprintf("%s %s",COMMAND,$ThisFile); $fp=popen($ThisCommand,"r"); while (!feof($fp)) { set_time_limit (20); $results = fgets($fp, 4096); if (strlen($results) == 0) { // stop the browser timing out echo " "; flush(); } else { $tok = strtok($results, "\n"); while ($tok !== false) { echo htmlentities(sprintf("%s\n",$tok))."<br/>"; flush(); $tok = strtok("\n"); } } } pclose($fp); to go from zero memory and 100% cpu to negligible memory and negligible cpu add a sleep. while (!feof($fp)) { set_time_limit (20); $results = fgets($fp, 256); if (strlen($results) == 0) { // stop the browser timing out echo " "; flush(); } else { $tok = strtok($results, "\n"); while ($tok !== false) { echo htmlentities(sprintf("%s\n",$tok))."<br/>"; flush(); $tok = strtok("\n"); } } // avoid a busy wait sleep(1); } I think the continued banging of the space to keep the browser awake triggered some issues in apache. ---- Server IP: 203.16.234.20 Probable Submitter: 202.1.119.190 (proxied: 192.168.3.112) ---- Manual Page -- http://www.php.net/manual/en/function.popen.php Edit -- https://master.php.net/note/edit/74500 Del: integrated -- https://master.php.net/note/delete/74500/integrated Del: useless -- https://master.php.net/note/delete/74500/useless Del: bad code -- https://master.php.net/note/delete/74500/bad+code Del: spam -- https://master.php.net/note/delete/74500/spam Del: non-english -- https://master.php.net/note/delete/74500/non-english Del: in docs -- https://master.php.net/note/delete/74500/in+docs Del: other reasons-- https://master.php.net/note/delete/74500 Reject -- https://master.php.net/note/reject/74500 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125375) next »