how to read write in pipe

Solutions on MaxInterview for how to read write in pipe by the best coders in the world

showing results for - "how to read write in pipe"
Marlene
15 Feb 2018
1#include<stdio.h>
2#include<unistd.h>
3
4int main() {
5   int pipefds[2];
6   int returnstatus;
7   char writemessages[2][20]={"Hi", "Hello"};
8   char readmessage[20];
9   returnstatus = pipe(pipefds);
10   
11   if (returnstatus == -1) {
12      printf("Unable to create pipe\n");
13      return 1;
14   }
15   
16   printf("Writing to pipe - Message 1 is %s\n", writemessages[0]);
17   write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
18   read(pipefds[0], readmessage, sizeof(readmessage));
19   printf("Reading from pipe – Message 1 is %s\n", readmessage);
20   printf("Writing to pipe - Message 2 is %s\n", writemessages[0]);
21   write(pipefds[1], writemessages[1], sizeof(writemessages[0]));
22   read(pipefds[0], readmessage, sizeof(readmessage));
23   printf("Reading from pipe – Message 2 is %s\n", readmessage);
24   return 0;
25}
Jean-Baptiste
17 Oct 2019
1#include<unistd.h>
2
3ssize_t read(int fd, void *buf, size_t count)
4
5ssize_t write(int fd, void *buf, size_t count)
similar questions
pipe system call