简体   繁体   English

使用管道的进程间通信 (IPC)

[英]Inter-Process Communication (IPC) using Pipes

Consider you have two variables for questions and answers as below char* questions[] = {"Quit", "In which university do you study?", "Which course are you studying?", "What is your area of interest?"};假设您有两个用于问答的变量,如下所示 char* questions[] = {"Quit", "In which university do you?", "which course you are learning?", "what is your area?" };

char* answers[] = {"Quit", "DAIICT", "Systems Software", "Kernel Programming"}; char* answers[] = {“退出”,“DAIICT”,“系统软件”,“内核编程”};

• Parent process will accept input as question number from user. • 父进程将接受用户输入的问题编号。 If the question number is between 0 to 3, the actual text of question will be sent from parent to child process using the following.如果问题编号在 0 到 3 之间,则问题的实际文本将使用以下方式从父进程发送到子进程。

write(fd1[WRITE], questions[que], strlen(questions[que])+1);写(fd1[写],问题[que],strlen(问题[que])+1);

• Once parent sends the question to child, it just waits for getting answer back from child using the following. • 一旦父母将问题发送给孩子,它就会使用以下内容等待孩子回复。

bytesRead = read(fd2[READ], message, MSGLEN); bytesRead = 读取(fd2[READ],消息,MSGLEN);

• When child process receives the question using the following. • 当子进程收到以下问题时。

bytesRead = read(fd1[READ], message, MSGLEN ); bytesRead = read(fd1[READ], message, MSGLEN);

• Child process then finds the index of the question that it receives from questions[] string array. • 子进程然后查找它从 questions[] 字符串数组接收到的问题的索引。 It uses that index to get the correct answer from the answers[] string array.它使用该索引从 answers[] 字符串数组中获取正确答案。

• Child process then replies to the parent the answer using using the following. • 子进程然后使用以下命令向父进程回复答案。

write(fd2[WRITE], answers[que], strlen(answers[que])+1);写(fd2[写],答案[que],strlen(答案[que])+1);

• If child finds that question number is equal to 0 (ie Quit) then it exits after closing all the open file handles. • 如果孩子发现问题编号等于0(即退出),则在关闭所有打开的文件句柄后退出。 Parent process upon reading question number equal to 0 from and after receiving “Quit” as the answer from child closes all open file handles and waits for child to exit using wait() system call to ensure that we don't create a zombie child process.父进程在从子进程中读取等于 0 的问题号并在收到“退出”作为子进程的答案后关闭所有打开的文件句柄并使用 wait() 系统调用等待子进程退出,以确保我们不会创建僵尸子进程. Once parent comes out of wait() system call, it also exits.一旦 parent 退出 wait() 系统调用,它也会退出。

#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define READ 0
#define WRITE 1

int main()
{
  int i,que,bytesRead;
  int fd1[2],fd2[2];
  char message[100];

  char* questions[] = {"Quit","In which university do you study?","Which course are you studying?","What is your area of interest?"};
  char* answers[] = {"Quit", "DAIICT", "Systems Software", "Kernel Programming"};

  pipe(fd1);
  pipe(fd2);

  if(fork() ==0)
  {
    close(fd1[WRITE]);
    close(fd2[READ]);

    while(1){
      bytesRead = read(fd1[READ], message,100 );
      for(i=0;i<4;i++)
      {
        if(strcmp(message,questions[i])==0)
        {`enter code here`
            break;

        }
      }

    write(fd2[WRITE], answers[i], strlen(answers[i])+1);
    if(i==0)
    {
      close(fd1[1]);
      close(fd2[0]);

      printf("Child exiting for you entered 0\n");
      exit(0);
    }
    }
  }


  else
  {

    close(fd1[READ]);
    close(fd2[WRITE]);
    while(1)
    {



      printf("Enter digit from 0 to 3\n");
      scanf("%d", &que);//dont show correct output if input is greater than 3
      printf("Your choice is  '%s'\n",questions[que]);

      write(fd1[WRITE], questions[que], strlen(questions[que])+1);
      bytesRead = read(fd2[READ], message, 100);

      if(strcmp(message,answers[0])==0)
      {
        close(fd1[WRITE]);
        close(fd2[READ]);

        wait(fork());
        printf("Parent exiting for you entered 0\n");
        exit(0);
      }
      printf("Answer is '%s' \n",message);
    }
    return 0;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM