Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling a program while it is running

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.

like image 214
Cemre Mengü Avatar asked Aug 20 '12 15:08

Cemre Mengü


People also ask

Is compiling the same as running a program?

Runtime and compile time are programming terms that refer to different stages of software program development. Compile-time is the instance where the code you entered is converted to executable while Run-time is the instance where the executable is running.

What is compiling and running the program?

A compiler is an executable program that takes program source code (text) as input and translates it into an executable program (binary machine code) that it writes into a file as output. That executable program can then be run to process input data and generate output according to whatever we wrote our program to do.

Can you run a program without compiling?

but, if you are asking what you are asking then NO you can't run a program without compiler. They are just set of instructions that doesn't make sense to a machine but make sense to a compiler which will write machine code which in turn will make sense to the machine.

Why do we compile a program before execution?

Because computer can't understand the source code directly. It will understand only object level code. Source codes are human readable format but the system cannot understand it.


1 Answers

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.

like image 118
jleahy Avatar answered Oct 07 '22 17:10

jleahy