[Linux system programming] 22.exec function, execlp, execl, execvp

Table of contents

exec function

execlp

parameter file

return value

test code 1

Test Results

execl

Test code 2

Test Results

execvp

Test code 3

Test Results

exec function

        After fork creates a child process, it executes the same program as the parent process, but it may execute different code branches. The child process often needs to call an exec function to execute another program. When a process calls an exec function, the process's user-space code and data are completely replaced by the new program, and execution starts from the new program's startup routine. Calling exec does not create a new process, so the process id does not change before and after calling exec. Replace the .text and .data of the current process with the .text and .data of the program to be loaded, and then let the process start to execute from the first instruction of the new .text, but the process ID remains unchanged, and the core does not change the shell.

        There are six functions starting with exec, collectively referred to as exec functions.

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char *const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execve(const char *path, char *const argv[], char *const envp[]);

execlp

Load new programs with the help of the PATH environment variable.

int execlp(const char *file, const char *arg, ...);

parameter file

        The name of the program to load. This function needs to be used with the PATH environment variable. When there is no parameter file after searching all directories in PATH, an error will be returned. This function is usually used to call system programs. Such as: ls, date, cp, cat and other commands.

return value

success: no return

Failure: return -1

test code 1

Use subprocess to execute the ls -al command.

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    printf("程序开始运行!\n");
    printf("当前进程的ID是%d。\n", getpid());
    pid_t jin_cheng = fork();
    if (jin_cheng < 0)
    {
        perror("创建进程错误!");
        exit(1);
    }
    else if (jin_cheng == 0)
    {
        execlp("ls","ls","-a","-l",NULL);//支持多个参数传入的函数,使用NULL作参数传入结束符,相当于哨兵
        perror("进程错误!");
        exit(1);
    }
    else if (jin_cheng > 0)
    {
        sleep(1);
        printf("这是父进程,当前进程的ID是%d。\n", getpid());
        printf("父程序结束!\n");
    }
    return 0;
}

Test Results

execl

Load a process by path + program name .

int execl(const char *path, const char *arg, ...);

Test code 2

/*
CeShi2_1.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    printf("程序开始运行!\n");
    printf("当前进程的ID是%d。\n", getpid());
    pid_t jin_cheng = fork();
    if (jin_cheng < 0)
    {
        perror("创建进程错误!");
        exit(1);
    }
    else if (jin_cheng == 0)
    {
        execlp("./CeShi2_2", "./CeShi2_2", NULL); //支持多个参数传入的函数,使用NULL作参数传入结束符
        perror("进程错误!");
        exit(1);
    }
    else if (jin_cheng > 0)
    {
        sleep(1);
        printf("这是父进程,当前进程的ID是%d。\n", getpid());
        printf("父程序结束!\n");
    }
    return 0;
}
/*
CeShi2_2.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    printf("这是子进程,当前进程的ID是%d,你好,世界!\n", getpid());
    return 0;
}

Test Results

execvp

Load a process, using a custom environment variable env. Encapsulates execution commands. It also needs to be used with environment variables.

int execvp(const char *file, char *const argv[]);

Test code 3

Use the execvp function to execute the ls -al command using a child process.

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    printf("程序开始运行!\n");
    printf("当前进程的ID是%d。\n", getpid());
    pid_t jin_cheng = fork();
    if (jin_cheng < 0)
    {
        perror("创建进程错误!");
        exit(1);
    }
    else if (jin_cheng == 0)
    {
        char *argv[] = {"ls", "-a", "-l", NULL};
        execvp("ls", argv);
        perror("进程错误!");
        exit(1);
    }
    else if (jin_cheng > 0)
    {
        sleep(1);
        printf("这是父进程,当前进程的ID是%d。\n", getpid());
        printf("父程序结束!\n");
    }
    return 0;
}

Test Results

Guess you like

Origin blog.csdn.net/CETET/article/details/132267031