diff --git a/.vscode/ipch/d017f139b4fd457c/mmap_address.bin b/.vscode/ipch/d017f139b4fd457c/mmap_address.bin new file mode 100644 index 000000000..862b8428b Binary files /dev/null and b/.vscode/ipch/d017f139b4fd457c/mmap_address.bin differ diff --git a/.vscode/ipch/da6cf284263e2cc/mmap_address.bin b/.vscode/ipch/da6cf284263e2cc/mmap_address.bin new file mode 100644 index 000000000..862b8428b Binary files /dev/null and b/.vscode/ipch/da6cf284263e2cc/mmap_address.bin differ diff --git a/.vscode/ipch/de4ae1eb1c013c04/EX2.ipch b/.vscode/ipch/de4ae1eb1c013c04/EX2.ipch new file mode 100644 index 000000000..f0850cf9a Binary files /dev/null and b/.vscode/ipch/de4ae1eb1c013c04/EX2.ipch differ diff --git a/.vscode/ipch/de4ae1eb1c013c04/mmap_address.bin b/.vscode/ipch/de4ae1eb1c013c04/mmap_address.bin new file mode 100644 index 000000000..862b8428b Binary files /dev/null and b/.vscode/ipch/de4ae1eb1c013c04/mmap_address.bin differ diff --git a/ex1/ex1 b/ex1/ex1 new file mode 100755 index 000000000..4436fbde0 Binary files /dev/null and b/ex1/ex1 differ diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..510eef9d2 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -10,5 +10,19 @@ int main(void) { // Your code here + pid_t pid = fork(); + + if (pid == 0) + { + int x = 200; + printf("\n This is the child, x = %d\n", x); + exit(1); + } + else + { + int x = 100; + printf("\n This is the parent, x = %d\n", x); + } + return 0; } diff --git a/ex2/ex2 b/ex2/ex2 new file mode 100644 index 000000000..dd6ee0f96 Binary files /dev/null and b/ex2/ex2 differ diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..b2bb9e2ac 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,23 @@ int main(void) { - // Your code here - + // Your code here + pid_t pid = fork(); + FILE *fp; + fp = fopen("text.txt", "w+"); + + if (pid == 0) + { + + fprintf(fp, "\n I\'m printing from the child\n"); + exit(1); + } + else + { + fprintf(fp, "\n I\'m printing from the parent\n"); + } + return 0; } + +// oddly enough only the child printed in my code.. did I do something wrong? \ No newline at end of file diff --git a/ex2/text.txt b/ex2/text.txt index e69de29bb..583fbce5e 100644 --- a/ex2/text.txt +++ b/ex2/text.txt @@ -0,0 +1,3 @@ + + I'm printing from the child + diff --git a/ex3/ex3 b/ex3/ex3 new file mode 100644 index 000000000..a801f8ddf Binary files /dev/null and b/ex3/ex3 differ diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..1d4816971 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -10,6 +10,18 @@ int main(void) { // Your code here + pid_t pid = fork(); + + if (pid == 0) + { + printf("\nHello\n"); + exit(1); + } + else + { + wait(NULL); + printf("\nGoodbye\n"); + } return 0; } diff --git a/ex4/ex4 b/ex4/ex4 new file mode 100755 index 000000000..6ca75ae89 Binary files /dev/null and b/ex4/ex4 differ diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..e2f863c8c 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -11,6 +11,20 @@ int main(void) { // Your code here + pid_t pid = fork(); + + if (pid == 0) { + // execlp("ls", "ls", NULL); + char *cmd = "ls"; + char *argv[3]; + argv[0] = "ls"; + argv[1] = "-la"; + argv[2] = NULL; + execvp(cmd, argv); //This will run "ls -la" as if it were a command + } else { + wait(NULL); + printf("Child process has ended\n"); + } return 0; } diff --git a/ex5/ex5 b/ex5/ex5 new file mode 100755 index 000000000..495958c9f Binary files /dev/null and b/ex5/ex5 differ diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..f91cbf665 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -10,13 +10,38 @@ #define MSGSIZE 16 -char* msg1 = "hello world #1"; -char* msg2 = "hello world #2"; -char* msg3 = "hello world #3"; +char* msg1 = "hello world #1\n"; +char* msg2 = "hello world #2\n"; +char* msg3 = "hello world #3\n"; int main(void) { // Your code here + int fd[2]; // init pipe array + pipe(fd); // init the pipe + + pid_t pid = fork(); // init the fork + + + char* messages[sizeof MSGSIZE]; + int buf[128]; + messages[0] = msg1; + messages[1] = msg2; + messages[2] = msg3; + + if (pid < 0) { + perror("fork failed\n"); + exit(1); + } else if (pid == 0) { + write(fd[1], messages[0], sizeof messages[0]); + write(fd[1], messages[1], sizeof messages[1]); + write(fd[1], messages[2], sizeof messages[2]); + } else { + wait(NULL); + read(fd[0], messages[0], sizeof buf); + read(fd[0], messages[1], sizeof buf); + read(fd[0], messages[2], sizeof buf); + } return 0; }