我想要做的是,我有一个父母和子女的过程。我尝试读取文件并将所有内容传递给孩子。输入文件中有记录。我必须一次读一遍,然后传给孩子。但不管我做什么。孩子只读第一个记录,而不是所有其他记录。我测试了我的方法从输入文件获取所有记录,但不能将它们全部写入子项。请任何想法。如何将多条记录从父母的叉子传递给孩子
struct product_record {
int idnumber; // Unique identification
char name[PRODUCTSIZE]; // String description
double price; // Unit cost
};
int pid, mypipe[2], status;
int main(int argc, char **argv)
{
int result;
result = pipe(mypipe);//create pipes
if (result < 0) {
perror("pipe creation error");//failure in creating a pipe
exit (1);
}//if
if ((pid = fork()) == 0)
{
read(mypipe[0], &product, sizeof(product));
exit(0);
}
else if (pid == -1)
{
cout << "Fork failed" << endl;
exit(1);
}
else
{//parent
parentReadsFromTheFile(argv);//argv inputfile name
wait(&status);
}
return 0;//Return to the OS.
}//main
void parentReadsFromTheFile(char **args)
{
while (getline(inputFile, line)) //read a line till EOF
{
//put read record into product
write(mypipe[1], &product, sizeof(product));//Write to pipe
}
}
正确地调整你的代码,就不做评论了明显的。 – chqrlie
发布'product'的定义。 – chqrlie
我编辑和解释得更好。谢谢。 – user2934615