Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将文件描述符的文件指针用于多个子进程而不获取;“错误的文件描述符”;C中的错误?_C_Linux_Pipe_File Descriptor_File Pointer - Fatal编程技术网

如何将文件描述符的文件指针用于多个子进程而不获取;“错误的文件描述符”;C中的错误?

如何将文件描述符的文件指针用于多个子进程而不获取;“错误的文件描述符”;C中的错误?,c,linux,pipe,file-descriptor,file-pointer,C,Linux,Pipe,File Descriptor,File Pointer,我正在实现一个场景,其中父进程分叉许多子进程,这些子进程执行计算并通过管道将结果返回给父进程。由于子进程必须使用外部库的数据类型(GMP的mpz_t类型),因此它们需要使用该库自己的输出流函数。此函数允许将文件指针作为输入而不是文件描述符。因此,我获取管道写入端的文件指针,并用于写入一些数据。下面给出了代码的子进程和父进程部分: pid_t ppid; ppid = getpid(); struct sigaction sig; sigemptyset(&sig.sa_mask); si

我正在实现一个场景,其中父进程分叉许多子进程,这些子进程执行计算并通过管道将结果返回给父进程。由于子进程必须使用外部库的数据类型(GMP的mpz_t类型),因此它们需要使用该库自己的输出流函数。此函数允许将文件指针作为输入而不是文件描述符。因此,我获取管道写入端的文件指针,并用于写入一些数据。下面给出了代码的子进程和父进程部分:

pid_t ppid;
ppid = getpid();
struct sigaction sig;
sigemptyset(&sig.sa_mask);
sig.sa_flags = 0;
sig.sa_handler = sig_usr;

if(sigaction(SIGINT,&sig,NULL) != 0)
    printf("\ncan't catch SIGINT\n");

if(sigaction(SIGUSR1,&sig,NULL) != 0)
    printf("\ncan't catch SIGINT\n");

pid_t childpid;

pid_t childpids[operand1Length*operand2Length];
int childPIDInd = 0;

//Create pipe: (must do before fork() so FDs are inherited by child)
int pipefd[2];  //array to hold pipe FDs
pipe(pipefd);

