Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The example I have is Minecraft. When running Bukkit on Linux I can remove or update the .jar files in the /plugins folder and simply run the 'reload' command.

In Windows, I have to take the whole server process down because it will complain that the .jar file is currently in use when I try to remove or replace it.

This is awesome to me, but why does it happen? What is Linux doing differently here?

share|improve this question
add comment

1 Answer

up vote 17 down vote accepted

Linux deletes a file completely differently than the way Windows does. First, a brief explanation on how files are managed in the *unix native file systems.

The file is kept on the disk in the multilevel structure called i-node. Each i-node has an unique number on the single filesystem. The i-node structure keeps different information about a file, like its size, data blocks allocated for the file etc., but for the sake of this answer the most important data element is a link counter. The directories are the files that keep records about the files. Each record has the i-node number it refers to, the file name length and the file name itself. This scheme allows one to have 'pointers', i.e. 'links' to the same file in different places with different names. The link counter of the i-node actually keeps the number of links that refer to this i-node.

What happens when some process opens the file? First the open() function searches for the file record. Then it checks if the in-memory i-node structure for this i-node already exists. This may happen if some application already had this file opened. Otherwise, the system initializes a new in-memory i-node structure. Then the system increases the in-memory i-node structure open counter and returns to the application its file descriptor.

The Linux library call to delete a file is called unlink. This function removes the file record from a directory and decrements the i-node's link counter. If the system found that an in-memory i-node structure exists and its open counter is not zero then this call returns the control to the application. Otherwise it checks if the link-counter became zero and if it does then the system frees all blocks allocated for the i-node and the i-node itself and returns to the application.

What happens that an application closes a file? The function close() decrements the open counter and checks its value. If the value is non-zero the function returns to the application. Otherwise it checks if the i-node link counter is zero. If it is zero, it frees all blocks of the file and the i-node before returning to the application.

This mechanism allows you to "delete" a file while it is opened. At the same time the application that opened a file still has access to the data in the file. So, JRE, in your example, still keeps its version of file opened while there is another, updated version on the disk.

More over, this feature allows you to update the glibc(libc) - the core library of all applications - in your system without interrupting its normal operation.

Windows

20 years ago we did not know any other file system than FAT under DOS. This file system has a different structure and management principles. These principles do not allow you to delete a file when it is opened, so the DOS and lately Windows has to deny any delete requests on a file that is open. Probably NTFS would allow the same behavior as *nix file systems but Microsoft decided to maintain the habitual behavior of the file deletion.

This is the answer. Not short, but now you have the idea.

share|improve this answer
    
Tried renaming plugin file while server running: i.imgur.com/xibyF.png –  ioSamurai Sep 27 '12 at 18:32
    
open cmd window, change to this directory and use ren MonsterB.jar MonsterB.ja_ - it should work. It works for dll and exe files definitely. –  Serge Sep 27 '12 at 18:36
    
i.imgur.com/4cQWl.png –  ioSamurai Sep 27 '12 at 18:38
    
tried renaming notepad++.exe while it was running, WAS able to rename, so it could be isolated to executables –  ioSamurai Sep 27 '12 at 18:39
5  
NTFS actually does support it, but the C Library fopen command calls CreateFile with the FILE_SHARE_DELETE flag, so it disallows it for most programs that open files. –  Random832 Oct 17 '12 at 15:25
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.