In the below code, I have created two pipes. My intention to use two pipe is to make the communication bidirectional. My code is working fine and I also get output. I want to get assured whether my code is really working as a bidirectional communication (write the file in one end and read it another end and again write the file in one end and read it in another end) or not. Is there any other efficient way?
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
/* Write COUNT copies of MESSAGE to filename, pausing for a second
between each. */
void writer (const char* message, int count, FILE* bus)
{
for (; count > 0 ; -- count) {
//printf("point 18, pid = %d \n ", getpid() );
/* Write the message to the filename, and send it off immediately.*/
fprintf (bus, "%s\n", message);
//printf("point 19, pid = %d \n ", getpid());
fflush (bus);
//printf("point 20, pid = %d \n ", getpid());
/* Snooze a while. */
sleep (1);
}
}
/* Read random strings from the filename as long as possible.
*/
void reader (FILE* dog)
{
char buffer[1024];
/* Read until we hit the end of the filename. fgets reads until
either a newline or the end-of-FILE. */
//printf("point 21, pid = %d \n ", getpid());
while (!feof (dog) && !ferror (dog) && fgets (buffer, sizeof (buffer), dog) != NULL)
fputs (buffer, stdout);
//printf("point 22, pid = %d \n ", getpid());
}
int main ()
{
int fds[2];
int fd [2];
pid_t pid;
//printf("point 1, pid = %d \n ", getpid());
/* Create a pipe. FILE descriptors for the two ends of the pipe are
placed in fds. */
pipe (fds);
pipe (fd);
//printf("point 2, pid = %d \n ", getpid());
/* Fork a child process. */
pid = fork ();
//printf("point 3, pid = %d \n ", getpid());
if (pid == (pid_t) 0)
{
FILE* filename;
//printf("point 4, pid = %d \n ", getpid());
/* This is the child process. Close our copy of the write end of
the FILE descriptor. */
close (fds[1]);
close (fd[0]);
//printf("point 5, pid = %d \n ", getpid());
/* Convert the read FILE descriptor to a FILE object, and read
from it. */
filename = fdopen (fds[0], "r"); // Open text file for reading
//printf("point 6, pid = %d \n ", getpid());
reader (filename);
//printf("point 7, pid = %d \n ", getpid());
close (fds[0]);
//printf("point 8, pid = %d \n ", getpid());
filename = fdopen (fd[1], "w"); // Open text file for reading
//printf("point 9, pid = %d \n ", getpid());
writer ("I want to learn c programming!", 2, filename);
//printf("point 10, pid = %d \n ", getpid());
close (fd[1]);
//printf("point 11, pid = %d \n ", getpid());
}
else {
/* This is the parent process. */
FILE* goru;
//printf("line %d from pid %d\n", __LINE__, getpid());
/* Close our copy of the read end of the FILE descriptor. */
close (fds[0]);
close (fd[1]);
//printf("point 12, pid = %d \n ", getpid());
/* Convert the write FILE descriptor to a FILE object, and write
to it. */
goru = fdopen (fds[1], "w");//create text file for writing
//printf("point 13, pid = %d \n ", getpid());
writer ("I want to learn c programming!", 5, goru);
//printf("point 14, pid = %d \n ", getpid());
close (fds[1]);
//printf("point 15, pid = %d \n ", getpid());
goru = fdopen (fd[0], "r");//create text file for writing
//printf("point 16, pid = %d \n ", getpid());
reader (goru);
//printf("point 17, pid = %d \n ", getpid());
close (fd[0]);
}
return 0;
}
No, you've done everything correctly.