diff options
author | Matt Strapp <matt@mattstrapp.net> | 2022-05-24 11:18:46 -0500 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2022-05-24 11:19:55 -0500 |
commit | 7a73162607544204032aa66cce755daf21edebda (patch) | |
tree | 58578e01f15f34a855d99c32898db9d7a1603e67 /OLD/csci4061/101920_breakout | |
parent | do some stuff (diff) | |
download | homework-7a73162607544204032aa66cce755daf21edebda.tar homework-7a73162607544204032aa66cce755daf21edebda.tar.gz homework-7a73162607544204032aa66cce755daf21edebda.tar.bz2 homework-7a73162607544204032aa66cce755daf21edebda.tar.lz homework-7a73162607544204032aa66cce755daf21edebda.tar.xz homework-7a73162607544204032aa66cce755daf21edebda.tar.zst homework-7a73162607544204032aa66cce755daf21edebda.zip |
Graduate
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to 'OLD/csci4061/101920_breakout')
-rw-r--r-- | OLD/csci4061/101920_breakout/pipe_template.c | 65 |
1 files changed, 0 insertions, 65 deletions
diff --git a/OLD/csci4061/101920_breakout/pipe_template.c b/OLD/csci4061/101920_breakout/pipe_template.c deleted file mode 100644 index 2c37941..0000000 --- a/OLD/csci4061/101920_breakout/pipe_template.c +++ /dev/null @@ -1,65 +0,0 @@ -// ----------------------------------------------------------------------------- -// Pipes -// -// Write a program that creates a child process and pipes the string -// "hello child" to the child to be printed. The parent should wait for the -// child to terminate -// -// Template: Implement the TODOs -// -// Expected output: -// Parent Sending: hello child -// Child Received: hello child -// ----------------------------------------------------------------------------- - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/wait.h> -#include <unistd.h> - -int main(void) { - // Open pipe - int ends[2]; - pipe(ends); - - char string_to_send[] = "hello child"; - int bytes_to_send_recv = strlen(string_to_send) + 1; - - // Create child process - pid_t pid = fork(); - if (pid < 0) { - // Error - fprintf(stderr, "ERROR: Failed to fork\n"); - } - else if (pid > 0) { - // Parent - printf("Parent Sending: %s\n", string_to_send); - - // Parent doesn't need read file descriptor - close(ends[0]); - // Write the string to the pipe - write(ends[1], string_to_send, bytes_to_send_recv); - // Done writing - close(ends[1]); - // Wait for child to terminate - wait(NULL); - } - else { - // Child - - char *recv_buffer = malloc(bytes_to_send_recv); - - // Child doesn't need write file descriptor - close(ends[1]); - // Read the string from the pipe - read(ends[0], recv_buffer, bytes_to_send_recv); - // Done reading - close(ends[0]); - // Print result - printf("Child Received: %s\n", recv_buffer); - free(recv_buffer); - } - - return 0; -} |