diff --git a/README.md b/README.md index 4b8dba5fa..64c2be1a0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # Processes and System Calls 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. diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100644 index 000000000..69ac95607 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..7c40448c1 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -9,6 +9,19 @@ int main(void) { // Your code here - + int x = 100; + int rc = fork(); + // if fork fails return value is -1 + if(rc < 0){ + // 'fprintf' is like 'printf' except you add (where, how, what) + fprintf(stderr, "failing forks\n"); + exit(1); + } else if (rc == 0) { + // successful child fork will return a 0, so this would select the child process + printf("This is a child pid: %d\n", (int) getpid()); + } else { + printf("This is the parent pid: %d parent to child pid: %d\n", (int) getpid(), rc); + } + // printf("%d", x); return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100644 index 000000000..b246ef69f Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..71593f462 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -9,6 +9,29 @@ int main(void) { // Your code here - + FILE *ptr_file; + char buf[1000]; + int rc = fork(); + + ptr_file = fopen("./text.txt", "r+"); + if(!ptr_file) { + return 1; + } + + while (fgets(buf,1000, ptr_file) != NULL) + if(rc < 0) { + fprintf(stderr, "unable to fork\n"); + exit(1); + } else if (rc == 0) { // this is the child fork + fputs("This has been added from the child process\n", ptr_file); + printf("This is from the child: %s\n", buf); + + } else { // this is the parent + fputs("This has been added from the parent process\n", ptr_file); + printf("This is from the Parent: %s\n", buf); + } + + fclose(ptr_file); + return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..d29fb00bb 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,2 @@ +this is the text fileThis has been added from the parent process +This has been added from the child process diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100644 index 000000000..6d1698143 Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..121572474 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) { + fprintf(stderr, "failing forking\n"); + } else if (rc == 0) { + printf("hello\n"); + } else { + int wc = waitpid(rc, NULL, 0); + printf("goodbye\n"); + } return 0; } diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..ec84dbf48 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,16 @@ int main(void) { - // Your code here + // Your code here + int rc = fork(); + if (rc < 0) { + fprintf(stderr, "forking fail\n"); + exit(1); + } else if (rc == 0) { + // child + } else { + // parent + } return 0; } diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..66f7d3fd2 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -17,6 +17,15 @@ char* msg3 = "hello world #3"; int main(void) { // Your code here - + int rc = fork(); + + if (rc < 0) { + fprintf(stderr, "fail fail fail\n"); + exit(1); + } else if (rc == 0) { + // this is the child + } else { + // this is the parent + } return 0; }