diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 000000000..5926bb190 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..0f399da4b 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -9,6 +9,27 @@ int main(void) { // Your code here + int x = 100; + printf("Hello there, before we call fork(), this is our processID is: (pid: %d) \n", (int)getpid()); + printf("This is the parents value for x: %d \n", x); + int rc = fork(); + + if (rc < 0) + { + printf(stderr, "Forking failed \n"); + } + else if (rc == 0) + { + + printf("Hello there, if rc==0, this is our child processID is: (pid: %d) \n", (int)getpid()); + printf("This is the childs value for x if rc==0: %d \n", x); + } + else + { + int x = 200; + printf("hello, if rc > 0, this is parent here (pid: %d) of child %d\n", (int)getpid(), rc); + printf("This is the childs value for x if rc > 0: %d \n", x); + } return 0; -} +} \ No newline at end of file diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 000000000..0faed262c Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..ac3060613 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -5,10 +5,72 @@ #include #include #include +#include int main(void) { // Your code here + FILE *file; + printf("Hello there, before we call fork(), this is our processID is: (pid: %d) \n", (int)getpid()); + file = fopen("text.txt", "a+"); + while (1) + { + int c = fgetc(file); + if (feof(file)) + { + break; + } + printf("%c", c); + } + + int rc = fork(); + + if (rc < 0) + { + printf(stderr, "\nForking failed \n"); + exit(1); + } + else if (rc == 0) + { + printf("\nWe are now attempting to wait for child process to finish. . . \n"); + file = fopen("text.txt", "a+"); + printf("\nHello there, if rc==0, this is our child processID is: (pid: %d) \n", (int)getpid()); + // printf("This is the childs value for file (opening a file): %d \n", file); + file = fopen("text.txt", "a+"); + fprintf(file, "%s %s %s %s %s %s %s", "\nWe", "are", "writing", "in", "the", "child", "process\n"); + fclose(file); + file = fopen("text.txt", "a+"); + while (1) + { + int c = fgetc(file); + if (feof(file)) + { + break; + } + printf("%c", c); + } + fclose(file); + } + else + { + // int wc = waitpid(rc, NULL, 0); + printf("\nOur child has now finished executing, we are back in Parent process with PID: (%d)\n", (int)getpid()); + file = fopen("text.txt", "a+"); + fprintf(file, "%s %s %s %s %s %s %s", "\nWe", "are", "writing", "after", "the", "child", "process\n"); + fclose(file); + // printf("This is the childs value for file (opening a file): %d \n", file); + file = fopen("text.txt", "a+"); + while (1) + { + int c = fgetc(file); + if (feof(file)) + { + break; + } + printf("%c", c); + } + fclose(file); + } return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..327a4a3dc 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,4 @@ + +We are writing after the child process + +We are writing in the child process diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 000000000..985d7c1c9 Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..c533c6cf5 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -10,6 +10,24 @@ int main(void) { // Your code here + int rc = fork(); + + if (rc < 0) + { + printf(stderr, "Forking failed \n"); + } + else if (rc == 0) + { + + printf("Hello there, if rc==0, this is our child processID is: (pid: %d) \n", (int)getpid()); + printf("Goodbye\n"); + } + else + { + int wc = waitpid(rc, NULL, 0); + printf("Hello there, if rc==0, this is our Parent processID is: (pid: %d) \n", (int)getpid()); + printf("Hello\n"); + } return 0; -} +} \ No newline at end of file diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 000000000..4bf4eaec7 Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..3a8044db9 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,22 @@ int main(void) { - // Your code here + // Your code here + int status; + // 2 strings, one is /bin/ls and the other is to finish with a NULL + char *args[2]; + + args[0] = "/bin/ls"; + args[1] = NULL; + int y = fork(); + + if (y == 0) { + execv(args[0], args); + + } else { + + wait(&status); + } return 0; }