我正在从文件中读取文本行,对于每一行,我使用几个{fork()->子进程调用execvp(),而父进程调用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());
}
}
}
答案 0 :(得分: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()); }
应该在派生之前用fflush(results)
刷新此文件,否则results
的缓冲区可能会刷新3次:在父级中,在子级中的两个副本中。
此外,在调用execvp之前,results
和student
应该用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);