473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Getting your own process ID

Hi All,

I am doing some work where I want to do locking, and prevent scripts from
running in parallel. I see that I could use the semaphore mechanism, but
I'd like for my code to be portable, and that extension is not enabled in
many places.

I need some way for a process to uniquely identify itself. It can then look
at the storage container (flat file, DB, whatever is appropriate in
context), check to see if the requested semaphore is available, and if it
is, acquire it and then mark itself as the owner. It can then check that it
did in fact get ownership (as opposed to another process which attempted to
acquire it at the exact same moment) before proceeding.

However I am stumped at the point where it needs to indicate ownership. How
does the PHP script identify itself? It can't use the script name,
obviously - lots of instances of the script may be running. It can't use
session ID - the user might submit duplicate requests, and they would both
have the same session ID. The best I have been able to think of is to use
the sha1(microtime()) to generate a unique key. But this isn't quite
foolproof, as it is theoretically possible, though unlikely, for two
requests to be at the exact same instant.

The answer that comes to my mind would be to use the process ID. This is
necessarily unique across the entire server, correct? It seems to be
exactly what I need. But I can't seem to figure out how to determine the
current process ID from within PHP. Is this even possible?

Any ideas?

-Josh

p.s. Please forgive me if I have misused the term "semaphore" - I know it
only from the context I have heard it used in, I don't know the textbook
definition.
Jul 17 '05 #1
7 6478
On Thu, 24 Mar 2005 23:31:12 GMT, "Joshua Beall"
<jb****@donotspam.remove.me.heraldic.us> wrote:
The answer that comes to my mind would be to use the process ID. This is
necessarily unique across the entire server, correct? It seems to be
exactly what I need. But I can't seem to figure out how to determine the
current process ID from within PHP. Is this even possible?


Depends how you're running. If you're running as an Apache module, the current
process ID would be the Apache worker process. It'd only be processing one PHP
script at a time so should be unique enough over the life of the PHP script.

In which case: http://uk.php.net/manual/en/function.posix-getpid.php

Still, this likely wouldn't work under Windows, where Apache (2.0) is
muti-threaded, not multi-process.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:k4********************************@4ax.com...
On Thu, 24 Mar 2005 23:31:12 GMT, "Joshua Beall"
<jb****@donotspam.remove.me.heraldic.us> wrote:

Depends how you're running. If you're running as an Apache module, the
current
process ID would be the Apache worker process. It'd only be processing one
PHP
script at a time so should be unique enough over the life of the PHP
script.

In which case: http://uk.php.net/manual/en/function.posix-getpid.php

Still, this likely wouldn't work under Windows, where Apache (2.0) is
muti-threaded, not multi-process.


Any other ideas? I can't think of anything else that is guaranteed to be
unique per request.

What about remote port number? Is it possible to have multiple connections
from the same remote IP and port? Or would
sha1(microtime().$_SERVER['REMOTE_ ADDR'].$_SERVER['REMOTE_PORT']) generate
a unique ID? We might get repeated requests from the same remote IP and
remote port, but as long as it is not possible for them to be at the same
time, this seems like it would work... ?

-Josh
Jul 17 '05 #3
On Thu, 24 Mar 2005 23:59:26 GMT, "Joshua Beall"
<jb****@donotspam.remove.me.heraldic.us> wrote:
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:k4********************************@4ax.com.. .
On Thu, 24 Mar 2005 23:31:12 GMT, "Joshua Beall"
<jb****@donotspam.remove.me.heraldic.us> wrote:

Depends how you're running. If you're running as an Apache module, the
current
process ID would be the Apache worker process. It'd only be processing one
PHP
script at a time so should be unique enough over the life of the PHP
script.

In which case: http://uk.php.net/manual/en/function.posix-getpid.php

Still, this likely wouldn't work under Windows, where Apache (2.0) is
muti-threaded, not multi-process.
Any other ideas? I can't think of anything else that is guaranteed to be
unique per request.

What about remote port number? Is it possible to have multiple connections
from the same remote IP and port?


*scrunches forehead* I _think_ this is guaranteed unique at any given time...
Or would sha1(microtime().$_SERVER['REMOTE_ ADDR'].$_SERVER['REMOTE_PORT']) generate
a unique ID?


