加上文件锁后的下单处理代码:

【一】.阻塞模式:(如果其他进程已经加锁文件,当前进程会一直等其他进程解锁文件后继续执行)

<?php
//连接数据库
$con=mysqli_connect("192.168.2.186","root","root","test");

//查询商品数量是否大于0,大于0才能下单,并减少库存

$fp = fopen("lock.txt", "r");
//加锁
if(flock($fp,LOCK_EX))
{
$res=mysqli_fetch_assoc(mysqli_query($con,'SELECT total FROM shop WHERE id=1 LIMIT 1'));
if($res['total']>0){mysqli_query($con,'UPDATE shop SET total=total-1 WHERE id=1');}
//执行完成解锁
flock($fp,LOCK_UN);
}
//关闭文件
fclose($fp);
unset($res);
mysqli_close($con);
?>

【二】.非阻塞模式:(如果其他进程已经加锁文件,当前进程不会等其他进程解锁文件,直接返回,也就是直接忽略加锁的代码到关闭文件那块)

<?php
//连接数据库
$con=mysqli_connect("192.168.2.186","root","root","test");

//查询商品数量是否大于0,大于0才能下单,并减少库存

$fp = fopen("lock.txt", "r");
//加锁
if(flock($fp,LOCK_EX | LOCK_NB))
{
$res=mysqli_fetch_assoc(mysqli_query($con,'SELECT total FROM shop WHERE id=1 LIMIT 1'));
if($res['total']>0){mysqli_query($con,'UPDATE shop SET total=total-1 WHERE id=1');}
//执行完成解锁
flock($fp,LOCK_UN);
}
//关闭文件
fclose($fp);
unset($res);
mysqli_close($con);
?>

使用ab.exe同样500并发,数据库的库存保持为0,成功解决