From 6e8f4b70307c362fecad990b8357a1c4978ea58a Mon Sep 17 00:00:00 2001 From: Remzi Arpaci-Dusseau Date: Wed, 15 Aug 2018 12:01:26 -0500 Subject: [PATCH] initial push of CPU API code --- cpu-api/Makefile | 18 ++++++++++++++++++ cpu-api/README.md | 8 ++++++++ cpu-api/p1.c | 23 +++++++++++++++++++++++ cpu-api/p2.c | 26 ++++++++++++++++++++++++++ cpu-api/p3.c | 32 ++++++++++++++++++++++++++++++++ cpu-api/p4.c | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 141 insertions(+) create mode 100644 cpu-api/Makefile create mode 100644 cpu-api/README.md create mode 100644 cpu-api/p1.c create mode 100644 cpu-api/p2.c create mode 100644 cpu-api/p3.c create mode 100644 cpu-api/p4.c diff --git a/cpu-api/Makefile b/cpu-api/Makefile new file mode 100644 index 0000000..55f651c --- /dev/null +++ b/cpu-api/Makefile @@ -0,0 +1,18 @@ + +all: p1 p2 p3 p4 + +clean: + rm -f p1 p2 p3 p4 + +p1: p1.c + gcc -o p1 p1.c -Wall + +p2: p2.c + gcc -o p2 p2.c -Wall + +p3: p3.c + gcc -o p3 p3.c -Wall + +p4: p4.c + gcc -o p4 p4.c -Wall + diff --git a/cpu-api/README.md b/cpu-api/README.md new file mode 100644 index 0000000..42cfca3 --- /dev/null +++ b/cpu-api/README.md @@ -0,0 +1,8 @@ + +To compile, just type "make". + +See the highly primitive "Makefile" for details. + +Then run p1, p2, p3, or p4, as need be. + + diff --git a/cpu-api/p1.c b/cpu-api/p1.c new file mode 100644 index 0000000..7ca12fa --- /dev/null +++ b/cpu-api/p1.c @@ -0,0 +1,23 @@ +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + printf("hello world (pid:%d)\n", (int) getpid()); + int rc = fork(); + if (rc < 0) { + // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } else if (rc == 0) { + // child (new process) + printf("hello, I am child (pid:%d)\n", (int) getpid()); + } else { + // parent goes down this path (original process) + printf("hello, I am parent of %d (pid:%d)\n", + rc, (int) getpid()); + } + return 0; +} diff --git a/cpu-api/p2.c b/cpu-api/p2.c new file mode 100644 index 0000000..03db05e --- /dev/null +++ b/cpu-api/p2.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + printf("hello world (pid:%d)\n", (int) getpid()); + int rc = fork(); + if (rc < 0) { + // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } else if (rc == 0) { + // child (new process) + printf("hello, I am child (pid:%d)\n", (int) getpid()); + sleep(1); + } else { + // parent goes down this path (original process) + int wc = wait(NULL); + printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", + rc, wc, (int) getpid()); + } + return 0; +} diff --git a/cpu-api/p3.c b/cpu-api/p3.c new file mode 100644 index 0000000..daa7699 --- /dev/null +++ b/cpu-api/p3.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + printf("hello world (pid:%d)\n", (int) getpid()); + int rc = fork(); + if (rc < 0) { + // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } else if (rc == 0) { + // child (new process) + printf("hello, I am child (pid:%d)\n", (int) getpid()); + char *myargs[3]; + myargs[0] = strdup("wc"); // program: "wc" (word count) + myargs[1] = strdup("p3.c"); // argument: file to count + myargs[2] = NULL; // marks end of array + execvp(myargs[0], myargs); // runs word count + printf("this shouldn't print out"); + } else { + // parent goes down this path (original process) + int wc = wait(NULL); + printf("hello, I am parent of %d (wc:%d) (pid:%d)\n", + rc, wc, (int) getpid()); + } + return 0; +} diff --git a/cpu-api/p4.c b/cpu-api/p4.c new file mode 100644 index 0000000..1be9aee --- /dev/null +++ b/cpu-api/p4.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + int rc = fork(); + if (rc < 0) { + // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } else if (rc == 0) { + // child: redirect standard output to a file + close(STDOUT_FILENO); + open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU); + + // now exec "wc"... + char *myargs[3]; + myargs[0] = strdup("wc"); // program: "wc" (word count) + myargs[1] = strdup("p4.c"); // argument: file to count + myargs[2] = NULL; // marks end of array + execvp(myargs[0], myargs); // runs word count + } else { + // parent goes down this path (original process) + int wc = wait(NULL); + assert(wc >= 0); + } + return 0; +}