Well, there you're throwing in a hashing function - SHA1 has a pretty large
range, but it does not guarantee uniqueness, just that it is Very Hard to
deliberately produce two different plain-data values that result in the same
SHA1 hash.

Drop the sha1() and you might actually have something more likely to be
unique.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4
"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> wrote in message
news:45I0e.20505$b_6.11577@trnddc01...
Hi All,

I am doing some work where I want to do locking, and prevent scripts from
running in parallel. I see that I could use the semaphore mechanism, but
I'd like for my code to be portable, and that extension is not enabled in
many places.

I need some way for a process to uniquely identify itself. It can then
look at the storage container (flat file, DB, whatever is appropriate in
context), check to see if the requested semaphore is available, and if it
is, acquire it and then mark itself as the owner. It can then check that
it did in fact get ownership (as opposed to another process which
attempted to acquire it at the exact same moment) before proceeding.

However I am stumped at the point where it needs to indicate ownership.
How does the PHP script identify itself? It can't use the script name,
obviously - lots of instances of the script may be running. It can't use
session ID - the user might submit duplicate requests, and they would both
have the same session ID. The best I have been able to think of is to use
the sha1(microtime()) to generate a unique key. But this isn't quite
foolproof, as it is theoretically possible, though unlikely, for two
requests to be at the exact same instant.

The answer that comes to my mind would be to use the process ID. This is
necessarily unique across the entire server, correct? It seems to be
exactly what I need. But I can't seem to figure out how to determine the
current process ID from within PHP. Is this even possible?

Any ideas?

-Josh

p.s. Please forgive me if I have misused the term "semaphore" - I know it
only from the context I have heard it used in, I don't know the textbook
definition.


Process ID would not be unique on a multi threaded server and hence not
portable if that is an requirement.

PHP has a function to generate a unique ID if that is what you are after:

http://se.php.net/manual/en/function.uniqid.php

Neither that function guarantees the ID to be unique, if generated on
several hosts at the same microsecond... probably won't happen in a trillion
years though...

You could use a transactional database to generate unique ids or you could
use file locking.

I serialize session requests per session ID by making a lock on the sesssion
ID in MySQL:

"SELECT GET_LOCK('$sessionID', 10)" and "DO RELEASE_LOCK('$sessionID')"

If you wish to serialize requests per script, I would use a lock file and
use file locking. That is how most applications do it.

If you want everything you do to be bullet proof, you will end up spending
most of your time on just that, and not on your application.

Best Regards,

Peter Albertsson
Jul 17 '05 #5
Carved in mystic runes upon the very living rock, the last words of
Joshua Beall of comp.lang.php make plain:
However I am stumped at the point where it needs to indicate
ownership. How does the PHP script identify itself?


http://www.php.net/manual/en/function.uniqid.php

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #6
"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> kirjoitti
viestissä:45I0e.20505$b_6.11577@trnddc01...
Hi All,

I am doing some work where I want to do locking, and prevent scripts from
running in parallel. I see that I could use the semaphore mechanism, but
I'd like for my code to be portable, and that extension is not enabled in
many places.

I need some way for a process to uniquely identify itself. It can then
look at the storage container (flat file, DB, whatever is appropriate in
context), check to see if the requested semaphore is available, and if it
is, acquire it and then mark itself as the owner. It can then check that
it did in fact get ownership (as opposed to another process which
attempted to acquire it at the exact same moment) before proceeding.

However I am stumped at the point where it needs to indicate ownership.
How does the PHP script identify itself? It can't use the script name,
obviously - lots of instances of the script may be running. It can't use
session ID - the user might submit duplicate requests, and they would both
have the same session ID. The best I have been able to think of is to use
the sha1(microtime()) to generate a unique key. But this isn't quite
foolproof, as it is theoretically possible, though unlikely, for two
requests to be at the exact same instant.

The answer that comes to my mind would be to use the process ID. This is
necessarily unique across the entire server, correct? It seems to be
exactly what I need. But I can't seem to figure out how to determine the
current process ID from within PHP. Is this even possible?

Any ideas?


