CS318 - Pintos
Pintos source browser for JHU CS318 course
child-sort.c
Go to the documentation of this file.
1 /** Reads a 128 kB file into static data and "sorts" the bytes in
2  it, using counting sort, a single-pass algorithm. The sorted
3  data is written back to the same file in-place. */
4 
5 #include <debug.h>
6 #include <syscall.h>
7 #include "tests/lib.h"
8 #include "tests/main.h"
9 
10 const char *test_name = "child-sort";
11 
12 unsigned char buf[128 * 1024];
13 size_t histogram[256];
14 
15 int
16 main (int argc UNUSED, char *argv[])
17 {
18  int handle;
19  unsigned char *p;
20  size_t size;
21  size_t i;
22 
23  quiet = true;
24 
25  CHECK ((handle = open (argv[1])) > 1, "open \"%s\"", argv[1]);
26 
27  size = read (handle, buf, sizeof buf);
28  for (i = 0; i < size; i++)
29  histogram[buf[i]]++;
30  p = buf;
31  for (i = 0; i < sizeof histogram / sizeof *histogram; i++)
32  {
33  size_t j = histogram[i];
34  while (j-- > 0)
35  *p++ = i;
36  }
37  seek (handle, 0);
38  write (handle, buf, size);
39  close (handle);
40 
41  return 123;
42 }
lib.h
main
int main(int argc UNUSED, char *argv[])
Definition: child-sort.c:16
buf
unsigned char buf[128 *1024]
Definition: child-sort.c:12
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
open
int open(const char *file)
Definition: syscall.c:103
arc4::i
uint8_t i
Definition: arc4.h:11
seek
void seek(int fd, unsigned position)
Definition: syscall.c:127
main.h
close
void close(int fd)
Definition: syscall.c:139
arc4::j
uint8_t j
Definition: arc4.h:11
test_name
const char * test_name
Reads a 128 kB file into static data and "sorts" the bytes in it, using counting sort,...
Definition: child-sort.c:10
histogram
size_t histogram[256]
Definition: child-sort.c:13
read
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115
quiet
bool quiet
Definition: lib.c:9
debug.h