当PHP,C ++和Shell脚本尝试访问同一文件时,如何避免错误? [英] How can I avoid errors when PHP, C++ and shell script try to access the same file?

查看:32
本文介绍了当PHP,C ++和Shell脚本尝试访问同一文件时,如何避免错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有PHP,C ++和bash脚本中的方法可以使相应程序在访问文件时等待运行?

Are there methods in PHP, C++ and bash scripts that can make the respective program wait its turn when accessing a file ?

我有一个用PHP编写的网页,使用户能够输入6个值:

I have a web-page written in PHP that gives the user the ability to input 6 values:

URL
URL Refresh Interval
Brightness
Color1 in hex
Color2 in hex
Color3 in hex

这些值将写在 configuration.txt 中.

每次打开网页 configuration.txt 时,PHP都会从那里获取一些值,然后将其关闭.提交上述一个或多个值后, configuration.txt 也将打开.

Each time the web-page is accessed configuration.txt gets opened, the PHP gets some values from there and then closes it. configuration.txt is also opened when one or more of the above values are submitted and then it gets closed.

接下来,我有一个bash,它定期 wgets configuration.txt 的URL,并将输出写入另一个名为 url_response.txt 的文件中.代码>.

Next, I have a bash that regularly wgets the URL from configuration.txt and writes the output to a different file, called url_response.txt.

while [ 0 ]
do
    line=$(head -n 1 data/configuration.txt)
    wget -q -i $line -O url_response.txt
    sleep 2
done

此脚本将放置在C ++程序中.

This script will be put inside a C++ program.

最后,同一C ++程序将必须访问 url_response.txt 并从中解析一些字符串,并且还必须访问 configuration.txt 来获取三种颜色.

Finally, the same C++ program will have to access url_response.txt to get and parse some strings from it and it will also have to access configuration.txt to get the three colors from it.

我非常确定这三个程序会在某一时刻相交,并且我不希望知道随后会发生什么.

I am pretty sure that these 3 programs will intersect at one point and I don't want to find out what happens then.

推荐答案

避免竞争情况的常见方法是使用锁定文件.当程序尝试读取或写入 configuration.txt 时,它将首先检查锁定文件.

A common way to avoid race conditions is to use a lock file. When a program tries to read or write to configuration.txt it checks the lock file first.

有两种锁:

  • 共享锁
  • 排他锁

只要没有其他程序具有排他锁,程序就可以获取共享锁(读取锁).这用于读取文件.只要没有其他程序写入文件,多个程序就可以读取该文件.

A program can get a shared lock (read lock) as long as no other program has an exclusive lock. This is used to read file. Multiple programs can read a file as long as no other program write to that file.

仅当没有其他程序具有锁(既不是排他锁也不是共享锁)时,程序才能获得排他锁(写锁).这用于写入文件.只要程序正在读取或写入文件,就不允许其他程序写入.

A program can get an exclusive lock (write lock) only if no other program has a lock (neither exclusive nor shared). This is used to write to a file. As long as a program is reading or writing to a file other programs are not allowed to write.

在Linux系统上,您可以使用 flock 来管理文件锁.

On a linux system you can use flock to manage file locks.

阅读:

flock --shared lockfile -c read.sh

flock --exclusive lockfile -c write.sh

通常,此命令将等待直到锁可用.与

Usually this command will wait until the lock is available. With

flock --nonblock锁定文件

flock --nonblock lockfile

该命令将立即失败,而不是等待.

the command will fail immediately instead of waiting.

从联机帮助页

简介

   flock [options] <file|directory> <command> [command args]
   flock [options] <file|directory> -c <command>
   flock [options] <file descriptor number>

说明

此实用程序从外壳程序脚本或命令行中管理flock(2)锁定.

This utility manages flock(2) locks from within shell scripts or the command line.

第一种形式和第二种形式以类似于su(1)或newgrp(1)的方式将锁包裹在执行命令的周围.它锁定创建的指定文件或目录(假设具有适当的权限)(如果尚不存在).默认情况下,如果无法立即获取该锁,则将flock等待直到锁可用.

The first and second forms wrap the lock around the executing a command, in a manner similar to su(1) or newgrp(1). It locks a specified file or directory, which is created (assuming appropriate permissions), if it does not already exist. By default, if the lock cannot be immediately acquired, flock waits until the lock is available.

第三种形式按文件描述符号使用打开的文件.查看示例,该如何使用.

The third form uses open file by file descriptor number. See examples how that can be used.

此处是c ++和

Here is the manpage for c++ and here is the manpage for shell scripts.

这篇关于当PHP,C ++和Shell脚本尝试访问同一文件时,如何避免错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