PHP forking

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

Post Reply
kumarrana
Forum Commoner
Posts: 26
Joined: Sat Sep 01, 2007 12:55 pm

PHP forking

Post by kumarrana »

I read some article about forking and optimizing the performance. I am writing a program that query the satellite modem's status. It became painful once I realize that there is quarter second delay to reach the host and return the value. On top, I have 255 loops to go through, means big latency. On my old box, it takes more than 10 minutes to finish one iterations. Can somebody help me to adapt either forking to optimize the loop or direct me to other paths. Here is my sample code.

Code: Select all

$EventDescriptionOID = ".1.3.6.1.4.1.XXXX.34.1.3.3.1.1.4."
$IP = "192.XXX.XXX.XXX";
for ($i = 1; $i <= 255; $i++)
{
$Get625EventDescription = shell_exec("/usr/bin/snmpget -c public -v 2c $IP $EventDescriptionOIDArr | awk '{print $4 $5 $6 $7 $8}'");  
echo "Index is: $EventDescriptionOIDArr" . " Query is: $Get625EventDescription <br>" ;
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP forking

Post by requinix »

How much do you know about multithreading and forking? Do you have a strategy in mind for how to use it?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP forking

Post by John Cartwright »

2nd post in a row where I would recommend another language suited for multithreading, i.e., c#.

By forking you are creating an additional process or each fork, which is not good when you want to fork 250 odd processes.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: PHP forking

Post by s.dot »

Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
kumarrana
Forum Commoner
Posts: 26
Joined: Sat Sep 01, 2007 12:55 pm

Re: PHP forking

Post by kumarrana »

I know very little about multi-threading and forking actually. I have been programming on PHP for over 5 years, but it was on and off. I am back after nearly 2 years. I do have strategy to use. I will go through the link that s.dot posted. I like John's advice but it is already too late to adapt new language. Ideal help from you guys would be a simple example that is similar to the code I posted.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP forking

Post by Weirdan »

What is actually different for every iteration (it's not clear from the code you posted)? If it's OID that changes, than you may issue a single request with multiple oids specified and suffer the roundtrip only once.
kumarrana
Forum Commoner
Posts: 26
Joined: Sat Sep 01, 2007 12:55 pm

Re: PHP forking

Post by kumarrana »

The last digit of OID will go from 1 to 255. So there will be 255 unique OIDs incrementing last digit from 1. Sorry about the confusion, I copied from working program just to show how I am doing, and asking for better solution.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP forking

Post by Weirdan »

kumarrana wrote:The last digit of OID will go from 1 to 255. So there will be 255 unique OIDs incrementing last digit from 1. Sorry about the confusion, I copied from working program just to show how I am doing, and asking for better solution.
So you could do something like this:

Code: Select all

$EventDescriptionOID = ".1.3.6.1.4.1.XXXX.34.1.3.3.1.1.4."
$IP = "192.XXX.XXX.XXX";
$oids = array();
for ($i = 1; $i <= 255; $i++)
{
   $oids[] = $EventDescriptionOID . $i;
}

var_dump(shell_exec("/usr/bin/snmpget -c public -v 2c " . escapeshellarg($IP) . " " . join(' ', array_map('escapeshellarg', $oids)) . "| awk '{print $4 $5 $6 $7 $8}'"));  
Last edited by Weirdan on Fri Oct 08, 2010 4:35 am, edited 1 time in total.
Reason: added missed join
kumarrana
Forum Commoner
Posts: 26
Joined: Sat Sep 01, 2007 12:55 pm

Re: PHP forking

Post by kumarrana »

Thanks Weirdan, I will implement your code and tell you how it go through.
Post Reply