aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/klaar/pfs.c
blob: fe1c3d74b4192cd5b826b58c16b395ed6dd4bdfd (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
/* klaar@ida (convert to built-in pintos test)
   
   Tests that read and write are properly synchronized. Also stresses
   the filesystem somewhat.

   pfs_reader and pfs_writer are needed child processes.
*/
  
#include <syscall.h>
#include <random.h>
#include <stdio.h>
#include <stdlib.h>

#include "../lib.h"
#include "pfs.h"

static void swap(char* a, char* b)
{
  char c = *a;
  *a = *b;
  *b = c;
}

static void start_rw(int rn, int wn)
{
  int rpid = -1;
  int wpid = -1;

  if ( rn > 0 )
  {
    rpid = exec ("pfs-reader");
    msg ("exec(\"pfs-reader\"): %d", rpid);
  }
  
  if ( wn > 0 )
  {
    int range = '~' - '!' + 1; // ascii 32 - 126
    char start = '!' + random_ulong () % range;
    char end = '!' + random_ulong () % range;
    
    if ( end < start )
      swap(&start, &end);

    const int CMDSIZE = 64;
    char cmd[CMDSIZE];
    snprintf (cmd, CMDSIZE, "pfs-writer %c %c", start, end);

    wpid = exec (cmd);
    msg ("exec(\"%s\"): %d", cmd, wpid);
  }

  if ( rn > 0 || wn > 0 )
    start_rw(rn-1, wn-1);

  if ( rpid != -1 )
    CHECK( wait( rpid ) != -1, "wait pfs-reader %d", rpid);
  
  if ( wpid != -1 )
    CHECK( wait( wpid ) != -1, "wait pfs-writer %d", wpid);
}

int main (int argc, char *argv[]) 
{
  if ( argc != 3 )
    fail("usage: pfs START END");
  
  quiet = true;
  test_name = argv[0];
  int num_reader = atoi(argv[1]);
  int num_writer = atoi(argv[2]);
  
  msg ("begin");
  
  CHECK (create ("file.1", BIG), "create \"file.1\"");
  CHECK (create ("messages", TIMES), "create \"messages\"");

  random_init (0);

  start_rw(num_reader, num_writer);
  
  msg ("end");
  return 0;
}