Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would it be possible to run the same C++ code simultaneously? [duplicate]

Say I compile some code and make it run. It will take 10 minutes to finish.

In the meantime, if I change some parameters in the code and compile it again using a separate terminal window and run it too (so there's now two programs running simultaneously using the same code), does the second run affect the first running program as the first compiled output is replaced by the second compiled output?

like image 441
Rohit Avatar asked Dec 09 '21 16:12

Rohit


People also ask

Can you mix Cpp and C?

If the C++ compiler provides its own versions of the C headers, the versions of those headers used by the C compiler must be compatible. Oracle Developer Studio C and C++ compilers use compatible headers, and use the same C runtime library. They are fully compatible.

How do you do two things at once in C++?

One way to do multiple things at the same time in a C++ program is to use threads. That a look at std::thread for more information. The point being that the threads run independently of one another and can perform completely separate tasks at the same time.

Can you write c code in C++?

If you are compiling the C code together, as part of your project, with your C++ code, you should just need to include the header files as per usual, and use the C++ compiler mode to compile the code - however, some C code won't compile "cleanly" with a C++ compiler (e.g. use of malloc will need casting).

How can C code be run on different processors?

How can C code be run on different processors? Bookmark this question. Show activity on this post. As I understand it, C code is compiled to machine (assembly) code on someone’s machine, and it becomes an exe file that can be run on lots of different processors (machines).

How do you compile a program to machine code?

When you compile a program to machine code, you have to select a machine code instruction set, perhaps a mode in which to run the machine (if applicable), and a format in which the resulting code is stored. All those choices limit the target platform on which the code can be executed.

Can multiple threads run at the same time?

Except that (as you probably know) "exact same time" isn't technically possible. Whether you're running on a single or multi-core process, you're at the mercy of the operating system's scheduler to run your threads. There is no guarantee that they will run "at the same time", instead they may time share a single core.


Video Answer


2 Answers

There are three possible scenarios:

  1. On Windows, an executable file will get locked for writing and removal while it is being executed so the build will just fail.

  2. On Linux, an executable file is not necessary protected from modification or removal while it is being executed.

2.1. If a file is deleted from the file system and then a new file is created with the same name then the old file content will still remain until already running executable exits while the new executable will use the new file. So the old executable will continue to run normally while the new one will be ready to work as well.

2.2. If a file is opened for writing and overwritten with new content then the already-running executable will be using the new machine code which is most likely be incompatible with the existing program state and will lead to crashes.

like image 103
user7860670 Avatar answered Oct 05 '22 23:10

user7860670


The behavior isn't defined by the C++ standard. In general, one of two things will happen (depends primarily on operating system)

  1. The second compilation will fail because the linker won't be able to open the executable file to write to it.

  2. Compilation will succeed, and existing invocations of the original executable will be unaffected; later invocations will use the new executable.

The third possibility -- the compilation succeeds, and messes up existing invocations -- is unheard-of in modern operating systems.

like image 22
Sneftel Avatar answered Oct 06 '22 00:10

Sneftel