nowait.phpt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. --TEST--
  2. Test sem_acquire with nowait option
  3. --EXTENSIONS--
  4. sysvsem
  5. pcntl
  6. --FILE--
  7. <?php
  8. $P_SEMKEY = ftok(__FILE__, 'P'); // Parent Semaphore key
  9. $C_SEMKEY = ftok(__FILE__, 'C'); // Child Semaphore key
  10. echo "P: parent process running.\n";
  11. pcntl_signal(SIGCHLD, SIG_IGN);
  12. // Get semaphore for parent
  13. $p_sem_id = sem_get($P_SEMKEY, 1);
  14. if ($p_sem_id === FALSE) {
  15. echo "P: failed to parent get semaphore.\n";
  16. exit;
  17. }
  18. echo "P: got semaphore.\n";
  19. // Get semaphore for child
  20. $c_sem_id = sem_get($C_SEMKEY, 1);
  21. if ($c_sem_id === FALSE) {
  22. echo "P: failed to child get semaphore.\n";
  23. exit;
  24. }
  25. // Acquire semaphore for parent
  26. if (!sem_acquire($p_sem_id)) {
  27. echo "P: fail to acquire semaphore.\n";
  28. sem_remove($p_sem_id);
  29. exit;
  30. }
  31. echo "P: acquired semaphore.\n";
  32. // Acquire semaphore for child
  33. if (!sem_acquire($c_sem_id)) {
  34. echo "P: failed to acquire semaphore.\n";
  35. sem_remove($c_sem_id);
  36. exit;
  37. }
  38. echo "P: acquired semaphore.\n";
  39. // Fork process
  40. $pid = pcntl_fork();
  41. if ($pid) {
  42. register_shutdown_function(function () use ($p_sem_id) {
  43. echo "P: removing semaphore.\n";
  44. sem_remove($p_sem_id);
  45. });
  46. // Release semaphore after 50ms
  47. usleep(50000);
  48. /* Wait for the child semaphore to be released to
  49. to release the parent semaphore */
  50. if (!sem_acquire($c_sem_id)) {
  51. echo "P: failed to acquire semaphore.\n";
  52. exit;
  53. }
  54. /* Release the child semahpore before releasing
  55. the releasing the parent semaphore and letting
  56. the child continue execution */
  57. sem_release($c_sem_id);
  58. echo "P: releasing semaphore.\n";
  59. if (!sem_release($p_sem_id)) {
  60. echo "P: failed to release semaphore.\n";
  61. }
  62. $status = null;
  63. pcntl_waitpid($pid, $status);
  64. } else {
  65. register_shutdown_function(function () use ($c_sem_id) {
  66. echo "C: removing semaphore.\n";
  67. sem_remove($c_sem_id);
  68. });
  69. echo "C: child process running.\n";
  70. // Have the semaphore after process forked
  71. echo "C: got semaphores.\n";
  72. // This should fail to get to the semaphore and not wait
  73. if (sem_acquire($p_sem_id, true)) {
  74. echo "C: test failed, Child was able to acquire semaphore.\n";
  75. exit;
  76. }
  77. // The child process did not wait to acquire the semaphore
  78. echo "C: failed to acquire semaphore.\n";
  79. echo "C: releasing semaphore.\n";
  80. if (!sem_release($c_sem_id)) {
  81. echo "C: failed to release semaphore.\n";
  82. }
  83. // Acquire semaphore with waiting
  84. if (!sem_acquire($p_sem_id)) {
  85. echo "C: fail to acquire semaphore.\n";
  86. exit;
  87. }
  88. echo "C: success acquired semaphore.\n";
  89. echo "C: releasing semaphore.\n";
  90. sem_release($p_sem_id);
  91. }
  92. ?>
  93. --EXPECT--
  94. P: parent process running.
  95. P: got semaphore.
  96. P: acquired semaphore.
  97. P: acquired semaphore.
  98. C: child process running.
  99. C: got semaphores.
  100. C: failed to acquire semaphore.
  101. C: releasing semaphore.
  102. P: releasing semaphore.
  103. C: success acquired semaphore.
  104. C: releasing semaphore.
  105. C: removing semaphore.
  106. P: removing semaphore.