-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
33 lines (30 loc) · 964 Bytes
/
main.cpp
File metadata and controls
33 lines (30 loc) · 964 Bytes
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
#include <iostream>
#include <cstdlib>
#include "comfork.h"
int main(int argc, char* argv[]) {
pid_t pid = fork();
if (pid < 0) {
printf("Failed to clone the current process: 0x%x\r\n", pid);
} if (pid > 0) {
// parent will wait
printf("Parent successfully cloned itself with PID: %d\r\n", pid);
int status = 0;
pid_t ret = waitpid(pid, &status, 0);
if (ret == 0) {
printf("Parent didn't wait\r\n");
} else {
printf("Clone exit with raw status: 0x%x\r\n", (status));
if (WIFEXITED(status)) {
printf("Clone exit with error status: 0x%x\r\n", WEXITSTATUS(status));
}
}
} else {
// child will waste time
printf("Clone is wasting time!\r\n");
for (int i = 0; i < 100000; i++);
printf("Clone is returning!\r\n");
// Terminate without clean-up
_exit(5);
}
return 0;
}