Reputation: 18754
I have a broad question:
Suppose that I have a C++ program and I started to run it on a file in the background with some configuration which can be set internally. While it was running, I changed these internal configurations, compiled it and started to run it on another file.
Is this going to affect the previous instance which was already running in the background ? or since it was already up and running it won't ? Any ideas on this is appreciated.
Upvotes: 17
Views: 6924
Reputation: 16855
It is not safe to modify an executable that is running. As per What happens when you overwrite a memory-mapped executable?
Under Linux, if you replace an executable while it is running, the results are unpredictable and it may crash.
If you delete the file and compile a new version of the program then what will happen is very well defined. The already running instance will use the previous code, and that will be held in memory by the operating system until the program terminates. Any new instances will use the new code.
The summary: You should make sure your build system deletes the old executable before recompiling, and so long as that is true then the recompile will not take effect until you rerun the program, otherwise the behaviour is undefined (read SIGSEGV).
Appendix of sorts:
JamesKanze rightly pointed out that the linker itself may delete the file before writing its output, if this is the case then it will always behave as if you'd deleted the file yourself before recompiling (the sane scenario). Looking at bfd/cache.c from the binutils cvs head:
/* Create the file.
Some operating systems won't let us overwrite a running
binary. For them, we want to unlink the file first.
However, gcc 2.95 will create temporary files using
O_EXCL and tight permissions to prevent other users from
substituting other .o files during the compilation. gcc
will then tell the assembler to use the newly created
file as an output file. If we unlink the file here, we
open a brief window when another user could still
substitute a file.
So we unlink the output file if and only if it has
non-zero size. */
So at least with GNU LD this is guaranteed to be fine. This does not necessarily extend to other linkers, however.
Upvotes: 21
Reputation:
No, it won't affect the original running instance - that's already loaded into memory and won't be changed.
Upvotes: 4
Reputation: 31851
What can/will happen depends on the OS, but in general your old program will never start running with the new code. I say it is OS dependent since on Windows I believe the file will be locked and you simply can't overwrite it, whereas on Linux, you'll essentiall unlink the old file, but the program will still use that old version, and your new version will technically be a different file.
Now there is a caveat here. If you have dynamic libraries, or other dynamic code resources, then you might get the new version. In any case you load a library on demand the version you load will be the one that exists at the time of loading. In most cases all libraries are loaded directly at the start of the program. One common case where they are not however is a plugin architecture, where they are loaded as needed.
Upvotes: 6