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



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

问题是:while 循环似乎迭代太多

,写入文件也是如此。

结果.csv文件包含 6 行,而不仅仅是 2 行(同时迭代迭代一个 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 ''
    student[strlen(student)-1] ='';
    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,0n",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,0n",student);
        } else { // matching outputs grade 100
            fprintf(results,"%s,100, pid: %dn",student,getpid());
        }
    }
}

获得三重条目的文件在此处打开:

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

以下行写入此results文件,略高于函数调用fork()

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

此文件应在分叉之前用 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 ''
    student[strlen(student)-1] ='';
    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);

最新更新