here's something of a suggestion

use a file, only one file handle can be opened at one time. This is the
first thing. Next is to use timestamps. The last is to build a system
that'll try and retry to open the file untill successfull. Last thing is to
ensure you don't get duplicate id's. Here's how:

<?php

for($i=0;$i<10;$i++){
// If for some reason the opening of the file never succeeds,
// attempt at most 10 times
$fp = fopen("timestamp.log","r+");
// attempt tp open file
if($fp){ // file opened
// read the previous timestamp from file
$prev_timestamp = fread($fp, 1024);
// create own timesampt from cpu time
$my_timestamp = microtime();
// make sure they don't match
if($my_timestamp!=$prev_timestamp){
// place filepointer in the beginning
rewind($fp);
// write your own timestamp as
//"previous" for the next instance
fwrite($fp, $my_timestamp, 1024);
// close, so the next instance can open
fclose($fp);
// exit for-loop
break;
}
}
// unsuccesful? wait 1 second and retry...
sleep(1);
}
?>

I'm not sure at all of this works or not and I haven't tested it, but it's
an idea for you to experiment with...
Jul 17 '05 #7
"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> writes:
Hi All,

I am doing some work where I want to do locking, and prevent scripts from
running in parallel. I see that I could use the semaphore mechanism, but
I'd like for my code to be portable, and that extension is not enabled in
many places.

I need some way for a process to uniquely identify itself. It can then look
at the storage container (flat file, DB, whatever is appropriate in
context), check to see if the requested semaphore is available, and if it
is, acquire it and then mark itself as the owner. It can then check that it
did in fact get ownership (as opposed to another process which attempted to
acquire it at the exact same moment) before proceeding.

However I am stumped at the point where it needs to indicate ownership. How
does the PHP script identify itself? It can't use the script name,
obviously - lots of instances of the script may be running. It can't use
session ID - the user might submit duplicate requests, and they would both
have the same session ID. The best I have been able to think of is to use
the sha1(microtime()) to generate a unique key. But this isn't quite
foolproof, as it is theoretically possible, though unlikely, for two
requests to be at the exact same instant.

The answer that comes to my mind would be to use the process ID. This is
necessarily unique across the entire server, correct? It seems to be
exactly what I need. But I can't seem to figure out how to determine the
current process ID from within PHP. Is this even possible?

Any ideas?

-Josh

p.s. Please forgive me if I have misused the term "semaphore" - I know it
only from the context I have heard it used in, I don't know the textbook
definition.

Check out the following function calls:
getmypid()
uniqid()

Complete documentation at http://www.php.net/

--
John
__________________________________________________ _________________
John Murtari Software Workshop Inc.
jmurtari@following domain 315.635-1968(x-211) "TheBook.Com" (TM)
http://thebook.com/
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
2
by: David Hearn | last post by:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Description: An unhandled exception occurred during the execution of the current...
0
by: Srinivas | last post by:
I have a Process which lauches Internet Explorer.Using this process I keep changing the URL of the Browser based on certain user event. I intend to kill this Process and restart it when the Process...
2
by: MSK | last post by:
Hi, Continued to my earlier post regaring "Breakpoints are not getting hit" , I have comeup with more input this time.. Kindly give me some idea. I am a newbie to .NET, recently I installed...
0
by: deviparimala | last post by:
Hi All, I'm trying to get the exit time of a process. But the event is not getting triggered when i'm closing the process. Below is the code which i have tried. Any help would do. ...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
4
by: Daniel | last post by:
is there some per-process-limit on memory in .net processes? is there any way to increase it? i keep getting System.OutOfMemoryException when my box has 8 gigs of unused memory.
1
by: desivirus | last post by:
hi admin.. i followed your tip in "HOW TO LIST PROCESS ID IN WINDOWS" thread..and iam trying to compile this code in cygwin , $gcc -mno-cygwin process.c -o -L"psapi.lib" process.exe psapi.h...
1
by: srinivasan srinivas | last post by:
HI, I am using Solaris and subprocess.Popen to spawn a process on a remote machine. Thanks, Srini ----- Original Message ---- From:Diez B. Roggisch <deets@nospam.web.de> To:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.