flock

星月流心

匿名 (未验证) 提交于 2019-12-02 22:56:40
1、一段PHP代码,确保多个进程同时写入同一个文件成功。 解答: 加锁 <?php $fp = fopen( "lock.txt" , "w+" ); if (flock( $fp ,LOCK_EX)) { //获得写锁,写数据 fwrite( $fp , "write something" ); // 解除锁定 flock( $fp , LOCK_UN); } else { echo "file is locking..." ; } fclose( $fp ); ?> 文章来源: 星月流心

Locking Files in Bash

陌路散爱 提交于 2019-12-01 21:17:42
I have a Problem to find a good concept on locking files in bash, Basically I want to achieve the following: Lock File Read in the data in the file (multiple times) Do stuff with the data. Write new stuff to the file (not necessarily to the end) Unlock that file Doing this with flock seems not possible to me, because the file descriptor will just move once to the end of the file. Also creating a Tempfile fails, because I might overwrite already read lines which is also not possible. Edit: Also note that other scripts I do not control might try to write to that file. So my question is how can I

Having issues with flock() function

懵懂的女人 提交于 2019-12-01 14:03:04
I have a question about how flock() works, particularly in python. I have a module that opens a serial connection (via os.open() ). I need to make this thread safe. It's easy enough making it thread safe when working in the same module using threading.Lock() , but if the module gets imported from different places, it breaks. I was thinking of using flock() , but I'm having trouble finding enough information about how exactly flock works. I read that flock() unlocks the file once the file is closed. But is there a situation that will keep the file open if python crashes? And what exactly is

Use flock() in the sigaction handler

倖福魔咒の 提交于 2019-12-01 06:39:05
flock() is generally async-signal-safe because it is a system call. Its semantics make it hard to implement it differently. It is not in the POSIX's list of async-signal-safe functions because it is not in POSIX at all. Is it possible to use flock() in the sigaction handler without problems? MOHAMED According to @alk answer in the following topic : We can develop our property flock() function (its name could be async_flock() ). we copy in this new function the content of the origin lockf() code and then we make the following changes in order to get an async-signal-safe function: replace _

flock-ing a C++ ifstream on Linux (GCC 4.6)

怎甘沉沦 提交于 2019-12-01 06:01:31
context I'm slowly writing a specialized web server application in C++ (using the C onion http server library and the JSONCPP library for JSON serialization, if that matters)., for a Linux system with GCC 4.6 compiler (I don't care about portability to non Linux systems, or to GCC before 4.5 or to Clang before 3.0). I decided to keep the user "database" (there will be very few users, probably one or two, so performance is not a concern, and O(n) access time is acceptable) in JSON format, probably as a small array of JSON objects like { "_user" : "basile" ; "_crypasswd" : "XYZABC123" ; "_email"

php 文件锁

泄露秘密 提交于 2019-12-01 05:05:59
<?php class Order{ /** * 阻塞模式(后面的进程会一直等待前面的进程执行完毕) */ public function createOrder1(){ $file = fopen(__DIR__.'/lock.txt','w+'); //加锁 if(flock($file,LOCK_EX)){ //TODO 执行业务代码 sleep(5); echo time(); flock($file,LOCK_UN);//解锁 } //关闭文件 fclose($file); } /** * 非阻塞模式(只要当前文件有锁存在,那么直接返回) */ public function createOrder2(){ $file = fopen(__DIR__.'/lock.txt','w+'); //加锁 if(flock($file,LOCK_EX|LOCK_NB)){ //TODO 执行业务代码 sleep(5); echo time(); flock($file,LOCK_UN);//解锁 }else{ //TODO 执行业务代码 返回系统繁忙等错误提示 echo "系统繁忙"; } //关闭文件 fclose($file); } } $a = new Order(); $a->createOrder1(); 来源: https://www.cnblogs.com

Check if a file is already locked using flock()? [duplicate]

∥☆過路亽.° 提交于 2019-12-01 04:14:25
This question already has an answer here: Test if file is locked 2 answers I have a file I'm writing to, but I need to lock it first (using flock() ), to prevent any other script from writing to it. So I have: $file=fopen($file_p); if (flock($file, LOCK_EX)) {//lock was successful fwrite($file,$write_contents); } But I need to check if it's already locked, to prevent other scripts from writing to it. How can I do this? I would check to see if I couldn't obtain a lock on the file, like this: if (!flock($file, LOCK_EX)) { throw new Exception(sprintf('Unable to obtain lock on file: %s', $file));

Use flock() in the sigaction handler

妖精的绣舞 提交于 2019-12-01 03:51:31
问题 flock() is generally async-signal-safe because it is a system call. Its semantics make it hard to implement it differently. It is not in the POSIX's list of async-signal-safe functions because it is not in POSIX at all. Is it possible to use flock() in the sigaction handler without problems? 回答1: According to @alk answer in the following topic: We can develop our property flock() function (its name could be async_flock() ). we copy in this new function the content of the origin lockf() code

How do I use the linux flock command to prevent another root process from deleting a file?

Deadly 提交于 2019-12-01 03:37:01
I would like to prevent one of my root processes from deleting a certain file. So I came across the flock command, it seems to fit my need, but I didn't get its syntax. If I only indicate a shared lock, it doesn't work: flock -s "./file.xml" If I add a timeout parameter, it still doesn't work: flock -s -w5 "./file.xml" It seems that way, it fits in flock [-sxun][-w #] fd# way. (What is this fd# parameter?) So, I tried: flock [-sxon][-w #] file [-c] command Using flock -s -w5 "./file.xml" -c "tail -3 ./file.xml" and it worked, tail command at ./file.xml was executed. But I would like to know,

Check if a file is already locked using flock()? [duplicate]

核能气质少年 提交于 2019-12-01 01:35:30
问题 This question already has answers here : Test if file is locked (2 answers) Closed 3 years ago . I have a file I'm writing to, but I need to lock it first (using flock() ), to prevent any other script from writing to it. So I have: $file=fopen($file_p); if (flock($file, LOCK_EX)) {//lock was successful fwrite($file,$write_contents); } But I need to check if it's already locked, to prevent other scripts from writing to it. How can I do this? 回答1: I would check to see if I couldn't obtain a