Looping Scripts

Consider the following script:

      <?php while(1) { } ?>

This would cause the php interpreter to loop. Although a bug this simple is easy to avoid, it is, according to the theory of Turing Machines, theoretically impossible to determine if an arbitrary program is safe from looping behavoir.

PHP is configured to kill a program after a certain amount of time, which is about the only way of dealing completely with this problem, as well as other related problems such as a script waiting for a stalled resource. Here is a top display when the page containing this script is requested:

top - 09:07:54 up 69 days, 18:18,  4 users,  load average: 0.50, 0.23, 0.08
Mem:    514536k total,   493556k used,    20980k free,    62380k buffers
Swap:  1020088k total,    23852k used,   996236k free,   355432k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                 
 1770 apache    25   0 17544 9.8m 6704 R 99.9  1.9   0:18.08 httpd                                   
 5523 burt      16   0  4016 1080  832 R  0.7  0.2   0:01.18 top                                     
    1 root      16   0  2420  248  220 S  0.0  0.0   0:09.04 init                                    
    2 root      RT   0     0    0    0 S  0.0  0.0   0:07.14 migration/0                             
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.10 ksoftirqd/0                             
    4 root      RT   0     0    0    0 S  0.0  0.0   0:05.08 migration/1                             
    5 root      34  19     0    0    0 S  0.0  0.0   0:00.06 ksoftirqd/1                             
    6 root       5 -10     0    0    0 S  0.0  0.0   0:03.59 events/0                                

The script gets killed after 30 seconds. In order to observe this, I have run the script by sending the HTTP GET command manually, using a telent connection to port 80, and typing the GET.

[cfri598@lee ~/private_html]$ telnet lee 80
Trying 172.19.0.2...
Connected to lee.cs.miami.edu (172.19.0.2).
Escape character is '^]'.
GET /~cfri598/loop.php HTTP/1.1
Host: lee.cs.miami.edu

HTTP/1.1 200 OK
Date: Wed, 23 May 2007 13:07:31 GMT
Server: Apache/2.0.52 (Red Hat)
X-Powered-By: PHP/4.3.9
Content-Length: 520
Connection: close
Content-Type: text/html; charset=UTF-8

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Loop</title>
<!--
<link href="mystyle.css" rel="stylesheet" type="text/css">
-->
<style type="text/css"><!--
   /* internal styles */
--></style>
</head>
<body>
 
<h1>Loop</h1>

<br />
<b>Fatal error</b>:  Maximum execution time of 30 seconds exceeded in <b>/davis/home/ugrad/cfri598/private_html/loop.php</b> on line <b>19</b><br />
Connection closed by foreign host.
[cfri598@lee ~/private_html]$ 

You can also look in the http error logs. Reminder: you must log into the web server, and look at the logs there. It is important to remember where things are acutally being computed.

[burt@lee httpd]$ pwd
/var/log/httpd
[burt@lee httpd]$ tail -5 error_log
[client 172.19.1.16] PHP Parse error:  parse error, unexpected $ in /davis/home/ugrad/cfri598/private_html/checker_board.php on line 59
[client 172.19.1.16] PHP Parse error:  parse error, unexpected T_STRING, expecting ',' or ';' in /davis/home/ugrad/cfri598/private_html/checker_board.php on line 27
[client 172.19.0.2] PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /davis/home/ugrad/cfri598/private_html/loop.php on line 20
[client 172.19.0.2] PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /davis/home/ugrad/cfri598/private_html/loop.php on line 20
[client 172.19.0.2] PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /davis/home/ugrad/cfri598/private_html/loop.php on line 19
[burt@lee httpd]$