首页 > 系统相关 >Linux多进程05-exec函数族

Linux多进程05-exec函数族

时间:2023-05-17 18:46:46浏览次数:46  
标签:00 14 exec int 0.0 05 pid Linux root

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;
}

标签:00,14,exec,int,0.0,05,pid,Linux,root
From: https://www.cnblogs.com/anqwjoe/p/17409723.html

相关文章

  • Linux多进程07-wait和waitpid
    进程回收在每个进程退出的时候,内核释放该进程所有的资源、包括打开的文件、占用的内存等。但是仍然为其保留一定的信息,这些信息主要主要指进程控制块PCB的信息(包括进程号、退出状态、运行时间等)。父进程可以通过调用wait或waitpid得到它的退出状态同时彻底清除掉这个进程。......
  • Linux多进程09-匿名管道实现ps aux
    /*实现psaux|grepxxx父子进程间通信子进程:psaux,子进程结束后将数据发送给父进程父进程:获取到数据,guolvpipe()execlp()子进程将标准输出stdout_fileno重定向到管道的写端dup2()*/#include<stdio.h>#include<stdlib.......
  • Linux多进程08-进程间通信与管道
    进程间通信进程是一个独立的资源分配单元,不同进程(这里所说的进程通常指的是用户进程)之间的资源是独立的,没有关联,不能在一个进程中直接访问另一个进程的资源。不同的进程需要进行信息的交互和状态传递(如:数据传输/通知事件/资源共享/进程控制),因此需要进程间通信(IPC:Int......
  • Linux多进程11-内存映射
    内存映射(Memory-mappedI/O)是将磁盘文件的数据映射到内存,用户通过修改内存就能修改磁盘文件。mmap#include<sys/mman.h>void*mmap(void*addr,size_tlength,intprot,intflags,intfd,off_toffset);功能:将一个文件或者设备的数据映射到内存中参数:......
  • Linux多进程10-有名管道实现简单版聊天功能
    chatA.c//有名管道实现简单版聊天功能#include<unistd.h>#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<stdlib.h>#include<fcntl.h>#include<string.h>intmain(intargc,charconst*argv[]){......
  • Linux多进程13-kill,raise,abort函数
    #include<sys/types.h>#include<signal.h>intkill(pid_tpid,intsig);-功能:给某个进程pid,发送某个信号sig-参数:-pid:>0:将信号发送给指定的进程=0:将信号发送给当前的进程组=-1:将信号发送给每一个......
  • Linux多进程12-信号
    信号概念信号是Linux进程间通信的最古老的方式之一,是事件发生时对进程的通知机制,有时也称之为软件中断,它是在软件层次上对中断机制的一种模拟,是一种异步通信的方式。信号可以导致一个正在运行的进程被另一个正在运行的异步进程中断,转而处理某一个突发事件。发往进程的诸多信......
  • STM32F051 MK电调 BLDC 直流无刷电机控制 基于STM32F051 cortex-M0
    STM32F051MK电调BLDC直流无刷电机控制基于STM32F051cortex-M0的电调开发板,包含原理图PCB工程文件,程序源码,BLDC控制入门资料,供初学者入门学习了解。ID:48299619798638569......
  • Linux多进程01-进程概述
    程序与进程程序是包含一系列信息的文件,这些信息描述了如何在运行时创建一个进程进程是正在运行的程序的实例。是一个具有一定独立功能的程序关于某个数据集合的一次运行活动。它是操作系统动态执行的基本单元,在传统的操作系统中,进程既是基本的分配单元,也是基本的执行单元。......
  • linux-ubuntu安装 lrzsz
    ubuntu安装lrzsz方法1:二进制包自动安装sudoapt-getinstalllrzsz方法2:源码包手动安装2.1下载cd/server/toolswgethttps://www.ohse.de/uwe/releases/lrzsz-0.12.20.tar.gztar-xzflrzsz-0.12.20.tar.gzcdlrzsz-0.12.20./configure--prefix=/usr/local/lrzszs......