c-如何在多个管道中同时读写

  • 本文关键字:读写 管道 c pipe fork
  • 更新时间 :
  • 英文 :


这只是我程序的一部分。

char temp[100];//stores the temporary value from the pipe read
if(close(fd[0][1])==-1){//closing the write of the first pipe
perror("Error: closing the write pipe [child]-");
exit(-1);
}
if(close(fd[1][0])==-1){//closing the read of the second pipe
perror("Error: closing the write pipe [child]-");
exit(-1);
}
if(!(read(fd[0][0],temp,10)>0)){//the first number read specifies the overall filter
fprintf(stderr,"Error: while reading the filter");
exit(-1);
}
filter=strtol(temp,NULL,10);//storing the first read as the filter
while(read(fd[0][0],temp,10)>0){//reading the rest of the data for filtering
printf("[%d] child received %sn",getpid(),temp);
if(strtol(temp,NULL,10)%filter!=0){//checking to see if the read number is divisible by the filter
//if not, we write it to the second pipe
if(write(fd[1][1],temp,10)==-1){
perror("Error: failed to write to pipe -");
exit(-1);
}
}
}
if(close(fd[1][1])==-1){//closing the write of the second pipe
perror("Error: closing the read pipe [child]-");
exit(-1);
}
if(close(fd[0][0])==-1){//closing the read of the first pipe
perror("Error: closing the read pipe [child]-");
exit(-1);
}

因此,我在父进程的代码中向fd[0][1]写入了10个单独的数字(转换为字符串((我在这里没有显示,但我知道它是有效的(。现在,我只能从fd[0][0]读取一个数据。如果我删除

if(write(fd[1][1],temp,10)==-1){
perror("Error: failed to write to pipe -");
exit(-1);
}

它将读取所有数据。为什么这会干扰阅读?如果我的代码中还有其他改进,请告诉我。

这是完整的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <sys/wait.h>
int main(int argc,char*argv[]){
/*---------------Error checking----------------*/
//checking to see if correct number of arguments is provided
if(argc!=2){
fprintf(stderr, "Usage:ntpfact nn");
exit(1);
}
//checking to see if correct argument is provided
char *leftOver;
int checkNumber=strtol(argv[1],&leftOver,10);
int squareroot_checkNumber=sqrt((double)checkNumber);
if(leftOver[0]!=''){
fprintf(stderr, "Usage:ntpfact nn");
exit(1);
}
if(checkNumber<=0){
fprintf(stderr, "Usage:ntpfact nn");
exit(1);
}
/*-------------------------------------------*/
char factors[checkNumber-1][100];
int factor_size=0;
strcpy(factors[0],"-1");
int child;
int number_process=0;//keeps track of number of processes created
int isInitialized=0;//sees if we are initialized yet or not
int filter;//stores the number used to filter the data
int fd[2][2];
//create the two pipes
if(pipe(fd[0]) == -1){
perror("Error: creating a pipe -");
}
if(pipe(fd[1]) == -1){
perror("Error: creating a pipe -");
}
do{
child=fork();
if(child<0){
perror("Error: making a child -");
exit(1);
}
if(child>0){
if(isInitialized==0){//not initialized yet //no data has been written to the pipe
isInitialized=1;//it has been initilized now
if(close(fd[0][0])==-1){//closing the read of the first pipe
perror("Error: closing the read pipe [parent]-");
exit(-1);
}
//writing 2 to m to the pipe
for(int i=2;i<=checkNumber;i++){
char number_string[100];
sprintf(number_string,"%d",i);
if(write(fd[0][1],number_string,10)==-1){
perror("Error: failed to write to pipe -");
exit(-1);
}
}
if(close(fd[0][1])==-1){//closing the write of the first pipe after finishing writing
perror("Error: closing the write pipe [parent]-");
exit(-1);
}
}else{
char temp_parent[100];//stores the temporary value from the pipe read
if(close(fd[0][0])==-1){//closing the read of the first pipe
perror("Error: closing the write first pipe [parent]-");
exit(-1);
}
if(close(fd[1][1])==-1){//closing the write of the second pipe
perror("Error: closing the write second pipe [parent]-");
exit(-1);
}
while(read(fd[1][0],temp_parent,10)>0){//reading the data from the second pipe
//write the data to the first pipe so that it can be read by the child for filtering
if(write(fd[0][1],temp_parent,10)==-1){
perror("Error: failed to write to pipe -");
exit(-1);
}
printf("[%d] parent received %sn",getpid(),temp_parent);
}

if(close(fd[0][1])==-1){//closing the write of the first pipe
perror("Error: closing the read first pipe [parent]-");
exit(-1);
}
if(close(fd[1][0])==-1){//closing the read of the second pipe
perror("Error: closing the read second pipe [parent]-");
exit(-1);
}
}
}else if(child==0){
char temp[100];//stores the temporary value from the pipe read
if(close(fd[0][1])==-1){//closing the write of the first pipe
perror("Error: closing the write pipe [child]-");
exit(-1);
}
if(close(fd[1][0])==-1){//closing the read of the second pipe
perror("Error: closing the write pipe [child]-");
exit(-1);
}
if(!(read(fd[0][0],temp,10)>0)){//the first number read specifies the overall filter
fprintf(stderr,"Error: while reading the filter");
exit(-1);
}
filter=strtol(temp,NULL,10);//storing the first read as the filter
while(read(fd[0][0],temp,10)>0){//reading the rest of the data for filtering
printf("[%d] child received %sn",getpid(),temp);
if(strtol(temp,NULL,10)%filter!=0){//checking to see if the read number is divisible by the filter
//if not, we write it to the second pipe
if(write(fd[1][1],temp,10)==-1){
perror("Error: failed to write to pipe -");
exit(-1);
}
}
}
if(close(fd[1][1])==-1){//closing the write of the second pipe
perror("Error: closing the read pipe [child]-");
exit(-1);
}
if(close(fd[0][0])==-1){//closing the read of the first pipe
perror("Error: closing the read pipe [child]-");
exit(-1);
}
}
}while(0 && child==0 && checkNumber/2<=filter);
}

我找到了解决方案。但这要归功于Barmar对我的帮助,并在评论中暗示了我可能存在的问题。Barmar实际上暗示的问题是"当你向fd[1][1]写入时,你会被卡住。问题是从fd[1][0]读取的过程"。显然,写入终止了程序,因为它知道没有其他进程可以读取数据(父进程完成执行或进程读取fd[1][0](。因此,在父进程(另一个进程(中添加等待将解决问题。

最新更新