provided code

This commit is contained in:
LabTS
2024-10-01 23:37:39 +01:00
commit 8724a2641e
697 changed files with 74252 additions and 0 deletions

19
src/examples/.gitignore vendored Normal file
View File

@@ -0,0 +1,19 @@
cat
cmp
cp
echo
halt
hex-dump
ls
mcat
mcp
mkdir
pwd
rm
shell
bubsort
insult
lineup
matmult
recursor
*.d

29
src/examples/Makefile Normal file
View File

@@ -0,0 +1,29 @@
SRCDIR = ..
# Test programs to compile, and a list of sources for each.
# To add a new test, put its name on the PROGS list
# and then add a name_SRC line that lists its source files.
PROGS = cat cmp cp echo halt hex-dump mcat mcp rm \
bubsort insult lineup matmult recursor
# Should work from task 2 onward.
cat_SRC = cat.c
cmp_SRC = cmp.c
cp_SRC = cp.c
echo_SRC = echo.c
halt_SRC = halt.c
hex-dump_SRC = hex-dump.c
insult_SRC = insult.c
lineup_SRC = lineup.c
ls_SRC = ls.c
recursor_SRC = recursor.c
rm_SRC = rm.c
# Should work in task 3; also in task 4 if VM is included.
bubsort_SRC = bubsort.c
matmult_SRC = matmult.c
mcat_SRC = mcat.c
mcp_SRC = mcp.c
include $(SRCDIR)/Make.config
include $(SRCDIR)/Makefile.userprog

38
src/examples/bubsort.c Normal file
View File

