|
From: E L. <cr...@my...> - 2008-03-07 16:26:09
|
I have a situation where it appears Windows does not release a file after immediately after my program calls fclose() when there are redirected child processes active. Is this standard operation? Or an artifact of mingw libs working with ms libs? Is there a way to avoid this problem?
Below is test code that shows the issue. When popen is not called, the test file can be deleted immediately. When popen is called, the test file can not be deleted until the pipe is closed (or at EOF)
child.c:
#include <stdio.h>
int main()
{
int i;
for (i=0;i<10;i++) {
printf("line %i\n", i);
fflush(stdout);
_sleep(100);
}
return 0;
}
parent.c:
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
int main()
{
FILE *f, *p;
int ch, err = 1;
/* create any file to test unlink */
f = fopen("junk.txt", "w");
fprintf(f, "junk\n");
fclose(f);
f = fopen("junk.txt", "r");
p = popen("child.exe", "r");
fclose(f);
do {
if (err) {
err = unlink("junk.txt");
printf("unlink %s\n", err ? "error" : "success");
}
ch = fgetc(p);
} while (err);
return 0;
}
|