Getting your own process ID

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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?


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

Re: Getting your own process ID

Carved in mystic runes upon the very living rock, the last words of
Joshua Beall of comp.lang.php make plain:

Quoted text here. Click to load it

Alan Little
Phorm PHP Form Processor /

Re: Getting your own process ID

On Thu, 24 Mar 2005 23:31:12 GMT, "Joshua Beall"

Quoted text here. Click to load it

 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:

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

< Space: disk usage analysis tool

Re: Getting your own process ID

Quoted text here. Click to load it

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... ?


Re: Getting your own process ID

On Thu, 24 Mar 2005 23:59:26 GMT, "Joshua Beall"

Quoted text here. Click to load it

 *scrunches forehead* I _think_ this is guaranteed unique at any given time...

Quoted text here. Click to load it

 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

< Space: disk usage analysis tool

Re: Getting your own process ID

Quoted text here. Click to load it

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:

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

Re: Getting your own process ID

Quoted text here. Click to load it

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:


    // 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
            // place filepointer in the beginning
            // write your own timestamp as
            //"previous" for the next instance
            fwrite($fp, $my_timestamp, 1024);
            // close, so the next instance can open
            // exit for-loop
    // unsuccesful? wait 1 second and retry...

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

Re: Getting your own process ID

Quoted text here. Click to load it
       Check out the following function calls:

        Complete documentation at /

John Murtari                              Software Workshop Inc.
jmurtari@following domain 315.635-1968(x-211)  "TheBook.Com" (TM) /

Site Timeline