Annotation of embedaddon/php/ext/sysvsem/tests/sysv.phpt, revision 1.1.1.3

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>