一. 进程控制
(1)进程创建
fork函数初识
在linux中fork函数时非常重要的函数,它从已存在进程中创建一个新进程。新进程为子进程,而原进程为父进程。
#include <unistd.h>
pid_t fork(void);
返回值:自进程中返回0,父进程返回子进程id,出错返回-1
进程调用fork,当控制转移到内核中的fork代码后,内核做:
分配新的内存块和内核数据结构给子进程
将父进程部分数据结构内容拷贝至子进程
添加子进程到系统进程列表当中
fork返回,开始调度器调度
当一个进程调用fork之后,就有两个二进制代码相同的进程。而且它们都运行到相同的地方。但每个进程都将可以开始它们自己的旅程。
fork函数返回值
子进程返回0,
父进程返回的是子进程的pid
写时拷贝
通常,父子代码共享,父子再不写入时,数据也是共享的,当任意一方试图写入,便以写时拷贝的方式各自一份副本。具体见下图:
fork常规用法
一个父进程希望复制自己,使父子进程同时执行不同的代码段。例如,父进程等待客户端请求,生成子
进程来处理请求。
一个进程要执行一个不同的程序。例如子进程从fork返回后,调用exec函数。
fork调用失败的原因
系统中有太多的进程
实际用户的进程数超过了限制
(2)进程终止
- 进程终止时,操作系统做了什么?
进程=内核相关数据结构+代码和数据。
当然要释放进程申请的相关的内核数据结构和对应的数据和代码,本质是释放系统资源 - 进程终止常见的方式
a. 代码跑完,结果正确。
b. 代码跑完,结果不正确。
c. 代码没有跑完,程序崩溃了。
我们从一个常见的问题入手,我们写main函数的时候,结尾都会写一个return 0,那么这个return是返回给谁呢,为什么是返回0呢,0是退出码,退出码一般是零和非零两种情况,返回零表示程序正常结束,非零返表示程序异常结束,返回的这个退出码一般是返回给父进程,main函数的父进程一般是bash,我们可以采用 echo $?来了解到最近一个进程退出时的退出码是多少
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
printf("hello world\n");
return 0;
}
我们能看到退出码就打印出来了。
下面我们了解一下strerror这个函数,这里面记录了非零退出码对应的错误信息
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
int main()
{
for(int i=0;i<100;i++)
{
printf("%d %s\n",i,strerror(i));
}
}
这里打印了前100个退出码对应的错误,这也是为什么我们的程序出问题的时候,编译器可以给出我们错误信息。
那么我们的进程怎么停下来呢
- 从main返回
- 调用exit
- _exit
_exit函数
#include <unistd.h>
void _exit(int status);
参数:status 定义了进程的终止状态,父进程通过wait来获取该值
exit函数
#include <unistd.h>
void exit(int status);
程序员可以在函数内部通过使用exit或者_exit函数来使进程终止,并且可以通过这些函数,提供给父进程退出码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
void fun()
{
int cnt=3;
while(cnt)
{
printf("%d hello world\n",cnt--);
sleep(1);
}
exit(5);
}
int main()
{
fun();
return 0;
}
这些都是代码跑完的情况下所得的退出码
那么不正确的情况下呢
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
void fun()
{
int cnt=3;
cnt/=0;
while(cnt)
{
printf("%d hello world\n",cnt--);
sleep(1);
}
exit(5);
}
int main()
{
fun();
return 0;
}
此时退出码就不是按照我们之前设置的了
接下来,谈论一下exit和_exit的区别,我们分别看一下两段代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
int main()
{
printf("hello world");
sleep(3);
exit(0);
return 0;
}
因为\n结尾会刷新缓冲区中的数据,所以结尾没带\n。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
int main()
{
printf("hello world");
sleep(3);
_exit(0);
return 0;
}
我们发现exit结束,会刷新缓冲区,_exit不会刷新缓冲区,_exit使系统接口,exit实际上底层封装的就是_exit,由此能看出,之前提出的缓冲区不是系统维护的,而是c库维护的。
exit最后也会调用exit, 但在调用exit之前,还做了其他工作:
- 执行用户通过 atexit或on_exit定义的清理函数。
- 关闭所有打开的流,所有的缓存数据均被写入
- 调用_exit
(3)进程等待
进程等待必要性
之前讲过,子进程退出,父进程如果不管不顾,就可能造成‘僵尸进程’的问题,进而造成内存泄漏。
另外,进程一旦变成僵尸状态,那就刀枪不入,“杀人不眨眼”的kill -9 也无能为力,因为谁也没有办法
杀死一个已经死去的进程。
最后,父进程派给子进程的任务完成的如何,我们需要知道。如,子进程运行完成,结果对还是不对,
或者是否正常退出。
父进程通过进程等待的方式,回收子进程资源,获取子进程退出信息
进程等待的方法
wait方法
#include<sys/types.h>
#include<sys/wait.h>
pid_t wait(int*status);
返回值:
成功返回被等待进程pid,失败返回-1。
参数:
输出型参数,获取子进程退出状态,不关心则可以设置成为NULL
waitpid方法
pid_ t waitpid(pid_t pid, int *status, int options);
返回值:
当正常返回的时候waitpid返回收集到的子进程的进程ID;
如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;
如果调用中出错,则返回-1,这时errno会被设置成相应的值以指示错误所在;
参数:
pid:
Pid=-1,等待任一个子进程。与wait等效。
Pid>0.等待其进程ID与pid相等的子进程。
status:
WIFEXITED(status): 若为正常终止子进程返回的状态,则为真。(查看进程是否是正常退出)
WEXITSTATUS(status): 若WIFEXITED非零,提取子进程退出码。(查看进程的退出码)
options:
WNOHANG: 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若正常结束,则返回该子进
程的ID。
也就是说我们能通过wait 或者waitpid两个函数,获取子进程的退出码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t id=fork();
if(id<0)
{
perror("fork");
}
else if(id==0)
{
int cnt=3;
while(cnt)
{
printf("我是子进程 pid:%d ppid:%d \n",getpid(),getppid());
sleep(1);
cnt--;
}
exit(105);
}
else
{
int state=0;
pid_t id=wait(&state);
printf("我是父进程 id:%d,子进程 id:%d 退出码:%d\n",getpid(),id,state);
}
//return 0;
}
我明明退出码定义的是105啊,为什么退出码这么大。
原因是
退出码在次8位,所以我们要操作一下
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t id=fork();
if(id<0)
{
perror("fork");
}
else if(id==0)
{
int cnt=3;
while(cnt)
{
printf("我是子进程 pid:%d ppid:%d \n",getpid(),getppid());
sleep(1);
cnt--;
}
exit(105);
}
else
{
int state=0;
pid_t id=wait(&state);
printf("我是父进程 id:%d,子进程 id:%d 退出码:%d\n",getpid(),id,(state>>8)&0xff);
}
//return 0;
}
前面七位,是信号,操作系统就是通过信号杀死进程的,操作系统的信号有这么多,没有0号信号
我们这次把信号带上
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t id=fork();
if(id<0)
{
perror("fork");
}
else if(id==0)
{
int cnt=3;
while(cnt)
{
printf("我是子进程 pid:%d ppid:%d \n",getpid(),getppid());
sleep(1);
cnt--;
}
exit(105);
}
else
{
int state=0;
pid_t id=wait(&state);
printf("我是父进程 id:%d,子进程 id:%d 信号:%d 退出码:%d\n",getpid(),id,state&0x7f,(state>>8)&0xff);
}
//return 0;
}
只要代码正常,信号就是0,要是出现问题呢
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t id=fork();
if(id<0)
{
perror("fork");
}
else if(id==0)
{
int cnt=3;
printf("我是子进程 pid:%d ppid:%d \n",getpid(),getppid());
cnt/=0;
exit(105);
}
else
{
int state=0;
pid_t id=wait(&state);
printf("我是父进程 id:%d,子进程 id:%d 信号:%d 退出码:%d\n",getpid(),id,state&0x7f,(state>>8)&0xff);
}
//return 0;
}
我们发现,程序异常退出,退出码就没有意义了,并且,系统发出了8号信号杀死了进程。
上述获取退出码太复杂了
也可以这么写
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t id=fork();
if(id<0)
{
perror("fork");
}
else if(id==0)
{
int cnt=3;
while(cnt)
{
printf("我是子进程 pid:%d ppid:%d \n",getpid(),getppid());
sleep(1);
cnt--;
}
exit(105);
}
else
{
int state=0;
pid_t id=waitpid(-1,&state,0);
if(WIFEXITED(state))
{
printf("子进程正常终止,退出码为%d\n",WEXITSTATUS(state));
}
else{
printf("子进程异常退出,退出码0\n");
}
//printf("我是父进程 id:%d,子进程 id:%d 信号:%d 退出码:%d\n",getpid(),id,state&0x7f,(state>>8)&0xff);
}
//return 0;
}
下面谈一下option这个参数,WNOHANG: 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若正常结束,则返回该子进
程的ID,子进程在运行时,父进程可以一边等待一边干自己的事情,这样可以提高效率
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
#include <vector>
typedef void (*handler)();
std::vector<handler> v;
void part_one()
{
printf("我是第一个任务\n");
}
void part_two()
{
printf("我是第二个任务\n");
}
void load()
{
v.push_back(part_one);
v.push_back(part_two);
}
int main()
{
pid_t id = fork();
if (id < 0)
{
perror("fork");
}
else if (id == 0)
{
int cnt = 3;
while (cnt)
{
printf("我是子进程 pid:%d ppid:%d \n", getpid(), getppid());
sleep(1);
cnt--;
}
exit(105);
}
else
{
int state = 0;
int st = 1;
while (st)
{
pid_t id = waitpid(-1, &state, WNOHANG);
if (id > 0)
{
printf("子进程退出,退出码%d\n",WEXITSTATUS(state));
st=0;
}
else if(id==0)
{
if(v.empty())
{
load();
}
printf("子进程还退出\n");
for(auto e:v)
{
e();
}
sleep(1);
}
else
{
printf("等待失败\n");
st=0;
}
}
}
// return 0;
}
(4)进程替换
替换原理
用fork创建子进程后执行的是和父进程相同的程序(但有可能执行不同的代码分支),子进程往往要调用一种exec函数
以执行另一个程序。当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动
例程开始执行。调用exec并不创建新进程,所以调用exec前后该进程的id并未改变
替换函数
其实有六种以exec开头的函数,统称exec函数:
#include <unistd.h>`
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[]);
函数解释
这些函数如果调用成功则加载新的程序从启动代码开始执行,不再返回。
如果调用出错则返回-1
所以exec函数只有出错的返回值而没有成功的返回值
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
printf("我还没被替换\n");
execl("/usr/bin/ls","ls","-a",nullptr);
printf("我已经被替换了\n");
}
这是简单的程序替换,把本进程替换成了ls,用法大概就是,告诉系统路径,然后xshell上怎么运行,函数里面就怎么写,最后nullptr结尾就行
模拟shell
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#define NUM 1024
#define SIZE 32
#define SEP " "
//保存完整的命令行字符串
char cmd_line[NUM];
//保存打散之后的命令行字符串
char *g_argv[SIZE];
// 写一个环境变量的buffer,用来测试
char g_myval[64];
// shell 运行原理 : 通过让子进程执行命令,父进程等待&&解析命令
int main()
{
extern char**environ;
//0. 命令行解释器,一定是一个常驻内存的进程,不退出
while(1)
{
//1. 打印出提示信息 [whb@localhost myshell]#
printf("[root@localhost myshell]# ");
fflush(stdout);
memset(cmd_line, '\0', sizeof cmd_line);
//2. 获取用户的键盘输入[输入的是各种指令和选项: "ls -a -l -i"]
if(fgets(cmd_line, sizeof cmd_line, stdin) == NULL)
{
continue;
}
cmd_line[strlen(cmd_line)-1] = '\0';
//"ls -a -l -i\n\0"
//printf("echo: %s\n", cmd_line);
//3. 命令行字符串解析:"ls -a -l -i" -> "ls" "-a" "-i"
// export myval=105
g_argv[0] = strtok(cmd_line, SEP); //第一次调用,要传入原始字符串
int index = 1;
if(strcmp(g_argv[0], "ls") == 0)
{
g_argv[index++] = "--color=auto";
}
if(strcmp(g_argv[0], "ll") == 0)
{
g_argv[0] = "ls";
g_argv[index++] = "-l";
g_argv[index++] = "--color=auto";
}
//?
while(g_argv[index++] = strtok(NULL, SEP)); //第二次,如果还要解析原始字符串,传入NULL
if(strcmp(g_argv[0], "export") == 0 && g_argv[1] != NULL)
{
strcpy(g_myval, g_argv[1]);
int ret = putenv(g_myval);
if(ret == 0) printf("%s export success\n", g_argv[1]);
//for(int i = 0; environ[i]; i++)
// printf("%d: %s\n", i, environ[i]);
continue;
}
//for debug
//for(index = 0; g_argv[index]; index++)
// printf("g_argv[%d]: %s\n", index, g_argv[index]);
//4.内置命令, 让父进程(shell)自己执行的命令,我们叫做内置命令,内建命令
//内建命令本质其实就是shell中的一个函数调用
if(strcmp(g_argv[0], "cd") == 0) //not child execute, father execute
{
if(g_argv[1] != NULL) chdir(g_argv[1]); //cd path, cd ..
continue;
}
//5. fork()
pid_t id = fork();
if(id == 0) //child
{
printf("下面功能让子进程进行的\n");
printf("child, MYVAL: %s\n", getenv("MYVAL"));
printf("child, PATH: %s\n", getenv("PATH"));
//cd cmd , current child path
//execvpe(g_argv[0], g_argv, environ); // ls -a -l -i
//不是说好的程序替换会替换代码和数据吗??
//环境变量相关的数据,会被替换吗??没有!
execvp(g_argv[0], g_argv); // ls -a -l -i
exit(1);
}
//father
int status = 0;
pid_t ret = waitpid(id, &status, 0);
if(ret > 0) printf("exit code: %d\n", WEXITSTATUS(status));
}
}
模拟shell的主要思路就是用父进程产生子进程,然后让子进程去执行任务,strtok可以把字符串分割,第一次要输入主要的地址,第二次用nullptr代替就行,不过有些方法不能用子进程执行,比如cd export等,这些需要父进程去做。