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

后端 未结 8 2171
再見小時候
再見小時候 2020-12-24 06:36

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

相关标签:
8条回答
  • 2020-12-24 06:52

    In Windows you can't if the executable is still running, the file will be locked. If the exe isn't actually running, the new runs should pick up the new one, depending on how your script is coded among other things.

    I don't know about Linux.

    0 讨论(0)
  • 2020-12-24 06:57

    It depends. From what I've experienced, on Linux you can still be running a program if you delete it (and it's not too large). But I don't think that's defined behavior.

    As far as the loop goes, depending on how you're invoking the executable, you will likely end out crashing your script when it goes to execute a program that's only halfway been written.

    0 讨论(0)
  • 2020-12-24 07:00

    Under Linux, executables are demand paged into memory as needed. The executable on disk becomes the backing store for the application. This means you cannot modify the executable on disk or you will affect a running application. If you try to open(2) an in-use executable for writing, you will get an ETXTBSY (Text file busy) error (check the man page for open(2)).

    As many others have said, you can remove the file from the filesystem (unlink(2)) and the kernel will maintain a reference to it and not delete it from disk until there are no more references (when the process exits, it will release its reference to the file). This means you can effectively "overwrite" an in-use executable by first removing it and then creating a new file with the same name as the old file.

    So, it comes down to how the compiler creates the executable when "overwriting" an existing file. If it just opens the file for writing and truncates it (O_WRONLY|O_CREAT|O_TRUNC), the it will fail with an ETXTBSY error. If it first removes the existing output file and creates a new one, it will work without error.

    0 讨论(0)
  • 2020-12-24 07:02

    I would imagine it wouldn't let you replace the file, since windows locked it down while it was in use.

    0 讨论(0)
  • 2020-12-24 07:04

    The Executable might be loaded completely into the memory on startup, however if it's large enough, and running long enough, the OS might decide to swap out some unused parts of it.

    Since the OS assumes that the program's file is still there, there is no reason to actually write these memory blocks into the swap file. So they are simply invalidated and reused. If the program needs these pages again, the OS loads them from the executable file.

    In Windows this actually happens automagically, since a loaded module is a memory mapped file. That also means that the file is locked during it's execution, and you will not be able to overwrite it easily.

    Not sure about Linux, but IIRC it does the swapping the same way.

    0 讨论(0)
  • 2020-12-24 07:07

    In Windows you can't delete a locked file but what most people don't know is that you can move or rename a running exe.

    So you could

    • move the old exe to a temp directory on the same drive
    • schedule it for deletion on the next reboot: MoveFileEx(name, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
    • move a new exe in its place.

    The old program will keep running but new processes will use the new file.

    0 讨论(0)
提交回复
热议问题