Page 1 of 2

Is it possible to tie up the server with a while loop?

Posted: Wed Sep 27, 2006 9:27 am
by ryeterrell
Here's the deal. I have two users logged on, users A and B, and I have user A waiting for user B to do something. I keep user A waiting with a while loop in the php code.

Thing is, it seems like user B can't do anything while that while loop is running.

So my question is, if PHP code is running on the server, does it keep other code from running?

Thanks

Rye

Posted: Wed Sep 27, 2006 9:36 am
by Mordred
No, each request is processed in parallel. A locking may occur if both users compete for a server resource, like unshared access to a file, but this is rare, probably your problem comes from somewhere else.

Anyway, your approach doesn't "smell" right, for starters the server will kill a stale script after a timeout (which is a GOOD thing, as it doesn't allow remote users to tie up server resources, so don't go thinking of going around it with set_time_limit(0)!)

A better way is to have a script that checks if B is done. If not, the script prints a meta-refresh tag thus instructing A's browser to try again in a while. In that way the waiting loop is transferred at the client.

Posted: Wed Sep 27, 2006 9:37 am
by SpecialK
User A will be waiting on the request from your server.

Depending on their browser it can lead to a timeout for User A if User B takes too long. If a packet gets dropped along the way both users will timeout from the deadlock.

Posted: Wed Sep 27, 2006 10:06 am
by onion2k
This is the sort of situation AJAX is really handy for.

Posted: Wed Sep 27, 2006 10:27 am
by Mordred
onion2k wrote:This is the sort of situation AJAX is really handy for.
The key problem here is designing the backend properly. Whether you refresh the page or wait with AJAX is irreleveant (although I agree that AJAX is better as far as the client-side is concerned)

Posted: Wed Sep 27, 2006 12:34 pm
by bokehman
onion2k wrote:This is the sort of situation AJAX is really handy for.
Are you serious? Script A is stuck in a loop using up server resources. This makes script B extremely sluggish. How is AJAX going to help?

Posted: Wed Sep 27, 2006 12:54 pm
by onion2k
bokehman wrote:
onion2k wrote:This is the sort of situation AJAX is really handy for.
Are you serious? Script A is stuck in a loop using up server resources. This makes script B extremely sluggish. How is AJAX going to help?
Using AJAX you can poll the server every n seconds using a script that generates a lightweight response. Rather than having a while() loop waiting for User B to do something, you have a script User A calls via AJAX that returns a yes or a no, and he can sit and watch a pretty "waiting" screen at the same time. Perhaps even with a timer or a progress bar.

There's no while() loop, the server isn't tied up at all, and User A isn't generating a complete page response as he would if it were done with a meta refresh.

Posted: Wed Sep 27, 2006 1:12 pm
by bokehman
onion2k wrote:Rather than having a while() loop
Maybe I misread the question. My understanding was the server was stuck in a loop by accident.

Posted: Wed Sep 27, 2006 3:01 pm
by onion2k
bokehman wrote:
onion2k wrote:Rather than having a while() loop
Maybe I misread the question. My understanding was the server was stuck in a loop by accident.
As I understand it ryeterrell is putting user A in a while() loop and checking a condition that an action by user B will meet. For example, the while loop could check a database value that the script user B calls updates.

Posted: Wed Sep 27, 2006 5:48 pm
by AKA Panama Jack
Actually it is very, very EASY for a single PHP program to completely tie up a server unless you have some kind of process load limiting installed.

The biggest culprit is the use of a WHILE function that has no end. The same thing can happen with a FOR function is the values being checked never change or keep getting reset. You will quickly max out the server load. I know I have done it on my development server a number of times in the past. I usually have to kill the process through command line when that happens. Any other connections to the server WILL slow to a virtual crawl until the process has finished or been killed.

You should NEVER use a loop of any kind to create a wait state in PHP because it will cause a load spike for the CPU.

Posted: Thu Sep 28, 2006 9:29 am
by a94060
it would be smarter to just do what onion said because like panama said,an infinite loop could bog down the server

Posted: Thu Sep 28, 2006 9:34 am
by MrPotatoes
you can halt a computer in an infinate while loop. try it, it's hilarious

while(1)
{echo 'i shouldn\'t have done this...<br />';}

Posted: Thu Sep 28, 2006 10:00 am
by waradmin
Its like testing a C++ program that was poorly made:

Code: Select all

int main() {
     int a;
     int b;
     a == 0;
     b == 1;
     while(a < b) {
          cout << "god damn it\n";
     }
}
Great way to crash a system.

Posted: Thu Sep 28, 2006 10:03 am
by MrPotatoes
waradmin wrote:Its like testing a C++ program that was poorly made:

Code: Select all

int main() {
     int a;
     int b;
     a == 0;
     b == 1;
     while(a < b) {
          cout << "god damn it\n";
     }
}
Great way to crash a system.
'=', not '==' ;)

and yes. i've done this and it's hard to get out of. especially when the programs you were making were fullscreen usually lol

Posted: Thu Sep 28, 2006 10:17 am
by waradmin
MrPotatoes wrote:and yes. i've done this and it's hard to get out of. especially when the programs you were making were fullscreen usually lol
I want to test the limits of my system by running a simple counting script. It would be interesting to know how much the computer will take before crashing. (Ive crashed my Mac OS x86 system doing this after it reaches about 8,000,000,000

Say using:

Code: Select all

#include <iostream>

using namespace std;

int main() {
     int a;
     double b;
     cout << "Input ending number:\n";
     cin >> b;
     a = 0;
     while(a < b)
     {
          cout << a;
          a++;
     }
     return 0;
}
But I believe it wont do much on a windows system.