In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "problem description of php multi-process simulation concurrent transactions". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
Through the example code, this paper introduces some problems caused by php multi-process simulation concurrent transactions, which are shared for your reference and study. I won't say much below, let's take a look at the detailed introduction.
Table
Drop table if exists `test`; create table if not exists `test` (id int not null auto_increment, count int default 0, primary key `id` (`id`)) engine=innodb character set utf8mb4 collate = utf8mb4_bin comment 'test table'; insert into test (`count`) values
Php code
/ / the number of processes $pro_count = 100 per capita pids = []; for ($I = 0 per $I
< $pro_count; ++$i){ $pid = pcntl_fork(); if ($pid < 0) { // 主进程 throw new Exception('创建子进程失败: ' . $i); } else if ($pid >0) {/ / main process $pids [] = $pid;} else {/ / Child process try {$pdo = new PDO (...); $pdo- > beginTransaction (); $stmt = $pdo- > query ('select `count` from test'); $count = $stmt- > fetch (PDO::FETCH_ASSOC) [' count']; $count = intval ($count); if ($count > 0) {$count--; $pdo- > query ('update test set `count` ='. $count. ' Where id = 2');} $pdo- > commit ();} catch (Exception $e) {$pdo- > rollBack (); throw $e;} / / exit child process exit;}
Expected result
It is expected that the amount of reduction in the count field is more than 100 and becomes negative! That is to say, more reduction!
Actual result
In the case of 200 parallel, the results of multiple runs are as follows:
1. Count = 65
2. Count = 75
3. Count = 55
4. Count = 84
...
It is a far cry from the expected result! Why is there such a phenomenon?
explain
First of all, be clear about the current program running environment, concurrent scenarios. What is concurrency, executed almost at the same time, is called concurrency. The specific explanation is as follows:
The process gets updates
1-40 create and run 100 99 at the same time
41-80 simultaneously create and run 99 98
81-100 simultaneously create and run 98 97
To explain the first line above, the 1st-40th subprocesses are created and run almost at the same time:
Process 1 gets count = 100, update 99
Process 2 gets count = 100, update 99
...
Process 40 gets count = 100, update 99
So, in fact, these processes have done consistent operations, not as expected: process 1 gets the count=100 and updates 99; process 2 gets the updated result count=99 of process 1, updates 98. Process 99 gets the updated count=1 of process 98, and updates 0.
The phenomenon is reduced!
Conclusion
Using the above approach to achieve the program, inventory is always > = 0.
Doubt
So how to design the program to simulate the scenario of overstock?
Still using the above code, put the following code:
If ($count > 0) {$count--; $pdo- > query ('update test set `count` ='. $count. ' Where id = 2');}
Modify it to look like this:
If ($count > 0) {$pdo- > query ('update test set `count` = `count`-1 where id = 2');}
As a result, there will be excess inventory!
Inventory 100, concurrently 200, the final inventory is reduced to-63. Why did this happen? The following describes the specific process of running the program
Process 1 acquires inventory 100, updates 99
Process 2 acquires inventory 100, updates 98 (99-1)
Process 3 acquires inventory 100, updates 97 (98-1)
....
Process 168 acquires inventory 1, updates 0 (1-1)
Process 169 get inventory 1, update-1 (0-1)
Process 170 acquires inventory 1, updates-2 (- 1-1)
....
Process 200 acquires inventory 1, updates-63 (- 62-1)
Now it seems to be very confused, but it is actually caused by the following sentence:
$pdo- > query ('update test set `count` = `count`-1 where id = 2')
Here, process 1 is described in detail, referred to as a; process 2, referred to as b, their specific execution order:
1. A query to inventory 100
2. B query to inventory 100
3. A update inventory to 99 (100-1), this should be understood in seconds
4. B Update inventory to 98 (99-1)
-b gets a updated inventory when performing the update operation!
-Why is that? Because the update statement is `update test set count = count-1 where id = 2`
This is the end of the description of the problems caused by php multi-process simulation of concurrent transactions. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.