PHP????????????????
???????????? ???????[ 2015/8/21 13:52:51 ] ?????????????
????????????в??????????????????????PHP????????????????????????????ζ?????е??????????????????????????????綩???????????????У?????????????????????????
?????????????????????????????PHP??????????????????????????
????????????????????????????
????1 mysql> select * from counter;
????2 +----+-----+
????3 | id | num |
????4 +----+-----+
????5 | 1 | 0 |
????6 +----+-----+
????7 1 row in set (0.00 sec)
???????????????????????????
1 <?php
2 function dummy_business() {
3 $conn = mysqli_connect('127.0.0.1'?? 'public'?? 'public') or die(mysqli_error());
4 mysqli_select_db($conn?? 'test');
5 for ($i = 0; $i < 10000; $i++) {
6 mysqli_query($conn?? 'UPDATE counter SET num = num + 1 WHERE id = 1');
7 }
8 mysqli_close($conn);
9 }
10
11 for ($i = 0; $i < 10; $i++) {
12 $pid = pcntl_fork();
13
14 if($pid == -1) {
15 die('can not fork.');
16 } elseif (!$pid) {
17 dummy_business();
18 echo 'quit'.$i.PHP_EOL;
19 break;
20 }
21 }
22 ?>
?????????????????10??????????????????????????????????????????num???????1???????????????10000?β???????num????????100000??
??????????δ???num??????????????????????
????1 mysql> select * from counter;
????2 +----+--------+
????3 | id | num |
????4 +----+--------+
????5 | 1 | 100000 |
????6 +----+--------+
????7 1 row in set (0.00 sec)
??????????????????????????UPDATE??????????????????????У?num??????????100000??
??????????????£???????????????е???????????????????У????????????????????????
1 <?php
2 function dummy_business() {
3 $conn = mysqli_connect('127.0.0.1'?? 'public'?? 'public') or die(mysqli_error());
4 mysqli_select_db($conn?? 'test');
5 for ($i = 0; $i < 10000; $i++) {
6 $rs = mysqli_query($conn?? 'SELECT num FROM counter WHERE id = 1');
7 mysqli_free_result($rs);
8 $row = mysqli_fetch_array($rs);
9 $num = $row[0];
10 mysqli_query($conn?? 'UPDATE counter SET num = '.$num.' + 1 WHERE id = 1');
11 }
12 mysqli_close($conn);
13 }
14
15 for ($i = 0; $i < 10; $i++) {
16 $pid = pcntl_fork();
17
18 if($pid == -1) {
19 die('can not fork.');
20 } elseif (!$pid) {
21 dummy_business();
22 echo 'quit'.$i.PHP_EOL;
23 break;
24 }
25 }
26 ?>
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11