flock

Linux中文件加锁

时光毁灭记忆、已成空白 提交于 2020-04-06 14:15:40
/proc/locks andrew@andrew-Thurley:/dev$ cat /proc/locks 1: POSIX ADVISORY WRITE 8968 08:01:11666907 1073741825 1073741825 2: POSIX ADVISORY READ 2433 08:01:11798469 128 128 .. . 35: FLOCK ADVISORY WRITE 1436 00:1a:7 0 EOF 51: FLOCK ADVISORY WRITE 1036 00:16:763 0 EOF 使用 ps -p PID 查看进程的相关信息 andrew@andrew-Thurley:/dev$ ps -p 8968 PID TTY TIME CMD 8968 ? 00:00:02 chrome 从上面的输出可以看出持有的锁的程序是 chrome ,即 google 浏览器 在 /dev 我下搜索主设备号为 8 次设备号为 1 的设备,是 /dev/sda1 andrew@andrew-Thurley:/dev$ ls -li /dev/sda1 | awk ' $6 =8' 351 brw-rw---- 1 root disk 8 1 12月 22 13:13 /dev/sda1 查看设备 /dev/sda1 的挂载点,并在该部分文件系统中搜索

File locking in Linux

亡梦爱人 提交于 2020-02-26 00:39:03
File locking in Linux https://gavv.github.io/articles/file-locks/ Table of contents Introduction Advisory locking Common features Differing features File descriptors and i-nodes BSD locks (flock) POSIX record locks (fcntl) lockf function Open file description locks (fcntl) Emulating Open file description locks Test program Command-line tools Mandatory locking Example usage Introduction File locking is a mutual-exclusion mechanism for files. Linux supports two major kinds of file locks: advisory locks mandatory locks Below we discuss all lock types available in POSIX and Linux and provide usage

对flock的理解

╄→гoц情女王★ 提交于 2020-02-08 13:22:08
有时候需要保证同时只能有一个进程存在, 类似singleton的概念. 这时候简单方法就是对文件尝试加锁, 加锁成功就继续, 否则退出 函数定义如下: int flock(int fd, int operation); // 第一个参数是fd man-pages中对fd参数说明如下: Locks created by flock() are associated with an open file table entry. This means that duplicate file descriptors (created by, for example, fork(2) or dup(2)) refer to the same lock, and this lock may be modified or released using any of these descriptors. 锁是和内核的file-entry绑定, 因此指向相同file-entry的fd是共享一个锁的. 比如父子进程, 或者通过dup复制的fd, 由于指向相同file-entry, 因此共享一个锁. 这里就有一个问题: 假设两个进程A和B. 分别open lockfile. 两个fd指向不同的file-entry. 此时A和B进程对fd加锁, 锁并没有关联到相同file-entry. 可以推断,

算法题 深/广搜-07-Counting Sheep

余生颓废 提交于 2020-01-31 22:44:55
A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I’d gone to bed. As always when my grandmother suggests things, I decided to try it out. The only problem was, there were no sheep around to be counted when I went to bed. Creative as I am, that wasn’t going to stop me. I sat down and wrote a computer program that made a grid of characters, where # represents a sheep, while . is grass (or whatever you like, just not sheep). To make the counting a little more interesting, I also

fopen(file,w+) truncates the file before I can check if it's locked with flock()

我是研究僧i 提交于 2020-01-11 03:16:05
问题 I have a function which receives a filename and a json object to write to a text file. The object is updated and needs to entirely replace the current contents of the file. Each site visitor has their own file. Multiple rapid changes create a situation where the file is truncated by fopen(file,w+) , then not written to as it's locked. End result is empty file. I'm sure there's a standard simply way to do this as it's such a usual activity. Ideally what I'm looking for is a way to check if a

Difference between return value of non-blocking flock function and the $wouldblock argument?

泄露秘密 提交于 2020-01-03 08:55:08
问题 I'm trying to understand non blocking flock and the wouldblock argument $fp = fopen('/tmp/lock.txt', 'r+'); if(flock($fp, LOCK_EX | LOCK_NB, $wouldblock)) { echo 'Lock obtained'; } else{ echo 'Unable to obtain lock'; } fclose($fp); Documentation says about wouldblock: The optional third argument is set to 1 if the lock would block (EWOULDBLOCK errno condition). Reproducing in a test enviroment the concurrent condition, if another process has gained lock, the flock function will immeditatly

PHP - Restrict cron job overlap with flock()

蹲街弑〆低调 提交于 2020-01-03 01:47:09
问题 I have a php script that processes and creates lots of images which is being run every 5 minutes using cron job. I want to be able to limit this so it can only run once at a time and not overlap if each run takes longer than 5 minutes. flock() seems like the best way to achieve this but i am struggling to understand how exactly i should add this into my existing script. My cron job is setup to run the following file - images.php: $array=array("Volvo","BMW","Toyota","Audi","Ford","Alfa",

flock locking order?

拜拜、爱过 提交于 2020-01-01 15:37:35
问题 im using a simple test script from http://www.tuxradar.com/practicalphp/8/11/0 like this <?php $fp = fopen("foo.txt", "w"); if (flock($fp, LOCK_EX)) { print "Got lock!\n"; sleep(10); flock($fp, LOCK_UN); } i opened 5 shell's and executed the script one after the other the scripts block until the lock is free'ed and then continues after released im not really interessted in php stuff, but my question is: anyone knows the order in which flock() is acquired? e.g. t0: process 1 lock's t1: process

LOCK_NB Ignored

穿精又带淫゛_ 提交于 2020-01-01 05:25:07
问题 running this code twice : $fp = @fopen('test.test', "wb"); if (flock($fp, LOCK_NB | LOCK_EX)){ @fwrite($fp, $data); echo 'written'; sleep(5); }else{ echo 'skipped , ok'; } @flock($fp, LOCK_UN); @fclose($fp); always gives me the output of "written" Means the LOCK_NB is skipped , any clues (on both winbdows and unix) EDIT (2012-03-29 still not fixed): https://bugs.php.net/bug.php?id=54453&edit=3 PHP Bug #54453 回答1: When using Apache+PHP I was tricked into believing LOCK_NB was ignored (it wasn

BASH local and flock

岁酱吖の 提交于 2019-12-24 05:53:00
问题 I try to use a flock like here https://stackoverflow.com/a/169969 but within a function ... and I try to update a local variable (locale to the function) from within the flock part, but it seems not update ... cat test.sh #!/bin/bash function _job_worker() { local z=1 local result= ( # Wait for lock on /var/lock/.manager.exclusivelock (fd 200) flock -x -w 10 200 || return z=2 echo "slot equal $z" ) 200>/var/lock/.manager.exclusivelock echo "slot equal $z" } _job_worker ./test.sh slot equal 2