Why does this executable still execute after I deleted it

executablefilesnfs

I deleted the executable, so why is it still running?

root@raspberrypi:/test# ls -la
total 11096
drwxrwxrwx  2 pi pi     4096 Mar 12 18:26 .
drwxrwxr-x 11 pi pi     4096 Feb 28 13:50 ..
-rwxrw-rw-  1 pi pi    12149 Feb 28 13:00 .cproject
-rwxrw-rw-  1 pi pi     3183 Mar 12 18:26 main.cpp
-rwxrw-rw-  1 pi pi     2169 Feb 28 14:28 main.cpp~
-rwxrw-rw-  1 pi pi     1862 Feb 28 13:20 original.cpp
-rwxrw-rw-  1 pi pi      984 Feb 28 13:09 .project
-rwxrw-rw-  1 pi pi 11323309 Jan 28 12:54 teatro.png
root@raspberrypi:/test# ./testedfb 
running...

–update:

Some time later (after I tried to compile again, but there's an error, and the executable was not created) the behavior changed to:

root@raspberrypi:/test# ./testedfb 
bash: ./testedfb: No such file or directory

But I'm still curious; why was it was running?

–update (2):

Just happened again, and I did the test:

root@raspberrypi:/test# killall -9 testedfb
testedfb: no process found
root@raspberrypi:/test# ./testedfb 
running...

And it still runs…

I'm on a Raspberry Pi's Bash.

–update (3):

It just happened the oposite now, the file was there, but it was not found:

root@raspberrypi:/test# ./testedfb 
bash: ./testedfb: No such file or directory
root@raspberrypi:/test# ls
main.cpp  main.cpp~  original.cpp  teatro.png  testedfb

(and on the next attempt it did run)

Maybe it's the NFS (this folder is mounted) that is too slow to update?
(thanks @derobert for asking about the filesystem) – this would answer this second case, not the first one, as I cannot see the executable, but still I can execute it… (and looks like the process is not running too)

Best Answer

To run a program, it is opened by the kernel (or its interpreter). As long as it is running, the file is kept open.

Deleting a file that is open in Unix deletes its name from the disk, the file itself is only deleted when it is closed.

Add the above two, and you see that the running program continues to use its program file, and that one will be deleted only when the program exits.