zoukankan      html  css  js  c++  java
  • 4412 进程

    进程基本概念

    进程是linux系统编程中非常重要的概念
    嵌入式linux 和嵌入式单片机最重要区别
    嵌入式非常容易实现多任务
    几个基本概念
    程序
    应用
    进程
    top命令
    x86-Ubuntu以及开发板上执行top命令

    进程ID

    每一个进程都有一个唯一的标识符,进程ID 简称pid
    进程id 一般默认的最大值为32768,不过也是可以修改的,当然一般情
    况下不需要这么做。如果当前进程是1000,那么下一个分配的进程就是
    1001,它是严格线性分配的
    除了init 进程,其它进程都是由其它进程创立的。创立新进程的进程叫父
    进程,新进程叫子进程
    man 2 getpid
    man 2 getppid

    获取子进程的函数
    pid_t getpid(void)
    参数:无
    返回值:成功返回进程号


    获取父进程的函数
    pid_t getppid(void);
    参数:无
    返回值:成功返回父进程
    编写编译运行测试

    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    void main()
    {
            pid_t idp, id;
    
            id = getpid();
            printf("pid = %d
    ", id);
    
            idp = getppid();
            printf("ppid = %d
    ", idp);
    }

    exec函数族

    linux中,可以使用exec函数族将程序载入内存,实现多个程序的运行

    man 3 exec
    exec函数族参数
    lv表示参数是以列表还是以数组的方式提供的
    p表示这个函数的第一个参数是*path,就是以绝对路径来提供程序的路径,也可以以当前目录作为目标
    e表示为程序提供新的环境变量
    编写编译运行测试

    #include <stdio.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
            int ret;
            char *path = "./helloexec";
    
            ret = execl(path, "helloexec", NULL);
            if(ret < 0) {
                    printf("execl failed.
    ");
                    exit(1);
            }
            printf("execl success.
    ");
            return 0;
    }

    helloexec

    #include <stdio.h>
    
    int main()
    {
            printf("this is helloexec
    ");
            return 0;
    }

    fork创建新进程

    linux中,可以使用fork函数创建和当前进程一模一样的进程,叫子进
    程,原来的进程叫父进程
    man 2 fork
    创建进程函数
    pid_t fork(void);
    参数:无
    返回值:执行成功,子进程pid 返回给父进程,0 返回给子进程;出现错误-1,返回给父进程。执行失败的唯一情况是内存不够或者id 号用尽,不过这种情况几乎很少发生。

    fork函数(看了例子之后再回头理解一次下面几句话)
    系统函数fork 调用成功,会创建一个新的进程,它几乎会调用差不多完
    全一样的fork 进程
    子进程的pid 和父进程不一样,是新分配的
    子进程的ppid 会设置为父进程的pid,也就是说子进程和父进程各自的父进程不一样
    子进程中的资源统计信息会清零
    挂起的信号会被清除,也不会被继承
    所有文件锁也不会被子进程继承
    编写编译运行测试
    简要分析变量

    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
            pid_t pid;
            int i = 100;
    
            pid = fork();
            if(pid == -1) {
                    printf("fork failed
    ");
                    return 1;
            } else if(pid == 0) {
                    i++;
                    printf("
    The child i = %d
    ", i);
                    printf("The child return value is %d
    ", pid);
                    printf("The child pid is %d
    ", getpid());
                    printf("The child ppid is %d
    ", getppid());
                    while(1);
            } else {
                    i++;
                    printf("
    The father i = %d
    ", i);
                    printf("The father return value is %d
    ", pid);
                    printf("The father pid is %d
    ", getpid());
                    printf("The father ppid is %d
    ", getppid());
                    while(1);
            }
            return 0;
    }

    综合例程

    linux命令的本质
    linux命令的本质是一些小程序
    综合例程
    包含fork以及所有的exec函数族
    编写编译运行测试

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    int main(void)
    {
            char *arg[] = {"ls", "-a", NULL};
    
            if(fork() == 0) {
                    printf("fork1 is Ok; execl
    ");
                    if(execl("/bin/ls", "ls", "-a", NULL) == -1) {
                            perror("execl error");
                            exit(1);
                    }
            }
    
            sleep(1);
            if(fork() == 0) {
                    printf("fork2 is Ok, execv
    ");
                    if(execv("/bin/ls", arg) == -1) {
                            perror("execv error");
                            exit(1);
                    }
            }
    
            sleep(1);
            if(fork() == 0) {
                    printf("fork3 is Ok, execlp
    ");
                    if(execlp("ls", "ls", "-a", NULL) == -1) {
                            perror("execlp error");
                            exit(1);
                    }
            }
    
            sleep(1);
            if(fork() == 0) {
                    printf("fork4 is Ok, execvp
    ");
                    if(execvp("ls", arg) == -1) {
                            perror("execvp error");
                            exit(1);
                    }
            }
    
            sleep(1);
            if(fork() == 0) {
                    printf("fork5 is Ok, execle
    ");
                    if(execle("/bin/ls", "ls", "-a", NULL, NULL) == -1) {
                            perror("execle error");
                            exit(1);
                    }
            }
    
            sleep(1);
            if(fork() == 0) {
                    printf("fork6 is Ok, execve
    ");
                    if(execve("/bin/ls", arg, NULL) == -1) {
                            perror("execve error");
                            exit(1);
                    }
            }
    
            return 0;
    }
    fork和exec的组合实例
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
    旋转二维数组
    replace empty char with new string,unsafe method和native implementation的性能比较
    判断一字符串是否可以另一字符串重新排列而成
    移除重复字符的几个算法简单比较
    也来纠结一下字符串翻转
    判断重复字符存在:更有意义一点
    程序员常去网站汇总
    sublime
    针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)
  • 原文地址:https://www.cnblogs.com/ch122633/p/9408165.html
Copyright © 2011-2022 走看看