Ted
Ted

Reputation: 3875

testing locking a file

I locked a file and I am trying to test how the system reacts to a locked file. I would like other processes to wait for the process to be released until they can require_once that file again and not to come back with an error.

The problem is no matter what I do, I don't seem to be able to lock the file and do some waiting while another process asks for the file, and see the reaction of the system to that locked file.

I tried
1. sleep() which doesn't work, it halts the whole php engine.
2. I created a batch file that simply pauses until you hit enter. and used exec("cmd.exe /c START c:\\1.bat");

So the hunch is that all processes are actually being one serial queue of tasks, so that if one is paused for any reason, so are all others ?

so How can I test if a file is locked ?
But of course if I am right, what's the point in locking the file whatsoever if other processes can't access it anyway?

Upvotes: 0

Views: 264

Answers (2)

Ahatius
Ahatius

Reputation: 4875

$file = fopen('somefile.php', 'r+');

while(!flock($file, LOCK_SH | LOCK_NB)) {
    sleep(1);
}

require 'somefile.php';

Since flock doesn't seem to work within php scripts, you need to check if a lock is present.

Upvotes: 1

Jackson
Jackson

Reputation: 264

the require_once is handled by php. Because of that, you cannot simply acquire a lock for it AFAIK. If you look at file locking, refer to this
It states that you should use locks whenever you try to access the locked file.

Upvotes: 1

Related Questions