Linux多进程05-exec函数族

execl : 执行文件函数

    #include <unistd.h>
    int execl(const char *pathname, const char *arg, ... );
    	执行参数path字符串所代表的文件路径
        参数:
            - path: 需要指定的执行的文件的路径或者名称(推荐使用绝对路径)
            - arg: 是执行可执行文件所需要的参数列表
                第一个参数通常写执行程序的名称
                第二个参数开始就是程序执行所需要的参数列表
                参数最后需要以NULL结束(哨兵)
        返回:
            -1 只有程序出错才有返回值 ,并且设置errno

execl.c

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

int main(int argc, char const *argv[])
{
    pid_t pid = fork();
    if (pid > 0)
    {
        printf("i am parent process, pid: %d\n", getpid());
        sleep(1);
    }
    else if (pid == 0)
    {
        //子进程
        // 执行程序
        // execl("test", "test", NULL);
        // 执行shell
        execl("/usr/bin/ps","ps","aux",NULL);
        printf("i am child process, pid: %d\n", getpid());
    }
    for (int i = 0; i < 3; i++)
    {
        printf("i = %d, pid = %d\n", i, getpid());
    }

    return 0;
}

i am parent process, pid: 5731
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.5 167416 11204 ?        Ss   14:44   0:07 /sbin/init splash
root           2  0.0  0.0      0     0 ?        S    14:44   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   14:44   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   14:44   0:00 [rcu_par_gp]
root           5  0.0  0.0      0     0 ?        I<   14:44   0:00 [netns]
root           7  0.0  0.0      0     0 ?        I<   14:44   0:00 [kworker/0:0H-events_highpri]
root           9  0.0  0.0      0     0 ?        I<   14:44   0:01 [kworker/0:1H-events_highpri]
root          10  0.0  0.0      0     0 ?        I<   14:44   0:00 [mm_percpu_wq]
...
...
...
root        5615  0.0  0.0      0     0 ?        I    20:50   0:00 [kworker/u256:0-events_power_ef
root        5624  0.0  0.0      0     0 ?        R    20:55   0:00 [kworker/u256:2-events_unbound]
i = 0, pid = 5731
i = 1, pid = 5731
i = 2, pid = 5731

可以看出, 子进程在执行完"ps aux"之后, 进程就退出了

execlp: 从PATH 环境变量中查找文件并执行

#include <unistd.h>
int execlp(const char *file, const char *arg, ...);
    会到环境变量中查找指定的可执行文件,如果找到了就执行,找不到就执行失败
    参数:
        - file: 需要指定的执行的文件的名称
            a.out 
            ps
        - arg: 是执行可执行文件所需要的参数列表
            第一个参数通常写执行程序的名称
            第二个参数开始就是程序执行所需要的参数列表
            参数最后需要以NULL结束(哨兵)
    返回:
        -1 只有程序出错才有返回值 ,并且设置errno

execlp.c

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

int main(int argc, char const *argv[])
{
    pid_t pid = fork();
    if (pid > 0)
    {
        printf("i am parent process, pid: %d\n", getpid());
        sleep(1);
    }
    else if (pid == 0)
    {
        //子进程
        // 执行程序
        // execl("test", "test", NULL);
        // 执行shell
        execlp("ps","ps","aux",NULL);
        printf("i am child process, pid: %d\n", getpid());
    }
    for (int i = 0; i < 3; i++)
    {
        printf("i = %d, pid = %d\n", i, getpid());
    }

    return 0;
}
posted @ 2023-05-17 18:35  言叶以上  阅读(9)  评论(0编辑  收藏  举报