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