进程控制块
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
- pid:
Pid = -1,等待任一个子进程,与wait等效。
Pid > 0,等待指定的子进程。
- status:
WIFEXITED(status)宏: 如果子进程正常退出,则返回真。(查看进程是正常退出还是运行崩溃)
WEXITSTATUS(status)宏: 若WIFEXITED为真,则返回子进程退出码。(查看进程的退出码)
- options:
默认为0,表示阻塞等待。也就是说父进程会停下来等待子进程结束,然后waitpid才会返回,父进程才能继续执行。
WNOHANG宏(值为1),表示非阻塞等待。 若 【pid指定的子进程/所有进程】没有结束,则waitpid()函数直接返回0,不予以等待。若正常结束,则返回该子进程的PID。
- 当正常返回的时候waitpid返回收集到的子进程的进程ID;
- 如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;
- 如果调用中出错,则返回-1,这时errno会被设置成相应的值以指示错误所在;
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()