xref: /PHP-8.0/ext/sysvsem/tests/sysv.phpt (revision c5401854)
1--TEST--
2General semaphore and shared memory test
3--SKIPIF--
4<?php
5if(!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	 =   ftok(__FILE__, 'P');  //  Semaphore key
13$SHMKEY	 =   ftok(__FILE__, 'Q');  //  Shared memory key
14
15echo "Start.\n";
16// Get semaphore
17$sem_id = sem_get($SEMKEY, 1);
18if ($sem_id === FALSE) {
19    echo "Fail to get semaphore";
20    exit;
21}
22echo "Got semaphore.\n";
23
24// Accuire semaphore
25if (! sem_acquire($sem_id)) {
26    echo "Fail to acquire semaphore.\n";
27    sem_remove($sem_id);
28    exit;
29}
30echo "Success acquire semaphore.\n";
31
32$shm_id = shm_attach($SHMKEY, $MEMSIZE);
33if ($shm_id === FALSE) {
34    echo "Fail to attach shared memory.\n";
35    sem_remove($sem_id);
36    exit;
37}
38echo "Success to attach shared memory.\n";
39
40// Write variable 1
41if (!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}
47echo "Write var1 to shared memory.\n";
48
49// Write variable 2
50if (!shm_put_var($shm_id, 2, "Variable 2")) {
51    echo "Fail to put var 2 on shared memory.\n";
52    sem_remove($sem_id);
53    shm_remove ($shm_id);
54    exit;
55}
56echo "Write var2 to shared memory.\n";
57
58// Read variable 1
59$var1   =   shm_get_var ($shm_id, 1);
60if ($var1 === FALSE) {
61    echo "Fail to retrieve Var 1 from Shared memory, 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);
68if ($var1 === FALSE) {
69    echo "Fail to retrieve Var 2 from Shared memory, return value=$var2.\n";
70} else {
71    echo "Read var2=$var2.\n";
72}
73// Release semaphore
74if (!sem_release($sem_id)) {
75    echo "Fail to release semaphore.\n";
76} else {
77    echo "Semaphore released.\n";
78}
79
80// remove shared memory segmant from SysV
81if (shm_remove ($shm_id)) {
82    echo "Shared memory successfully removed from SysV.\n";
83} else {
84    echo "Fail to remove shared memory from SysV.\n";
85}
86
87// Remove semaphore
88if (sem_remove($sem_id)) {
89    echo "semaphore removed successfully from SysV.\n";
90} else {
91    echo "Fail to remove semaphore from SysV.\n";
92}
93echo "End.\n";
94?>
95--EXPECT--
96Start.
97Got semaphore.
98Success acquire semaphore.
99Success to attach shared memory.
100Write var1 to shared memory.
101Write var2 to shared memory.
102Read var1=Variable 1.
103Read var2=Variable 2.
104Semaphore released.
105Shared memory successfully removed from SysV.
106semaphore removed successfully from SysV.
107End.
108