-1

I am studying exec family of functions.Its man page says,it replaces the current process image with a new process image. If it replaces the binary,then after returning back,how does it get the previous parameters of the process which called exec?As replacing process image means replacing all its memory sections.Please correct me if I am wrong or having less knowledge.

Radha
  • 53
  • 1
  • 8

3 Answers3

1

The real job is done by the execve(2) system call. All other functions (like execvp ...) are calling execve.

The execve is quite a complex system call. When successful it does not return. But the process state (including address space) has been rewritten [almost] entirely.

So basically, the address space is becoming fresh. It contains segments from the binary executable.

The program arguments, environment, etc... have been copied (at the bottom of the stack segment) into the new address space. Hence they are limited (by ARG_MAX, typically 128Kbytes -but you could raise that by recompiling your kernel).

The address space change is actually mostly done lazily (using copy on write); in reality the paging is invalidated, and subsequent accesses get pages fault, which the kernel serves by providing the new page, etc etc...

On Linux, I suggest looking into /proc/ (see proc(5) for more). In particular, try cat /proc/self/maps which will show you the address space map of the process running that cat.

Of course execve is often used after fork(2), and probably also with dup2(2) and/or pipe(2), and some waiting syscall like waitpid(2) or wait4(2), perhaps handling SIGCHLD signal -see signal(7) & sigaction(2). Please read e.g. advanced linux programming (which you can read online).

You could also consider using popen(3) or system(3) (they are calling pipe for popen, then fork & execve of /bin/sh -c ....).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I have some part of code like I have a loop for(index = 0; index < SOME_MAX_VAL;index++) { exec(SOME_EXECUTABLE,index); } then where the value of argument index will be stored, i.e. after returning from exec executable,how it will get index? – Radha May 13 '13 at 06:10
  • ypu wont get it, since your process adress is overwritten – Basile Starynkevitch May 13 '13 at 06:30
  • @Radha: you need to read the *advanced linux programming* book, it has an entire chapter to explain you `fork`, `execve`, `pipe` ... and it will do that better than we have time to explain you. – Basile Starynkevitch May 13 '13 at 07:26
  • @Radha: and you can read http://advancedlinuxprogramming.com/ online. – Basile Starynkevitch May 13 '13 at 07:45
  • If I use, exec & vfork together , then parent will be blocked till child completes its execution , and exec returns only on failure,then when should I use _exit in child?How would I know it? – Radha May 13 '13 at 08:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29823/discussion-between-radha-and-basile-starynkevitch) – Radha May 13 '13 at 08:33
0

A new memory block is allocated. The parameters are copied to this block. Only then are the pages from the old executable's memory freed. Note that there may be other steps in between as well. For example, the new executable is mapped into memory before the old executable is freed as well.

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • U mean to say,a totally new memory is allocated for new executable ,after that previous memory is freed?So, at a point of time one process is consuming so much of memory?How this happens , means according to 4 sections of memory in current process? – Radha May 13 '13 at 06:07
  • I mean Overwriting the existing memory – akhil May 13 '13 at 11:57
  • A small amount of memory is allocated for data such as the command line arguments. The executable is mmapped, which means it may not be loaded immediately. Instead, each page may be loaded as it is accessed later. The memory from the old process is discarded before the new process begins execution. – Dark Falcon May 13 '13 at 14:30
0

In Linux fork + exec is used create child process. the fork() creates the new process and exec function loads/overwrite the image/executable, given as the argument of exec(), into the process space and start executing.

akhil
  • 732
  • 3
  • 13