C 和 php 之间的锁定文件

发布于 2024-10-14 05:09:05 字数 177 浏览 3 评论 0原文

尽管标题提到了文件,但它不一定是文件。任何锁定机制都可以。

情况是这样的:我有一个用C编写的守护进程,还有一个用php编写的网页。我想要一种相互锁定的方法,以便在某些情况下,C 守护进程会锁定文件,并且 php 检测到这种情况并告诉客户端系统正忙。

有一个简单的方法可以做到这一点吗?

谢谢,

Although the title mentions file, it doesn't have to be a file. Any locking mechanism would do.

Here is the situation: I have a daemon process written in C, and a web page in php. I'd like to have a way of mutual locking so that under certain situation, the C daemon would lock a file and php detects the situation and tells the client side that the system is busy.

Is there an easy way of doing this?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生寂 2024-10-21 05:09:05

flock 做得很好。

在 PHP 脚本中,使用非阻塞锁

$fd = fopen('/var/run/lock.file', 'r+');
if (!flock($fd, LOCK_SH | LOCK_NB, $wouldblock) && $wouldblock) {
    // buzy
}

LOCK_NB 标志使该调用成为非阻塞。如果文件被独占锁定,它将立即返回。将允许多个页面同时锁定文件。

您可以使用以下命令释放锁

flock($fd, LOCK_UN);

:在您的 C 守护程序中,使用阻塞和独占锁:

flock(fd, LOCK_EX); // This will wait until no page has locked the file

请参阅 PHP 的 flock() 文档和 C 的 一个

flock does it properly.

In your PHP script, use a non-blocking lock:

$fd = fopen('/var/run/lock.file', 'r+');
if (!flock($fd, LOCK_SH | LOCK_NB, $wouldblock) && $wouldblock) {
    // buzy
}

The LOCK_NB flag make this call non blocking. If the file is locked exclusively, it will return immediately. Multiple pages will be allowed to lock the file at the same time.

You can release the lock with

flock($fd, LOCK_UN);

In your C daemon, use a blocking and exclusive lock:

flock(fd, LOCK_EX); // This will wait until no page has locked the file

See PHP's flock() documentation and C's one

鱼窥荷 2024-10-21 05:09:05

你可以让你的守护进程在繁忙时创建一个文件,并在不忙时将其删除,然后在 PHP 中执行以下操作:

if (file_exists('/path/to/file')) {
    echo 'System busy';
}

You could have your daemon create a file when busy, and remove it when not, then in PHP do something like:

if (file_exists('/path/to/file')) {
    echo 'System busy';
}
萌能量女王 2024-10-21 05:09:05

如果您的 PHP 应用程序是数据库驱动的,则应该很容易更新该数据库的某一列以指示“系统正忙”。

您的 cronjob 将设置和重置此标志,并且您的 PHP 应用程序可以读取其值。

If your PHP application is database driven, it should be easy to update a certain column of that database to indicate "the system is busy".

Your cronjob would set and reset this flag and your PHP application can read its value.

土豪我们做朋友吧 2024-10-21 05:09:05

您只想让 PHP 检测到守护进程正忙吗?或者你真的希望他们互相等待?使用独占锁会有一个缺点,即 C 守护进程必须等待所有 PHP 实例完成其工作,然后才能获取其锁并继续。

如果您只想检测 C 守护进程是否繁忙(即仅在一个方向上),则仅测试繁忙令牌文件(或信号量或共享内存对象 - 平台相关)是否存在可能是更好的选择。然而,创建文件往往比在共享内存中设置一个简单的标志更昂贵。

Do you only want PHP to detect that the daemon is busy? Or do you actually want them to wait on each other? Using an exclusive lock would have the downside that the C daemon will have to wait for any and all PHP instances to finish their work before it can grab its lock and continue on.

If you're only looking to detect that the C daemon is busy (ie, only in one direction), just testing for the existance of a busy token file (or semaphore, or shared memory object - platform dependant) might be a better option. Creating files tends to be more expensive than just setting a simple flag in shared memory however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文