需要检查管道是不是双向工作

Posted

技术标签:

【中文标题】需要检查管道是不是双向工作【英文标题】:Need to check whether pipe is working as bidirectional or not需要检查管道是否双向工作 【发布时间】:2014-07-14 11:23:30 【问题描述】:

在下面的代码中,我创建了两个管道。我使用两个管道的意图是使通信双向。我的代码工作正常,我也得到了输出。我想确定我的代码是否真的作为双向通信工作(在一端写入文件并在另一端读取它,然后再次在一端写入文件并在另一端读取它)与否。还有其他有效的方法吗?

      #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;
            

【问题讨论】:

您可以考虑在下次发布时删除所有注释掉的代码,这样可以让您的示例不那么混乱。 @user3751012 使用fclose() 关闭分配给fdopen()FILE* 并丢失匹配的close() 调用。当流通过fclose() 关闭时,描述符也是如此,并且 filstream 数据将被正确清理。如果您将有意义的宏名称(PARENT_READCHILD_WRITE 等)作为单个描述符数组的索引(当然,最后一部分是我的意见),那么您的代码将更容易阅读,但通常会成功)。 Example of comment from above. 你用错了feof***.com/questions/5431941/…在这种情况下,逻辑是有效的,但完全是多余的。 【参考方案1】:

不,您已正确完成所有操作。

【讨论】:

以上是关于需要检查管道是不是双向工作的主要内容,如果未能解决你的问题,请参考以下文章

管道双向通信

命名管道类似于“mkfifo”创建,但双向

创建双向管道或任何类似行为的 Unix 方式?

popen() 可以制作像 pipe() + fork() 这样的双向管道吗?

如何将 SonarQube 分析的质量门检查到工作中

双向加密:我需要存储可以检索的密码