在下面的代码中,我创建了两个管道。我打算使用两个管道是为了使通信双向。我的代码工作正常,我也得到输出。我想确定我的代码是否真的用作双向通信(在一端写入文件并在另一端读取文件并在另一端再次编写文件并在另一端读取)或不是。还有其他有效的方法吗?
#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;
}
答案 0 :(得分:1)
不,你已经完成了所有事情。