Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用管道的进程间通信(IPC)_C_Linux_Operating System_Ipc_Software Design - Fatal编程技术网

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

使用管道的进程间通信(IPC),c,linux,operating-system,ipc,software-design,C,Linux,Operating System,Ipc,Software Design,假设您有两个问题和答案的变量,如下所示 char*问题[]={“退出”,“你在哪所大学学习?”,“哪门课程?” 你在学习吗?,“你感兴趣的领域是什么?” char*answers[]={“退出”、“DAIICT”、“系统软件”、“内核编程”} •父流程将接受用户输入的问题编号。如果问题编号介于 从0到3,问题的实际文本将使用以下命令从父进程发送到子进程 写作(fd1[写作]、问题[que]、斯特伦(问题[que])+1) •一旦家长将问题发送给孩子,它只需等待孩子使用 跟着 bytesRead=

假设您有两个问题和答案的变量,如下所示 char*问题[]={“退出”,“你在哪所大学学习?”,“哪门课程?” 你在学习吗?,“你感兴趣的领域是什么?”

char*answers[]={“退出”、“DAIICT”、“系统软件”、“内核编程”}

•父流程将接受用户输入的问题编号。如果问题编号介于 从0到3,问题的实际文本将使用以下命令从父进程发送到子进程

写作(fd1[写作]、问题[que]、斯特伦(问题[que])+1)

•一旦家长将问题发送给孩子,它只需等待孩子使用 跟着

bytesRead=read(fd2[read],消息,MSGLEN)

•当子流程使用以下命令收到问题时

bytesRead=read(fd1[read],消息,MSGLEN)

•然后,子进程查找它从questions[]字符串数组接收的问题的索引。信息技术 使用该索引从answers[]字符串数组中获取正确答案

•子进程然后使用以下命令向父进程回复答案

书写(fd2[书写]、回答[que]、斯特伦(回答[que])+1)

•如果孩子发现问题编号等于0(即退出),则在关闭所有打开的问题编号后退出 文件句柄。收到后阅读等于0的问题编号时的父进程 “退出”,因为来自子级的答案关闭所有打开的文件句柄,并使用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;
}
}
#包括 #包括 #定义读取0 #定义写入1 int main() { inti,que,bytesRead; int-fd1[2],fd2[2]; 字符消息[100]; char*问题[]={“退出”,“你在哪所大学学习?”,“你在学习哪门课程?”,“你感兴趣的领域是什么?”}; char*answers[]={“退出”、“DAIICT”、“系统软件”、“内核编程”}; 管道(fd1); 管道(fd2); 如果(fork()==0) { 关闭(fd1[WRITE]); 关闭(fd2[READ]); 而(1){ bytesRead=read(fd1[read],消息,100);
对于(i=0;i)问题是什么?答案在标题中:使用管道。