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

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

ryeterrell
Forum Newbie
Posts: 1
Joined: Wed Sep 27, 2006 9:21 am

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

Post 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
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

This is the sort of situation AJAX is really handy for.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

it would be smarter to just do what onion said because like panama said,an infinite loop could bog down the server
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post 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 />';}
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post 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
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
Post Reply