PKUOS - Pintos
Pintos source browser for PKU Operating System course
child-syn-rw.c
Go to the documentation of this file.
1/** Child process for syn-rw.
2 Reads from a file created by our parent process, which is
3 growing it. We loop until we've read the whole file
4 successfully. Many iterations through the loop will return 0
5 bytes, because the file has not grown in the meantime. That
6 is, we are "busy waiting" for the file to grow.
7 (This test could be improved by adding a "yield" system call
8 and calling yield whenever we receive a 0-byte read.) */
9
10#include <random.h>
11#include <stdlib.h>
12#include <syscall.h>
14#include "tests/lib.h"
15
16const char *test_name = "child-syn-rw";
17
18static char buf1[BUF_SIZE];
19static char buf2[BUF_SIZE];
20
21int
22main (int argc, const char *argv[])
23{
24 int child_idx;
25 int fd;
26 size_t ofs;
27
28 quiet = true;
29
30 CHECK (argc == 2, "argc must be 2, actually %d", argc);
31 child_idx = atoi (argv[1]);
32
33 random_init (0);
34 random_bytes (buf1, sizeof buf1);
35
36 CHECK ((fd = open (file_name)) > 1, "open \"%s\"", file_name);
37 ofs = 0;
38 while (ofs < sizeof buf2)
39 {
40 int bytes_read = read (fd, buf2 + ofs, sizeof buf2 - ofs);
41 CHECK (bytes_read >= -1 && bytes_read <= (int) (sizeof buf2 - ofs),
42 "%zu-byte read on \"%s\" returned invalid value of %d",
43 sizeof buf2 - ofs, file_name, bytes_read);
44 if (bytes_read > 0)
45 {
46 compare_bytes (buf2 + ofs, buf1 + ofs, bytes_read, ofs, file_name);
47 ofs += bytes_read;
48 }
49 }
50 close (fd);
51
52 return child_idx;
53}
const char * test_name
Child process for syn-rw.
Definition: child-syn-rw.c:16
int main(int argc, const char *argv[])
Definition: child-syn-rw.c:22
static char buf1[BUF_SIZE]
Definition: child-syn-rw.c:18
static char buf2[BUF_SIZE]
Definition: child-syn-rw.c:19
int atoi(const char *s)
Converts a string representation of a signed decimal integer in S into an ‘int’, which is returned.
Definition: stdlib.c:10
void close(int fd)
Definition: syscall.c:139
int open(const char *file)
Definition: syscall.c:103
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115
void compare_bytes(const void *read_data_, const void *expected_data_, size_t size, size_t ofs, const char *file_name)
test/lib.h
Definition: lib.c:163
bool quiet
Definition: lib.c:9
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29
void random_bytes(void *buf_, size_t size)
Writes SIZE random bytes into BUF.
Definition: random.c:54
void random_init(unsigned seed)
Initializes or reinitializes the PRNG with the given SEED.
Definition: random.c:34
#define BUF_SIZE
Definition: syn-read.h:4
static const char file_name[]
tests/filesys/base/syn-read.h
Definition: syn-read.h:5