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

后端 未结 8 2232
再見小時候
再見小時候 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 07:11

    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.

提交回复
热议问题