Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4115628
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:36:25+00:00 2026-05-20T22:36:25+00:00

I do this to ensure only once instance of this process is running (pseudo

  • 0

I do this to ensure only once instance of this process is running (pseudo code php/mysql innodb):

START TRANSACTION
$rpid = SELECT `value` FROM locks WHERE name = "lock_name" FOR UPDATE
$pid = posix_getpid();
if($rpid > 0){
  $isRunning = posix_kill($rpid, 0);
  if(!$isRunning){ // isRunning
    INSERT INTO locks values('lock_name', $pid) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)
  }else{
    ROLLBACK
    echo "Allready running...\n";
    exit();
  }
}else{ // if rpid == 0 -
  INSERT INTO locks values('lock_name', $pid) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)
}
COMMIT

...............

//free the pid
INSERT INTO locks values('lock_name', 0) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)

Table locks contain these fields:

id - primary, autoinc
name - varchar(64) unique key
description - text
value - text

I believe the time from START TRANSACTIN to COMMIT/ROLLBACK is really milliseconds – there is no enough time to even get timeout. How is it possible to get a deadlock with this code? I don’t use other tables within this transaction. It looks that deadlock is not possible. If 2 processes start at the same time the first that gets the lock on that row will will proceed and the other will wait the lock to be released. If the lock is not released within 1 minute the error is “timeout”, not deadlock.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:36:25+00:00Added an answer on May 20, 2026 at 10:36 pm

    Just figured it out thanks to Quassnoi‘s answer…

    I can do:

    $myPid = posix_getpid();
    $gotIt = false;
    while(true){
      START TRANSACTION;
      $pid = SELECT ... FOR UPDATE; // read pid and get lock on it
      if(mysql_num_rows($result) == 0){
        ROLLBACK;// release lock to avoid deadlock
        INSERT IGNORE INTO locks VALUES('lockname', $myPid);
      }else{
        //pid existed, no insert is needed
        break;
      }
    }
    
    if($pid != $myPid){ //we did not insert that
      if($pid>0 && isRunning($pid)){
        ROLLBACK;
        echo 'another process is running';
        exit;
      }{
        // no other process is running - write $myPid in db
        UPDATE locks SET value = $myPid WHERE name = 'lockname'; // update is safe
        COMMIT;
      }
    }else{
      ROLLBACK; // release lock
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Quick-and-dirty way to ensure only one instance of a shell script is
How can I ensure my table data function is called only once when app
The following code implements a simple singleton that ensures only 1 instance of my
if i use <BODY TOPMARGIN=0 LEFTMARGIN=0 MARGINHEIGHT=0 MARGINWIDTH=0> tag like this, will this ensure
How can I ensure that this does NOT open in a new window?? onclick=ob=this.form.table;window.open(ob.options[ob.selectedIndex].value)/>
i would like to be able to ensure that if a table (in this
This is my code: <CheckBox android:id=@+id/sprint_checkbox android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/sprint_game /> <CheckBox android:id=@+id/marathon_checkbox android:layout_width=fill_parent android:layout_height=wrap_content
This code below allows me to find the word error in all my files
I have the following class: I want to ensure the class url is only
How can I ensure that the headScript and headStyle helper include css files only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.