嵌入式Linux C多进程编程(四)——进程创建

简介: 嵌入式Linux C多进程编程(四)——进程创建

一、进程的创建(实例:读写鼠标键盘)


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
    pid_t pid;
    pid = fork();
    if (pid < 0)
    {
        perror("fork error!");
        exit(1);
    }
    if (pid > 0)
    {
        int fd = open("/dev/input/mouse0", O_RDWR);
        int cor = 0;
        while (1)
        {
            read(fd, &cor, sizeof(cor));
            //sleep(1);
            printf("cor = %d\n", cor);
        }
    }
    if (pid == 0)
    {
        char buffer[1024];
        while (1)
        {
            memset(buffer, 0, sizeof(buffer));
            int n_r = read(0, buffer, sizeof(buffer) - 1);
            buffer[n_r] = '\0';
            printf("buffer = %s\n", buffer);
        }
    }
    return 0;
}


同一文件,进程的读写位置不同,读写位置不被共享

如果在创建进程前,打开文件,则读写位置被共享


二、exec函数族


在一个进程中调用里一个程序

调用之后,原文件下面的程序会被覆盖,不予执行

要以NULL结尾


0a2653c851af460fa595bd959398a8f1.png2d65d23f6d4748949b924e4057485923.png


2.1 execl


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


2.1.1 demo.c


demo


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
    for (size_t i = 0; i < argc; i++)
    {
        printf("argc[%ld] = %s\n", i, argv[i]);
    }
    pid_t pid = getpid();
    pid_t ppid = getppid();
    printf("pid = %d\n",pid);
    printf("ppid = %d\n",ppid);
    char buffer[1024];
    int n_r = read(0, buffer, sizeof(1024) - 1);
    buffer[n_r] = '\0';
    printf("buffer = %s\n", buffer);
    return 0;
}


2.1.2 execl.c


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
    printf("hello world\n");
    execl("./demo","./demo","xasxa","hahaha",NULL);
    printf("快乐暑假\n");
}

0a2653c851af460fa595bd959398a8f1.png


2.2 execv


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


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
    printf("hello world\n");
    //execl("./demo","./demo","xasxa","hahaha",NULL);
    char *arg[] = {"./demo", "hello1", "hello2", NULL};
    execv("./demo", arg);
    printf("快乐暑假\n");
}

0a2653c851af460fa595bd959398a8f1.png


2.3 execlp


int execIp (const char *file, const char *arg,...)


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
    printf("hello world\n");
    //execl("./demo","./demo","xasxa","hahaha",NULL);
    //char *arg[] = {"./demo", "hello1", "hello2", NULL};
    //execv("./demo", arg);
    execlp("/home/jsetc/2022.7c++/多进程/demo","./demo","world1","world2",NULL);
    printf("快乐暑假\n");
}

0a2653c851af460fa595bd959398a8f1.png


2.4 execvpe


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


env是环境变量


2.4.1 demo.c


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const *argv[], char **env)
{
    for (size_t i = 0; i < argc; i++)
    {
        printf("argc[%ld] = %s\n", i, argv[i]);
    }
    for (size_t i = 0; i < argc; i++)
    {
        printf("env[%ld] = %s\n", i, env[i]);
    }
    pid_t pid = getpid();
    pid_t ppid = getppid();
    printf("pid = %d\n",pid);
    printf("ppid = %d\n",ppid);
    char buffer[1024];
    int n_r = read(0, buffer, sizeof(1024) - 1);
    buffer[n_r] = '\0';
    printf("buffer = %s\n", buffer);
    return 0;
}


2.4.2 execvpe


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
    printf("hello world\n");
    //execl("./demo","./demo","xasxa","hahaha",NULL);
    char *arg[] = {"./demo", "hello1", "hello2", NULL};
    char *env[] = {"UXAS = admin","PASSWD = 123","trytrytry",NULL};
    //execv("./demo", arg);
    //execlp("/home/jsetc/2022.7c++/多进程/demo","./demo","world1","world2",NULL);
    execvpe("/home/jsetc/2022.7c++/多进程/demo", arg, env);
    printf("快乐暑假\n");
}


0a2653c851af460fa595bd959398a8f1.png


三、vfork、system


3.1 vfok


2d65d23f6d4748949b924e4057485923.png

先执行子进程


如果没有exec,则会共享父进程资源,子进程一定要加exit(1)进行异常退出,不会造成二次释放


vfork比fork更节省空间

只有遇到exec的时候,才会开辟新空间


3.2 system


0a2653c851af460fa595bd959398a8f1.png

相关文章
|
16天前
|
Linux
Linux编程: 在业务线程中注册和处理Linux信号
本文详细介绍了如何在Linux中通过在业务线程中注册和处理信号。我们讨论了信号的基本概念,并通过完整的代码示例展示了在业务线程中注册和处理信号的方法。通过正确地使用信号处理机制,可以提高程序的健壮性和响应能力。希望本文能帮助您更好地理解和应用Linux信号处理,提高开发效率和代码质量。
42 17
|
25天前
|
Linux
Linux编程: 在业务线程中注册和处理Linux信号
通过本文,您可以了解如何在业务线程中注册和处理Linux信号。正确处理信号可以提高程序的健壮性和稳定性。希望这些内容能帮助您更好地理解和应用Linux信号处理机制。
54 26
|
5月前
|
Shell Linux
Linux shell编程学习笔记30:打造彩色的选项菜单
Linux shell编程学习笔记30:打造彩色的选项菜单
|
1月前
|
Ubuntu Linux 开发者
Ubuntu20.04搭建嵌入式linux网络加载内核、设备树和根文件系统
使用上述U-Boot命令配置并启动嵌入式设备。如果配置正确,设备将通过TFTP加载内核和设备树,并通过NFS挂载根文件系统。
108 15
|
2月前
|
存储 监控 Linux
嵌入式Linux系统编程 — 5.3 times、clock函数获取进程时间
在嵌入式Linux系统编程中,`times`和 `clock`函数是获取进程时间的两个重要工具。`times`函数提供了更详细的进程和子进程时间信息,而 `clock`函数则提供了更简单的处理器时间获取方法。根据具体需求选择合适的函数,可以更有效地进行性能分析和资源管理。通过本文的介绍,希望能帮助您更好地理解和使用这两个函数,提高嵌入式系统编程的效率和效果。
125 13
|
3月前
|
运维 监控 Shell
深入理解Linux系统下的Shell脚本编程
【10月更文挑战第24天】本文将深入浅出地介绍Linux系统中Shell脚本的基础知识和实用技巧,帮助读者从零开始学习编写Shell脚本。通过本文的学习,你将能够掌握Shell脚本的基本语法、变量使用、流程控制以及函数定义等核心概念,并学会如何将这些知识应用于实际问题解决中。文章还将展示几个实用的Shell脚本例子,以加深对知识点的理解和应用。无论你是运维人员还是软件开发者,这篇文章都将为你提供强大的Linux自动化工具。
|
5月前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
5月前
|
Linux Shell
Linux系统编程:掌握popen函数的使用
记得在使用完 `popen`打开的流后,总是使用 `pclose`来正确关闭它,并回收资源。这种做法符合良好的编程习惯,有助于保持程序的健壮性和稳定性。
250 6
|
5月前
|
Linux Shell
Linux系统编程:掌握popen函数的使用
记得在使用完 `popen`打开的流后,总是使用 `pclose`来正确关闭它,并回收资源。这种做法符合良好的编程习惯,有助于保持程序的健壮性和稳定性。
232 3
|
5月前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
191 0

热门文章

最新文章