PHP Lockdown Overview

Source: Internet
Author: User
Tags flock

1, why should I lock it?

When there is a small amount of access to a feature, you can add log directly and then determine whether the operation was done based on the record. But once I was working on a batch of data--Scheduling tasks
In the automatic execution of that script, customer service in the manual execution of the script, the result create_time the same number of seconds, this time the query results and if statement is not limited, need to use
Lock.
When multiple scripts concurrently access the server side at the same time, the access script will be incorrect, the problem needs to be locked (locking mechanism) to resolve.
2,mysql Lock
(1), read lock, lock process all clients read this table only.
(2), write lock, only the locked table customer service can operate, the other can only be released to this lock release.
Locking: LOCK Table Table 1 read| WRITE, table 2 read| WRITE ...
Unlock: UNLOCK TABLES (TABLES unlock multiple tables)
3, the file lock code is as follows:
Build a file
$f = fopen ('/tmp/'. $task [' id ']. ". txt", "w+");
Locked
Flock ($f, LOCK_EX);
Here is the business code
The middle code can only be accessed by one person at a time, and the principle is to build a file allowing only one person to enter and exit
Release lock
Flock ($f, LOCK_EX);
Fclose ($f);
deleting files
Unlink ('/tmp/'. $task [' id ']. ". TXT ");

PHP Lockdown Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.