当前位置:网站首页>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');
边栏推荐
- What do you mean by waiting for insurance records? Where should I go for filing?
- Leetcode -- wrong set
- Linux MySQL installation
- 深入了解 border
- linux(centos7.9)安装部署mysql-cluster 7.6
- 【LeetCode】387. First unique character in string
- 支持向量机(SVC,NuSVC,LinearSVC)
- 怎么把mdf和ldf文件导入MySQL workbench中
- [noi Simulation Competition] send (tree DP)
- [use picgo+ Tencent cloud object to store cos as a map bed]
猜你喜欢
[e325: attention] VIM editing error
4275. Dijkstra sequence
Huawei Router: IPSec Technology
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
【LeetCode】541. Reverse string II
零基础自学SQL课程 | 子查询
Some common pitfalls in getting started with jupyter:
【LeetCode】415. String addition
学习太极创客 — ESP8226 (十二)ESP8266 多任务处理
Solution: the word of jmeter5.5 on the win11 lower interface is very small
随机推荐
Data midrange: analysis of full stack technical architecture of data midrange, with industry solutions
What do you mean by waiting for insurance records? Where should I go for filing?
Every (), map (), forearch () methods. There are objects in the array
零基础自学SQL课程 | HAVING子句
How to import MDF and LDF files into MySQL workbench
Data middle office: middle office practice and summary
4275. Dijkstra sequence
Installation of sophus package in slam14 lecture
Target of cmake command_ compile_ options
【LeetCode】541. Reverse string II
2022.6.13-6.19 AI行业周刊(第102期):职业发展
每周推荐短视频:计算的终极形态是“元宇宙”?
Linux (centos7.9) installation and deployment of MySQL Cluster 7.6
[bug] @jsonformat has a problem that the date is less than one day when it is used
【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
"I can't understand Sudoku, so I choose to play Sudoku."
Depens:*** but it is not going to be installed
The ambition of JD instant retailing from 618
Time series data augmentation for deep learning: paper reading of a survey
Tools