diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100644 index 000000000..619cf6f23 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..be4b9cc6c 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -9,6 +9,25 @@ int main(void) { // Your code here + printf("hello world!\n"); + int x = 101; + int fp = fork(); + if (fp < 0) + { // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } + else if (fp == 0) + { // child process satisfies this branch + printf("x is: %d \n", x); + x = 102; + } + else + { + printf("hello, parent here (pid: %d) of child %d\n", (int)getpid(), fp); + } + + return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100644 index 000000000..c0c38ee81 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..815cc7d2c 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -9,6 +9,25 @@ int main(void) { // Your code here + FILE *file; + file = fopen("text.txt", "r+"); + int fp = fork(); + if (fp < 0) + { // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } + else if (fp == 0) + { // child process satisfies this branch + //fprintf(file, "%s"); + fprintf(file, "Hello from child: %d\n", (int)getpid()); + } + else + { + fprintf(file, "Hello from parent: %d\n", (int)getpid()); + } + + fclose(file); return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..50d34d50b 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,2 @@ +Hello from parent: 54 +Hello from child: 55 diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..5fbee3ffe 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -10,6 +10,21 @@ int main(void) { // Your code here + int fp = fork(); + if (fp < 0) + { // fork failed; exit + fprintf(stderr, "fork failed\n"); + exit(1); + } + else if (fp == 0) + { // child process satisfies this branch + printf("hello\n"); + } + else + { + int wc = waitpid(fp, NULL, 0); + printf("goodbye\n"); + } return 0; }