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

时间:2017-12-01 17:23:26

标签: c operating-system fork producer-consumer

我必须使用fork()函数编写代码,其中一个文件即生产者将打开 DATA.txt 文件并添加一个取自 testfile.txt 文件的字符并将传递给consumer.c文件,再次打开 DATA.txt 文件并从文件中取出该字符并将其打印在屏幕上。我知道逻辑是如何工作的,但我陷入无限循环,不知道如何摆脱它。任何帮助将不胜感激。 DATA和TURN一次不能包含多个字符 以下是该程序的文件。

output: 
Segmentation fault: 11

...

//main.c file

#include"header.h" //  including the header file

int main(){

FILE* fp = fopen("TURN.txt","w+"); 
if(fp== NULL){
    exit(1); // exit with an error
}
fputc('0',fp) ; // writing to file TURN.txt
fclose(fp); 
// including consumer.c and producer.c files
// now we have access to functions in consumer.c and producer.c

int pid = fork(); 
if(pid == -1) exit(1) ; // Failure exit


if(pid == 0){consumer();}
if(pid != 0){producer();}

return 0 ; // success(Erfolg)}

...

//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

2 个答案:

答案 0 :(得分:1)

您的代码存在一些问题。

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

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

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

编辑:我取代了我之前的,可以说是非常可怕的反馈。下面的评论基本上是指我在复制粘贴中的错误,其中制作人最终等待'1',这让我对这种情况产生了错误的印象。

答案 1 :(得分:1)

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

declare  @p  char(1) = '1'

select o.docNum  
from OINV o
where @p  <> '1'
OR 
(
    SUBSTRING(convert(varchar(10/*whatever size is appropriate*/), o.docNum), 1, 1) <> '9' 
    and 
    LEN(convert(varchar(10/*whatever size is appropriate*/), o.docNum)  ) < 7
)

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