-
Notifications
You must be signed in to change notification settings - Fork 1
/
orphan.c
56 lines (39 loc) · 810 Bytes
/
orphan.c
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Tool creating orphan processes
Author: R. Koucha
Date: 11-Apr-2020
*/
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <libgen.h>
#include <stdlib.h>
#include <time.h>
#include "util.h"
int main(int ac, char *av[])
{
int i;
int nb;
int t;
if (ac != 2) {
fprintf(stderr, "Usage: %s nb_orphans\n", basename(av[0]));
return 1;
}
if (!is_unsigned_integer(av[1])) {
fprintf(stderr, "Specify a number of orphans instead of '%s'\n", av[1]);
return 1;
}
nb = atoi(av[1]);
// Set the seed
srand(time(NULL));
for (i = 0; i < nb; i ++) {
t = rand() % 20;
if (0 == fork()) {
printf("Process#%d: Sleeping %d seconds...\n", getpid(), t);
sleep(t);
break;
}
} // End fork
return 0;
} // main