我有以下任務要做:Unix進程PIPE
乒乓。兩個進程將玩乒乓球遊戲。 第一個進程將生成一個5000到15000之間的隨機數,並將其發送到另一個進程。 此過程將減去一個隨機值(介於50和1000之間)並將發回數字, 這些過程之間的聊天將使用管道來實現。 遊戲在值低於零時結束。 每個進程將打印收到的值。
所以我寫了下面的代碼:
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int p[2];
int a, n;
pipe(p);
int pid = fork();
if (pid == 0)
{
int r = 0;
srand(time(NULL));
while (r < 50 || r > 1000)
{
r = rand();
}
if ((a=read(p[0], &n, sizeof(int)))==-1)
perror("Error read pipe:");
printf("Process 2 recived %d\n", a);
a = a - r;
close(p[0]); close(p[1]);
}
else if (pid > 0)
{
srand(time(NULL));
while (n < 5000 || n > 15000) {
n = rand();
}
while (n > 0)
{
printf("Process 1 recived %d\n", n);
if (write(p[1], &n, sizeof(int))==-1)
perror("Error write pipe:");
wait(0);
}
}
return 0;
}
當執行它會在一個無限循環,其打印"Process 1 received 4"
,我不知道爲什麼。
我創造了另一個管道,現在它正確打印第一個接收值,但在第二過程中它會發生同樣的事情
Process 1 recived 9083
Process 2 recived 4
Process 1 recived 4
and infinite loop
請不要在你的問題的更新代碼。 – jdarthenay
請注意,兩個隨機數序列實際上保證是相同的(如果其中一個進程在T時刻執行'time(NULL)',另一個進程在時間T + 1執行,它們只會有所不同大多數他們都會在時間T執行它)。一個簡單的解決方法是使用'time(NULL)+ pid'來代替'time(NULL)'。 –