-1

In terms of memory allocation efficiency is it right to say that after a fork() in the code of the child if I execute a program with execve() this would be more efficient than the same program executed without execve() because the child won't have allocated the stack and heap of the father but only is own?

Naife example:

without execve

 [..some father code...]
int i;
if(!fork()) {
 sum() //from an #include "porg.h"
}

with execve

 [..some father code...]

if(!fork()) {
 execve("sum", NULL, NULL); //sum is a program which executes i=2+3
}

The second in terms of memory allocation is better? Is it better to replace the entire virtual address space of my process or it is better to get running the mentioned code with a call to a function in another program which is included with #include "prog" in terms of number of number of operations done by the so and in terms of the memory carried behind during the execution of the program?

trincot
  • 317,000
  • 35
  • 244
  • 286
Zeno Raiser
  • 207
  • 2
  • 10
  • Please define what you mean by `memory`. I'm understanding it as [virtual memory](https://en.wikipedia.org/wiki/Virtual_memory). So **edit your question** to improve it. It currently is unclear. If possible, provide some [MCVE] – Basile Starynkevitch Jan 31 '18 at 17:04
  • 1
    This makes no sense - how can it possibly be cheaper (in terms of either CPU or memory) to execute a whole different program than a single instruction?! After all, *both* code snippets invoke `fork()`, and the second one does much much more. – user4815162342 Jan 31 '18 at 17:04
  • 1
    Please define what you mean by *efficient* in **memory allocation efficiency**. – wallyk Jan 31 '18 at 17:06
  • @wallyk edited hope its clear – Zeno Raiser Feb 01 '18 at 11:05

1 Answers1

1

Read carefully some book on Linux or POSIX programming, perhaps the old ALP. Read also intro(2), fork(2), execve(2).

After a successful execve, the entire virtual address space of your process has been replaced and reinitialized , according to the ELF executable that was executed (see elf(5); so execve does not return except on failure).

So your sum program don't see anything from the previous program executing it (except the mandatory argv & environ, which get copied by execve). BTW, your usage of execve is wrong. You should provide some argv array, and some environ array (both should be non-null, the argv should be non-empty, and both should end with a NULL string)... You often would prefer some exec*(3) function.

And after a successful fork both (child and father) processes have their own virtual address space (initially, nearly identical copies). BTW, your code forgot to check failure of fork.

The second in terms of memory allocation is better?

So that question has no sense. The entire "memory" -actually, virtual address space- has been reset and reinitialized (and that includes the call stack and the heap).

You can explore the virtual address space of your process by using /proc/. See proc(5). Try cat /proc/$$/maps and cat /proc/self/maps to get a better intuition about virtual address space.

Play also with strace(1) to understand the system calls done by some program or process. Try for example strace date (and see also this).

than the same program executed without execve()

That is impossible, since execve is the only way to execute a program (except of course init, today called systemd - see init(1), which gets started magically by the kernel at boot time; and some few other weird kernel initiated processes....)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • than the same program executed without execve() : for program i meant code sorry, for memory allocation efficiency i meant : Is it better to replace the entire virtual address space of my process or it is better to get running the mentioned code with a call to a function in another program which is included with #include "prog", in terms of number of operations by the so and in terms of the memory carried behind during the execution of the program? – Zeno Raiser Feb 01 '18 at 11:03
  • Please edit your question (e.g. improve it a lot and provide some [MCVE]), don't comment my answer. I still don't understand what you mean (and I'm guessing your don't understand what `fork` & `execve` do, and what is a virtual address space, or a process). I don't understand what should be *better* than what? – Basile Starynkevitch Feb 01 '18 at 12:17