liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
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
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
 
constexpr int LOOP_COUNT = 100000000;
 
volatile int a[2];
void ParentFunction() {
  volatile int* p = a + atoi("0");
  for (int i = 0; i < LOOP_COUNT; ++i) {
    *p = i;
  }
}
 
void ChildFunction() {
  volatile int* p = a + atoi("1");
  for (int i = 0; i < LOOP_COUNT; ++i) {
    *p = i;
  }
}
 
int main() {
  while (true) {
    pid_t pid = fork();
    if (pid == 0) {
      ChildFunction();
      return 0;
    } else {
      ParentFunction();
      waitpid(pid, nullptr, 0);
    }
  }
  return 0;
}