当在内部使用fork时,C程序迭代太多

问题描述 投票:1回答:1

我正在从文件中读取文本行,并且对于每行我使用几个{fork() - >子进程调用execvp()来处理它,而parent调用wait()}。在进程结束时我将结果写入文件。

问题是:while循环似乎迭代太多,也写入文件。

results.csv文件包含6行而不是2行(while迭代迭代一个带有2行的文本文件,但是当我使用printf时,似乎最后一行被读取两次)。

我在这里错过了什么?

代码示例是:

FILE* results = fopen("results.csv", "w");
if (results == NULL){
    fclose(fp);
    perror("Failed opening results file");
    exit(-1);
}
fdIn = open(inputPath, O_RDONLY);
if (fdIn < 0){
    perror("Failed opening input file");
    exit(-1);
}
while (fgets(student, sizeof(student), fp) != NULL) {
    // override end line char of unix ('\n') with '\0'
    student[strlen(student)-1] ='\0';
    pid = fork();
    if (pid < 0){
        close(fdIn);
        perror("Failed creating process for executing student's program");
        exit(-1);
    }
    if (pid == 0) {// son process code
        fdOut = open("tempOutput.txt", (O_WRONLY | O_CREAT | O_TRUNC), 0666);
        if (fdOut < 0){
            perror("Failed opening temporary output file");
            exit(-1);
        }
        close(1);
        dup(fdOut);
        close(fdOut);
        close(0);
        dup(fdIn);
        close(fdIn);
        char studProgPath[bufSize];
        strcpy(studProgPath,studentsFolderPath);
        strcat(studProgPath,"/");
        strcat(studProgPath,student);
        strcat(studProgPath,"/");
        strcat(studProgPath,"a.out");
        char * args[] = {"a.out", NULL};
        ret_code = execvp(studProgPath,args);
        if (ret_code == -1){
            perror("Failed executing student program");
            exit(-1);
        }
    }
    waited = wait(&stat);
    if (stat == -1){ // need to grade 0
        printf("%s,0\n",student);
    }else{ // open process to compare the output with the expected
        pid = fork();
        if (pid < 0){
            perror("Failed opening process for comparing outputs");
            exit(-1);
        }
        if(pid == 0) { // son process
            char * args[] = {"comp.exe",outputPath,"tempOutput.txt",NULL};
            ret_code = execvp("comp.exe",args);
            exit(ret_code);
        }
        waited = wait(&stat);
        if (stat == -1) {
            perror("Failed executing comparing program");
            exit(-1);
        } else if (stat == 0 || stat == 1) { // if outputs are not the same
            fprintf(results,"%s,0\n",student);
        } else { // matching outputs grade 100
            fprintf(results,"%s,100, pid: %d\n",student,getpid());
        }
    }
}
c fork
1个回答
1
投票

获取三个条目的文件在此处打开:

FILE* results = fopen("results.csv", "w");

在函数调用results之前,以下几行写入此fork()文件:

} else if (stat == 0 || stat == 1) { // if outputs are not the same
  fprintf(results,"%s,0\n",student); 
} else { // matching outputs grade 100 
  fprintf(results,"%s,100, pid: %d\n",student,getpid()); 
}

这个文件应该在fork之前用fflush(results)刷新,否则results的缓冲区可能会刷三次:在父级中,在子级中的两个副本中。

此外,在调用execvp之前,resultsstudent应该用fclose(results)student关闭。如果文件未关闭,则a.out可能会操纵results文件。我假设a.out是一个你无法控制的外部代码。

while (fgets(student, sizeof(student), fp) != NULL) {
    // override end line char of unix ('\n') with '\0'
    student[strlen(student)-1] ='\0';
    fflush(results); // otherwise each child may flush the same chars
    pid = fork();
    if (pid < 0){
        fclose(results); // otherwise ./a.out might write to this file
        fclose(fp); // better also close it.
        close(fdIn);
© www.soinside.com 2019 - 2024. All rights reserved.