Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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
使用fork用C语言编写消费者和生产者代码?_C_Operating System_Fork_Producer Consumer - Fatal编程技术网

使用fork用C语言编写消费者和生产者代码?

使用fork用C语言编写消费者和生产者代码?,c,operating-system,fork,producer-consumer,C,Operating System,Fork,Producer Consumer,我必须使用fork()编写代码函数,其中一个文件,即制作人将打开一个DATA.txt文件,并添加一个取自testfile.txt文件的字符,然后将转向传递给consumer.c文件,consumer.c文件将再次打开DATA.txt文件,并从文件中提取字符并在屏幕上打印。我知道逻辑是如何工作的,但我被一个无限循环困住了,不知道如何摆脱它。任何帮助都将不胜感激。 数据和回合一次不能有多个字符 以下是该程序的文件 output: Segmentation fault: 11 } 您的代码

我必须使用fork()编写代码函数,其中一个文件,即制作人将打开一个DATA.txt文件,并添加一个取自testfile.txt文件的字符,然后将转向传递给consumer.c文件,consumer.c文件将再次打开DATA.txt文件,并从文件中提取字符并在屏幕上打印。我知道逻辑是如何工作的,但我被一个无限循环困住了,不知道如何摆脱它。任何帮助都将不胜感激。 数据和回合一次不能有多个字符 以下是该程序的文件

output: 
Segmentation fault: 11

}


您的代码有几个问题

首先,您应该检查文件指针是否返回NULL,因为
fopen()
函数可能会失败,然后返回NULL

其次,发生分段错误是因为您
fp++
,但您在该内存位置只
fopen()
”了一个文件。递增指针仅在使用指向数组的指针时有效

第三,在处理源代码时,我注意到您似乎对
fgetc()
有错误的假设。如果该函数失败,它将返回
EOF
常量。但是,此EOF常量通常大于有效返回字节可能具有的值范围,因此
fgetc()
的返回值为int类型,以便能够指示


编辑:我替换了我以前的,可以说是相当糟糕的回复。下面的注释基本上是指我在复制粘贴中的错误,制作人最终等待
'1'
,这给了我一个错误的印象。

您应该让父进程等待其子进程。试试这个:

pid = fork();
if(pid < 0){
//print some kind of error
} else if(pid == 0){
        producer();
        exit(0);
} else if(pid > 0){
        wait(&childStatus);// wait for the child to finish
        consumer();
}
pid=fork();
if(pid<0){
//打印某种错误
}否则如果(pid==0){
生产者();
出口(0);
}否则,如果(pid>0){
wait(&childStatus);//等待子进程完成
消费者();
}

*childStatus是在阻塞等待函数中分配的正常int变量。

fp++;//增加文件指针
是不正确的。当您执行诸如
fread(3)
fwrite(3)
之类的操作时,底层I/O库负责移动文件中的读/写指针。从您的角度来看,
文件*
是一种不透明的结构,您对此一无所知。如果您在调试器中运行代码,您应该能够确定segfault的来源。好的,谢谢您的通知。但是代码仍然会运行无限长的时间。也请阅读,在这种情况下,您建议我做什么,因为我不知道如何解决该问题?我认为我有问题,尽管事实证明生产者确实在等待“0”,不知怎的,我开始认为它错误地等待了“1”。然而,在尝试构建源代码并对valgrind运行它之后,源代码的问题在于producer.c:
fp++
中有一条毫无意义的指令。它不仅没有任何用处,还导致了您正在体验的segfault。我还根据您的代码生成了一个工作版本,您可以在这里看到:
//producer.c file

#include"header.h" 

void producer(){

char t; // for TURN LOOP
char p;

FILE *fp = fopen("testfile.txt", "rt");  

while(!feof(fp)){ 
    do{
        FILE *TURN = fopen("TURN.txt", "rt");  
        t = fgetc(TURN) ; 
        fclose(TUdRN);  // closing turn file
    } while(t!= '0') ; // wait for turn 


    FILE *DATA = fopen("DATA.txt", "w+"); // opening data file to write a  character   in it
    p = fgetc(fp); // getting character from  testfile to write in data file

    fputc(p, DATA); 
    fclose(DATA) ;  // closing data file

    FILE *TURN = fopen("TURN.txt", "r+"); // opening turn file to change the value to 1 and pass the turn to consumer.c

    fputc('1', TURN);  // overwriting 1 in the turn file so now turn as only 1 in it. 
    fp++ ;  // incrementing the file pointer
} // end of the while loop

 fclose(fp);
#include"header.h" //  including the header file

void consumer (){
char c; 
char t;

while(1){

    do{
        FILE *ft = fopen("TURN.txt","r");  // opening turn file
        t = fgetc(ft) ;  
        fclose(ft) ; // closing the turn file
    } while( t!= '1' ); 

    FILE *dt = fopen("DATA.txt","r");  // opening data file to read from it
    c = fgetc(dt); 
    if(c =='\0'){ 
        break ;  
    }
    //else data file in not empty print the character on the screen in same order as testfile
    printf("%c",c) ; 

    FILE *ft = fopen("TURN.txt","r+"); 
    fputc('0',ft);  // producer.c turn

    //closing both turn and data file
    fclose(dt);  
    fclose(ft) ;
} // end of while loop
} //  end of consumer method
// header files for consumer. and producer.c and main.c
#ifndef HEADER_H // Including guards to avoid reimplimentation
#define HEADER_H
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
// function signatures from the producer.c and consumer.c
void producer(); 
void consumer();
#endif // !LIST_H
//make file
program: main.o consumer.o producer.o header.h
    gcc -o program main.c consumer.c producer.c
main.o: consumer.c producer.c header.h
    gcc -c consumer.c producer.c
consumer.o: consumer.c header.h
    gcc -c consumer.c
producer.o: producer.c header.h
    gcc -c producer.c
clean:
    rm *.o program.o
pid = fork();
if(pid < 0){
//print some kind of error
} else if(pid == 0){
        producer();
        exit(0);
} else if(pid > 0){
        wait(&childStatus);// wait for the child to finish
        consumer();
}