CS318 - Pintos
Pintos source browser for JHU CS318 course
child-qsort.c
Go to the documentation of this file.
1 /** Reads a 128 kB file onto the stack and "sorts" the bytes in
2  it, using quick sort, a multi-pass divide and conquer
3  algorithm. The sorted data is written back to the same file
4  in-place. */
5 
6 #include <debug.h>
7 #include <syscall.h>
8 #include "tests/lib.h"
9 #include "tests/main.h"
10 #include "tests/vm/qsort.h"
11 
12 const char *test_name = "child-qsort";
13 
14 int
15 main (int argc UNUSED, char *argv[])
16 {
17  int handle;
18  unsigned char buf[128 * 1024];
19  size_t size;
20 
21  quiet = true;
22 
23  CHECK ((handle = open (argv[1])) > 1, "open \"%s\"", argv[1]);
24 
25  size = read (handle, buf, sizeof buf);
26  qsort_bytes (buf, sizeof buf);
27  seek (handle, 0);
28  write (handle, buf, size);
29  close (handle);
30 
31  return 72;
32 }
lib.h
buf
static char buf[BUF_SIZE]
Definition: child-syn-read.c:16
write
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
CHECK
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29
UNUSED
#define UNUSED
GCC lets us add "attributes" to functions, function parameters, etc.
Definition: debug.h:7
test_name
const char * test_name
Reads a 128 kB file onto the stack and "sorts" the bytes in it, using quick sort, a multi-pass divide...
Definition: child-qsort.c:12
open
int open(const char *file)
Definition: syscall.c:103
qsort.h
qsort_bytes
void qsort_bytes(unsigned char *buf, size_t size)
Sorts the SIZE bytes in BUF into nondecreasing order, using the quick-sort algorithm.
Definition: qsort.c:114
seek
void seek(int fd, unsigned position)
Definition: syscall.c:127
main
int main(int argc UNUSED, char *argv[])
Definition: child-qsort.c:15
main.h
close
void close(int fd)
Definition: syscall.c:139
read
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115
quiet
bool quiet
Definition: lib.c:9
debug.h