1--TEST-- 2sem_acquire with nowait 3--SKIPIF-- 4<?php // vim600: ts=4 sw=4 syn=php fdm=marker 5if(!extension_loaded('sysvsem') || !extension_loaded('pcntl')) { 6 die("skip sysvsem and pcntl required"); 7} 8?> 9--FILE-- 10<?php 11$SEMKEY = ftok(__FILE__, 'P'); // Semaphore key 12 13$pid = pcntl_fork(); 14 15if ($pid) { 16 echo "Parent.\n"; 17 18 pcntl_signal(SIGCHLD, SIG_IGN); 19 20 // Get semaphore 21 $sem_id = sem_get($SEMKEY, 1); 22 if ($sem_id === FALSE) { 23 echo "P: fail to get semaphore"; 24 exit; 25 } 26 echo "P: got semaphore $sem_id.\n"; 27 28 register_shutdown_function(function () use ($sem_id) { 29 echo "P: cleanup.\n"; 30 sem_remove($sem_id); 31 }); 32 33 // Acquire semaphore 34 if (! sem_acquire($sem_id)) { 35 echo "P: fail to acquire semaphore $sem_id.\n"; 36 sem_remove($sem_id); 37 exit; 38 } 39 echo "P: success acquire semaphore $sem_id.\n"; 40 41 usleep(20000); 42 43 echo "P: releases.\n"; 44 sem_release($sem_id); 45 46 usleep(5000); 47 48 // Acquire semaphore 49 if (! sem_acquire($sem_id)) { 50 echo "P: fail to acquire semaphore $sem_id.\n"; 51 sem_remove($sem_id); 52 exit; 53 } 54 echo "P: success acquire semaphore $sem_id.\n"; 55 56 $status = null; 57 pcntl_waitpid($pid, $status); 58 59} else { 60 usleep(10000); 61 echo "Child.\n"; 62 63 // Get semaphore 64 $sem_id = sem_get($SEMKEY, 1); 65 if ($sem_id === FALSE) { 66 echo "C: fail to get semaphore"; 67 exit; 68 } 69 echo "C: got semaphore $sem_id.\n"; 70 71 // Acquire semaphore 72 if (! sem_acquire($sem_id)) { 73 echo "C: fail to acquire semaphore $sem_id.\n"; 74 exit; 75 } 76 echo "C: success acquire semaphore $sem_id.\n"; 77 78 echo "C: releases.\n"; 79 sem_release($sem_id); 80 81 usleep(10000); 82 83 // Acquire semaphore 84 if (! sem_acquire($sem_id, true)) { 85 echo "C: fail to acquire semaphore $sem_id.\n"; 86 exit; 87 } 88 echo "C: success acquire semaphore $sem_id.\n"; 89} 90 91?> 92--EXPECTF-- 93Parent. 94P: got semaphore Resource id #%i. 95P: success acquire semaphore Resource id #%i. 96Child. 97C: got semaphore Resource id #%i. 98P: releases. 99C: success acquire semaphore Resource id #%i. 100C: releases. 101P: success acquire semaphore Resource id #%i. 102C: fail to acquire semaphore Resource id #%i. 103P: cleanup. 104