for(i=operand2Length-1, k=0; i>=0; i--, k++){
    for(j=operand1Length-1, l=0; j>=0; j--, l++){

        childpid = fork();

        switch(childpid){
            case -1:
                //fork error
                perror("fork failed!\n");
                exit(EXIT_FAILURE);

            case 0:
                close(pipefd[0]);
                subOperandLength = subOperands[k].length;

                FILE* fp = NULL;

                fhe_mul(subOperands[k].operand[subOperandLength-1-k-l], num1->operand[j], num2->operand[i], pk);

                while(WritePermit);  // unless parent process sends a signal any child process cannot enter this critical section.

                fp = fdopen(pipefd[1], "w");

                if(fp == NULL)
                    fprintf(stderr, "Child Process #%d file pointer is NULL. Error: %s. Pipe FD: %d\n", getpid(), strerror(errno), pipefd[1]);  
                    //Except the child process which enters the critical section first, 
                    //for all other child processes fp is NULL.

                gmp_fprintf(fp, "%Zd\n", subOperands[k].operand[subOperandLength-1-k-l]);
                gmp_fprintf(fp, "%d\n", k);
                gmp_fprintf(fp, "%d\n", subOperandLength-1-k-l);

                fflush(fp);
                fclose(fp);

                kill(ppid, SIGUSR1);

                exit(EXIT_SUCCESS);

            default:
                childpids[childPIDInd] = childpid;
                childPIDInd++;

                close(pipefd[1]);

                if(i == 0 && j == 0){ // last child was created
                    kill(childpids[0], SIGINT);

                    mpz_t deneme;
                    mpz_init(deneme);

                    FILE* fs = fdopen(pipefd[0], "r");

                    int forIndex, pidIndex;

                    for(forIndex=0, pidIndex=1; forIndex<4; forIndex++, pidIndex++){
                        while(WritePermit2);
                        while((gmp_fscanf(fs, "%Zx\n", &deneme)) > 0){
                            gmp_fprintf(stdout, "Parent Process #%d: %Zd\n", getpid(), deneme);
                        }
                        kill(childpids[pidIndex], SIGINT);
                        WritePermit2=1;
                    }

                    fclose(fs);

                    int status;
                    int i=0;
                    int clean = 1;
                    while (i < operand1Length*operand2Length) {
                        wait(&status);
                        if(!WIFEXITED(status))
                            clean = 0;
                        i++;
                    }

                    if(!clean){
                        printf("I am having some problems with my children! :'(\n");
                        exit(EXIT_FAILURE);
                    }

        }
    }
}
pid\t ppid;
ppid=getpid();
结构sig动作sig;
sigemptyset(&sig.sa_mask);
sig.sa_标志=0;
sig.sa_handler=sig_usr;
if(sigaction(SIGINT,&sig,NULL)!=0)
printf(“\n无法捕获SIGINT\n”);
if(sigaction(SIGUSR1,&sig,NULL)!=0)
printf(“\n无法捕获SIGINT\n”);
pid_t childpid;
pid_t childPID[操作数1长度*操作数2长度];
int childPIDInd=0;
//创建管道:(必须在fork()之前执行,以便子级继承FD)
int-pipefd[2]//用于容纳管道FDs的阵列
管道(pipefd);
对于(i=operation2length-1,k=0;i>=0;i--,k++){
对于(j=operation1length-1,l=0;j>=0;j--,l++){
childpid=fork();
开关(childpid){
案例1:
//分叉错误
perror(“fork失败!\n”);
退出(退出失败);
案例0:
关闭(pipefd[0]);
subOperandLength=subOperands[k]。长度;
FILE*fp=NULL;
fhe_mul(子操作数[k]。操作数[subOperands-length-1-k-l],num1->操作数[j],num2->操作数[i],pk);
while(WritePermit);//除非父进程发送信号,否则任何子进程都不能进入此关键部分。
fp=fdopen(pipefd[1],“w”);
如果(fp==NULL)
fprintf(stderr,“子进程#%d文件指针为空。错误:%s。管道FD:%d\n”、getpid()、strerror(errno)、pipefd[1]);
//除了首先进入关键部分的子进程,
//对于所有其他子进程,fp为NULL。
gmp_fprintf(fp,“%Zd\n”,子操作数[k]。操作数[suboperandth-1-k-l]);
gmp_fprintf(fp,“%d\n”,k);
gmp_fprintf(fp,“%d\n”,子循环长度-1-k-l);
fflush(fp);
fclose(fp);
kill(ppid,SIGUSR1);
退出(退出成功);
违约:
childpids[childPIDInd]=childpid;
childPIDInd++;
关闭(pipefd[1]);
如果(i==0&&j==0){//创建了最后一个子项
kill(childpids[0],SIGINT);
德涅姆音乐学院;
mpz_init(deneme);
文件*fs=fdopen(pipefd[0],“r”);
int-forIndex,pidinex;
for(forIndex=0,pidIndex=1;forIndex=0){
gmp_fprintf(stdout,“父进程#%d:%Zd\n”,getpid(),deneme);
}
kill(childpids[pidinex],SIGINT);
WritePermit2=1;
}
fclose(fs);
智力状态;
int i=0;
int clean=1;
while(i<操作数1长度*操作数2长度){
等待(&状态);
如果(!妻子退出(状态))
清洁=0;
i++;
}
如果(!清洁){
printf(“我和我的孩子有一些问题!:”(\n”);
退出(退出失败);
}
}
}
}
fp只对其中一个子进程有效。然后,不知何故,它变为NULL,因此gmp_fprintf在其他子进程上引发错误


请毫不犹豫地询问您是否需要有关代码的更多详细信息。提前感谢您的帮助!

有了代码后,
默认值:
开关的案例,在双嵌套
for
循环中,包含
关闭(pipefd[1]);
。显然,这只在内部循环的第一次迭代中起作用;此后,管道断开。后续的子级不会获得可操作的管道,因为它们的父级关闭了文件描述符


修复方法是确保在创建所有子级之前,父级不会关闭管道的写入端。

您显示的小代码不包含错误,因此它可能存在于您未显示的代码中。您能否尝试创建一个并向我们显示?您需要向我们显示与该代码相关的分支位置,以及如何关闭管道文件descriptors等。我修改了问题并试图给出更多的代码细节。@Joachimpileborg有代码存在时,开关的
默认值:
案例,在双嵌套
for
循环中,包含
关闭(pipefd[1])
。显然,这只在内部循环的第一次迭代中起作用;此后,管道被破坏。后续的子级无法获得可操作的管道,因为它们的父级关闭了文件描述符。您完全正确@JonathanLeffler。我将其移动到了
if(I==0&&j==0){//最后一个子块是创建的
block,它可以无缝工作,甚至不需要信令。非常感谢!)