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 823869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:00:46+00:00 2026-05-15T03:00:46+00:00

Read some texts about locking in PHP. They all, mainly, direct to http://php.net/manual/en/function.flock.php .

  • 0

Read some texts about locking in PHP.
They all, mainly, direct to http://php.net/manual/en/function.flock.php .

This page talks about opening a file on the hard-disk!!

Is it really so? I mean, this makes locking really expensive – it means each time I want to lock I’ll have to access the hard-disk )=

Can anymore comfort me with a delightful news?

Edit:

Due to some replies I’ve got here, I want to ask this;
My script will run only by one thread, or several? Because if it’s by one then I obviously don’t need a mutex. Is there a concise answer?

What exactly I’m trying to do

Asked by ircmaxell.
This is the story:

I have two ftp servers. I want to be able to show at my website how many online users are online.
So, I thought that these ftp servers will “POST” their stats to a certain PHP script page. Let’s assume that the URL of this page is “http://mydomain.com/update.php“.

On the website’s main page (“http://mydomain.com/index.php“) I will display the cumulative statistics (online users).

That’s it.

My problem is that I’m not sure if, when one ftp server updates his stats while another does it too, the info will get mixed.
Like when multi-threading; Two threads increase some “int” variable at the same time. It will not happen as expected unless you sync between them.
So, will I have a problem? Yes, no, maybe?

Possible solution

Thinking hard about it all day long, I have an idea here and I want you to give your opinion.
As said these ftp servers will post their stats, once every 60sec.
I’m thinking about having this file “stats.php”.
It will be included at the updating script that the ftp servers go to (“update.php”) and at the “index.php” page where visitors see how many users are online.
Now, when an ftp server updates, the script at “update.php” will modify “stats.php” with the new cumulative statistics.
First it will read the stats included at “stats.php”, then accumulate, and then rewrite that file.

If I’m not mistaken PHP will detect that the file (“stats.php”) is changed and load the new one. Correct?

  • 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-15T03:00:46+00:00Added an answer on May 15, 2026 at 3:00 am

    Well, most of PHP runs in a different process space (there are few threading implementations). The easy one is flock. It’s guaranteed to work on all platforms.

    However, if you compile in support, you can use a few other things such as the Semaphore extension. (Compile PHP with –enable-sysvsem). Then, you can do something like (note, sem_acquire() should block. But if it can’t for some reason, it will return false):

    $sem = sem_get(1234, 1);
    if (sem_acquire($sem)) {
        //successful lock, go ahead
        sem_release($sem);
    } else {
        //Something went wrong...
    }
    

    The other options that you have, are MySQL user level locks GET_LOCK('name', 'timeout'), or creating your own using something like APC or XCache (Note, this wouldn’t be a true lock, since race conditions could be created where someone else gets a lock between your check and acceptance of the lock).

    Edit: To answer your edited question:

    It all depends on your server configuration. PHP May be run multi-threaded (where each request is served by a different thread), or it may be run multi-process (where each request is served by a different process). It all depends on your server configuration…

    It’s VERY rare that PHP will serve all requests serially, with only one process (and one thread) serving all requests. If you’re using CGI, then it’s multi-process by default. If you’re using FastCGI, it’s likely multi-process and multi-thread. If you’re using mod_php with Apache, then it depends on the worker type:

    1. mpm_worker will be both multi-process and multi-thread, with the number of processes dictated by the ServerLimit variable.
    2. prefork will be multi-process
    3. perchild will be multi-process as well

    Edit: To answer your second edited question:

    It’s quite easy. Store it in a file:

    function readStatus() {
        $f = fopen('/path/to/myfile', 'r');
        if (!$f) return false;
        if (flock($f, LOCK_SH)) {
            $ret = fread($f, 8192);
            flock($f, LOCK_UN);
            fclose($f);
            return $ret;
        }
        fclose($f);
        return false;
    }
    
    function updateStatus($new) {
        $f = fopen('/path/to/myfile', 'w');
        if (!$f) return false;
        if (flock($f, LOCK_EX)) {
            ftruncate($f, 0);
            fwrite($f, $new);
            flock($f, LOCK_UN);
            fclose($f);
            return true;
        }
        fclose($f);
        return false;
    }
    
    function incrementStatus() {
        $f = fopen('/path/to/myfile', 'rw');
        if (!$f) return false;
        if (flock($f, LOCK_EX)) {
            $current = fread($f, 8192);
            $current++;
            ftruncate($f, 0);
            fwrite($f, $current);
            flock($f, LOCK_UN);
            fclose($f);
            return true;
        }
        fclose($f);
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 415k
  • Answers 415k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The ListBox in your UserControl isn't correctly binding to LBItems.… May 15, 2026 at 9:07 am
  • Editorial Team
    Editorial Team added an answer The two don't really provide the same set of functionality.… May 15, 2026 at 9:07 am
  • Editorial Team
    Editorial Team added an answer If you want change tracking you need to generate the… May 15, 2026 at 9:07 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.