@@ -0,0 +1,38 @@
/* sort.c
Test program to sort a large number of integers.
Intention is to stress virtual memory system.
Ideally, we could read the unsorted array off of the file
system, and store the result back to the file system! */
#include <stdio.h>
/* Size of array to sort. */
#define SORT_SIZE 128
int
main (void)
{
/* Array to sort. Static to reduce stack usage. */
static int array[SORT_SIZE];
int i, j, tmp;
/* First initialize the array in descending order. */
for (i = 0; i < SORT_SIZE; i++)
array[i] = SORT_SIZE - i - 1;
/* Then sort in ascending order. */
for (i = 0; i < SORT_SIZE - 1; i++)
for (j = 0; j < SORT_SIZE - 1 - i; j++)
if (array[j] > array[j + 1])
{
tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
printf ("sort exiting with code %d\n", array[0]);
return array[0];
}

34
src/examples/cat.c Normal file
View File

@@ -0,0 +1,34 @@
/* cat.c
Prints files specified on command line to the console. */
#include <stdio.h>
#include <syscall.h>
int
main (int argc, char *argv[])
{
bool success = true;
int i;
for (i = 1; i < argc; i++)
{
int fd = open (argv[i]);
if (fd < 0)
{
printf ("%s: open failed\n", argv[i]);
success = false;
continue;
}
for (;;)
{
char buffer[1024];
int bytes_read = read (fd, buffer, sizeof buffer);
if (bytes_read == 0)
break;
write (STDOUT_FILENO, buffer, bytes_read);
}
close (fd);
}
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}

68
src/examples/cmp.c Normal file
View File

@@ -0,0 +1,68 @@
/* cat.c
Compares two files. */
#include <stdio.h>
#include <syscall.h>
int
main (int argc, char *argv[])
{
int fd[2];
if (argc != 3)
{
printf ("usage: cmp A B\n");
return EXIT_FAILURE;
}
/* Open files. */
fd[0] = open (argv[1]);
if (fd[0] < 0)
{
printf ("%s: open failed\n", argv[1]);
return EXIT_FAILURE;
}
fd[1] = open (argv[2]);
if (fd[1] < 0)
{
printf ("%s: open failed\n", argv[1]);
return EXIT_FAILURE;
}
/* Compare data. */
for (;;)
{
int pos;
char buffer[2][1024];
int bytes_read[2];
int min_read;
int i;
pos = tell (fd[0]);
bytes_read[0] = read (fd[0], buffer[0], sizeof buffer[0]);
bytes_read[1] = read (fd[1], buffer[1], sizeof buffer[1]);
min_read = bytes_read[0] < bytes_read[1] ? bytes_read[0] : bytes_read[1];
if (min_read == 0)
break;
for (i = 0; i < min_read; i++)
if (buffer[0][i] != buffer[1][i])
{
printf ("Byte %d is %02hhx ('%c') in %s but %02hhx ('%c') in %s\n",
pos + i,
buffer[0][i], buffer[0][i], argv[1],
buffer[1][i], buffer[1][i], argv[2]);
return EXIT_FAILURE;
}
if (min_read < bytes_read[1])
printf ("%s is shorter than %s\n", argv[1], argv[2]);
else if (min_read < bytes_read[0])
printf ("%s is shorter than %s\n", argv[2], argv[1]);
}
printf ("%s and %s are identical\n", argv[1], argv[2]);
return EXIT_SUCCESS;
}

55
src/examples/cp.c Normal file
View File

@@ -0,0 +1,55 @@
/* cat.c
Copies one file to another. */
#include <stdio.h>
#include <syscall.h>
int
main (int argc, char *argv[])
{
int in_fd, out_fd;
if (argc != 3)
{
printf ("usage: cp OLD NEW\n");
return EXIT_FAILURE;
}
/* Open input file. */
in_fd = open (argv[1]);
if (in_fd < 0)
{
printf ("%s: open failed\n", argv[1]);
return EXIT_FAILURE;
}
/* Create and open output file. */
if (!create (argv[2], filesize (in_fd)))
{
printf ("%s: create failed\n", argv[2]);
return EXIT_FAILURE;
}
out_fd = open (argv[2]);
if (out_fd < 0)
{
printf ("%s: open failed\n", argv[2]);
return EXIT_FAILURE;
}
/* Copy data. */
for (;;)
{
char buffer[1024];
int bytes_read = read (in_fd, buffer, sizeof buffer);
if (bytes_read == 0)
break;
if (write (out_fd, buffer, bytes_read) != bytes_read)
{
printf ("%s: write failed\n", argv[2]);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

14
src/examples/echo.c Normal file
View File

@@ -0,0 +1,14 @@
#include <stdio.h>
#include <syscall.h>
int
main (int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++)
printf ("%s ", argv[i]);
printf ("\n");
return EXIT_SUCCESS;
}

14
src/examples/halt.c Normal file
View File

@@ -0,0 +1,14 @@
/* halt.c
Simple program to test whether running a user program works.
Just invokes a system call that shuts down the OS. */
#include <syscall.h>
int
main (void)
{
halt ();
/* not reached */
}

35
src/examples/hex-dump.c Normal file
View File

@@ -0,0 +1,35 @@
/* hex-dump.c
Prints files specified on command line to the console in hex. */
#include <stdio.h>
#include <syscall.h>
int
main (int argc, char *argv[])
{
bool success = true;
int i;
for (i = 1; i < argc; i++)
{
int fd = open (argv[i]);
if (fd < 0)
{
printf ("%s: open failed\n", argv[i]);
success = false;
continue;
}
for (;;)
{
char buffer[1024];
int pos = tell (fd);
int bytes_read = read (fd, buffer, sizeof buffer);
if (bytes_read == 0)
break;
hex_dump (pos, buffer, bytes_read, true);
}
close (fd);
}
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}

369
src/examples/insult.c Normal file
View File

@@ -0,0 +1,369 @@
/* Insult.c
This is a version of the famous CS 107 random sentence
generator. I wrote a program that reads a grammar definition
file and writes a C file containing that grammar as hard code
static C strings. Thus the majority of the code below in
machine generated and totally unreadable. The arrays created
are specially designed to make generating the sentences as
easy as possible.
Originally by Greg Hutchins, March 1998.
Modified by Ben Pfaff for PintOS, Sept 2004. */
char *start[] =
{ "You", "1", "5", ".", "May", "13", ".", "With", "the", "19", "of", "18",
",", "may", "13", "."
};
char startLoc[] = { 3, 0, 4, 7, 16 };
char *adj[] = { "3", "4", "2", ",", "1" };
char adjLoc[] = { 3, 0, 1, 2, 5 };
char *adj3[] = { "3", "4" };
char adj3Loc[] = { 2, 0, 1, 2 };
char *adj1[] =
{ "lame", "dried", "up", "par-broiled", "bloated", "half-baked", "spiteful",
"egotistical", "ungrateful", "stupid", "moronic", "fat", "ugly", "puny", "pitiful",
"insignificant", "blithering", "repulsive", "worthless", "blundering", "retarded",
"useless", "obnoxious", "low-budget", "assinine", "neurotic", "subhuman", "crochety",
"indescribable", "contemptible", "unspeakable", "sick", "lazy", "good-for-nothing",
"slutty", "mentally-deficient", "creepy", "sloppy", "dismal", "pompous", "pathetic",
"friendless", "revolting", "slovenly", "cantankerous", "uncultured", "insufferable",
"gross", "unkempt", "defective", "crumby"
};
char adj1Loc[] =
{ 50, 0, 1, 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 };
char *adj2[] =
{ "putrefied", "festering", "funky", "moldy", "leprous", "curdled", "fetid",
"slimy", "crusty", "sweaty", "damp", "deranged", "smelly", "stenchy", "malignant",
"noxious", "grimy", "reeky", "nasty", "mutilated", "sloppy", "gruesome", "grisly",
"sloshy", "wormy", "mealy", "spoiled", "contaminated", "rancid", "musty",
"fly-covered", "moth-eaten", "decaying", "decomposed", "freeze-dried", "defective",
"petrified", "rotting", "scabrous", "hirsute"
};
char adj2Loc[] =
{ 40, 0, 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 };
char *name[] =
{ "10", ",", "bad", "excuse", "for", "6", ",", "6", "for", "brains", ",",
"4", "11", "8", "for", "brains", "offspring", "of", "a", "motherless", "10", "7", "6",
"7", "4", "11", "8"
};
char nameLoc[] = { 7, 0, 1, 6, 10, 16, 21, 23, 27 };
char *stuff[] =
{ "shit", "toe", "jam", "filth", "puss", "earwax", "leaf", "clippings",
"bat", "guano", "mucus", "fungus", "mung", "refuse", "earwax", "spittoon", "spittle",
"phlegm"
};
char stuffLoc[] = { 14, 0, 1, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 17, 18 };
char *noun_and_prep[] =
{ "bit", "of", "piece", "of", "vat", "of", "lump", "of", "crock", "of",
"ball", "of", "tub", "of", "load", "of", "bucket", "of", "mound", "of", "glob", "of", "bag",
"of", "heap", "of", "mountain", "of", "load", "of", "barrel", "of", "sack", "of", "blob", "of",
"pile", "of", "truckload", "of", "vat", "of"
};
char noun_and_prepLoc[] =
{ 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36,
38, 40, 42 };
char *organics[] =
{ "droppings", "mung", "zits", "puckies", "tumors", "cysts", "tumors",
"livers", "froth", "parts", "scabs", "guts", "entrails", "blubber", "carcuses", "gizards",
"9"
};
char organicsLoc[] =
{ 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
char *body_parts[] =
{ "kidneys", "genitals", "buttocks", "earlobes", "innards", "feet"
};
char body_partsLoc[] = { 6, 0, 1, 2, 3, 4, 5, 6 };
char *noun[] =
{ "pop", "tart", "warthog", "twinkie", "barnacle", "fondue", "pot",
"cretin", "fuckwad", "moron", "ass", "neanderthal", "nincompoop", "simpleton", "11"
};
char nounLoc[] = { 13, 0, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
char *animal[] =
{ "donkey", "llama", "dingo", "lizard", "gekko", "lemur", "moose", "camel",
"goat", "eel"
};
char animalLoc[] = { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
char *good_verb[] =
{ "love", "cuddle", "fondle", "adore", "smooch", "hug", "caress", "worship",
"look", "at", "touch"
};
char good_verbLoc[] = { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11 };
char *curse[] =
{ "14", "20", "23", "14", "17", "20", "23", "14", "find", "your", "9",
"suddenly", "delectable", "14", "and", "14", "seek", "a", "battleground", "23"
};
char curseLoc[] = { 4, 0, 3, 7, 13, 20 };
char *afflictors[] =
{ "15", "21", "15", "21", "15", "21", "15", "21", "a", "22", "Rush",
"Limbaugh", "the", "hosts", "of", "Hades"
};
char afflictorsLoc[] = { 6, 0, 2, 4, 6, 8, 12, 16 };
char *quantity[] =
{ "a", "4", "hoard", "of", "a", "4", "pack", "of", "a", "truckload", "of",
"a", "swarm", "of", "many", "an", "army", "of", "a", "4", "heard", "of", "a", "4",
"platoon", "of", "a", "4", "and", "4", "group", "of", "16"
};
char quantityLoc[] = { 10, 0, 4, 8, 11, 14, 15, 18, 22, 26, 32, 33 };
char *numbers[] =
{ "a", "thousand", "three", "million", "ninty-nine", "nine-hundred,",
"ninty-nine", "forty-two", "a", "gazillion", "sixty-eight", "times", "thirty-three"
};
char numbersLoc[] = { 7, 0, 2, 4, 5, 7, 8, 10, 13 };
char *adv[] =
{ "viciously", "manicly", "merrily", "happily", ",", "with", "the", "19",
"of", "18", ",", "gleefully", ",", "with", "much", "ritualistic", "celebration", ",",
"franticly"
};
char advLoc[] = { 8, 0, 1, 2, 3, 4, 11, 12, 18, 19 };
char *metaphor[] =
{ "an", "irate", "manticore", "Thor's", "belch", "Alah's", "fist", "16",
"titans", "a", "particularly", "vicious", "she-bear", "in", "the", "midst", "of", "her",
"menstrual", "cycle", "a", "pissed-off", "Jabberwock"
};
char metaphorLoc[] = { 6, 0, 3, 5, 7, 9, 20, 23 };
char *force[] = { "force", "fury", "power", "rage" };
char forceLoc[] = { 4, 0, 1, 2, 3, 4 };
char *bad_action[] =
{ "spit", "shimmy", "slobber", "find", "refuge", "find", "shelter", "dance",
"retch", "vomit", "defecate", "erect", "a", "strip", "mall", "build", "a", "26", "have", "a",
"religious", "experience", "discharge", "bodily", "waste", "fart", "dance", "drool",
"lambada", "spill", "16", "rusty", "tacks", "bite", "you", "sneeze", "sing", "16",
"campfire", "songs", "smite", "you", "16", "times", "construct", "a", "new", "home", "throw",
"a", "party", "procreate"
};
char bad_actionLoc[] =
{ 25, 0, 1, 2, 3, 5, 7, 8, 9, 10, 11, 15, 18, 22, 25, 26, 27, 28, 29, 33,
35, 36, 40, 44, 48, 51, 52 };
char *beasties[] =
{ "yaks", "22", "maggots", "22", "cockroaches", "stinging", "scorpions",
"fleas", "22", "weasels", "22", "gnats", "South", "American", "killer", "bees", "spiders",
"4", "monkeys", "22", "wiener-dogs", "22", "rats", "22", "wolverines", "4", ",", "22",
"pit-fiends"
};
char beastiesLoc[] =
{ 14, 0, 1, 3, 5, 7, 8, 10, 12, 16, 17, 19, 21, 23, 25, 29 };
char *condition[] =
{ "frothing", "manic", "crazed", "plague-ridden", "disease-carrying",
"biting", "rabid", "blood-thirsty", "ravaging", "slavering"
};
char conditionLoc[] = { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
char *place[] =
{ "in", "24", "25", "upon", "your", "mother's", "grave", "on", "24", "best",
"rug", "in", "the", "26", "you", "call", "home", "upon", "your", "heinie"
};
char placeLoc[] = { 5, 0, 3, 7, 11, 17, 20 };
char *relation[] =
{ "your", "your", "your", "your", "father's", "your", "mother's", "your",
"grandma's"
};
char relationLoc[] = { 6, 0, 1, 2, 3, 5, 7, 9 };
char *in_something[] =
{ "entrails", "anal", "cavity", "shoes", "house", "pantry", "general",
"direction", "pants", "bed"
};
char in_somethingLoc[] = { 8, 0, 1, 3, 4, 5, 6, 8, 9, 10 };
char *bad_place[] =
{ "rat", "hole", "sewer", "toxic", "dump", "oil", "refinery", "landfill",
"porto-pottie"
};
char bad_placeLoc[] = { 6, 0, 2, 3, 5, 7, 8, 9 };
char **daGrammar[27];
char *daGLoc[27];
static void
init_grammar (void)
{
daGrammar[0] = start;
daGLoc[0] = startLoc;
daGrammar[1] = adj;
daGLoc[1] = adjLoc;
daGrammar[2] = adj3;
daGLoc[2] = adj3Loc;
daGrammar[3] = adj1;
daGLoc[3] = adj1Loc;
daGrammar[4] = adj2;
daGLoc[4] = adj2Loc;
daGrammar[5] = name;
daGLoc[5] = nameLoc;
daGrammar[6] = stuff;
daGLoc[6] = stuffLoc;
daGrammar[7] = noun_and_prep;
daGLoc[7] = noun_and_prepLoc;
daGrammar[8] = organics;
daGLoc[8] = organicsLoc;
daGrammar[9] = body_parts;
daGLoc[9] = body_partsLoc;
daGrammar[10] = noun;
daGLoc[10] = nounLoc;
daGrammar[11] = animal;
daGLoc[11] = animalLoc;
daGrammar[12] = good_verb;
daGLoc[12] = good_verbLoc;
daGrammar[13] = curse;
daGLoc[13] = curseLoc;
daGrammar[14] = afflictors;
daGLoc[14] = afflictorsLoc;
daGrammar[15] = quantity;
daGLoc[15] = quantityLoc;
daGrammar[16] = numbers;
daGLoc[16] = numbersLoc;
daGrammar[17] = adv;
daGLoc[17] = advLoc;
daGrammar[18] = metaphor;
daGLoc[18] = metaphorLoc;
daGrammar[19] = force;
daGLoc[19] = forceLoc;
daGrammar[20] = bad_action;
daGLoc[20] = bad_actionLoc;
daGrammar[21] = beasties;
daGLoc[21] = beastiesLoc;
daGrammar[22] = condition;
daGLoc[22] = conditionLoc;
daGrammar[23] = place;
daGLoc[23] = placeLoc;
daGrammar[24] = relation;
daGLoc[24] = relationLoc;
daGrammar[25] = in_something;
daGLoc[25] = in_somethingLoc;
daGrammar[26] = bad_place;
daGLoc[26] = bad_placeLoc;
}
#include <ctype.h>
#include <debug.h>
#include <random.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syscall.h>
void expand (int num, char **grammar[], char *location[], int handle);
static void
usage (int ret_code, const char *message, ...) PRINTF_FORMAT (2, 3);
static void
usage (int ret_code, const char *message, ...)
{
va_list args;
if (message != NULL)
{
va_start (args, message);
vprintf (message, args);
va_end (args);
}
printf ("\n"
"Usage: insult [OPTION]...\n"
"Prints random insults to screen.\n\n"
" -h: this help message\n"
" -s <integer>: set the random seed (default 4951)\n"
" -n <integer>: choose number of insults (default 4)\n"
" -f <file>: redirect output to <file>\n");
exit (ret_code);
}
int
main (int argc, char *argv[])
{
int sentence_cnt, new_seed, i, file_flag, sent_flag, seed_flag;
int handle;
new_seed = 4951;
sentence_cnt = 4;
file_flag = 0;
seed_flag = 0;
sent_flag = 0;
handle = STDOUT_FILENO;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[1], "-h") == 0)
usage (0, NULL);
else if (strcmp (argv[i], "-s") == 0)
{
if (seed_flag++)
usage (-1, "Can't have more than one seed");
if (++i >= argc)
usage (-1, "Missing value for -s");
new_seed = atoi (argv[i]);
}
else if (strcmp (argv[i], "-n") == 0)
{
if (sent_flag++)
usage (-1, "Can't have more than one sentence option");
if (++i >= argc)
usage (-1, "Missing value for -n");
sentence_cnt = atoi (argv[i]);
if (sentence_cnt < 1)
usage (-1, "Must have at least one sentence");
}
else if (strcmp (argv[i], "-f") == 0)
{
if (file_flag++)
usage (-1, "Can't have more than one output file");
if (++i >= argc)
usage (-1, "Missing value for -f");
/* Because files have fixed length in the basic PintOS
file system, the 0 argument means that this option
will not be useful until task 4 is
implemented. */
create (argv[i], 0);
handle = open (argv[i]);
if (handle < 0)
{
printf ("%s: open failed\n", argv[i]);
return EXIT_FAILURE;
}
}
else
usage (-1, "Unrecognized flag");
}
init_grammar ();
random_init (new_seed);
hprintf (handle, "\n");
for (i = 0; i < sentence_cnt; i++)
{
hprintf (handle, "\n");
expand (0, daGrammar, daGLoc, handle);
hprintf (handle, "\n\n");
}
if (file_flag)
close (handle);
return EXIT_SUCCESS;
}
void
expand (int num, char **grammar[], char *location[], int handle)
{
char *word;
int i, which, listStart, listEnd;
which = random_ulong () % location[num][0] + 1;
listStart = location[num][which];
listEnd = location[num][which + 1];
for (i = listStart; i < listEnd; i++)
{
word = grammar[num][i];
if (!isdigit (*word))
{
if (!ispunct (*word))
hprintf (handle, " ");
hprintf (handle, "%s", word);
}
else
expand (atoi (word), grammar, location, handle);
}
}

1
src/examples/lib/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.d

View File

1
src/examples/lib/user/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.d

46
src/examples/lineup.c Normal file
View File

@@ -0,0 +1,46 @@
/* lineup.c
Converts a file to uppercase in-place.
Incidentally, another way to do this while avoiding the seeks
would be to open the input file, then remove() it and reopen
it under another handle. Because of Unix deletion semantics
this works fine. */
#include <ctype.h>
#include <stdio.h>
#include <syscall.h>
int
main (int argc, char *argv[])
{
char buf[1024];
int handle;
if (argc != 2)
exit (1);
handle = open (argv[1]);
if (handle < 0)
exit (2);
for (;;)
{
int n, i;
n = read (handle, buf, sizeof buf);
if (n <= 0)
break;
for (i = 0; i < n; i++)
buf[i] = toupper ((unsigned char) buf[i]);
seek (handle, tell (handle) - n);
if (write (handle, buf, n) != n)
printf ("write failed\n");
}
close (handle);
return EXIT_SUCCESS;
}

57
src/examples/matmult.c Normal file
View File

@@ -0,0 +1,57 @@
/* matmult.c
Test program to do matrix multiplication on large arrays.
Intended to stress virtual memory system.
Ideally, we could read the matrices off of the file system,
and store the result back to the file system!
*/
#include <stdio.h>
#include <syscall.h>
/* You should define DIM to be large enough that the arrays
don't fit in physical memory.
Dim Memory
------ --------
16 3 kB
64 48 kB
128 192 kB
256 768 kB
512 3,072 kB
1,024 12,288 kB
2,048 49,152 kB
4,096 196,608 kB
8,192 786,432 kB
16,384 3,145,728 kB */
#define DIM 128
int A[DIM][DIM];
int B[DIM][DIM];
int C[DIM][DIM];
int
main (void)
{
int i, j, k;
/* Initialize the matrices. */
for (i = 0; i < DIM; i++)
for (j = 0; j < DIM; j++)
{
A[i][j] = i;
B[i][j] = j;
C[i][j] = 0;
}
/* Multiply matrices. */
for (i = 0; i < DIM; i++)
for (j = 0; j < DIM; j++)
for (k = 0; k < DIM; k++)
C[i][j] += A[i][k] * B[k][j];
/* Done. */
exit (C[DIM - 1][DIM - 1]);
}

45
src/examples/mcat.c Normal file
View File

@@ -0,0 +1,45 @@
/* mcat.c
Prints files specified on command line to the console, using
mmap. */
#include <stdio.h>
#include <syscall.h>
int
main (int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++)
{
int fd;
mapid_t map;
void *data = (void *) 0x10000000;
int size;
/* Open input file. */
fd = open (argv[i]);
if (fd < 0)
{
printf ("%s: open failed\n", argv[i]);
return EXIT_FAILURE;
}
size = filesize (fd);
/* Map files. */
map = mmap (fd, data);
if (map == MAP_FAILED)
{
printf ("%s: mmap failed\n", argv[i]);
return EXIT_FAILURE;
}
/* Write file to console. */
write (STDOUT_FILENO, data, size);
/* Unmap files (optional). */
munmap (map);
}
return EXIT_SUCCESS;
}

68
src/examples/mcp.c Normal file
View File

@@ -0,0 +1,68 @@
/* mcp.c
Copies one file to another, using mmap. */
#include <stdio.h>
#include <string.h>
#include <syscall.h>
int
main (int argc, char *argv[])
{
int in_fd, out_fd;
mapid_t in_map, out_map;
void *in_data = (void *) 0x10000000;
void *out_data = (void *) 0x20000000;
int size;
if (argc != 3)
{
printf ("usage: cp OLD NEW\n");
return EXIT_FAILURE;
}
/* Open input file. */
in_fd = open (argv[1]);
if (in_fd < 0)
{
printf ("%s: open failed\n", argv[1]);
return EXIT_FAILURE;
}
size = filesize (in_fd);
/* Create and open output file. */
if (!create (argv[2], size))
{
printf ("%s: create failed\n", argv[2]);
return EXIT_FAILURE;
}
out_fd = open (argv[2]);
if (out_fd < 0)
{
printf ("%s: open failed\n", argv[2]);
return EXIT_FAILURE;
}
/* Map files. */
in_map = mmap (in_fd, in_data);
if (in_map == MAP_FAILED)
{
printf ("%s: mmap failed\n", argv[1]);
return EXIT_FAILURE;
}
out_map = mmap (out_fd, out_data);
if (out_map == MAP_FAILED)
{
printf ("%s: mmap failed\n", argv[2]);
return EXIT_FAILURE;
}
/* Copy files. */
memcpy (out_data, in_data, size);
/* Unmap files (optional). */
munmap (in_map);
munmap (out_map);
return EXIT_SUCCESS;
}

34
src/examples/recursor.c Normal file
View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>
int
main (int argc, char *argv[])
{
char buffer[128];
pid_t pid;
int retval = 0;
if (argc != 4)
{
printf ("usage: recursor <string> <depth> <waitp>\n");
exit (1);
}
/* Print args. */
printf ("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
/* Execute child and wait for it to finish if requested. */
if (atoi (argv[2]) != 0)
{
snprintf (buffer, sizeof buffer,
"recursor %s %d %s", argv[1], atoi (argv[2]) - 1, argv[3]);
pid = exec (buffer);
if (atoi (argv[3]))
retval = wait (pid);
}
/* Done. */
printf ("%s %s: dying, retval=%d\n", argv[1], argv[2], retval);
exit (retval);
}

21
src/examples/rm.c Normal file
View File

@@ -0,0 +1,21 @@
/* rm.c
Removes files specified on command line. */
#include <stdio.h>
#include <syscall.h>
int
main (int argc, char *argv[])
{
bool success = true;
int i;
for (i = 1; i < argc; i++)
if (!remove (argv[i]))
{
printf ("%s: remove failed\n", argv[i]);
success = false;
}
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}