linux's fork and system resources
- by Dmitry Bespalov
Suppose I have following code in linux:
int main()
{
FILE* f = fopen("file.txt", "w");
fork();
fwrite("A", 1, 1, f);
fclose(f);
return 0;
}
What I know about fork from documentation, is that it makes the copy of current process. It copies state of the memory as well, so *f should be equal in both instances. But what happens with system resources, such as a file handle? In this example I open the file with write intentions, so only one instance can write into file, right? Which of the instances will actually write into file? Who should care further about the file handle, and call fclose() ?