Skip to content

Commit dade55e

Browse files
vagrantvagrant
vagrant
authored and
vagrant
committed
Add exercise files
1 parent b2dcf17 commit dade55e

10 files changed

+352
-2
lines changed

asm3/tailcall4.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//!-O0
1+
//!-O3
22
extern int sum(int a, int b);
33

44
void f(int a, int b) {
5-
sum(a, b);
5+
(long)sum(b, a);
66
}

storage4/GNUmakefile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
PROGRAMS = stdio syscall mmap create_mmap_file direct time-mmap time-syscall
2+
3+
all: $(PROGRAMS)
4+
5+
include ../common/rules.mk
6+
7+
%.o: %.c $(BUILDSTAMP)
8+
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPCFLAGS) $(O) -o $@ -c $<
9+
10+
%: %.o
11+
$(CC) $(CFLAGS) -o $@ $^
12+
13+
clean:
14+
rm -f *.o $(PROGRAMS) *.out
15+
rm -rf $(DEPSDIR) *.dSYM
16+
17+
.PHONY: all clean

storage4/create_mmap_file.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <sys/types.h>
2+
#include <fcntl.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <unistd.h>
6+
#include "params.h"
7+
8+
int main(int argc, char *argv[]) {
9+
(void)argc;
10+
(void)argv;
11+
12+
const char *datafile = "mmap-data.out";
13+
int fd;
14+
15+
fd = open(datafile, O_CREAT | O_EXCL | O_WRONLY, 0644);
16+
if (fd < 0) {
17+
perror("open");
18+
exit(1);
19+
}
20+
if (lseek(fd, MMAP_FILE_SIZE - 1, SEEK_SET) != MMAP_FILE_SIZE - 1) {
21+
perror("lseek");
22+
exit(1);
23+
}
24+
if (write(fd, "", 1) != 1) {
25+
perror("write");
26+
exit(1);
27+
}
28+
if (close(fd)) {
29+
perror("close");
30+
exit(1);
31+
}
32+
33+
exit(0);
34+
}

storage4/direct.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "../storage3/iobench.h"
2+
3+
int main(int argc, char *argv[]) {
4+
#ifndef O_DIRECT
5+
fprintf(stderr, "ERROR: O_DIRECT not supported here\n");
6+
# define O_DIRECT 0
7+
#endif
8+
#ifndef O_DSYNC
9+
# define O_DSYNC 0
10+
#endif
11+
12+
if (argc < 2) {
13+
printf("Usage: r01-directsector file\n");
14+
exit(1);
15+
}
16+
17+
int fd = STDIN_FILENO;
18+
if (isatty(fd))
19+
fd = open(argv[1], O_RDONLY | O_DIRECT | O_DSYNC);
20+
if (fd < 0) {
21+
perror("open");
22+
exit(1);
23+
}
24+
25+
size_t size = filesize(fd);
26+
size_t block_size = 512;
27+
char* buf;
28+
posix_memalign((void **) &buf, 512, block_size);
29+
30+
size_t n = 0;
31+
while (n < size) {
32+
ssize_t r = read(fd, buf, block_size);
33+
if (r == -1) {
34+
perror("read");
35+
exit(1);
36+
} else if (r != (ssize_t) block_size)
37+
break;
38+
n += r;
39+
printf("%s\n", buf);
40+
}
41+
42+
close(fd);
43+
fprintf(stderr, "Done\n");
44+
}

