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