Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is most of the code for a C program that uses a named pipe. Here\'s the sh

ID: 3568078 • Letter: B

Question

Below is most of the code for a C program that uses a named pipe. Here's the short
version:
-- The parent process ("main") creates a child process. We want two processes so that
one can write to the pipe (in this case, the parent), and the other can read from the
pipe (in this case, the child). The forking. code is provided.
-- The parent writes to the pipe. This code is provided.
-- The child reads from the pipe. This code is not provided: the assignment is to
implement the read_pipe function with your code.
Here's the output when the code is run with both read_pipe and write_pipe in place:
This is the way the world ends, not with a bang but a whimper.
Bye, bye...
In the version of the code given below, nothing prints because there is no reader.
A final point of documentation: the library function "fdopen" is high-level, like "fopen",
but its first argument is a file descriptor rather than a file name. The write_pipe code
illustrates.
Hint: The reader of a pipe is reading a stream. The length of a stream is unknown in principle.
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define Read (0) /* read end of pipe */
#define Write (1) /* write end of pipe */
#define EOS (-1) /* end-of-stream */
/* Read something from the pipe. */
void read_pipe(int file) {
   /* your code here */
}
/* Write some random text to the pipe. */
void write_pipe(int file) {
FILE *stream;
stream = fdopen(file, "w");
/* newlines flush the buffer */
fprintf(stream, "This is the way the world ends, not with a bang but a whimper. ");
fprintf(stream, "Bye, bye... ");
fclose(stream); /* close stream to signal we're done writing */
}
int main () {
pid_t pid;
int my_pipe[2];

/* Create an unnamed pipe. */
if (pipe(my_pipe)) {
perror("Pipe creation failed. ");
return -1;
}
/* The parent process "main" writes to the pipe, and the child process
   reads from it. */
pid = fork();
if (pid < 0) {
perror("Fork failed: exiting.");
exit(-1);
}

/* child process: the reader */
if (pid == 0) {
read_pipe(my_pipe[Read]);
return 0;
}
/* parent process: the writer */
else {
write_pipe(my_pipe[Write]);
return 0;
}

Explanation / Answer

writer.c

reader.c

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote