How it works in the OS. So first unused file descriptor is 3 in file descriptor table. After that in close system call is free it this 3 file descriptor and then after set 3 file descriptor as null.
So when we called second open , then first unused fd is also 3. So, output of this program is 3. A successful read updates the access time for the file. Also, some times read system call should read less bytes than cnt.
Those bytes are as follows: 0 0 0 foo. Suppose that foobar. Then what is the output of the following program? Skip to content. Change Language. Related Articles. Table of Contents. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Visit chat. Linked Related Hot Network Questions.
Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. In any event, the kernel has two levels of indirection. There is the internal structure that represents the file itself, which is reference counted. There is an "open file description" that is reference counted. And then there is the file handle, which is not reference counted. The file structure points the way to the inode itself.
The open file description contains things like the open mode and file pointer. When you call close, you always close the file handle. When a file handle is closed, the reference count on its open file description is decremented. If it goes to zero, the open file description is also released and the reference count on the file itself is decremented. Only if that goes to zero is the kernel's file structure freed. There is no chance for one process to release a resource another process is using because shared resources are reference counted.
In this case not a lot will happen. The reference counter for the file descriptor will be decremented by one. The same file descriptor is usually held by the shell from which the program was run, so the file descriptor needs to be kept. The kernel keeps reference counts for all files inodes that are open. As long as the reference count is greater than zero the file will be kept. I would expect a separate counter is kept for open file handles. Once this hits zero the kernel can release the memory used by the file handle.
When all references to the file directory entries and file handles have been removed, the file system code will mark the inode for reuse. Any blocks the file has are made available for allocation. Many file systems will clear the block pointers in the inode when it is released. This makes recovering a deleted file difficult. Updates to disk may be buffered and completed at a later time. Sign up to join this community. The best answers are voted up and rise to the top.
Stack Overflow for Teams — Collaborate and share knowledge with a private group.
0コメント