V
V
Vadim Ushakov2021-09-19 13:06:39
linux
Vadim Ushakov, 2021-09-19 13:06:39

Is it possible to create a completely independent process from a program?

____ It makes sense to think towards creating independent processes from a C++ program. Suppose, on Linux, the fork / exec strategy produces a copy of the current process, and upon completion of the main one, the copy continues to work, but the condition of an absolutely independent process is not reached, i.e. when fork-e, the main program is copied and the handler of the executable file is not released, what needs to be achieved. Does it make sense to look in this direction? Those. is there any solution to this problem, for example, by launching a process independent from the main program ... inside which to change the executable file of the main program via ofstream or echo "test" >> prog .

____ Interested in the same solution, but also on Windows .

Conclusionecho "data" >>

1625992443.sh: 1: 1625992443.sh: cannot create bin/Release/licenses: Text file busy

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
ValdikSS, 2021-09-19
@ValdikSS

Linux does not allow you to change the file of a running program because it is not always loaded into RAM completely, but can be read from disk at different points in the program's execution.
Direct answer to your question:

fork-e copies the main program and does not release the handler of the executable file, which must be achieved

There will be a copy of all mapped segments to RAM, proper memory setup, and munmap() of the original mappings.
The simplest "execve not through a file" option is to use the memfd_create call to create a "file" entirely in RAM (Linux 3.17+). This approach is described in detail here.
Alternative generic options are writing your own ELF/PE loader instead of using execve.
Or just compress the file with upx, its loader will load the file into RAM in its entirety, without file mmaps.
It is not clear what problem you want to solve initially. You can simply delete the file with rm, then create a new one in its place, with the same name, and write new content to it.
If the task is to edit the code or program data on the fly, then this can be done directly in memory by writing to /proc/PID/mem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question