diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 000000000..27e8388d0 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..6b6fa41f6 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -10,5 +10,26 @@ int main(void) { // Your code here + int x = 100; + + int rc = fork(); + + if (rc < 0) { + printf("Fork failed...\n"); + } else if (rc == 0) { + // child returns 0 to fork... + printf("The value of x in child is %d.\n", x); + + x = 101; + + } else { + // parent returns child's PID -> getpid() + printf("The value of x in parent is %d.\n", x); + + x = 102; + } + + printf("The final value of x is %d\n", x); + return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 000000000..096940011 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..da1537e48 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -9,6 +9,33 @@ int main(void) { // Your code here + FILE *fp; + int c; + + fp = fopen("text.txt", "r+"); + int rc = fork(); + + if (rc < 0) { + printf("Fork failed...\n"); + } else if (rc == 0) { + fprintf(fp, "This is the child.\n"); + } else { + fprintf(fp, "This is the parent.\n"); + } + + while(1) { + + c = fgetc(fp); + + if (feof(fp)) { + break; + } + printf("%c", c); + } + printf("\n"); + + fclose(fp); + return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..c9c038e4f 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,6 @@ +This is the parent. +This is the child. +This is the child. +This is the child. +This is the child. +This is the child. diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 000000000..c59f9a5f8 Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..73b8b2be1 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -10,6 +10,16 @@ int main(void) { // Your code here + int rc = fork(); + + if (rc < 0) { + printf("Fork failed...\n"); + } else if (rc == 0) { + printf("hello\n"); + } else { + waitpid(rc, NULL, 0); + printf("goodbye\n"); + } return 0; } diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 000000000..42b3864a8 Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..2df05baf6 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -7,10 +7,26 @@ #include #include #include +#include -int main(void) +int main(int argc, char *argv[]) { - // Your code here + // Your code here + int rc = fork(); + + + if (rc < 0) { + printf("Fork failed...\n"); + } else if (rc == 0) { + + char *my_args[3]; + + my_args[0] = strdup("/bin/ls"); + my_args[1] = strdup("ex4.c"); + my_args[1] = NULL; + + execvp(my_args[0], my_args); + } return 0; } diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100755 index 000000000..b7b1d61b1 Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..35d616c9a 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -17,6 +17,38 @@ char* msg3 = "hello world #3"; int main(void) { // Your code here + char inbuf[MSGSIZE]; + int p[2]; + + if (pipe(p) < 0) { + fprintf(stderr, "pipe failed...\n"); + exit(1); + } + + int rc = fork(); + + if (rc < 0) { + printf("fork failed...\n"); + } else if (rc == 0) { + close(p[0]); + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + close(p[1]); + } else { + + waitpid(rc, NULL, 0); + + close(p[1]); + + for (int i = 0; i < 3; i++) { + + read(p[0], inbuf, MSGSIZE); + printf("%s\n", inbuf); + } + + close(p[0]); + } return 0; -} +} \ No newline at end of file diff --git a/ex5/exercise5 b/ex5/exercise5 new file mode 100755 index 000000000..f112566d0 Binary files /dev/null and b/ex5/exercise5 differ