summaryrefslogtreecommitdiff
path: root/ext/sysvsem/tests/sysv.phpt
blob: 14270ee608fa96b991688a3d5c8911a2cdd90ddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
--TEST--
General semaphore and shared memory test
--SKIPIF--
<?php
if(!extension_loaded('sysvsem') || !extension_loaded('sysvshm')) {
	die("skip Both sysvsem and sysvshm required");
}
?>
--FILE--
<?php
$MEMSIZE = 512;  //  size of shared memory to allocate
$SEMKEY	 =   ftok(__FILE__, 'P');  //  Semaphore key
$SHMKEY	 =   ftok(__FILE__, 'Q');  //  Shared memory key

echo "Start.\n";
// Get semaphore
$sem_id = sem_get($SEMKEY, 1);
if ($sem_id === FALSE) {
    echo "Fail to get semaphore";
    exit;
}
echo "Got semaphore.\n";

// Accuire semaphore
if (! sem_acquire($sem_id)) {
    echo "Fail to acquire semaphore.\n";
    sem_remove($sem_id);
    exit;
}
echo "Success acquire semaphore.\n";

$shm_id = shm_attach($SHMKEY, $MEMSIZE);
if ($shm_id === FALSE) {
    echo "Fail to attach shared memory.\n";
    sem_remove($sem_id);
    exit;
}
echo "Success to attach shared memory.\n";

// Write variable 1
if (!shm_put_var($shm_id, 1, "Variable 1")) {
    echo "Fail to put var 1 on shared memory $shm_id.\n";
    sem_remove($sem_id);
    shm_remove ($shm_id);
    exit;
}
echo "Write var1 to shared memory.\n";

// Write variable 2
if (!shm_put_var($shm_id, 2, "Variable 2")) {
    echo "Fail to put var 2 on shared memory.\n";
    sem_remove($sem_id);
    shm_remove ($shm_id);
    exit;
}
echo "Write var2 to shared memory.\n";

// Read variable 1
$var1   =   shm_get_var ($shm_id, 1);
if ($var1 === FALSE) {
    echo "Fail to retrieve Var 1 from Shared memory, return value=$var1.\n";
} else {
    echo "Read var1=$var1.\n";
}

// Read variable 1
$var2   =   shm_get_var ($shm_id, 2);
if ($var1 === FALSE) {
    echo "Fail to retrieve Var 2 from Shared memory, return value=$var2.\n";
} else {
    echo "Read var2=$var2.\n";
}
// Release semaphore
if (!sem_release($sem_id)) {
    echo "Fail to release semaphore.\n";
} else {
    echo "Semaphore released.\n";
}

// remove shared memory segmant from SysV
if (shm_remove ($shm_id)) {
    echo "Shared memory successfully removed from SysV.\n";
} else {
    echo "Fail to remove shared memory from SysV.\n";
}

// Remove semaphore
if (sem_remove($sem_id)) {
    echo "semaphore removed successfully from SysV.\n";
} else {
    echo "Fail to remove semaphore from SysV.\n";
}
echo "End.\n";
?>
--EXPECT--
Start.
Got semaphore.
Success acquire semaphore.
Success to attach shared memory.
Write var1 to shared memory.
Write var2 to shared memory.
Read var1=Variable 1.
Read var2=Variable 2.
Semaphore released.
Shared memory successfully removed from SysV.
semaphore removed successfully from SysV.
End.