【Linux系统编程】-进程

前言

这篇文章对Linux的进程做了简单概括,适合初学者快速的了解Linux进程的基本内容。以及程序开发时快速查阅相关内容!本文最后有Linux进程的详细讲解链接。

1.进程查看

ps aux # 查看系统中所有的进程信息

ps axj # 可以查看进程的父进程号

top #动态查看3S更新一次

2.孤儿进程

如果父进程比子进程先退出,那么此时子进程就叫做孤儿进程。而操作系统不会让这个子进程孤苦伶仃的运行在操作系统中,所以此时孤儿进程会被init进程(也就是1号进程,即所有进程的祖先)领养,从此以后孤儿进程的状态和最后的PCB空间释放都是由init进程负责了。

3.僵尸进程

即进程已经结束了,但是父进程没有使用wait()系统调用,此时父进程不能读取到子进程退出返回的信息,此时就该进程就进入僵死状态。僵死状态需要父进程发出wait()系统调用终止进程,如果父进程不终止进程,那么此时要消灭僵尸进程只能通过找到僵尸进程的父进程,然后kill掉这个父进程,然后僵尸进程就会成为孤儿进程,此时由init进程领养这个进程然后杀死这个僵尸进程。

4.创建进程

1.使用./运行某一个可执行程序,这种是最常见的方式

2.使用系统调用接口创建进程,即使用fork()

使用fork()创建

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

int main()    
{
    pid_t pid = fork();
    if (pid < 0) {
        printf("error");
    }
    if (pid == 0) {    
        printf("i am a child process\n"); // 输出 
    } else {    
        printf("i am a father process\n"); // 输出
    }
    return 0;    
}

注意

fork函数调用一次,返回两次。上面的代码是如何实现执行两个不同的分支语句的呢?其实是因为fork函数会返回两个返回值,一个是子进程会返回0,一个是父进程会返回子进程的PID。所以会同时进程两个分支语句中。

5.退出进程 exit()

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

int main()    
{
    pid_t pid = fork();
    if (pid < 0) {
        printf("error");
    }
    if (pid == 0) {    
        printf("i am a child process\n"); // 输出 
exit(0);//结束子进程
    } else {    
        printf("i am a father process\n"); // 输出
    }
    return 0;    
}

6.进程等待(回收进程、消除僵尸进程)

 

 1.wait() (阻塞)

#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t pid = fork();
    if (pid == 0) {
        // child
        int count= 5;
        while (count --) {
            printf("I am child process, PID:%d, PPID:%d\n", getpid(), getppid());
            sleep(1);
        }
        exit(10);
    } else {
        // father
        int status = 0;
        pid_t res = wait(&status); // 如果wait成功,返回值为子进程的PID
        if (res > 0) {
            printf("wait child process success!\n");
            if (WIFEXITED(status)) {
                // 正常退出
                printf("exit code: %d\n", WEXITSTATUS(status));
            } else {
                // 异常退出
                printf("exit signal:%d\n", status & 0x7f);
            }
        }
    }
    return 0;
}

2.waitpid() (阻塞)

#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t pid = fork();
    if (pid == 0) {
        // child
        int count= 5;
        while (count --) {
            printf("I am child process, PID:%d, PPID:%d\n", getpid(), getppid());
            sleep(1);
        }
        exit(10);
    } else {
        // father
        int status = 0;
        pid_t res = waitpid(pid, &status, 0); // 阻塞等待指定的子进程
        if (res > 0) {
            printf("wait child process success!\n");
            if (WIFEXITED(status)) {
                printf("exit code: %d\n", WEXITSTATUS(status));
            } else {
                printf("exit signal: %d\n", status & 0x7f);
            }
        }
    }
    return 0;
}

3.waitpid() (非阻塞)

#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t pid = fork();
    if (pid == 0) {
        // child
        int count= 5;
        while (count --) {
            printf("I am child process, PID:%d, PPID:%d\n", getpid(), getppid());
            sleep(2);
        }
        exit(10);
    } else {
        while (1) {
            int status = 0;
            pid_t res = waitpid(pid, &status, WNOHANG);
            if (res > 0) {
                printf("wait child success!\n");
                printf("exit code: %d\n", WEXITSTATUS(status));
                break;
            } else if (res == 0) {
                printf("father process can do something!\n");
                sleep(1);
            }
        }
    }
    return 0;
}

有不当的地方欢迎大家指正!谢谢!

【Linux】进程详解一:进程概念

【Linux】进程详解二:进程控制

相关推荐

  1. Linux系统编程进度条的编写

    2023-12-28 10:24:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 10:24:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 10:24:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 10:24:01       82 阅读
  4. Python语言-面向对象

    2023-12-28 10:24:01       91 阅读

热门阅读

  1. Illegal unit of measure (pt inserted)

    2023-12-28 10:24:01       61 阅读
  2. linux和windows获取RAM全局瞬时占用

    2023-12-28 10:24:01       49 阅读
  3. C++ 图形界面的贪吃蛇。

    2023-12-28 10:24:01       49 阅读
  4. uniapp中各种状态的按钮

    2023-12-28 10:24:01       67 阅读
  5. conda环境配置torch+cuda+cudnn+dgl用到的一些命令

    2023-12-28 10:24:01       45 阅读
  6. 多元函数极值@条件极值

    2023-12-28 10:24:01       67 阅读
  7. Springboot2+mybatisplus+多数据源更换mysql数据库为pgsql

    2023-12-28 10:24:01       54 阅读
  8. Track-Anything 追踪万物

    2023-12-28 10:24:01       67 阅读
  9. 算法设计与分析 | 动态规划

    2023-12-28 10:24:01       52 阅读
  10. 【pg】多个字段查重过滤

    2023-12-28 10:24:01       72 阅读