Files
RedBear-OS/tests/waitpid_multiple.c
Red Bear OS 1b3e94a20d Red Bear OS relibc baseline
From release 0.1.0 pre-patched archive.
This includes all Red Bear modifications previously maintained
as patches in local/patches/relibc/.
2026-06-27 09:19:26 +03:00

59 lines
1.6 KiB
C

#include <stdio.h>
#include <assert.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
// Spawn three childrens, one with same pgid and two with pgid set to its own
// pid, so the exit order is two different pgid, then followed with same pgid
pid_t pid_samepgid = fork();
ERROR_IF(fork, pid_samepgid, == -1);
if (pid_samepgid == 0) {
// child
usleep(300000);
_Exit(2);
}
pid_t pid_diffpgids[2];
for (int i = 0; i < 2; i++) {
pid_diffpgids[i] = fork();
ERROR_IF(fork, pid_diffpgids[i], == -1);
if (pid_diffpgids[i] == 0) {
int ret = setpgid(0, 0);
ERROR_IF(setpgid, ret, == -1);
// child
usleep((i + 1) * 100000);
_Exit(i);
}
}
int status;
pid_t wid;
// First, check that the first different-pgid proc is recognized.
wid = waitpid(-1, &status, 0);
ERROR_IF(waitpid, wid, == -1);
assert(wid == pid_diffpgids[0]);
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 0);
// Then, check that the longest-waiting proc with the same pgid is properly matched.
wid = waitpid(0, &status, 0);
ERROR_IF(waitpid, wid, == -1);
assert(wid == pid_samepgid);
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 2);
// Finally, the last different-pgid must have completed.
wid = waitpid(-1, &status, WNOHANG);
ERROR_IF(waitpid, wid, == -1);
assert(wid == pid_diffpgids[1]);
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 1);
}