当前位置:网站首页>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');
边栏推荐
- Target detection series fast r-cnn
- leetcode--字符串
- Pytoch read data set (two modes: typical data set and user-defined data set)
- [ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
- Dynamic saving and recovery of FPU context under risc-v architecture
- I heard that you are still spending money to buy ppt templates from the Internet?
- 支持向量机(SVC,NuSVC,LinearSVC)
- Applet wx show
- The ambition of JD instant retailing from 618
- Depens:*** but it is not going to be installed
猜你喜欢

Ebanb B1 Bracelet brush firmware abnormal interrupt handling

"I can't understand Sudoku, so I choose to play Sudoku."

Kaformer personal notes

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

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

4275. Dijkstra sequence
![[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization](/img/a3/9a50e83ece43904a3a622dcdb05b3c.png)
[redis realize Secondary killing Business ①] Overview of Secondary killing Process | Basic Business Realization

MySQL data (Linux Environment) scheduled backup

Data middle office: middle office architecture and overview

Data middle office: detailed explanation of technical architecture of data middle office
随机推荐
牛客网 十进制整数转十六进制字符串
MYCAT read / write separation and MySQL master-slave synchronization
Numpy numpy中的np.c_和np.r_详解
【bug】@JsonFormat 使用时出现日期少一天的问题
Easyexcel single sheet and multi sheet writing
Redis实现全局唯一ID
学习太极创客 — ESP8226 (十三)OTA
4275. Dijkstra sequence
Lu Qi: I am most optimistic about these four major technology trends
【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
Kaformer personal notes
Data middle office: detailed explanation of technical architecture of data middle office
可直接套用的Go编码规范
PM2 deploy nuxt3 JS project
Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing
198. 打家劫舍
Squid proxy application
普通人没有学历,自学编程可以月入过万吗?
Common emoticons
零基础自学SQL课程 | 子查询