PHP文件锁用法详解
PHP出现文件锁与mysql表锁有大概想的用法,就是同一时间只能让一个人操作,这样就避免了同时有多个人操作同一文件,这样导致数据丢失的情况了,下面我来给大家介绍PHP文件锁用法。
PHP自带了文件锁函数:
bool flock ( int $handle , int $operation [, int &$wouldblock ] )
$handle 是打开的文件指针;
$operation 可以是
“LOCK_SH”,共享锁定;“LOCK_EX”,独占锁定;“LOCK_UN”,释放锁定;“LOCK_NB”,防止flock锁定时堵塞。
这里主要说说“LOCK_EX”和“LOCK_NB”。
比如我们有两个文件,如下。
flocka.php
代码如下 | 复制代码 |
$file = 'temp.txt'; |
flockb.php
代码如下 | 复制代码 |
$file = 'temp.txt'; $fp = fopen($file,'a'); for($i = 0;$i { fwrite($fp, "22222222n"); } fclose($fp); |
先运行flocka.php,然后马上运行flockb.php。
结果:
11111111
22222222
22222222
22222222
22222222
22222222
11111111
11111111
11111111
11111111
说明不加文件锁时,两个文件会同时对txt文件进行写入操作。
下面修改一下两个php文件的代码。
flocka.php
代码如下 | 复制代码 |
$file = 'temp.txt'; $fp = fopen($file,'a'); if(flock($fp,LOCK_EX)) { for($i = 0;$i { fwrite($fp, "11111111n"); sleep(1); } flock($fp,LOCK_UN); } fclose($fp); |
flockb.php
代码如下 | 复制代码 |
$file = 'temp.txt'; $fp = fopen($file,'a'); if(flock($fp,LOCK_EX)) { for($i = 0;$i { fwrite($fp, "22222222n"); } flock($fp,LOCK_UN); } fclose($fp); |
同样先运行flocka.php,然后马上运行flockb.php。
会发现在flocka.php运行结束前,flockb.php一直处于等待状态,只有当flocka.php运行结束后,flockb.php才会继续执行。
输出结果:
11111111
11111111
11111111
11111111
11111111
22222222
22222222
22222222
22222222
22222222
另外,在执行flock时,文件锁会自动释放。
还有一种办法
如下代码简单模拟了这种事务并发状态: process1.php
代码如下 | 复制代码 |
$num = 100; $filename = "processdata.txt"; $fp = fopen($filename, "a"); for ($i = 0; $i fwrite($fp, "process1: " . $i . "rn"); usleep(100000); } fclose($fp); ?> |
我们需要先执行第一个事务,在processdata.txt文件中写入这100行。
process2.php
代码如下 | 复制代码 |
$num = 100; |
第二个事务,继续向processdata.txt文件中写入100行。
第二个事务,继续向processdata.txt文件中写入100行。
多次同时执行,虽然都写了100行,但是事务1和事务2的数据交错写入,这并不是我们想要的结果。我们要的是事务完整的执行,此时我们需要有个机制去保证在第一个事务执行完后再执行第二个。在PHP中,flock函数完成了这一使命。在事物1和事务2的循环前面都加上: flock($fp, LOCK_EX); 就能满足我们的需求,将两个事务串行。
当某一个事务执行完flock时,因为我们在这里添加的是LOCK_EX(独占锁定),所以所有对资源的操作都会被阻塞,只有当事务执行完成后,后面的事务才会执行。我们可以通过输出当前的时间的方法来确认这一点。
关于在尾部追加写入,在unix系统的早期版本中存在一个并发写入的问题,如果要在尾部追加,需要先lseek位置,再write。当多个进程同时操作时,会因为并发导致的覆盖写入的问题,即两个进程同时获取尾部的偏移后,先后执行write操作,后面的操作会将前面的操作覆盖。这个问题在后面以添加打开时的O_APPEND操作而得到解决,它将查找和写入操作变成了一个原子操作。
在PHP的fopen函数的实现中,如果我们使用a参数在文件的尾部追加内容,其调用open函数中oflag参数为 O_CREAT|O_APPEND,即我们使用追加操作不用担心并发追加写入的问题。
在PHP的session默认存储实现中也用到了flock文件锁,当session开始时就调用PS_READ_FUNC,且以O_CREAT | O_RDWR | O_BINARY 打开session数据文件,此时会调用flock加上写锁,如果此时有其它进程访问此文件(即同一用户再次发起对当前文件的请求),就会显示页面加载中,进程被阻塞了。加写锁其出发点是为了保证此次会话中对session的操作事务能完整的执行,防止其它进程的干扰,保证数据的一致性。如果一个页面没有session修改操作,可以尽早的调用session_write_close()释放锁。
文件锁是针对文件的锁,除了这种释义,还可以理解为用文件作为锁。在实际工作中,有时为确保单个进程的执行,我们会在程序执行前判断文件是否存在,如果不存在则创建一个空文件,在进程结束后删除这个空文件,如果存在,则不执行。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
