Getting your own process ID

From: Joshua Beall (jbeall_at_donotspam.remove.me.heraldic.us)
Date: 03/25/05

  • Next message: Andy Hassall: "Re: Image function puzzle"
    Date: Thu, 24 Mar 2005 23:31:12 GMT
    
    

    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.


  • Next message: Andy Hassall: "Re: Image function puzzle"

    Relevant Pages

    • Getting your own process ID
      ... context), check to see if the requested semaphore is available, and if it ... does the PHP script identify itself? ... have the same session ID. ... requests to be at the exact same instant. ...
      (comp.lang.php)
    • Re: Fw: [PHP] Custom database session handler and data concurrent access from ajax request
      ... If it's not "none" there is something wrong with the semaphore ... the life time of the current session since i could'nt know which ... executed before another and which script could remove the ... So semaphore are removed by session garbage collector. ...
      (php.general)
    • Re: session management with database: optimal parameters in php.ini
      ... PHP will pass the sessionid to your ... It is testing access to the SESSION by the same person with high speed access to the session. ... Your sessionlogic should BLOCK the executing script untill that script can 'get' the session for itself alone. ... Your browser sends out 20 requests to the server to get the HTML to fill the frames. ...
      (alt.php)
    • Re: session management with database: optimal parameters in php.ini
      ... It is testing access to the SESSION by the same person with high speed ... Your sessionlogic should BLOCK the executing script untill that script ... Your browser will send requests for all 20 pages to the server at the ...
      (alt.php)
    • Re: Reentrant POST hangs the session
      ... Then every request to ANY script will wait that uses that session. ... PHP runs requests to all kind of pages simultaniously. ... That is why you should try to disable it if you want to test the systemcall. ...
      (comp.lang.php)