【Linux进程总结】PCB 进程创建 进程等待 进程替换 进程通信 前台后台进程

进程控制块

struct ProcessControlBlock 
{
   
    int pid;                                   // 进程标识符 (PID)
    int ppid;                                  // 父进程标识符 (PPID)
    std::string state;                         // 进程状态
    int priority;                              // 进程优先级
    SchedulingInfo scheduling;                 // 调度信息
    void* program_counter;                     // 程序计数器
    RegisterSet registers;                     // 寄存器集合
    MemoryManagementInfo memory;               // 内存管理信息
    struct files_struct* files;          	   // 文件信息
    SignalHandlingInfo signals;                // 信号处理信息
    int uid;                                   // 用户 ID
    int gid;                                   // 组 ID
    CPUUsageInfo cpu_usage;                    // CPU 使用信息
    WaitQueue wait_queue;                      // 等待队列信息
    ThreadInfo thread_info;                    // 线程信息
};

进程等待

wait 等待任一个进程 成功返回childPid 失败返回-1

waitpid

  1. pid:

Pid = -1,等待任一个子进程,与wait等效。
Pid > 0,等待指定的子进程。

  1. status:

WIFEXITED(status)宏: 如果子进程正常退出,则返回真。(查看进程是正常退出还是运行崩溃)

WEXITSTATUS(status)宏: 若WIFEXITED为真,则返回子进程退出码。(查看进程的退出码)

  1. options:

默认为0,表示阻塞等待。也就是说父进程会停下来等待子进程结束,然后waitpid才会返回,父进程才能继续执行。
WNOHANG宏(值为1),表示非阻塞等待。 若 【pid指定的子进程/所有进程】没有结束,则waitpid()函数直接返回0,不予以等待。若正常结束,则返回该子进程的PID。

  1. 当正常返回的时候waitpid返回收集到的子进程的进程ID;
  2. 如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;
  3. 如果调用中出错,则返回-1,这时errno会被设置成相应的值以指示错误所在;
  4. waitpid(-1, NULL, 0)的效果与wait(NULL)相同

waitpid几种常用方式

waitpid(-1, NULL, 0) 成功返回pid 失败返回-1
waitpid(-1, NULL, WNOHANG) 成功pid/0 失败-1

等待代码【宏版本】

#include <iostream>

using namespace std;

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
// g++ test.cc -o test -std=c++11
int main()
{
   
    pid_t id = fork();
    if (id < 0)
    {
   
        printf("创建子进程失败!\n");
        exit(1);
    }
    else if (id == 0)
    {
   
        // printf("我是子进程: pid: %d, 我将异常退出!\n", getpid());
        // int *p = NULL;
        // *p = 1; // 引发异常

        printf("我是子进程: pid: %d, 我将2s后正常退出!\n", getpid());
        sleep(2);
    }
    else
    {
   
        printf("我是父进程: pid: %d, 我将耐心地等待子进程!\n", getpid());
        int status = 0;
        pid_t ret = waitpid(id, &status, 0);
        if (ret > 0)
        {
   
            if (WIFEXITED(status))
            {
   
                printf("子进程正常退出,退出码: %d\n", WEXITSTATUS(status)); //(status>>8)&0xFF
            }
            else if (WIFSIGNALED(status))
            {
   
                printf("子进程异常退出,信号编号: %d\n", WTERMSIG(status)); // status & 0x7F
            }
        }
        else if (ret == -1)
        {
   
            cout << "waitpid调用失败, 等待子进程失败!" << endl;
        }
    }
    return 0;
}

bash脚本

while :; do ps axj | head -1 && ps axj | grep myproc | grep -v grep; echo "分隔符"; sleep 1; done

进程替换

在这里插入图片描述

参数 描述
l(list) 参数采用列表形式
v(vector) 参数用数组形式
p(path) 自动搜索环境变量PATH
e(env) 自己维护环境变量
#include <unistd.h>
extern char **environ;

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char * const envp[]);

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

execve是真正的系统调用,其余的是系统提供的基本封装。 他们会将接收到的参数进行合并处理,最后底层还是会调用execve。

在这里插入图片描述

文件描述符

dup2()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿猿收手吧!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值