storage4/mmap.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#define _POSIX_SOURCE
2+
#include <sys/mman.h>
3+
#include <fcntl.h>
4+
#include <signal.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
#include <unistd.h>
9+
#include "params.h"
10+
11+
int keepgoing = 1;
12+
13+
// Signal handler
14+
void breakme(int sig) {
15+
(void)sig;
16+
keepgoing = 0;
17+
}
18+
19+
int main(int argc, char *argv[]) {
20+
const char *datafile = "mmap-data.out";
21+
char *buf, *p;
22+
int fd;
23+
struct sigaction sa;
24+
25+
if (argc < 2) {
26+
printf("Usage: stdio <str>\n");
27+
exit(1);
28+
}
29+
memset(&sa, 0, sizeof(sa));
30+
sa.sa_handler = breakme;
31+
sigaction(SIGINT, &sa, NULL);
32+
33+
// We're going to open a file for mmapping and then we're going to
34+
// loop 'writing' our string to the mmapped region.
35+
fd = open(datafile, O_RDWR, 0644);
36+
if (fd < 0) {
37+
perror("open");
38+
exit(1);
39+
}
40+
buf = mmap(NULL,
41+
MMAP_FILE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
42+
p = buf;
43+
44+
ssize_t sz = strlen(argv[1]);
45+
ssize_t bytes_left = 0;
46+
while (keepgoing) {
47+
// Write a bunch so that we fill buffers more quickly
48+
for (int i = 0; i < 50; i++) {
49+
// Check for wrap around
50+
if (bytes_left < sz) {
51+
p = buf;
52+
bytes_left = MMAP_FILE_SIZE;
53+
}
54+
memcpy(p, argv[1], sz);
55+
p += sz;
56+
bytes_left -= sz;
57+
58+
}
59+
sleep(1);
60+
}
61+
62+
if (munmap(buf, MMAP_FILE_SIZE) != 0) {
63+
perror("munmap");
64+
exit(1);
65+
}
66+
if (close(fd)) {
67+
perror("close");
68+
exit(1);
69+
}
70+
71+
exit(0);
72+
}

storage4/params.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define MMAP_FILE_SIZE ((off_t)(1024 * 1024))

storage4/stdio.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#define _POSIX_SOURCE
2+
#include <signal.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <unistd.h>
7+
8+
int keepgoing = 1;
9+
10+
// Signal handler
11+
void breakme(int sig) {
12+
(void)sig;
13+
keepgoing = 0;
14+
}
15+
16+
int main(int argc, char *argv[]) {
17+
const char *datafile = "stdio-data.out";
18+
FILE *fp;
19+
struct sigaction sa;
20+
21+
if (argc < 2) {
22+
printf("Usage: stdio <str>\n");
23+
exit(1);
24+
}
25+
memset(&sa, 0, sizeof(sa));
26+
sa.sa_handler = breakme;
27+
sigaction(SIGINT, &sa, NULL);
28+
29+
// We're going to open a file for writing and
30+
// loop writing our buffer to it (via stdio),
31+
// sleeping between each write.
32+
fp = fopen(datafile, "w+");
33+
if (fp == NULL) {
34+
perror("fopen");
35+
exit(1);
36+
}
37+
38+
size_t sz = strlen(argv[1]);
39+
while (keepgoing) {
40+
// Write a bunch so that we fill buffers more quickly
41+
for (int i = 0; i < 50; i++) {
42+
if (fwrite(argv[1], sz, 1, fp) != 1) {
43+
perror("fwrite");
44+
exit(1);
45+
}
46+
}
47+
sleep(1);
48+
}
49+
50+
if (fclose(fp)) {
51+
perror("fclose");
52+
exit(1);
53+
}
54+
55+
exit(0);
56+
}

storage4/syscall.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#define _POSIX_SOURCE
2+
#include <fcntl.h>
3+
#include <signal.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
#include <unistd.h>
8+
9+
int keepgoing = 1;
10+
11+
// Signal handler
12+
void breakme(int sig) {
13+
(void)sig;
14+
keepgoing = 0;
15+
}
16+
17+
int main(int argc, char *argv[]) {
18+
const char *datafile = "syscall-data.out";
19+
int fd;
20+
struct sigaction sa;
21+
22+
if (argc < 2) {
23+
printf("Usage: stdio <str>\n");
24+
exit(1);
25+
}
26+
memset(&sa, 0, sizeof(sa));
27+
sa.sa_handler = breakme;
28+
sigaction(SIGINT, &sa, NULL);
29+
30+
// We're going to open a file for writing and
31+
// loop writing our string to it sleeping between
32+
// each write.
33+
fd = open(datafile, O_APPEND | O_CREAT | O_WRONLY, 0644);
34+
if (fd < 0) {
35+
perror("open");
36+
exit(1);
37+
}
38+
39+
ssize_t sz = strlen(argv[1]);
40+
while (keepgoing) {
41+
// Write a bunch so that we fill buffers more quickly
42+
for (int i = 0; i < 50; i++) {
43+
if (write(fd, argv[1], sz) != sz) {
44+
perror("write");
45+
exit(1);
46+
}
47+
}
48+
sleep(1);
49+
}
50+
51+
if (close(fd)) {
52+
perror("close");
53+
exit(1);
54+
}
55+
56+
exit(0);
57+
}

storage4/time-mmap.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "../storage3/iobench.h"
2+
#include <sys/mman.h>
3+
4+
int main() {
5+
int fd = STDIN_FILENO;
6+
if (isatty(fd))
7+
fd = open("data", O_RDONLY);
8+
if (fd < 0) {
9+
perror("open");
10+
exit(1);
11+
}
12+
13+
size_t size = filesize(fd);
14+
size_t block_size = 512;
15+
char* buf = (char*) malloc(block_size);
16+
double start = tstamp();
17+
18+
char* file_data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
19+
if (file_data == MAP_FAILED) {
20+
perror("mmap");
21+
exit(1);
22+
}
23+
24+
size_t n = 0;
25+
while (n < size) {
26+
memcpy(buf, &file_data[n], block_size);
27+
n += block_size;
28+
if (n % PRINT_FREQUENCY == 0)
29+
report(n, tstamp() - start);
30+
}
31+
32+
munmap(file_data, size);
33+
close(fd);
34+
report(n, tstamp() - start);
35+
fprintf(stderr, "\n");
36+
}

storage4/time-syscall.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "../storage3/iobench.h"
2+
3+
int main() {
4+
int fd = STDIN_FILENO;
5+
if (isatty(fd))
6+
fd = open("data", O_RDONLY);
7+
if (fd < 0) {
8+
perror("open");
9+
exit(1);
10+
}
11+
12+
size_t size = filesize(fd);
13+
size_t block_size = 512;
14+
char* buf = (char*) malloc(block_size);
15+
double start = tstamp();
16+
17+
size_t n = 0;
18+
while (n < size) {
19+
ssize_t r = read(fd, buf, block_size);
20+
if (r == -1) {
21+
perror("read");
22+
exit(1);
23+
} else if (r != (ssize_t) block_size)
24+
break;
25+
n += r;
26+
if (n % PRINT_FREQUENCY == 0)
27+
report(n, tstamp() - start);
28+
}
29+
30+
close(fd);
31+
report(n, tstamp() - start);
32+
fprintf(stderr, "\n");
33+
}

0 commit comments

Comments
 (0)