
The exec() family of library functions are present on top of the execve() system call. They are responsible for replacing current process image with a new process image and also run the new process with the same existing PID.
The execve() system call is like changing the clothes of a person. It loads a new program into a process’s memory, replacing the old one. The new program can have its own arguments and environment settings. It’s like starting fresh with a new program, and the old program’s code is thrown away. The memory is set up anew for the new program. This is often called “execing a new program.”.
The execve(pathname, argv, envp) system call loads a new program(pathname) with argument list and environment list envp into a processβs memory. The existing program text is discared and the stack and heap segement are freshly created for the new program.
When the execve(pathname, argv, envp) system call is invoked, a brand-new program (specified by pathname) is loaded into the memory space of a process, bringing along its set of arguments and environment list (envp). The existing program’s code is discarded, and a stack and heap segment are created exclusively for the newly introduced program. This operation is referred to as execing a new program.
What is the difference between them and when to use them?
int πππππ( const char *pathname, const char *arg,β¦);
Imagine you’re a conductor of a quirky orchestra, and execl is your musical score. You have to arrange all the instruments (arguments) in perfect harmony. It’s like conducting a symphony, where each note needs to be precisely placed. If you mix up the instruments or miss a beat, you might end up with a hilarious disharmony that will leave everyone scratching their heads.
int ππππππ( const char *filename, const char *arg,β¦);
Picture yourself as a master magician performing a spellbinding trick. With execlp, you just need to say the magic word (program name), and it will conjure up the program from thin air, searching in all the enchanting places(in same dir). It’s like performing a mesmerizing magic trick without worrying about the technicalities.
int πππππ(const char *pathname, char *const argv[]);
You’re the map holder, and execv is your trusted compass. You need to provide the precise coordinates (pathname) and a map of clues (arguments) to find the hidden treasure (new program). It’s like navigating through uncharted territory, carefully following the clues and avoiding traps. Keep your map updated with a treasure mark (NULL) at the end, and let the treasure hunt begin.
int ππππππ(const char *filename, char *const argv[]);
You’re on the hunt for a mysterious suspect (program), and execvp is your loyal partner in crime-solving. Together, you search the bustling city streets (directories) to catch the elusive suspect. It’s like an exciting detective story, where you gather your evidence (arguments) and let your partner unravel the mystery.
int ππππππ( const char *pathname, const char *arg,β¦,/*(char*)NULL,char *const envp[] */);
Imagine you’re a program magician, ready to cast a spell to summon a new program into existence. With execle, you not only wave your wand (pathname) to bring the program to life, but you also sprinkle it with a special blend of arguments (arg) and enchanting environmental variables (envp). It’s like creating a magical recipe that combines all the right ingredients to conjure up a new program.
int πππππππ( const char *file, char *const argv[], const *const envp[]);
With execvpe, you gather all your fresh ingredients (argv) and bring them to your culinary workstation (file). But what sets you apart is your secret spice rack (envp) filled with unique flavors. You sprinkle just the right amount of seasoning to enhance the taste and aroma of your program creation. It’s like creating a flavorful recipe that satisfies the programming palate.
Note: It is the responsibility of the programmer to use the right function based on the requirements.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
An Article by: Yashwanth Naidu Tikkisetty
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
