简体   繁体   English

带有UNIX进程和管道的“乒乓”游戏

[英]“Ping pong” game with unix processes and pipes

Can someone please help me fix this problem? 有人可以帮我解决这个问题吗?

I wrote the complete problem below. 我在下面写下了完整的问题。

Ping-pong. 乒乓。 Two processes will play the ping-pong game. 乒乓游戏将通过两个过程进行。 The first process will generate a random number between 5000 and 15000 that will be send to the other process. 第一个进程将生成一个介于5000和15000之间的随机数,该随机数将被发送到另一个进程。 This process will subtract a random value (between 50 and 1000) and will send the number back, The chat between the processes will be implemented using pipe channels. 此过程将减去一个随机值(介于50到1000之间)并将该数字发回。进程之间的聊天将使用管道通道实现。 The game ends when the value is below zero. 值小于零时,游戏结束。 Each process will print the received value. 每个过程将打印接收到的值。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

int main ()
{

  int  p2c[2], c2p[2], number, n;

  pipe(p2c);    //parent to child pipe
  pipe(c2p);    //child to parent pipe

  number = rand() %10000 + 5000;
  printf("Generated number: %d\n", number);

  if (fork () == 0 ) 
  { //child process
      while(number > 0)
      {
         printf("in child process\n");
         read(p2c[0], &number, sizeof(int));
         printf("(F).received number: %d\n", number);
         n = rand() %1000 + 50;
         number = number - n;
         write(c2p[1], &number, sizeof(int));
       } 
      close(p2c[0]); close(p2c[1]);
      close(c2p[0]); close(c2p[1]);
      exit(0);

  } else {
    while(number > 0) 
    {
      printf("in parent process\n");
      read(c2p[0], &number, sizeof(int));
      printf("(P).received number: %d\n", number);
      number = number - n;
      write(p2c[1], &number, sizeof(int));
    }
   close(p2c[0]); close(p2c[1]);
   close(c2p[0]); close(c2p[1]);
   wait();
 }
printf ("The final number is: %d\n", number);
}

It does not work and I don't understand why. 它不起作用,我不明白为什么。 All that it prints is: 它打印的全部是:

Generated number: (a random number...)
in parent process
in child process

Also I don't understand why it first goes to the parent process and no to the child process. 我也不明白为什么它首先进入父进程而不是子进程。 Can someone please help me fix this? 有人可以帮我解决这个问题吗?

First of all, there is no order to fork ed processes. 首先,没有fork进程的命令。 Child and parent execute more-or-less simultaneously, despite the fact that child is higher in source code than parent. 尽管子级在源代码中比父级更高,但子级和父级或多或少同时执行。 Here, parent just happened to slap their hand on the standard output a bit faster than child. 在这里,父母恰好比孩子更快地拍打标准输出。

The main problem you are having is a deadlock. 您遇到的主要问题是僵局。 The parent is waiting for the child to say something ( read(c2p[0]...) ); 父母正在等待孩子说些什么( read(c2p[0]...) ); the child is waiting for the parent to break the silence ( read(p2c[0], ...) ). 孩子正在等待父母打破沉默( read(p2c[0], ...) )。 One of them has to say something first, or they will just die of old age before they move. 他们中的一个必须先说些什么,否则他们将在搬家之前死于老年。

Start one of them talking before the loop by telling the other the initial state. 通过告诉其他人初始状态,开始其中一个在循环之前进行交谈。 Then it should work okay later, as they alternate between talking and listening. 然后,以后在对话和聆听之间交替时,它应该可以正常工作。

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

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