diff --git a/.gitignore b/.gitignore index e43b0f988..2fea5da15 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .DS_Store +examples +a.out \ No newline at end of file diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..27fd48ee1 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -8,7 +8,48 @@ int main(void) { - // Your code here + printf("BEFORE FORK (pid: %d) \n", (int)getpid()); + int x = 10; + int *xp = &x; + + int f = fork(); + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + printf("Greetings from child (pid: %d)\n", f); + printf("Childs x: %i\n", x); + printf("Childs pointer to x: %p\n", xp); + printf("Childs pointer to x value: %i\n", *xp); + x = x + 5; + printf("%s\n", "Child adding 5 to x"); + printf("Childs x: %i\n", x); + printf("Childs pointer to x: %p\n", xp); + printf("Childs pointer to x value: %i\n", *xp); + int *npx = &x; + printf("Childs NEW pointer to x: %p\n", npx); + printf("%s\n", "---END CHILD---\n\n"); + } + else + { + // we are in the parent branch + printf("Greetings from parent (pid: %d)\n", f); + printf("Parents x: %i\n", x); + printf("Parents pointer to x: %p\n", xp); + printf("Parents pointer to x value: %i\n", *xp); + x = x + 5; + printf("%s\n", "Parent adding 5 to x"); + printf("Parents x: %i\n", x); + printf("Parents pointer to x: %p\n", xp); + printf("Parents pointer to x value: %i\n", *xp); + int *npx = &x; + printf("Childs NEW pointer to x: %p\n", npx); + printf("%s\n", "---END PARENT---\n\n"); + } return 0; } diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..2d7dfe6d4 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -1,5 +1,5 @@ -// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory -// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor +// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory +// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor // returned by `fopen()`? What happens when they are written to the file concurrently? #include @@ -8,7 +8,28 @@ int main(void) { - // Your code here - + FILE *fp; + fp = fopen("text.txt", "r+"); + int f = fork(); + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + fprintf(fp, "Greetings from child: %d\n", (int)getpid()); + } + else + { + // we are in the parent branch + fprintf(fp, "Greetings from parent: %d\n", (int)getpid()); + } + fclose(fp); + return 0; } + +// Answer yes they were both able to write +// The parent wrote first the the child diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..690ec1d0f 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,2 @@ +Greetings from parent: 10998 +Greetings from child: 10999 diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..fdef59496 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -9,7 +9,23 @@ int main(void) { - // Your code here + int f = fork(); + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + printf("%s\n", "hello"); + } + else + { + // we are in the parent branch + wait(NULL); + printf("%s\n", "goodbye"); + } return 0; } diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..60cdf8300 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -1,6 +1,6 @@ // Write a program that calls `fork()` and then calls some form of `exec()` -// to run the program `/bin/ls`. Try a few variants of `exec()`, such as -// `execl()`, `execle()`, `execv()`, and others. Why do you think there +// to run the program `/bin/ls`. Try a few variants of `exec()`, such as +// `execl()`, `execle()`, `execv()`, and others. Why do you think there // are so many variants of the same basic call? #include @@ -10,7 +10,25 @@ int main(void) { - // Your code here + int f = fork(); + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + char *args[] = {"ls", "-a", NULL}; + // execvp(args[0], args); + execvp(args[0], args); + } + else + { + // we are in the parent branch + int wc = waitpid(f, NULL, 0); + printf("%s\n", "Child finished, exiting program"); + } return 0; } diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..ff4bb5e78 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -1,7 +1,7 @@ // Write a program that forks a child and creates a shared pipe -// between the parent and child processes. Have the child write -// the three messages to the parent and have the parent print out -// the messages. +// between the parent and child processes. Have the child write +// the three messages to the parent and have the parent print out +// the messages. #include #include @@ -10,13 +10,46 @@ #define MSGSIZE 16 -char* msg1 = "hello world #1"; -char* msg2 = "hello world #2"; -char* msg3 = "hello world #3"; +char *msg1 = "hello world #1"; +char *msg2 = "hello world #2"; +char *msg3 = "hello world #3"; int main(void) { // Your code here - + char inbuf[MSGSIZE]; + int p[2]; + if (pipe(p) < 0) + { + fprintf(stderr, "pipe failde\n"); + exit(1); + } + int f = fork(); + if (f < 0) + { + printf("%s\n", "Fork faliled"); + exit(1); + } + else if (f == 0) + { + // we are in the child branch + close(p[0]); // close the read branch + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + close(p[1]); // close the write branch + } + else + { + // we are in the parent branch + close(p[1]); // close the write branch + wait(NULL); + for (int i = 0; i < 3; i++) + { + read(p[0], inbuf, MSGSIZE); + printf("%s\n", inbuf); + } + close(p[0]); // close the read branch + } return 0; } diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..c3243ebb2 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -12,15 +12,27 @@ and `clock_gettime()` should work just fine. */ #include -#include +#include +#include #include +#include #define number_iter 1000000 #define BILLION 1000000000L +struct timespec start, end; + int main() { - // Your code here - + clock_gettime(CLOCK_MONOTONIC, &start); + for (int i = 0; i < number_iter; i++) + { + write(fileno(stdout), NULL, 0); + } + clock_gettime(CLOCK_MONOTONIC, &end); + double diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + double individual = diff / number_iter; + printf("Average time in nanoseconds: %lf\n", individual); + return 0; }