xref: /PHP-5.4/ext/sysvsem/tests/sysv.phpt (revision ac40c0b5)
1--TEST--
2General semaphore and shared memory test
3--SKIPIF--
4<?php // vim600: ts=4 sw=4 syn=php fdm=marker
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	 =   1;  //  Semaphore key
13$SHMKEY	 =   2;  //  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 $sem_id.\n";
23
24// Accuire semaphore
25if (! sem_acquire($sem_id)) {
26	echo "Fail to acquire semaphore $sem_id.\n";
27	sem_remove($sem_id);
28	exit;
29}
30echo "Success acquire semaphore $sem_id.\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 : $shm_id.\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 $shm_id.\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 $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);
68if ($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
74if (!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
81if (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
88if (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}
93echo "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--
101Start.
102Got semaphore Resource id #%i.
103Success acquire semaphore Resource id #%i.
104Success to attach shared memory : %s.
105Write var1 to shared memory.
106Write var2 to shared memory.
107Read var1=Variable 1.
108Read var2=Variable 2.
109Semaphore Resource id #%s released.
110Shared memory successfully removed from SysV.
111semaphore removed successfully from SysV.
112End.
113