管叉,它是如何可能的程序运行时,如果和其他的相同的条件 [英] pipe, fork, how it is possible that program runs if and else of the same condition

查看:153
本文介绍了管叉,它是如何可能的程序运行时,如果和其他的相同的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序运行一次,它都将引发数据到管道并在这应该是相互​​排斥的相同的条件下得到出来(在如果和其他)。
我不知道这些的?
这是如何工作的?
我有这种编程的经验。

 的#include< SYS / types.h中>
     #包括LT&;&unistd.h中GT;
     #包括LT&;&stdio.h中GT;
     #包括LT&;&stdlib.h中GT;

从管道中读取字符,并将其回显到标准输出。

 无效
     read_from_pipe(INT文件)
     {
       FILE *流;
       INT℃;
       流= fdopen(文件,R);
       而((C =龟etc(流))!= EOF)
         的putchar(C);
       FCLOSE(流);
     }

写一些随机文字到管道。

 无效
     write_to_pipe(INT文件)
     {
       FILE *流;
       流= fdopen(文件,在w);
       fprintf中(流,你好,世界\\ n!);
       fprintf中(流,再见,世界\\ n!);
       FCLOSE(流);
     }     INT
     主要(无效)
     {
       将为pid_t PID;
       INT mypipe [2];       / *创建管道。 * /
       如果(管(mypipe))
         {
           fprintf中(标准错误,管子失败\\ n);
           返回EXIT_FAILURE;
         }       / *创建子进程。 * /
       PID =叉();
       如果(PID ==(将为pid_t)0)
         {
           / *这是子进程。
              关闭另一端第一。 * /
           关闭(mypipe [1]);
           read_from_pipe(mypipe [0]);
           返回EXIT_SUCCESS;
         }
       否则如果(PID≤(将为pid_t)0)
         {
           / *叉失败。 * /
           fprintf中(标准错误,叉失败\\ n);
           返回EXIT_FAILURE;
         }
       其他
         {
           / *这是父进程。
              关闭另一端第一。 * /
           关闭(mypipe [0]);
           write_to_pipe(mypipe [1]);
           返回EXIT_SUCCESS;
         }
     }


解决方案

这行后,右:

  PID =叉();

您不再有一个计划,但是两个不同的人(当成功)。

这不是同时运行相同的程序。在子过程中,返回0,但在父返回子的PID。

父运行,如果 / 的一个分支别人结构,孩子运行另一个。 (和第三只运行,在父,如果失败。)

Program runs once and it both throws data to the pipe and gets it out in the same condition that should be mutually exclusive (in if and else). What I don't get here? How does that work? I have no experience with this kind of programming.

   #include <sys/types.h>
     #include <unistd.h>
     #include <stdio.h>
     #include <stdlib.h>

Read characters from the pipe and echo them to stdout.

     void
     read_from_pipe (int file)
     {
       FILE *stream;
       int c;
       stream = fdopen (file, "r");
       while ((c = fgetc (stream)) != EOF)
         putchar (c);
       fclose (stream);
     }

Write some random text to the pipe.

     void
     write_to_pipe (int file)
     {
       FILE *stream;
       stream = fdopen (file, "w");
       fprintf (stream, "hello, world!\n");
       fprintf (stream, "goodbye, world!\n");
       fclose (stream);
     }

     int
     main (void)
     {
       pid_t pid;
       int mypipe[2];

       /* Create the pipe. */
       if (pipe (mypipe))
         {
           fprintf (stderr, "Pipe failed.\n");
           return EXIT_FAILURE;
         }

       /* Create the child process. */
       pid = fork ();
       if (pid == (pid_t) 0)
         {
           /* This is the child process.
              Close other end first. */
           close (mypipe[1]);
           read_from_pipe (mypipe[0]);
           return EXIT_SUCCESS;
         }
       else if (pid < (pid_t) 0)
         {
           /* The fork failed. */
           fprintf (stderr, "Fork failed.\n");
           return EXIT_FAILURE;
         }
       else
         {
           /* This is the parent process.
              Close other end first. */
           close (mypipe[0]);
           write_to_pipe (mypipe[1]);
           return EXIT_SUCCESS;
         }
     }

解决方案

Right after the line that says:

pid = fork();

You no longer have one program, but two distinct ones (when fork succeeds).

It's not the same program that runs both. In the child process, fork returns 0, but in the parent it returns the child PID.

The parent runs one branch of the if/else construct, the child runs another. (And a third is run, in the parent only, if fork fails.)

这篇关于管叉,它是如何可能的程序运行时,如果和其他的相同的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