From d0aff4bfe5c1196dbcf39fac38f2c03a2e929645 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Wed, 20 Feb 2019 13:18:39 -0700 Subject: [PATCH 1/9] Ian Cameron Processes --- ex1/ex1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..82e707000 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -8,7 +8,7 @@ int main(void) { - // Your code here + int x = 100; return 0; } From 99f73175c25421f888800aac551e579bbe69e154 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Wed, 20 Feb 2019 14:13:17 -0700 Subject: [PATCH 2/9] adds fork --- ex1/ex1.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ex1/ex1.c b/ex1/ex1.c index 82e707000..819421d38 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -10,5 +10,22 @@ int main(void) { int x = 100; + printf("Parent PID: %d, x = %d \n", (int)getpid(), x); + + int rc = fork(); + + if (rc == 0) + { + printf("Fork successful. We are the child process.\n Child PID: %d, Parent PID: %d", getpid(), getppid()); + } + else if (rc > 0) + { + printf("We are the parent process.\n Parent PID: %d of Child PID: %d", getpid(), rc, x); + } + else + { + printf(stderr, "Fork failed"); + } + return 0; } From 08012e6a7d296233d7d0c365b50f7ac5eec9cc31 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Wed, 20 Feb 2019 14:57:27 -0700 Subject: [PATCH 3/9] adds fopen --- ex2/ex2.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..e92fa146b 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -8,7 +8,19 @@ int main(void) { - // Your code here - + FILE *fptr; + fptr = fopen("text.txt", "w"); + + int rc = fork(); + + if (rc < 0) { // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } else if (rc == 0) { // child process satisfies this branch + printf("hello, child here (pid: %d) \n", (int) getpid()); + } else { + printf("hello, parent here (pid: %d) of child %d\n", (int) getpid(), rc); + } + return 0; } From 5cc331486d465decef403420dc540c0138b0f2e4 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Wed, 20 Feb 2019 15:40:03 -0700 Subject: [PATCH 4/9] adds wait --- ex2/ex2.c | 6 ++++++ ex3/ex3.c | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ex2/ex2.c b/ex2/ex2.c index e92fa146b..e6bc2670d 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -22,5 +22,11 @@ int main(void) printf("hello, parent here (pid: %d) of child %d\n", (int) getpid(), rc); } + if (fptr == NULL) + { + printf("Error opening text file!"); + exit(1); + } + return 0; } diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..1ded242dc 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -9,7 +9,22 @@ int main(void) { - // Your code here + int rc = fork(); + if (rc < 0) + { + fprintf(stderr, "Fork failed"); + exit(1); + } + else if (rc == 0) + { + printf("Hello from Child %d", (int) getpid()); + } + else + { + int wc = waitpid(rc, NULL, 0); //wait or child to terminate, dont care about status, no flags + printf("Goodbye from Parent %d of Child %d", (int) getpid(), rc); + } + return 0; } From 3f00c1968f0eeb043bef9ae9a96010cb5768f3aa Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Wed, 20 Feb 2019 15:53:52 -0700 Subject: [PATCH 5/9] adds exec --- ex4/ex4.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..19ea3039d 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,17 @@ int main(void) { - // Your code here + int rc = fork; + + if (rc < 0) + { + fprintf(stderr, "Fork failed!\n"); + exit(1); + } + else + { + execl("/bin/ls", "ls", NULL); + } return 0; } From da52d42057d2e4cd9517bbd443d8e27cc37c02df Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Wed, 20 Feb 2019 16:26:09 -0700 Subject: [PATCH 6/9] adds execvp --- ex4/ex4.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ex4/ex4.c b/ex4/ex4.c index 19ea3039d..11b508291 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -19,7 +19,9 @@ int main(void) } else { - execl("/bin/ls", "ls", NULL); + //execl("/bin/ls", "ls", NULL); + char *args[] = {"ls", "-l", NULL}; + execvp(args[0], args); } return 0; From 6ad82a4383eb5297255934495d9ebc68fc5ae2f3 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Thu, 21 Feb 2019 13:53:57 -0700 Subject: [PATCH 7/9] starts pipe --- ex5/ex5.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..1a561db66 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -16,7 +16,14 @@ char* msg3 = "hello world #3"; int main(void) { - // Your code here + printf("parent %d\n", (int) getpid()); + char inbuffer[MSGSIZE]; + int p; + + if (pipe(p) > 0) + { + + } return 0; } From 9d38dfe2228d9751c0f57b3c1c67138a4d640dd7 Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Thu, 21 Feb 2019 15:29:50 -0700 Subject: [PATCH 8/9] updates pipe --- ex5/ex5.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ex5/ex5.c b/ex5/ex5.c index 1a561db66..24ddf1c8b 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -22,7 +22,8 @@ int main(void) if (pipe(p) > 0) { - + fprintf(stderr, "Pipe failure\n"); + exit(1); } return 0; From 098350d2de21bc35ac8a48751943e312c7a153dc Mon Sep 17 00:00:00 2001 From: IAN CAMERON Date: Thu, 21 Feb 2019 16:48:50 -0700 Subject: [PATCH 9/9] finishes pipe --- ex5/ex5.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/ex5/ex5.c b/ex5/ex5.c index 24ddf1c8b..9bf72d734 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -18,13 +18,38 @@ int main(void) { printf("parent %d\n", (int) getpid()); char inbuffer[MSGSIZE]; - int p; + int p[2]; - if (pipe(p) > 0) + if (pipe(p) < 0) { fprintf(stderr, "Pipe failure\n"); exit(1); } + int rc = fork(); + if (rc < 0) + { + fprintf(stderr, "Fork failure\n"); + exit(2); + } + else if (rc == 0) + { + printf("Child writing to pipe\n"); + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + } + else + { + close(p[1]); //close write branch + wait(NULL); //not necessary b/c processes can run at same time but doesnt hurt + //ssize_t read(int fd, void *buf, size_t count); + for (int i = 0; i < 3; i++) + { + read(p[0], inbuffer, MSGSIZE); + printf("%s\n", inbuffer); + } + close(p[0]); //close read + } return 0; }