File: stress.c

package info (click to toggle)
libpulp 0.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,976 kB
  • sloc: ansic: 11,792; python: 1,216; sh: 881; makefile: 871; cpp: 582; asm: 387
file content (80 lines) | stat: -rw-r--r-- 1,426 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>

#define NUM_PROCESSES 4000

int value(void);

typedef enum
{
  NONE = 0,
  READY,
  LIVEPATCHED,
} state_t;

int
child_main(volatile state_t *state)
{
  *state = READY;

  while (value()) {
    sleep(1);
  }

  *state = LIVEPATCHED;
  return 0;
}

int
main(void)
{
  pid_t pids[NUM_PROCESSES];
  volatile state_t *states;
  states = mmap(NULL, NUM_PROCESSES * sizeof(state_t), PROT_READ | PROT_WRITE,
                MAP_SHARED | MAP_ANONYMOUS, -1, 0);

  for (int i = 0; i < NUM_PROCESSES; i++) {
    states[i] = NONE;
    pids[i] = fork();

    if (pids[i] == 0) {
      /* Child.  */
      return child_main(&states[i]);
    }
  }

  for (int i = 0; i < NUM_PROCESSES; i++) {
    while (states[i] != READY)
      usleep(1000);
  }

  puts("Processes launched");

  for (int i = 0; i < NUM_PROCESSES; i++) {
    while (states[i] != LIVEPATCHED)
      usleep(1000);
  }

  for (int i = 0; i < NUM_PROCESSES; i++) {
    int wstatus;
    waitpid(pids[i], &wstatus, 0);

    if (WIFEXITED(wstatus)) {
      int r = WEXITSTATUS(wstatus);
      if (r) {
        printf("Process %d returned non-zero: %d\n", pids[i], r);
      }
    }
    else {
      printf("Process %d ended without calling exit\n", pids[i]);
    }
  }

  munmap((void *)states, NUM_PROCESSES * sizeof(state_t));
  puts("Processes finished");

  return 0;
}