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