父母和2个孩子之间的管道
问题描述:
我不明白为什么只有一个孩子发送数据给家长(只有第一个孩子).. 当我做睡眠(5)后,通过管道发送数据到父母然后第二个孩子向父母发送相同的素数。父母和2个孩子之间的管道
有人可以帮助我吗?因为他们已经开始使用相同的种子
//--------------------------Consts---------------------------------
#define NUM_OF_CHILDS 2
#define N 20
#define WIN 5
struct msg{
\t pid_t _pid;
\t int _prime;
};
//--------------------------Prototypes-----------------------------
bool is_prime(int num);
void terminate(pid_t child_pid[],int fd[2]);
void do_child(int fd[2]);
void print_pair(const int f_arr[],const int s_arr[]);
//--------------------------Main-------------------------------------
int main()
{
\t int f_arr[N] = {0},
\t \t s_arr[N] = {0},
\t \t ind, //running on children fork
\t \t count1 = 0,
\t \t count2 = 0,
\t \t victory1 = 0,
\t \t victory2 = 0,
\t \t min = 0;
\t int fd[2];
\t bool read1 = false,
\t \t read2 = false;
\t srand((unsigned)time(NULL));
\t pid_t child_pid [NUM_OF_CHILDS];//children pid status array
\t struct msg msg1;
\t if (pipe(fd) == -1)//pipe fd
\t {
\t \t perror("cannot open pipe");
\t \t exit(EXIT_FAILURE);
\t }
\t for(ind = 0; ind < NUM_OF_CHILDS; ind++)
\t {
\t \t child_pid[ind] = fork();// duplicate the current process
\t \t if (child_pid[ind] < 0)//fork failed
\t \t {
\t \t \t perror("Cannot fork()");
\t \t \t exit(EXIT_FAILURE);
\t \t }
\t \t if(child_pid[ind] == 0)/* child : sends message to parent*/
\t \t \t do_child(fd);
\t }
\t /* parent : receives message from child */
\t close(fd[1]); // close the write-end of the pipe
\t //read data from pipe
\t while(read(fd[0],&msg1,sizeof(struct msg)) > 0)
\t {
\t \t if(child_pid[0] == msg1._pid)
\t \t {
\t \t \t f_arr[count1++] = msg1._prime;
\t \t \t read1 = true;
\t \t }
\t \t else
\t \t {
\t \t \t s_arr[count2++] = msg1._prime;
\t \t \t read2 = true;
\t \t }
\t \t if(read1 && read2)
\t \t {
\t \t \t if(f_arr[min] > s_arr[min])
\t \t \t \t victory1++;
\t \t \t else if(f_arr[min] < s_arr[min])
\t \t \t \t victory2++;
\t \t \t read1 = false;
\t \t \t read2 = false;
\t \t \t min++;
\t \t }
\t \t if(victory1 == WIN || victory2 == WIN)
\t \t \t terminate(child_pid,fd);
\t }
\t close(fd[0]);// close the read-end of the pipe
\t print_pair(f_arr,s_arr);
\t return EXIT_SUCCESS ;
}
//---------------------------------------------------------------------
//checking if number is a prime number or not
//and return true or false
bool is_prime(int num)
{
\t int i;
\t if(num==0 || num==1 || num==2)
\t \t return false;
\t for(i=2;i<=num/2;i++)
\t {
\t \t //the number is not prime
\t \t if(num%i == 0)
\t \t \t return false;
\t }
\t //the number is prime
\t return true;
}
//----------------------------------------------------------------
void do_child(int fd[2])
{
\t struct msg message;
\t int num;
\t close(fd[0]);
\t while (1)
\t {
\t \t num = rand() % 1000;
\t \t if(is_prime(num))
\t \t {
\t \t \t message._prime = num;
\t \t \t message._pid = getpid();
\t \t \t write(fd[1], &message, sizeof(struct msg));
\t \t }
\t }
}
//----------------------------------------------------------------
void terminate(pid_t child_pid[],int fd[2])
{
\t int ind,
\t loop;
\t for(ind = 0; ind < NUM_OF_CHILDS; ind++)
\t {
\t \t close(fd[1]);
\t \t //first to give the process an opportunity to die gratefully before
\t \t //using SIGKILL
\t \t kill(child_pid[ind], SIGTERM);
\t \t bool died = false;
\t \t //It will give the process 5 seconds to die gracefully
\t \t for (loop = 0; loop < 5 && !died; ++loop)
\t \t {
\t \t \t int pid;
\t \t \t //the time the child process takes to close down gracefully.
\t \t \t sleep(1);
\t \t \t //to get the return status of that process and prevent zombie processes.
\t \t \t if (waitpid(child_pid[ind], &pid, WNOHANG) == child_pid[ind])
\t \t \t \t died = true;
\t \t }
\t \t //if SIGTERM did not killed the child do SIGKILL
\t \t if (!died)
\t \t {
\t \t \t int pid;
\t \t \t kill(child_pid[ind], SIGKILL);
\t \t \t waitpid(child_pid[ind], &pid, 0);// harvest the zombie
\t \t }
\t }
}
//------------------------------------------------------------------
void print_pair(const int f_arr[],const int s_arr[])
{
\t int ind;
\t for(ind = 0; ind < N; ind++)
\t {
\t \t if(f_arr[ind] == 0 && s_arr[ind] == 0)
\t \t \t break;
\t \t printf("(%d,%d)\n",f_arr[ind],s_arr[ind]);
\t }
}
答
首先,两个子进程产生相同的伪随机序列。要获得不同数字的机会,您需要将它们播种到叉子后面,并且可能使用每秒更改超过一次的东西(即使您移动了srand(time(NULL))
,它们的两个具有不同值time()
的机会也非常小之后)。
其次,您正在接收来自第一个过程的所有数字,因为它具有领先优势。在第二个过程正在创建时,有足够的时间写入管道。直到创建了两个子项之后,父项才开始读取,因此第一个子项将填充管道缓冲区,然后进行阻塞。管道缓冲区至少有几千字节。
即使当我通过将数字打印到stderr来减慢子进程时,第一个数字仍然会在第二个数字出现之前生成数百个数字。
那么当从孩子1到达数百条消息而没有从孩子2到达时,在主循环中会发生什么?你的f_arr
数组溢出,因为它只有20的空间。之后,任何事情都可能发生。
,以防止将尝试一些存储到f_arr[count1++]
前检查是否count1 == N
最简单的方法,如果是这样,只是continue;
下一条消息。您应该对来自第二个孩子的消息也这样做,即使这不可能发生。
这样你最多可以接收来自每个孩子的N
消息,而忽略其余的。您需要向主循环添加另一个结束条件:如果两个孩子都发送了N
消息,则需要停止。
另一种方法是为每个孩子使用一个单独的管道,并从两个管道交替读取以保持其同步,但我有一种感觉,你故意避免这种情况。
“但我有一种感觉,你故意避免这种情况” - 你是对的。 Tnx为您的评论! –
我不能得到它的工作:( –