Question: In C , NO FILE POINTERS, Write a set of programs that transfers string data entered in one program to the other program over a

In C, NO FILE POINTERS, Write a set of programs that transfers string data entered in one program to the other program over a FIFO.
The first program should prompt the user to enter words in a manner similar to the following:
Please enter text at the producer: prompt
producer:
The second program should then respond to receiving the data over the pipe by outputting
consumer:
Existing code:
producer.c:
#include stdio.h
#include stdlib.h
#include string.h
#include unistd.h
#include fcntl.h
#include sys/stat.h
#define FIFO_NAME "myfifo"
int main(){
char input[100];
// Create the FIFO (named pipe)
mkfifo(FIFO_NAME, 0666);
printf("Please enter text at the producer: ");
fflush(stdout);
while (1){
// Get user input
fgets(input, sizeof(input), stdin);
// Open the FIFO for writing
int fd = open(FIFO_NAME, O_WRONLY);
// Write data to the FIFO
write(fd, input, strlen(input)+1);
close(fd);
}
return 0;
}
consumer.c:
#include stdio.h
#include stdlib.h
#include unistd.h
#include fcntl.h
#include sys/stat.h
#define FIFO_NAME "myfifo"
int main(){
char received_data[100];
// Create the FIFO (named pipe)
mkfifo(FIFO_NAME, 0666);
while (1){
// Open the FIFO for reading
int fd = open(FIFO_NAME, O_RDONLY);
// Read data from the FIFO
read(fd, received_data, sizeof(received_data));
close(fd);
// Display the received data
printf("consumer: %s", received_data);
}
return 0;
}
The first program dosen't end after I entered text, the second program dosen't print anything.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!