xref: /PHP-7.3/ext/sysvsem/tests/nowait.phpt (revision fa1f98f2)
1--TEST--
2Test sem_acquire with nowait option
3--SKIPIF--
4<?php // vim600: ts=4 sw=4 syn=php fdm=marker
5if(!extension_loaded('sysvsem') || !extension_loaded('pcntl')) {
6	die("skip sysvsem and pcntl required");
7}
8?>
9--FILE--
10<?php
11$P_SEMKEY = ftok(__FILE__, 'P');  //  Parent Semaphore key
12$C_SEMKEY = ftok(__FILE__, 'C');  //  Child Semaphore key
13
14echo "P: parent process running.\n";
15
16pcntl_signal(SIGCHLD, SIG_IGN);
17
18// Get semaphore for parent
19$p_sem_id = sem_get($P_SEMKEY, 1);
20if ($p_sem_id === FALSE) {
21	echo "P: failed to parent get semaphore\n";
22	exit;
23}
24
25echo "P: got semaphore $p_sem_id.\n";
26
27// Get semaphore for child
28$c_sem_id = sem_get($C_SEMKEY, 1);
29if ($c_sem_id === FALSE) {
30	echo "P: failed to child get semaphore\n";
31	exit;
32}
33
34
35// Acquire semaphore for parent
36if (!sem_acquire($p_sem_id)) {
37	echo "P: fail to acquire semaphore $p_sem_id.\n";
38	sem_remove($p_sem_id);
39	exit;
40}
41echo "P: acquired semaphore $p_sem_id.\n";
42
43// Acquire semaphore for child
44if (!sem_acquire($c_sem_id)) {
45	echo "P: fail to acquire semaphore $c_sem_id.\n";
46	sem_remove($c_sem_id);
47	exit;
48}
49echo "P: acquired semaphore $c_sem_id\n";
50// Fork process
51$pid = pcntl_fork();
52
53if ($pid) {
54
55	register_shutdown_function(function () use ($p_sem_id) {
56		echo "P: removing semaphore $p_sem_id.\n";
57		sem_remove($p_sem_id);
58	});
59
60	// Release semaphore after 50ms
61	usleep(50000);
62
63	/* Wait for the child semaphore to be released to
64	   to release the parent semaphore */
65	if (!sem_acquire($c_sem_id)) {
66		echo "P: failed to acquire semaphore $c_sem_id.\n";
67		exit;
68	}
69
70	/* Release the child semahpore before releasing
71	   the releasing the parent semaphore and letting
72	   the child continue execution */
73	sem_release($c_sem_id);
74
75	echo "P: releasing semaphore $p_sem_id.\n";
76	if (!sem_release($p_sem_id)) {
77		echo "P: failed to release semaphore\n";
78	}
79
80	$status = null;
81	pcntl_waitpid($pid, $status);
82
83} else {
84
85	register_shutdown_function(function () use ($c_sem_id) {
86		echo "C: removing semaphore $c_sem_id.\n";
87		sem_remove($c_sem_id);
88	});
89
90	echo "C: child process running.\n";
91
92	// Have the semaphore after process forked
93	echo "C: got semaphore $p_sem_id and $c_sem_id.\n";
94
95	// This should fail to get to the semaphore and not wait
96	if (sem_acquire($p_sem_id, true)) {
97		echo "C: test failed, Child was able to acquire semaphore $p_sem_id.\n";
98		exit;
99	}
100
101	// The child process did not wait to acquire the semaphore
102	echo "C: failed to acquire semaphore $p_sem_id.\n";
103
104	echo "C: releasing semaphore $c_sem_id\n";
105	if (!sem_release($c_sem_id)) {
106		echo "C: Failed to release semaphore\n";
107	}
108
109	// Acquire semaphore with waiting
110	if (!sem_acquire($p_sem_id)) {
111		echo "C: fail to acquire semaphore $p_sem_id.\n";
112		exit;
113	}
114	echo "C: success acquired semaphore $p_sem_id.\n";
115
116	echo "C: releasing semaphore $p_sem_id.\n";
117	sem_release($p_sem_id);
118}
119
120?>
121--EXPECTF--
122P: parent process running.
123P: got semaphore Resource id #%i.
124P: acquired semaphore Resource id #%i.
125P: acquired semaphore Resource id #%i
126C: child process running.
127C: got semaphore Resource id #%i and Resource id #%i.
128C: failed to acquire semaphore Resource id #%i.
129C: releasing semaphore Resource id #%i
130P: releasing semaphore Resource id #%i.
131C: success acquired semaphore Resource id #%i.
132C: releasing semaphore Resource id #%i.
133C: removing semaphore Resource id #%i.
134P: removing semaphore Resource id #%i.
135