栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

系统级程序设计-3进程管理

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

系统级程序设计-3进程管理

exec函数族

ork:子进程复制父进程的堆栈段和数据段,子进程一旦开始运行,它继承了父进程的一切数据,但实际上数据却已经分开,相互之间不再影响
exec:一个进程调用exec类函数,它本身就"死亡"了,系统把代码段替换成新的程序代码,废弃原有数据段和堆栈段,并为新程序分配新数据段与堆栈段

wait()函数:
功能:调用wait()函数的进程会被挂起, 进入阻塞状态,直到wait()捕捉到僵尸子进程并回收该子进程的资源,若没有僵尸子进程,wait()函数则会让进程一直处于阻塞状态。
若当前由多个进程, 只需要捕捉到一个僵尸子进程, wait()函数就会返回并是进程恢复执行。

若子进程是其父进程的先决进程,调用wait()进程使进程同步。

#include 
#include 
#include 
#include 
int main()
{
    pid_t tempPid, tempW;
    tempPid = fork();
    if (tempPid == -1){         //error
        perror("fork error.n");
        exit(1);
    }
    else if(tempPid == 0){          //child process
        sleep(3);
        printf("this is child process, pid = %d, ppid = %dn", getpid(), getppid());

    }
    else{                       //parent process
        tempW = wait(NULL);         // wait() returns child process's pid
        printf("Create a child process, pid = %d, ppid = %dn", tempW, getppid());
    }
    printf("finish!!n");
    return 0;
}

学习内容:

提示:这里可以添加要学的内容

例如:

  1. 搭建 Java 开发环境
  2. 掌握 Java 基本语法
  3. 掌握条件语句
  4. 掌握循环语句

wait()进程具有一定的局限性:若当前由多个子进程,那么该函数无法确保作为先决条件的子进程在父进程之前执行。需要使用waitpid() 来解决这一问题。

waitpid()
功能:waitpid()可以等待指定的子进程,也可以在父进程不阻塞的情况下获取子进程的状态。

waitpid()等待指定子进程

#include 
#include 
#include 
#include 

int main()
{   pid_t tempPid, tempW, tempP;
    tempPid = fork();
    if(tempPid == -1){
        perror("fork1 error.n");
        exit(1);
    }
    else if(tempPid == 0){
        sleep(5);       // 该进程睡眠5秒
        printf("Create the first child process, pid = %d, ppid = %dn", getpid(), getppid());
    }

    else{   
        tempP = tempPid;   // tempP 存放 第一个子进程的pid
        int i;
    
        for (i = 0; i<3; i++){   // 创建三个新的子进程
            if((tempPid = fork()) == 0){
                break;
            }
        }
        if(tempPid == -1){
            perror("fork errorn");
            exit(2);
        }
        else if(tempPid == 0){
            printf("child process, pid = %dn", getpid());
            exit(0);
        }
        else{
            tempW = waitpid(tempP, NULL, 0); // pid参数赋值为 第一个子进程的pid, 指定收集该进程
            if(tempW == tempP){  // 若收集成功,则waitpid()返回值为被回收进程的pid
                printf("Catch a child process:pid = %dn", tempW);

            }
            else{
                printf("wait error.n");
            }
        }

    }
    return 0;
}

学习总结:

僵尸进程不能再次被运行,会占用一定的内存空间,并占据进程编号,当僵尸进程较多时,将会消耗系统内存,新进程可能因内存不足或无法获取pid而无法创建;
父进程通过wait()和waitpid()函数可以有效防止僵尸进程的产生,对于已存在的僵尸进程,则可通过杀死其父进程的方法解决;
当僵尸进程的父进程被终止后,僵尸进程将作为孤儿进程被init进程接收,init进程会不断调用wait()函数获取子进程状态,对已处于僵尸态的进程进行处理;
孤儿进程永远不会成为僵尸进程。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885308.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号