进程互斥问题?

作者站长头像
站长
· 阅读数 14
<?php
ini_set('display_errors',1);

ini_set('display_startup_errors',1);

error_reporting(-1);

$file = "/www/tp/signal.txt";
$key  = ftok($file, "x");
$semaphore = sem_get($file, 1, 0666, false);
sem_acquire($semaphore);
echo "hello world";
sleep(20);

我写了一个简单的互斥信号量程序,在两个终端中打开,在没有主动释放信号量的同时,当一个进程结束为什么另一个进程没有被阻塞而是拿到了信号量?进程结束会主动释放信号量吗(程序里没有释放信号量的操作)?希望有大佬帮忙解答一下,为什么两个进程都正常运行了,谢谢!进程互斥问题?

回复
1个回答
avatar
test
2024-06-29

sem_acquire

sem_acquire() by default blocks (if necessary) until the semaphore can be acquired. A process attempting to acquire a semaphore which it has already acquired will block forever if acquiring the semaphore would cause its maximum number of semaphore to be exceeded.

After processing a request, any semaphores acquired by the process but not explicitly released will be released automatically and a warning will be generated.

请求处理完会自动释放。


操作系统层面上,参见sem_close

All open named semaphores are automatically closed on process termination, or upon execve(2).
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容