linux's fork and system resources

Posted by Dmitry Bespalov on Stack Overflow See other posts from Stack Overflow or by Dmitry Bespalov
Published on 2012-06-24T21:12:47Z Indexed on 2012/06/24 21:15 UTC
Read the original article Hit count: 213

Filed under:
|
|

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() ?

© Stack Overflow or respective owner

Related posts about linux

Related posts about resources