File: fork-wait.c

package info (click to toggle)
proot 5.1.0-1.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, trixie
  • size: 2,228 kB
  • sloc: ansic: 16,491; sh: 1,327; asm: 41; makefile: 8
file content (26 lines) | stat: -rw-r--r-- 534 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdlib.h> /* exit(3), */
#include <unistd.h> /* fork(2), sleep(3), */
#include <sys/types.h> /* wait(2), */
#include <sys/wait.h>  /* wait(2), */

int main(int argc, char *argv[])
{
	int child_status;
	int status;

	switch (fork()) {
	case -1:
		exit(EXIT_FAILURE);

	case 0: /* Child */
		argc > 1 && sleep(2);
		exit(EXIT_SUCCESS);

	default: /* Parent */
		argc <= 1 && sleep(2);
		status = wait(&child_status);
		if (status < 0 || !WIFEXITED(child_status))
			exit(EXIT_FAILURE);
		exit(WEXITSTATUS(child_status));
	}
}