当前位置:网站首页>php文件锁
php文件锁
2022-06-24 08:08:00 【BigChen_up】
cpu的线程: 就代表同一时间可以做多少件事。资源独占: 某个资源同一时间只能一个线程使用, 那就需要通过添加 排他锁, 来防止其他人使用。共享锁: 一个资源可以被很多人查看, 但是查看的时候不能改。
//写文件的时候, 就需要添加 排他锁, 放置其他线程同时写入出现错乱
function put($path, $data) {
$handle = fopen($path, 'w');
//flock(); 加锁. 参数2代表锁的类型.
//LOCK_EX: 排他锁
if (flock($handle, LOCK_EX)) {
//写入内容
fwrite($handle, $data);
//LOCK_UN: 解锁
flock($handle, LOCK_UN);
}
fclose($handle);
}
//共享锁: 文件在被阅读的时候, 其他线程只能读不能改
function get($path) {
$handle = fopen($path, 'r');
//LOCK_SH: share共享
if (flock($handle, LOCK_SH)) {
//fread() 按字节读
$text = fread($handle, filesize($path));
//解锁
flock($handle, LOCK_UN);
}
fclose($handle);
return $text;
}
// echo get('d:/php7.0/php.ini');
put('abc.txt', 'AAAAAAAAAAA');
边栏推荐
- Redis实现全局唯一ID
- Huawei Router: GRE Technology
- Lu Qi: I am most optimistic about these four major technology trends
- CF566E-Restoring Map【bitset】
- How to import MDF and LDF files into MySQL workbench
- Data middle office: middle office architecture and overview
- 12、 Demonstration of all function realization effects
- Solution: the word of jmeter5.5 on the win11 lower interface is very small
- Directly applicable go coding specification
- 牛客网 实现简单计算器功能
猜你喜欢

解决:jmeter5.5在win11下界面上的字特别小

CDGA|到底怎么才能做好数据治理呢?

Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?

Huawei Router: GRE Technology

【LeetCode】415. String addition
![[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)](/img/b3/b156d75c7b4f03580c449f8499cd74.png)
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)

【Redis實現秒殺業務①】秒殺流程概述|基本業務實現

Cdga | how can we do well in data governance?

2022.6.13-6.19 AI行业周刊(第102期):职业发展

CF566E-Restoring Map【bitset】
随机推荐
Webrtc series - network transmission 5: select the optimal connection switching
Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing
PM2 deploy nuxt3 JS project
P6117-[JOI 2019 Final]コイン集め【贪心】
NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读
2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
MySQL data (Linux Environment) scheduled backup
零基础自学SQL课程 | 子查询
198. house raiding
零基础自学SQL课程 | HAVING子句
Spark - the number of leftouterjoin results is inconsistent with that of the left table
The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
解决:jmeter5.5在win11下界面上的字特别小
L01_一条SQL查询语句是如何执行的?
Vidéo courte recommandée chaque semaine: Soyez sérieux en parlant de "métaunivers"
【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
Mba-day25 best value problem - application problem
[noi Simulation Competition] geiguo and time chicken (structure)
Framework tool class obtained by chance for self use
Every (), map (), forearch () methods. There are objects in the array