进程互斥问题?

<?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);

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

阅读 2.4k
1 个回答

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).
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题