-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfork.c
88 lines (78 loc) · 2.05 KB
/
fork.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* fork.c
*
* Copyright (C) 2018 < Daniel Finneran, [email protected] >
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the GPL license. See the LICENSE file for details.
*/
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
int forkSelf (int zombieCount)
{
pid_t child_pid;
for (int i = 0; i < zombieCount; i++) {
child_pid = fork();
if (child_pid > 0) {
// parent process will sleep for 30 seconds and exit, without a call to wait()
fprintf(stderr,"parent process - %d\n", getpid());
//sleep(1);
//exit(0);
}
else if (child_pid == 0) {
// Child process is killed immediately, but the parent never cleans up! ZOMBIE TIME
fprintf(stderr,"child process - %d\n", getpid());
signal(SIGHUP, SIG_IGN);
close(0);
close(1);
close(2);
//
chdir("/");
//
//
setsid();
//
//while(1) {
// sleep(1);
//}
// setsid();
//
exit(0);
}
else if (child_pid == -1) {
// fork() error
perror("fork() call failed");
exit (-1);
}
else {
// this should not happen
fprintf(stderr, "unknown return value of %d from fork() call", child_pid);
exit (-2);
}
}
return 0;
}
int becomeParent() {
pid_t pid;
if (( pid = fork()) < 0) exit(0);
else if(pid != 0) exit(0); // kill parent process
fprintf(stderr,"child running...pid=%d\n",getpid());
signal(SIGHUP, SIG_IGN);
close(0);
close(1);
close(2);
//
chdir("/");
//
//
setsid();
//
while(1) {
sleep(1);
}
}