diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 000000000..5164d0951 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..48bd43892 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -9,6 +9,16 @@ int main(void) { // Your code here + int x = 100; + if(fork() == 0){ + x = 30; + printf("Child %i\n", x); + }else{ + x = 20; + printf("Parent %i\n", x); + } return 0; } + +//A: the variable will be unique to each instance when the variable value is changed diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100755 index 000000000..18368eb30 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..9c8846570 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -9,6 +9,19 @@ int main(void) { // Your code here - + FILE * file; + file = fopen("text.txt", "r+"); + + if (fork() == 0){ + fprintf(file, "%s", "child!"); + printf("child: %d\n", fileno(file)); + }else{ + fprintf(file, "%s", "parent"); + printf("parent %d\n", fileno(file)); + } + fclose(file); return 0; } + + +// A: Yes the child and the parent can access the file descriptor returned by fopen and they both get written in! \ No newline at end of file diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..3cb3b9193 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1 @@ +parentchild! \ No newline at end of file diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100755 index 000000000..ee8699765 Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..317eca779 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -10,6 +10,12 @@ int main(void) { // Your code here - + int temp = fork(); + wait(NULL); + if(temp == 0){ + printf("Child -> Hello \n"); + }else{ + printf("Parent -> Goodbye \n"); + } return 0; } diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 000000000..4e018a130 Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..6c0ca1c8f 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,22 @@ int main(void) { - // Your code here + // Your code here + int temp = fork(); + if(temp == 0){ + char *args[2]; + args[0] = "bin/ls"; + args[1] = NULL; + execvp(args[0], args); + printf("Child process\n"); + }else if(temp < 0){ + printf("fork failed\n"); + } + else + { + int wc = waitpid(temp, NULL, 0); + printf("Completed child process\n"); + } return 0; } diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100755 index 000000000..4948b460b Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..fb9ae9af7 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -17,6 +17,25 @@ char* msg3 = "hello world #3"; int main(void) { // Your code here + char buffer[MSGSIZE]; + int p[2]; + if(pipe(p) < 0){ + fprintf(stderr, "error error error\n"); + exit(1); + } + int temp = fork(); + if(temp == 0){ + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + } + else{ + for (int i = 0; i < 3; i++){ + read(p[0], buffer, MSGSIZE); + printf("%s\n", buffer); + } + } + return 0; -} +} \ No newline at end of file diff --git a/ex6/ex6 b/ex6/ex6 new file mode 100755 index 000000000..e1162f6fb Binary files /dev/null and b/ex6/ex6 differ diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..7e151d62e 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -21,6 +21,14 @@ and `clock_gettime()` should work just fine. int main() { // Your code here - + int timed = 0; + for(int i = 0; i < number_iter; i++){ + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + write(fileno(stdout), NULL, 0); + clock_gettime(CLOCK_MONOTONIC, &end); + timed += BILLION * (end.tv_sec - start.tv_sec) + end.tv_sec - start.tv_sec; + } + printf("%d\n", timed / number_iter); return 0; -} +} \ No newline at end of file