flock

Test if file is locked

蓝咒 提交于 2019-11-28 09:27:24
In PHP, how can I test if a file has already been locked with flock ? For example, if another running script has called the following: $fp = fopen('thefile.txt', 'w'); flock($fp, LOCK_EX); Prateek if (!flock($fp, LOCK_EX|LOCK_NB, $wouldblock)) { if ($wouldblock) { // another process holds the lock } else { // couldn't lock for another reason, e.g. no such file } } else { // lock obtained } As described in the docs , use LOCK_NB to make a non-blocking attempt to obtain the lock, and on failure check the $wouldblock argument to see if something else holds the lock. Check it like this: if (!flock

PHP flock() alternative

感情迁移 提交于 2019-11-28 09:12:42
PHP's documentation page for flock() indicates that it's not safe to use under IIS. If I can't rely on flock under all circumstances, is there another way I could safely achieve the same thing? There is no alternative available to safely achieve the same under all imaginary possible circumstances. That's by design of computer systems and the job is not trivial for cross-platform code . If you need to make safe use of flock() , document the requirements for your application instead. Alternatively you can create your own locking mechanism, however you must ensure it's atomic. That means, you

bash flock: exit if can't acquire lock

跟風遠走 提交于 2019-11-28 07:33:46
The following lock mechanism is used for preventing a cron job from running concurrently: #!/bin/bash echo "Before critical section" ( flock -e 200 echo "In critical section" sleep 5 ) 200>/tmp/blah.lockfile echo "After critical section" When running two instances together, the later waits until the first finishes, and then runs. This can cause backlogs of scripts waiting to run. How do I alter this script so that if flock can't acquire the lock, it terminates the script? I've tried -n without success. flock -n -e 200 || exit 1 flock -n tells you it failed by returning a failure code

Multiple users write to the same file at the same time using PHP

时光毁灭记忆、已成空白 提交于 2019-11-28 05:46:42
问题 I have a website where at the same moment, there can be multiple users writing to the same file at the same time. An example of my code is below. PHP 5.6.20 <?php $root=realpath($_SERVER["DOCUMENT_ROOT"]); $var=time()."|"; echo $var."<br/>"; $filename=$root."/Testing/Testing/test1.txt"; $myfile=fopen($filename,"a"); fwrite($myfile,$var); fclose($myfile); $myfile=fopen($filename,"r"); $contents = fread($myfile, filesize($filename)); echo $contents; fclose($myfile); ?> I read that in PHP if

Release of flock in case of errors?

雨燕双飞 提交于 2019-11-28 02:47:42
问题 Imagine the following Perl code (here in pseudo code): successfully acquired flock for FILEHANDLER # line 1 some error or maybe simply a call to exit() # line 2 close FILEHANDLER (which also releases the lock) # line 3 In this case I wouldn't release the lock, as the Perl script ends in line 2. In that case, is the lock ever released by the operating system? Does it see "hey, the script that acquired the lock crashed" and release the lock? Does it release the lock immediately? Also, is there

flock(): removing locked file without race condition?

和自甴很熟 提交于 2019-11-27 21:14:37
I'm using flock() for inter-process named mutexes (i.e. some process can decide to hold a lock on "some_name", which is implemented by locking a file named "some_name" in a temp directory: lockfile = "/tmp/some_name.lock"; fd = open(lockfile, O_CREAT); flock(fd, LOCK_EX); do_something(); unlink(lockfile); flock(fd, LOCK_UN); The lock file should be removed at some point, to avoid filling the temp directory with hundreds of files. However, there is an obvious race condition in this code; example with processes A, B and C: A opens file A locks file B opens file A unlinks file A unlocks file B

multiple threads able to get flock at the same time

亡梦爱人 提交于 2019-11-27 15:32:44
I was under the impression that flock(2) is thread safe, I recently, ran across the case in the code, where multiple threads are able to get a lock on the same file which are all synchronized with the use of obtaining exclusive lock using the c api flock. The process 25554 is multi-threaded app which has 20 threads, the number of threads having lock to the same file varies when the deadlock happens. The multi threaded app testEvent is writer to the file, where was the push is the reader from the file. Unfortunately the lsof does not print the LWP value so I cannot find which are the threads

秒杀活动防止库存负数问题

旧城冷巷雨未停 提交于 2019-11-27 14:23:19
解决这个问题比较流行的思路: 1、用额外的单进程处理一个队列,下单请求放到队列里,一个个处理,就不会有并发的问题了,但是要额外的后台进程以及延迟问题,不予考虑。(使用队列消耗资源比较多,并且有延迟性,用户体验不好) 2、数据库乐观锁(悲观锁),大致的意思是先查询库存,然后立马将库存-1,然后订单生成后,在更新库存前再查询一次库存,看看跟预期的库存数量是否保持一致,不一致就回滚,提示用户库存不足。 3、根据update结果来判断,我们可以在sql2的时候加一个判断条件update ... where 库存>0,如果返回false,则说明库存不足,并回滚事务。(这里开启事务处理,事务处理会比较慢,并且可能导致死锁问题) 4、借助文件排他锁,在处理下单请求的时候,用flock锁定一个文件,如果锁定失败说明有其他订单正在处理,此时要么等待要么直接提示用户"服务器繁忙"。 注意: 文件锁有一个问题就是如果程序在对文件进行锁操作的时候,如果程序在处理完业务逻辑后,可能程序的执行时间超时,或者其他原因导致没有对文件进行解锁(死锁问题),那么会导致后面的进程就不能操作文件 本文要说的是第4种方案,大致代码如下: 阻塞模式: $fp = fopen("lock.txt", "w+"); if(flock($fp, LOCK_EX)) { //..处理订单 flock($fp, LOCK_UN); }

crontab使用进程锁解决冲突

邮差的信 提交于 2019-11-27 05:06:24
想到一个问题,如果在crontab里有个定时任务设置为一分钟执行一次,但是它执行的时间可能会超过一分钟,此时crontab一分钟后会再次运行该脚本吗?这样会不会出现冲突呢?网上找了下,说可以用Linux中的进程锁控制crontab执行的并发问题。 给一个shell脚本加锁,使用flock命令。 一般格式: flock [-sxun][-w #] fd# flock [-sxon][-w #] file [-c] command... 常用选项: -s, --shared :获得一个共享的锁。 -x, --exclusive :获得一个独占的锁。 -u, --unlock :移除一个锁,通常是不需要的,脚本执行完后会自动丢弃锁。 -n, --nonblock :如果没有立即获得锁直接失败而不是等待。 -w, --timeout :如果没有立即获得锁就等待指定的时间。 -o, --close :在运行命令前关闭文件的描述符。用于如果命令产生子进程时会不受锁的管控。 -c, --command :在shell中运行一个单独的命令。 -h, --help :显示帮助。 -V, --version :显示版本。 测试一下看看: 在/home目录下建立一个test.sh。 vim /home/test.sh 输入: #!/bin/bash wget --limit-rate=200k -P

Test if file is locked

淺唱寂寞╮ 提交于 2019-11-27 02:30:02
问题 In PHP, how can I test if a file has already been locked with flock? For example, if another running script has called the following: $fp = fopen('thefile.txt', 'w'); flock($fp, LOCK_EX); 回答1: if (!flock($fp, LOCK_EX|LOCK_NB, $wouldblock)) { if ($wouldblock) { // another process holds the lock } else { // couldn't lock for another reason, e.g. no such file } } else { // lock obtained } As described in the docs, use LOCK_NB to make a non-blocking attempt to obtain the lock, and on failure