diff --git a/README.md b/README.md index 4b8dba5fa..37e4fa518 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Processes and System Calls +init.... + Now that we've talked a bit about how processes and system calls work at a high level, it's time to apply these concepts by doing some exercises related to process creation and making system calls. We'll be utilizing the `fork()`, `exec()`, `wait()`, and `pipe()` system calls in order to create processes and even have them pass messages to each other. ## Objective diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 000000000..e21eff2b1 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..f4dc679af 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -9,6 +9,28 @@ int main(void) { // Your code here + // init x + 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("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; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 000000000..c79504c46 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..c60f08f69 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -9,6 +9,31 @@ int main(void) { // Your code here + FILE *fp; + + fp = fopen("text.txt", "w+"); + printf("Before forking pid: %d\n", (int)getpid()); + + fprintf(fp, "%s %s %s %s %d\n", "File", "write", "before", "fork", (int)getpid()); + + pid_t pid = fork(); + + if (pid < 0) //fork failed + { + exit(1); + } + else if (pid == 0) //child process + { + printf("AFTER forking CHILD pid: %d\n", (int)getpid()); + fprintf(fp, "%s %s %d\n", "Child-File", "write", (int)getpid()); + } + else + { + printf("AFTER forking PARENT pid: %d\n", (int)getpid()); + fprintf(fp, "%s %s %d\n", "Parent-File", "write", (int)getpid()); + } + fclose(fp); + return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..96a3e83bb 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,4 @@ +File write before fork 4563 +Parent-File write 4563 +File write before fork 4563 +Child-File write 4564 diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 000000000..fb836c912 Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..43ee8339e 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -10,6 +10,23 @@ int main(void) { // Your code here + printf("Before forking pid: %d\n", (int)getpid()); + + pid_t pid = fork(); + + if (pid < 0) + { + exit(1); + } + else if (pid == 0) + { + printf("AFTER forking CHILD says Hello pid: %d\n", (int)getpid()); + } + else + { + waitpid(pid, NULL, 0); + printf("AFTER forking PARENT says Goodbye pid: %d\n", (int)getpid()); + } return 0; }