summaryrefslogtreecommitdiffstats
path: root/src/examples/longrun.c
blob: e3399f0281dfc3155b5bc03ea49cfd63d0b88b9c (plain) (blame)
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
/* Start a lot of processes and let them finish to test if we
 * eventually run out of process slots.
 * To run the test effectively, if you have a limit on number of
 * processes, lower the limit to 5 processes and run this test like
 *
 *  'longrun 5 400'
 *
 * It will fill (and if it works empty) your process table 400 times
 * :-)
 *
 * Note that some pintos tests requires at least 16 simultaneous
 * processes to work, so be sure to increase the limit before running
 * pintos tests.
 */

#include <syscall.h>
#include <stdlib.h>
#include <stdio.h>

#define MAX_SIMULTANEOUS 50
#define MAX_REPEAT 1000

int main(int argc, char* argv[])
{
  char cmd[15];
  int pid[MAX_SIMULTANEOUS];
  unsigned i = 0;
  unsigned j = 0;
  unsigned simul = 0;
  unsigned repeat = 0;

  if (argc != 3)
  {
    printf("Usage: %s simultaneous repeat\n"
           "Where 'simultaneous' is the number of simultaneous processes to \n"
           "start and wait for, and 'repeat' how many times this should be \n"
           "repeated.\n", argv[0]
      );
    return -1;
  }

  simul = atoi(argv[1]);
  repeat = atoi(argv[2]);

  if (simul > MAX_SIMULTANEOUS)
  {
    printf("This test program is compiled with a limitation to max %d \n"
           "simultaneos processes.\n", MAX_SIMULTANEOUS);
    return -1;
  }
  
  if (repeat > MAX_REPEAT)
  {
    printf("This test program is compiled with a limitation to max %d \n"
           "repetitions.\n", MAX_REPEAT);
    return -1;
  }
  
  printf("Will try to start a total of %d processes in groups of %d\n",
         simul * repeat, simul);
  
  for (j = 0; j < repeat; ++j)
  {
    for (i = 0; i < simul; ++i)
    {
      snprintf(cmd, 15, "dummy %i", j * simul + i);
      pid[i] = exec(cmd);
    }
    
    /* There will never be more than 'simul' processes running
     * simultaneously, but in total there will be a lot of processes
     * (repeat * simul). Just as you can expect in a real system.
     */
    for (i = 0; i < simul; ++i)
    {
      /* probably nothing left to wait for */
      if (pid[i] == -1)
      {
        printf("ERROR: Aborting test. Some execution(s) failed\n");
        return j+1;
      }
      wait(pid[i]);      
    }
  }
  return 0;
}