In Linux, the program _ 9_process control exec function, _ 9 exec

Source: Internet
Author: User

In Linux, the program _ 9_process control exec function, _ 9 exec

Exec Function

When a process calls the exec function, the execution program of the process is completely replaced with the new program. The new program starts to execute its main function;

After a child process is created using the fork function, the child process usually uses the exec function to execute another program.

Note: calling the exec function does not create a new process, so the process ID does not change before and after the creation, exec only replaces the code segment, data segment, heap, and stack of the currently running program with a brand 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 can we remember so many functions?

P indicates that the first parameter is filename and the executable file is searched based on the PATH environment variable;

A parameter table with the letter l indicates that the function obtains a parameter table (mutually exclusive with the letter v). You want to receive a list of parameters separated by commas (,). The list uses the NULL pointer as the end mark)

V indicates that the function obtains an argv []; (you want to receive a pointer to a string array ending with NULL)

E indicates that the function obtains an envp [] array. (The function passes the specified parameter envp, which allows you to change the environment of a sub-process. If no suffix is specified, the sub-process uses the environment of the current program)


The following 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))        {            perror("execle error!");            exit(-1);        }    }    waitpid(pid, &status, 0);    // execve    if(0 == (pid=fork()))    {        if(-1 == execve("/bin/ps", arg, NULL))        {            perror("execve error!");            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;}

Running 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 80 0-1745-pts/1 00:00:01 bash
0 S 506 20964 11120 0 80 0-459-pts/1 00:00:00 main
0 R 506 20967 20964 0 80 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 80 0-1745-pts/1 00:00:01 bash
0 S 506 20964 11120 0 80 0-459-pts/1 00:00:00 main
0 R 506 20969 20964 3 80 0-1618-pts/1 00:00:00 ps


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.