Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to recompile an executable while it's running?

What happens if I recompile an executable while it's running? Does the operating system read all of the executable's contents into memory when it starts running it, so it will never read the new executable file? Or will it read sections of the new executable file thinking it hasn't changed, leading to possibly undefined behaviour?

What if I have a script running which repeatedly invokes an executable in a loop, and I recompile the executable while the script is running. Is it guaranteed that future iterations of the loop will invoke the new executable, and only the result of the invocation that was in progress when the switch was made might be corrupted?

My OS is Linux, but I'm also curious about what happens on Windows.

like image 436
HighCommander4 Avatar asked Jul 28 '10 19:07

HighCommander4


People also ask

Is executable file is generated after running of C program?

No ... each platform may have a different executable format requirements, different hardware architectures, different executable memory layouts determined by the linker, etc. A compiled executable is "native" to it's currently compiled platform, not other platforms.

Does compiling create an executable?

You produce executable code by compiling and linking in one step. An executable file has a filename extension of .exe. To produce executable code, you need to modify the Output Type setting in the project properties.

What is a compiled executable?

Executable files contain binary machine code that has been compiled from source code. This low-level code instructs a computer's central processing unit on how to run a program. The processor interprets the machine code and tells the computer's hardware what to do. Eye on Tech.

Are executable files readable?

Until an exe runs its just a binary file, so yes you can read it.


2 Answers

Since this is a conventional compiler, that writes out an executable file, let's follow it in Linux.

The first thing to know is that a Linux filename doesn't directly refer to the file, but rather to a directory entry, which is independent of the filename. A file doesn't actually need to have a filename, but if it doesn't it will be difficult to refer to it.

If a process is using a file, and you replace or delete it, the process will continue using that file through its directory entry. Any new process using the file, or looking it up, will get the new version (if you replaced it) or fail to find it (if you deleted it). Once all the processes are through with the old file, it will be deleted from the file system.

Therefore, if you recompile and create a new executable of the same name, you won't affect the running process. It will continue to use the old executable. Any new process that tries to open the file will get the new one. If you've got system("foo"); in a loop, each time it executes it it will see what the filename foo means right then.

Windows handles files differently. In general, if there's a process using a file, the file is locked and may not be deleted or replaced.

like image 183
David Thornley Avatar answered Oct 17 '22 21:10

David Thornley


It depends.

If the OS read the whole of the executable into memory and doesn't refer back to the disk image then yes you can recompile it while it was "in use".

In practice this doesn't always happen. If the OS keeps a file handle open (like Windows does) on the executable this will prevent the file being deleted and/or overwritten.

With Linux/Unix it is possible to overwrite a file that's "in use". See David Thornley's answer for a detailed explanation.

like image 22
ChrisF Avatar answered Oct 17 '22 20:10

ChrisF