1.使用ps指令来查看当下进程。实际工作中配合grep来查找程序中是否存在某个进程。
2.使用top指令查看,类似windows任务管理器
3.进程标识符:每一个进程都有一个非负整数表示唯一ID,叫做pid.
4.相关函数
getpid()为获取进程进程标识符
getppid()获取父进程进程标识符
头文件
#include <sys/types.h>
#include <unistd.h>
函数原型
pid_t getpid(void);
pid_t getppid(void);
5. vfork
#include <sys/types.h>
#include <unistd.h>
函数原型
pid_t vfork(void);
return和fork()一样
6.进程退出的方式
a,正常退出
b.异常退出
7.exit()函数
头文件
#include <stdlib.h>
函数形式
void exit(int status);
status:退出标识符
return :无
8.wait()函数
头文件
#include <sys/types.h>
#include <sys/wait.h>
函数形式
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
pid:fork()函数返回的子进程的参数
status:退出标识符的地址
options:选择子进程是否会等待子进程
作用:等待子进程结束,父进程才继续开始执行
在用以下宏定义来“解释”这个地址就得到退出标识符。
9.孤儿进程
父进程在没有等待子进程结束过后父进程就结束了,然后由系统init来继续充当父进程,等待子进程结束
10.僵尸进程
子进程结束过后,结束状态没被父进程收集,变为僵尸进程。
11.exec族部分函数
头文件
#include <unistd.h>
函数
int execl(const char *path, const char *arg, …);
int execlp(const char *file, const char *arg, …);
说明:可以在程序中调用其他程序
没有返回值,只要在程序终止时,才会返回一个错误值,可以用perror()来解析错误原因
path:文件地址
例如
execlp("./kd",“kd”,“test.config”,NULL)
arg:要以NULL结尾,其他都是参数。
12.system();
头文件
#include <stdlib.h>
函数
int system(const char *command);
command:(./+执行文件的名字)
和exec()的区别,exec()结束后不会在执行后面的内容了,system()可以执行
13.popen()函数
头文件
#include <stdio.h>
函数原型
FILE *popen(const char *command, const char *type);
return:程序执行完会返回最终的结果;
command:要执行的命令;
type:只有w和r;
————————————————
(师上官可编程)
参考原文内容
原文链接:https://blog.csdn.net/qq_46518045/article/details/112687398