Annotation of embedaddon/php/ext/sysvsem/tests/sysv.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: General semaphore and shared memory test
! 3: --SKIPIF--
! 4: <?php // vim600: ts=4 sw=4 syn=php fdm=marker
! 5: if(!extension_loaded('sysvsem') || !extension_loaded('sysvshm')) {
! 6: die("skip Both sysvsem and sysvshm required");
! 7: }
! 8: ?>
! 9: --INI--
! 10: magic_quotes_runtime=0
! 11: --FILE--
! 12: <?php
! 13: $MEMSIZE = 512; // size of shared memory to allocate
! 14: $SEMKEY = 1; // Semaphore key
! 15: $SHMKEY = 2; // Shared memory key
! 16:
! 17: echo "Start.\n";
! 18: // Get semaphore
! 19: $sem_id = sem_get($SEMKEY, 1);
! 20: if ($sem_id === FALSE) {
! 21: echo "Fail to get semaphore";
! 22: exit;
! 23: }
! 24: echo "Got semaphore $sem_id.\n";
! 25:
! 26: // Accuire semaphore
! 27: if (! sem_acquire($sem_id)) {
! 28: echo "Fail to aquire semaphore $sem_id.\n";
! 29: sem_remove($sem_id);
! 30: exit;
! 31: }
! 32: echo "Success aquire semaphore $sem_id.\n";
! 33:
! 34: $shm_id = shm_attach($SHMKEY, $MEMSIZE);
! 35: if ($shm_id === FALSE) {
! 36: echo "Fail to attach shared memory.\n";
! 37: sem_remove($sem_id);
! 38: exit;
! 39: }
! 40: echo "Success to attach shared memory : $shm_id.\n";
! 41:
! 42: // Write variable 1
! 43: if (!shm_put_var($shm_id, 1, "Variable 1")) {
! 44: echo "Fail to put var 1 on shared memory $shm_id.\n";
! 45: sem_remove($sem_id);
! 46: shm_remove ($shm_id);
! 47: exit;
! 48: }
! 49: echo "Write var1 to shared memory.\n";
! 50:
! 51: // Write variable 2
! 52: if (!shm_put_var($shm_id, 2, "Variable 2")) {
! 53: echo "Fail to put var 2 on shared memory $shm_id.\n";
! 54: sem_remove($sem_id);
! 55: shm_remove ($shm_id);
! 56: exit;
! 57: }
! 58: echo "Write var2 to shared memory.\n";
! 59:
! 60: // Read variable 1
! 61: $var1 = shm_get_var ($shm_id, 1);
! 62: if ($var1 === FALSE) {
! 63: echo "Fail to retrieve Var 1 from Shared memory $shm_id, return value=$var1.\n";
! 64: } else {
! 65: echo "Read var1=$var1.\n";
! 66: }
! 67:
! 68: // Read variable 1
! 69: $var2 = shm_get_var ($shm_id, 2);
! 70: if ($var1 === FALSE) {
! 71: echo "Fail to retrieve Var 2 from Shared memory $shm_id, return value=$var2.\n";
! 72: } else {
! 73: echo "Read var2=$var2.\n";
! 74: }
! 75: // Release semaphore
! 76: if (!sem_release($sem_id)) {
! 77: echo "Fail to release $sem_id semaphore.\n";
! 78: } else {
! 79: echo "Semaphore $sem_id released.\n";
! 80: }
! 81:
! 82: // remove shared memory segmant from SysV
! 83: if (shm_remove ($shm_id)) {
! 84: echo "Shared memory successfully removed from SysV.\n";
! 85: } else {
! 86: echo "Fail to remove $shm_id shared memory from SysV.\n";
! 87: }
! 88:
! 89: // Remove semaphore
! 90: if (sem_remove($sem_id)) {
! 91: echo "semaphore removed successfully from SysV.\n";
! 92: } else {
! 93: echo "Fail to remove $sem_id semaphore from SysV.\n";
! 94: }
! 95: echo "End.\n";
! 96: /* NOTE: assigned semids differ depending on the kernel, since
! 97: * there are actually 3 semaphores per PHP-created
! 98: * semaphores in effect, to keep state information.
! 99: * That's the reason for EXPECTF.
! 100: */
! 101: ?>
! 102: --EXPECTF--
! 103: Start.
! 104: Got semaphore Resource id #%i.
! 105: Success aquire semaphore Resource id #%i.
! 106: Success to attach shared memory : %s.
! 107: Write var1 to shared memory.
! 108: Write var2 to shared memory.
! 109: Read var1=Variable 1.
! 110: Read var2=Variable 2.
! 111: Semaphore Resource id #%s released.
! 112: Shared memory successfully removed from SysV.
! 113: semaphore removed successfully from SysV.
! 114: End.
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>