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

Here is a multiprocessing program that uses named pipes. On a sample run, the pr

ID: 662960 • Letter: H

Question

Here is a multiprocessing program that uses named pipes. On a sample run, the program should ouput:

This is the way the world ends, not with a bang but a whimper.
Bye, bye...

The assignment is to write the appropriate code for the read_pipe function. For safety and robustness,
the read_pipe program should read a byte at a time and output each byte to the screen.
The write_pipe code, and the code in main, are ready to go.

#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

#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 */
FILE *stream;
stream = fdopen(file, "r");

if(!stream)
{
   printf("coulnot open pipe !! ");
   return;
}

char *buff;

while(true)//run infinite loop till '/0' is found i.e. end of file
{
fgets(buff, sizeof (buff), stream); //read byte by byte
  
if(*buff == '/0') //check if buff is end of string then exit
   break;
else
   printf("%c", *buff); //else print the character

};
}
/* 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;
}
}

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