-
Notifications
You must be signed in to change notification settings - Fork 17
/
testa.c
48 lines (40 loc) · 886 Bytes
/
testa.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
// 2012, Georg Sauthoff <[email protected]>
// GPLv3+
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
static void fill_some(size_t n, char ch)
{
size_t bytes = n * (size_t) 1024 * (size_t) 1024;
char *b = malloc(bytes);
if (!b) {
fprintf(stderr, "Failed to allocate %zu MiB\n", n);
exit(1);
}
printf("Allocating %zu MiBs\n", n);
for (size_t i = 0; i<bytes; i+=1024) {
b[i] = ch;
}
sleep(1);
}
int main(int argc, char **argv)
{
char ch = argv[1][0];
printf("Parent PID is %zd\n", (ssize_t)getpid());
fill_some(atoi(argv[2]), ch);
for (int r = 3; r<argc; ++r) {
pid_t ret = fork();
if (ret < 0) {
perror("Fork failed");
exit(2);
}
if (!ret) {
printf("New Child: %zd\n", (ssize_t)getpid());
fill_some(atoi(argv[r]), ch);
exit(0);
}
}
sleep(argc);
return 0;
}