diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100644 index 000000000..3e5db74ad Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..b7cfa9860 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -9,6 +9,23 @@ int main(void) { // Your code here + int x = 100; + int my_fork = fork(); + + if (my_fork == 0) { + printf("x child is %d\n", x); + x = 111; + printf("x child is now %d\n", x); + } + if (my_fork > 0 ) { + printf("x parent is %d\n", x); + x = 222; + printf("x parent is now %d\n", x); + } + if (my_fork < 0) { + printf("Error!"); + } + return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100644 index 000000000..78ab1e1ee Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..795d1f588 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -8,7 +8,21 @@ int main(void) { - // Your code here - + // Your code here + int x = 100; + FILE * fp; + fp = fopen("text.txt", "w"); + int my_fork = fork(); + if (my_fork == 0) { + fprintf(fp, "%s", "Child\n"); + printf("Child\n"); + fclose(fp); + } + else if (my_fork > 0) { + fprintf(fp, "%s", "Parent\n"); + printf("Parent\n"); + fclose(fp); + } + return 0; } diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..0880aea58 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,2 @@ +Parent +Child diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100644 index 000000000..b934ac935 Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..6d374f319 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -11,5 +11,19 @@ int main(void) { // Your code here + int my_fork = fork(); + if(my_fork == 0) { + printf("hello\n"); + + } + else if(my_fork > 0 ) { + wait(NULL); + printf("goodbye\n"); + + } else { + printf("Error!"); + exit(1); + } + return 0; } diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100644 index 000000000..4e275fc8b Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..be355d3fb 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,18 @@ int main(void) { - // Your code here + // Your code here + pid_t pid; + pid = fork(); + if (pid < 0) { + printf("Error %u\n", getpid()); + } else if (pid == 0) { + printf("Child, pid = %u\n", getpid()); + char *args[] = {"/bin/ls", NULL}; + execv(args[0], args); + } else { + printf("Parent, pid = %u\n", getpid()); + } return 0; } diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100644 index 000000000..a4e93e3d1 Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..f3e732b44 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -16,7 +16,22 @@ char* msg3 = "hello world #3"; int main(void) { - // Your code here - + char inbuf [MSGSIZE]; // buffer that will hold incoming data being written + int p[2]; // two element array to hold and read write file descriptors + + if (pipe(p) < 0) { + fprintf(stderr, "pipe failed\n"); + exit(1); + } + + write(p[1], msg1, MSGSIZE); + write(p[1], msg2, MSGSIZE); + write(p[1], msg3, MSGSIZE); + + for (int i = 0; i<3; i++){ + //read 16 bytes of data from file descriptor + read(p[0], inbuf, MSGSIZE); + printf("%s\n", inbuf); + } return 0; } diff --git a/ex6/ex6 b/ex6/ex6 new file mode 100644 index 000000000..7c72b6b20 Binary files /dev/null and b/ex6/ex6 differ diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..5c6f8790b 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -21,6 +21,16 @@ and `clock_gettime()` should work just fine. int main() { // Your code here - + int diff; + struct timespec start, end; + int i; + clock_gettime(CLOCK_MONOTONIC, &start); + for (i = 0; i < number_iter; i++) + { + write(fileno(stdout), NULL, 0); + } + clock_gettime(CLOCK_MONOTONIC, &end); + diff = (BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec) / number_iter; + printf("elapsed time = %llu nanoseconds\n", (long long unsigned int)diff); return 0; }