diff --git a/ex1/a.out b/ex1/a.out new file mode 100644 index 000000000..fde773770 Binary files /dev/null and b/ex1/a.out differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..6ea571d38 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -8,7 +8,19 @@ int main(void) { - // Your code here + + int x = 50; + int pid = fork(); + + if (pid == 0) + { + printf("Child value: %i\n", x); + } + else + { + + printf("Parent value: %i\n", pid); + } return 0; } diff --git a/ex2/a.out b/ex2/a.out new file mode 100644 index 000000000..ff919f22a Binary files /dev/null and b/ex2/a.out differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..41a0b0bbb 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,21 @@ int main(void) { - // Your code here - + FILE *fp; + + fp = fopen("text.txt", "w+"); + + fprintf(fp, "%s %s", "Hello", "World"); + fclose(fp); + + int rc = fork(); + if (rc == 0) + { + printf("I am the child, pid: %d\n", (int)getpid()); + } + else + { + printf("I am the parent, pid: %d, pid of child: %d\n", (int)getpid(), rc); + } return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..5e1c309da 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/ex3/a.out b/ex3/a.out new file mode 100644 index 000000000..27306f137 Binary files /dev/null and b/ex3/a.out differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..9f2f2e3c0 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -9,7 +9,18 @@ int main(void) { - // Your code here + // printf("goodbye (pid: %d)\n", (int)getpid()); + int rc = fork(); + // child process starts executing here + if (rc == 0) + { + printf("Child: hello\n"); + } + else + { + int wc = waitpid(rc, NULL, 0); + printf("Parent: goodbye\n"); + } return 0; } diff --git a/ex4/a.out b/ex4/a.out new file mode 100644 index 000000000..a334a10a2 Binary files /dev/null and b/ex4/a.out differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..0a1a65216 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,11 @@ int main(void) { - // Your code here + printf("Hello World (pid: %d)\n", (int)getpid()); + + int rc = fork(); + + rc = execl("/bin/ls", "ls", NULL); return 0; } diff --git a/ex5/a.out b/ex5/a.out new file mode 100644 index 000000000..d1691d133 Binary files /dev/null and b/ex5/a.out differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..7282d583d 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,34 @@ #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], i; + + if (pipe(p) < 0) + exit(1); + + int rc = fork(); + if (rc == 0) + { + printf("Child process\n"); + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + } + else + { + for (i = 0; i < 3; i++) + { + read(p[0], inbuf, MSGSIZE); + printf("% s\n", inbuf); + } + } + return 0; } diff --git a/ex6/a.out b/ex6/a.out new file mode 100644 index 000000000..5999a93ca Binary files /dev/null and b/ex6/a.out differ diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..532560e88 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -20,7 +20,26 @@ and `clock_gettime()` should work just fine. int main() { - // Your code here - + const char msg[] = "Hello World!"; + int time; + + for (int i = 0; i < number_iter; i++) + { + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); /* mark start time */ + + write(STDOUT_FILENO, msg, sizeof(msg) - 1); + + clock_gettime(CLOCK_MONOTONIC, &end); /* mark the end time */ + + time += BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + ; + } + + time /= number_iter; + + printf("average elapsed time = %d nanoseconds\n", time); + return 0; }