Skip to content
Browse files

Adding exercise from chapter 14.

  • Loading branch information...
1 parent 8fd23ae commit 9b3cc49575525452c34846a6071f2ef35cffb2f3 Matt Spaulding committed Mar 11, 2012
Showing with 102 additions and 0 deletions.
  1. +16 −0 14-filesystems/Makefile
  2. +86 −0 14-filesystems/benchmark.c
View
16 14-filesystems/Makefile
@@ -0,0 +1,16 @@
+CC=gcc
+SOURCE=benchmark.c
+OBJECT=benchmark.o
+EXE=benchmark
+
+all: $(EXE)
+
+$(EXE): $(OBJECT)
+ $(CC) $(LDFLAGS) $(OBJECT) -o $@
+
+.o:
+ $(CC) $(CFLAGS) $< -o $@
+
+clean:
+ rm -f $(OBJECT) $(EXE)
+
View
86 14-filesystems/benchmark.c
@@ -0,0 +1,86 @@
+/*
+ * benchmark - time drive operations
+ * Copyright (C) 2012 Matt Spaulding
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+
+#define FILE_NUM 50000
+
+int main(int argc, char *argv[])
+{
+ char template[8];
+ char *files[FILE_NUM];
+ struct timeval start, end;
+ int64_t t;
+ int i;
+ int fd;
+
+ if (gettimeofday(&start, NULL) == -1) {
+ perror("gettimeofday");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("creating files... ");
+ fflush(stdout);
+ for (i = 0; i < FILE_NUM; i++) {
+ strncpy(template, "nXXXXXX", 7);
+ files[i] = alloca(8);
+ strncpy(files[i], mktemp(template), 7);
+
+ fd = open(files[i], O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH );
+ if (fd == -1) {
+ perror("open");
+ exit(EXIT_FAILURE);
+ }
+
+ if (write(fd, "\1", 1) == -1) {
+ perror("write");
+ exit(EXIT_FAILURE);
+ }
+
+ if (close(fd) == -1) {
+ perror("close");
+ exit(EXIT_FAILURE);
+ }
+ }
+ printf("done\n");
+
+ printf("removing files... ");
+ fflush(stdout);
+ for (i = 0; i < FILE_NUM; i++) {
+ if (unlink(files[i]) == -1) {
+ perror("unlink");
+ exit(EXIT_FAILURE);
+ }
+ }
+ printf("done\n");
+
+ if (gettimeofday(&end, NULL) == -1) {
+ perror("gettimeofday");
+ exit(EXIT_FAILURE);
+ }
+
+ t = end.tv_sec - start.tv_sec;
+ printf("Time Elapsed: %ld secs\n", t);
+
+ exit(EXIT_SUCCESS);
+}
+

0 comments on commit 9b3cc49

Please sign in to comment.
Something went wrong with that request. Please try again.