summaryrefslogtreecommitdiff
path: root/user/grind.c
diff options
context:
space:
mode:
authorRobert Morris <[email protected]>2019-11-06 11:18:43 -0500
committerRobert Morris <[email protected]>2019-11-06 11:18:43 -0500
commit16b3b63f06c1ea17da484aeebea4a57fb2a6e44a (patch)
treece4ddb9b3fe85219aa3fb57d290d4e302efe5098 /user/grind.c
parent028af2764622d489583cd88935cd1d2a7fbe8248 (diff)
downloadxv6-labs-16b3b63f06c1ea17da484aeebea4a57fb2a6e44a.tar.gz
xv6-labs-16b3b63f06c1ea17da484aeebea4a57fb2a6e44a.tar.bz2
xv6-labs-16b3b63f06c1ea17da484aeebea4a57fb2a6e44a.zip
grind: run parallel system calls forever
Diffstat (limited to 'user/grind.c')
-rw-r--r--user/grind.c160
1 files changed, 160 insertions, 0 deletions
diff --git a/user/grind.c b/user/grind.c
new file mode 100644
index 0000000..97a8a93
--- /dev/null
+++ b/user/grind.c
@@ -0,0 +1,160 @@
+//
+// run random system calls in parallel forever.
+//
+
+#include "kernel/param.h"
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user/user.h"
+#include "kernel/fs.h"
+#include "kernel/fcntl.h"
+#include "kernel/syscall.h"
+#include "kernel/memlayout.h"
+#include "kernel/riscv.h"
+
+// from FreeBSD.
+int
+do_rand(unsigned long *ctx)
+{
+/*
+ * Compute x = (7^5 * x) mod (2^31 - 1)
+ * without overflowing 31 bits:
+ * (2^31 - 1) = 127773 * (7^5) + 2836
+ * From "Random number generators: good ones are hard to find",
+ * Park and Miller, Communications of the ACM, vol. 31, no. 10,
+ * October 1988, p. 1195.
+ */
+ long hi, lo, x;
+
+ /* Transform to [1, 0x7ffffffe] range. */
+ x = (*ctx % 0x7ffffffe) + 1;
+ hi = x / 127773;
+ lo = x % 127773;
+ x = 16807 * lo - 2836 * hi;
+ if (x < 0)
+ x += 0x7fffffff;
+ /* Transform to [0, 0x7ffffffd] range. */
+ x--;
+ *ctx = x;
+ return (x);
+}
+
+unsigned long rand_next = 1;
+
+int
+rand(void)
+{
+ return (do_rand(&rand_next));
+}
+
+void
+go()
+{
+ int fd = -1;
+ static char buf[999];
+ char *break0 = sbrk(0);
+
+ while(1){
+ int what = rand() % 20;
+ if(what == 1){
+ close(open("a", O_CREATE|O_RDWR));
+ } else if(what == 2){
+ close(open("b", O_CREATE|O_RDWR));
+ } else if(what == 3){
+ unlink("a");
+ } else if(what == 4){
+ unlink("b");
+ } else if(what == 5){
+ close(fd);
+ fd = open("a", O_CREATE|O_RDWR);
+ } else if(what == 6){
+ close(fd);
+ fd = open("b", O_CREATE|O_RDWR);
+ } else if(what == 7){
+ write(fd, buf, sizeof(buf));
+ } else if(what == 8){
+ read(fd, buf, sizeof(buf));
+ } else if(what == 9){
+ mkdir("a");
+ } else if(what == 10){
+ mkdir("b");
+ } else if(what == 11){
+ unlink("b");
+ link("a", "b");
+ } else if(what == 12){
+ unlink("a");
+ link("b", "a");
+ } else if(what == 13){
+ int pid = fork();
+ if(pid == 0){
+ exit(0);
+ }
+ wait(0);
+ } else if(what == 14){
+ int pid = fork();
+ if(pid == 0){
+ fork();
+ fork();
+ exit(0);
+ }
+ wait(0);
+ } else if(what == 15){
+ sbrk(6011);
+ } else if(what == 16){
+ if(sbrk(0) > break0)
+ sbrk(-(sbrk(0) - break0));
+ } else if(what == 17){
+ int pid = fork();
+ if(pid == 0){
+ close(open("a", O_CREATE|O_RDWR));
+ exit(0);
+ }
+ kill(pid);
+ wait(0);
+ } else if(what == 18){
+ int pid = fork();
+ if(pid == 0){
+ kill(getpid());
+ exit(0);
+ }
+ wait(0);
+ }
+ }
+}
+
+int
+main()
+{
+ int pid1 = fork();
+ if(pid1 < 0){
+ printf("grind: fork failed\n");
+ exit(1);
+ }
+ if(pid1 == 0){
+ rand_next = 31;
+ go();
+ exit(0);
+ }
+
+ int pid2 = fork();
+ if(pid2 < 0){
+ printf("grind: fork failed\n");
+ exit(1);
+ }
+ if(pid2 == 0){
+ rand_next = 7177;
+ go();
+ exit(0);
+ }
+
+ int st1 = -1;
+ wait(&st1);
+ if(st1 != 0){
+ kill(pid1);
+ kill(pid2);
+ }
+ int st2 = -1;
+ wait(&st2);
+
+ exit(0);
+}