An UNIX System Call.
This is useful when you want to run a program that is different from the calling program.
fork()
is only useful if you want to keep running copies of the different program, while exec lets you run a different program.
Example:
Given the name of an executable (wc()
in this case) and some arguments (e.g., p3.c
), it loads code and static data from that executable and overwrites its current code segment (and current static data) with it.
The heap and stack and other parts of the memory space of the program are reinitialized. Then the OS simply runs that program, passing in any arguments as argv
of that process. Thus, no new processes are created, but it transforms the currently running program (formerly p3
) into a different running program (wc
). After the exec()
in the child, it is almost as if p3.c
never ran; a successful call to exec()
never returns.