PKUOS - Pintos
Pintos source browser for PKU Operating System 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
10const char *test_name = "child-sort";
11
12unsigned char buf[128 * 1024];
13size_t histogram[256];
14
15int
16main (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}
int main(int argc UNUSED, char *argv[])
Definition: child-sort.c:16
size_t histogram[256]
Definition: child-sort.c:13
unsigned char buf[128 *1024]
Definition: child-sort.c:12
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
#define UNUSED
GCC lets us add "attributes" to functions, function parameters, etc.
Definition: debug.h:7
void close(int fd)
Definition: syscall.c:139
int open(const char *file)
Definition: syscall.c:103
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
void seek(int fd, unsigned position)
Definition: syscall.c:127
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115
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