1,使用ps指令来查看当下进程。实际工作中配合grep来查找程序中是否存在某个进程。
2,使用top指令查看,类似windows任务管理器
3,进程标识符:每一个进程都有一个非负整数表示唯一ID,叫做pid.
pid = 0:称为交换进程 作用:进程调度
pid = 1:init进程,作用系统初始化(类似于KTV点歌机一开机就是点歌界面)
相关函数
a,getpid()为获取进程进程标识符
b,getppid()获取父进程进程标识符
头文件
#include <sys/types.h>
#include <unistd.h>
函数原型
pid_t getpid(void);
pid_t getppid(void);
这两个的返回值为进程标识符
C,fork()创建一个子进程
头文件 #include <unistd.h>
函数原型 pid_t fork(void);
4,详细说明父进程与子进程的关系
父进程执行到fork()函数时创建一个子进程(此时子进程将把下图的栈,堆,未初始化数据,初始化数据,正文都拷贝一份然后父进程继续执行和子进程去执行拷贝的代码但是他们还是都在一个程序里面的)
5,copy on write
写时拷贝:就是当子进程里面的变量被改变时子进程在子进程里的地址才拷贝一份变量然后去改变此变量。
6,vfork()函数
头文件
#include <sys/types.h>
#include <unistd.h>
函数原型
pid_t vfork(void);
return和fork()一样
7,进程退出的方式
a,正常退出
b,异常退出
说明
8,exit()函数
头文件
#include <stdlib.h>
函数形式
void exit(int status);
status:退出标识符
return :无
9,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:选择子进程是否会等待子进程
作用:等待子进程结束,父进程才继续开始执行
在用以下宏定义来“解释”这个地址就得到退出标识符。
10,孤儿进程
父进程在没有等待子进程结束过后父进程就结束了,然后由系统init来继续充当父进程,等待子进程结束
11,僵尸进程
子进程结束过后,结束状态没被父进程收集,变为僵尸进程。
12,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;
(师上官可编程)