The EXEC function of Linux system programming _9_ Process Control

Source: Internet
Author: User

exec function

When the process calls the EXEC function, the executing program for the process is completely replaced with the new program. The new program starts with its main function;

Once a child process is created using the fork function, the child process will often use the EXEC function to execute another program.

Note: Calling the EXEC function does not create a new process, so the process ID before and after creation does not change, and exec simply replaces the code snippet, data segment, heap, and stack of the currently running program with a completely new program.

#include <unistd.h>

extern char **environ;
int execl (const char *path, const char *arg, ...);
int execv (const char *path, char *const argv[]);

int execle (const char *path, const char *arg, ..., char * const envp[]);

int Execve (const char *path, char *const argv[], char *const envp[]);

int EXECLP (const char *filename, const char *arg, ...);

int EXECVP (const char *filename, char *const argv[]);

How do so many functions remember?

With the letter p, the first parameter is filename, and the executable file is searched according to the PATH environment variable;

With the letter L, the function takes a parameter table, (with the letter v mutually exclusive; want to receive a comma-delimited list of parameters, the list with a null pointer as the end flag)

With the letter V, the function takes a argv[]; ( you want to receive a pointer to a null-terminated array of strings)

the function takes a envp[] array with the letter E, ( the function passes the specified parameter envp, allowing the environment to change the child process, no suffix e, the child process uses the current program's environment )


Here is an example:

#include <stdio.h> #include <stdlib.h> #include <unistd.h>int main () {pid_t pid;    int status;    Char *arg[] = {"PS" "-L", NULL};    Execl pid = fork ();        if ( -1 = = pid) {printf ("Fork error!\n");    return-1; } else if (0 = = pid) {if ( -1 = = Execl ("/bin/ls", "ls", "-a", NULL)) {perror ("Execl error!"            );        Exit (-1);    }} waitpid (PID, &status, 0);             Execv if (0 = = (pid=fork ())) {if ( -1 = = Execv ("/bin/ps", Arg)) {perror ("Execv error!");        Exit (-1);    }} waitpid (PID, &status, 0); Execle if (0 = = (pid=fork ())) {if ( -1 = = Execle ("/bin/ps", "PS", "-L", NULL, NULL)) {per            Ror ("Execle error!");        Exit (-1);    }} waitpid (PID, &status, 0); Execve if (0 = = (pid=fork ())) {if ( -1 = = Execve ("/bin/ps", ARG, NULL)) {perror ("Execve E            Rror! ");Exit (-1);    }} waitpid (PID, &status, 0);  EXECLP if (0 = = (pid=fork ())) {if ( -1 = = EXECLP ("PS", "PS", "-l", NULL)) {perror ("execle            Error! ");        Exit (-1);    }} waitpid (PID, &status, 0); return 0;}

Operation Result:

.  .. echo.sh Main
PID TTY Time CMD
11120 PTS/1 00:00:01 Bash
20964 PTS/1 00:00:00 Main
20966 PTS/1 00:00:00 PS
F S UID PID PPID C PRI NI ADDR SZ Wchan TTY time CMD
0 S 506 11120 11119 0 0-1745-pts/1 00:00:01 Bash
0 S 506 20964 11120 0 0-459-pts/1 00:00:00 Main
0 R 506 20967 20964 0 0-694-PTS/1 00:00:00 PS
PID TTY Time CMD
11120 PTS/1 00:00:01 Bash
20964 PTS/1 00:00:00 Main
20968 PTS/1 00:00:00 PS
F S UID PID PPID C PRI NI ADDR SZ Wchan TTY time CMD
0 S 506 11120 11119 0 0-1745-pts/1 00:00:01 Bash
0 S 506 20964 11120 0 0-459-pts/1 00:00:00 Main
0 R 506 20969 20964 3 0-1618-PTS/1 00:00:00 PS


The EXEC function of Linux system programming _9_ Process Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.