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