进程创建
1.vfork函数
vfork函数创建出来的子进程的虚拟地址空间和父进程共用一份,如果父子进程同时并行运行,有可能会导致函数调用栈混乱。
vfork解决函数调用栈混乱的方式:子进程先运行,子进程运行完毕之后,父进程再运行
2.fork函数
#include <unistd.h>
pid_t fork();
// 返回值:
// fork成功:父进程返回子进程id;子进程返回0
// fork失败:返回-1
进程终止
1、正常终止:代码运行完毕
正常终止方式:
1)main函数的return返回
① 代码运行完毕,结果正确
② 代码运行结束,结果不正确
2)_exit函数:系统调用函数,操作系统为程序员提供的函数
#include <unistd.h>
void _exit(int status);
// status:定义了进程的终止状态,父进程通过wait来获取该值
代码示例:
#include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/wait.h>
5
6 int main()
7 {
8 printf("Hello");
9 _exit(0);
10 }
3)exit函数:库函数,C库当中为程序员提供的函数
#include <unistd.h>
void exit(int status);
代码示例:
#include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <sys/wait.h>
5
6 int main()
7 {
8 printf("Hello");
9 exit(0);
10 }
- _exit和exit的区别
exit会刷新缓冲区
1)缓冲区是在C库当中维护的,并不是操作系统内核维护的
2)刷新缓冲区的方式
① main函数的return返回
② fflush函数可以强制刷新缓冲区
③ \n也会刷新缓冲区
④ exit函数也会刷新缓冲区
- atexit:
作用:注册终止函数(即main执行结束后调用的函数)
atexit函数可以注册一个函数,当程序结束的时候,再来执行注册的函数,这个被注册的函数称之为回调函数
#include <stdlib.h>
void atexit(void(*func)(void));
2、异常终止:代码没有执行完毕
异常终止方式:
1)调用abort
2)接到一个信号并终止
3)最后一个线程对取消请求做出响应
进程等待
1、作用:防止僵尸进程的产生
2、进程等待的方法
1)wait函数:阻塞等待
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int* status);
// status:出参 参数类型为int 获取子进程退出状态
// 返回值:
// 成功:返回被等待进程pid
// 失败:返回-1
代码示例:验证wait函数是阻塞等待
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
pid_t pid = fork();
if(pid < 0)
{
perror("fork");
return 0;
}
else if(pid == 0)
{
int count = 5;
//1int* lp = NULL
//*lp = 10;
while(count > 0)
{
printf("i am child pid:%d, ppid:%d\n", getpid(), getppid());
count--;
printf("%d\n", count);
sleep(1);
}
exit(10);
}
else{
printf("BEGIN...i am father pid:%d, ppid:%d\n", getpid(), getppid());
int st;
wait(&st); // st中保存了子进程异常退出发送的信号
printf("sig_code: %d\n", st & 0x7f);
printf("coredump_code: %d\n", (st>>7) & 0x1);
// 如果是wait函数是阻塞等待,那么一定要等到
while(1)
{
printf("i am father pid:%d, ppid:%d\n", getpid(), getppid());
sleep(1);
}
}
return 0;
}
2)waitpid函数
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int* status, int options);
// 参数
// pid:告诉waitpid函数,需要等待的进程
// 传-1:表示等待任意子进程
// pid > 0:表示等待指定子进程,传入的pid就是子进程的进程号
// status:出参,获取子进程退出状态
// options:
// 0:阻塞
// WNOHANG:非阻塞 如果调用非阻塞,当子进程没有退出时,waitpid函数会报错返回
代码示例:以被等待进程正常终止为例
1.waitpid阻塞等待
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
pid_t pid = fork();
if(pid < 0)
{
perror("fork");
return 0;
}
else if(pid == 0)
{
int count = 10;
while(1)
{
if(count < 1)
{
break;
}
printf("I am child: pid = %d, ppid = %d\n", getpid(), getppid());
count--;
sleep(1);
}
exit(10);
}
else{
printf("BEGIN...i am father: pid = %d, ppid = %d\n", getpid(), getppid());
int st;
// 阻塞等待
// 此时,父进程将会阻塞在waitpid函数,直到子进程中count减到0,退出循环,然后执行exit(10)
// 父进程等待成功后,拿到st,因为子进程是正常退出,所以st中使用高8位,可以获取退出码10
waitpid(-1, &st, 0);
printf("exit_code: %d\n", (st >> 8) & 0xff); // 10
printf("sig_code: %d\n", st & 0x7f); // 0
printf("coredump_code: %d\n", (st >> 7) & 0x1); // 0
// 下面这段代码只是为了验证是否阻塞
// 如果阻塞,那么这段代码要到子进程退出,父进程等待成功才会打印
// 如果非阻塞,那么这段代码
while(1)
{
printf("I am father:pid = %d, ppid = %d\n", getpid(), getppid());
sleep(1);
}
}
return 0;
}
2. waitpid非阻塞等待:一般要搭配循环使用
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
pid_t pid = fork();
if(pid < 0)
{
perror("fork");
return 0;
}
else if(pid == 0)
{
int count = 10;
while(1)
{
if(count < 1)
{
break;
}
printf("I am child: pid = %d, ppid = %d\n", getpid(), getppid());
count--;
sleep(1);
}
exit(10);
}
else{
printf("BEGIN...i am father: pid = %d, ppid = %d\n", getpid(), getppid());
int st;
// 非阻塞等待
// 此时,如果子进程未执行完毕,父进程的waitpid直接返回并报错
// 非阻塞一般会搭配循环使用
while((waitpid(-1, &st, WNOHANG)) == 0);
printf("exit_code: %d\n", (st >> 8) & 0xff);
// 异常退出后边两行代码的值才不会为0
printf("sig_code: %d\n", st & 0x7f);
printf("coredump_code: %d\n", (st >> 7) & 0x1);
// 下面这段代码只是为了验证是否阻塞
// 如果阻塞,那么这段代码要到子进程退出,父进程等待成功才会打印
// 如果非阻塞,那么这段代码
while(1)
{
printf("I am father:pid = %d, ppid = %d\n", getpid(), getppid());
sleep(1);
}
}
return 0;
}
- wait和waitpid的关系
- waitpid(pid>0, status, 0)相当于wait函数
- wait函数就是调用waitpid函数实现的
3、获取子进程status
1)status的类型是int,但是它只用到后两个字节;
2)进程正常退出时,高8位被使用,低8位为0;高8位保存的是程序的退出码;
获取退出码:(ststus >> 8) & 0xFF
3)进程异常退出时,只用到低8位;其中低7位表示进程收到的终止信号,第8位是coredump标志位
获取coredump标志位:(status>>7) & 0x1
获取进程终止信号:(status) & 0x7f
代码示例:被等待进程正常终止情况可参考上面代码;这里只验证异常终止的status(用wait函数测试)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
// 异常终止时,coredump标志位的获取及终止信号的获取
int main()
{
pid_t pid = fork();
if(pid < 0)
{
perror("fork");
return 0;
}
else if(pid == 0)
{
// 空指针赋值,会产生段错误,异常终止信号为11
int* p = NULL;
*p = 7;
// 下边代码用来判断异常终止时,代码是未执行完的
while(1)
{
printf("child pid = %d\n", getpid());
sleep(1);
}
}
else{
int status;
waitpid(-1, &status, 0);
printf("coredump_code : %d\n", (status >> 7) & 0x1);
printf("sig_code : %d\n", status & 0x7f);
}
return 0;
}
进程程序替换
1、替换原理
原因:父进程创建出来的子进程的虚拟地址空间拷贝了一份父进程的虚拟地址空间,所以父子进程执行相同的代码,但是,创建子进程往往是为了让子进程去干和父进程不同的事情,所以就有了进程程序替换。
原理:进程替换并不是新创建了一个进程,而是将已经存在的进程替换为别的可执行程序,然后执行替换后的程序的代码,所以进程号没有改变
2、替换函数:exec函数簇
- 先简单介绍下函数名:
函数名中带v表示,参数采用vector来保存;带l表示,参数用list来保存
函数名中带p表示,会自动搜索环境变量,只传入可执行程序名称即可;不带p表示,不会自动搜索环境变量,所以要传递带路径的可执行程序名称
函数名中带e表示,需要程序员自己组织环境变量;不带e表示,不需要自己组织环境变量 - 返回值:
调用成功:则加载新的程序,从启动代码执行,不再返回;调用失败:返回-1 - 除了execve是系统调用函数外,其余五个(execl、execlp、execle、execv、execvp)都是库函数
1)execl函数
#include <unistd.h>
int execl(const char* path, const char* arg, ...);
// path:带路径的可执行程序 带替换的程序
// arg:给替换的可执行程序传递参数,相当于在命令行中给可执行程序传递命令行参数
// ...:可变参数列表
// 规定:
// a.第一个传递的参数必须是可执行程序的名称
// b.如果后面还有要传递的参数,使用","进行间隔,依次传递
// c.以NULL结尾,告诉execl函数,参数传递完毕
2)execlp函数
#include <unistd.h>
int execlp(const char* file, const char* arg, ...);
// file:待替换的可执行程序名称
// 注意:替换的可执行程序必须是在PATH环境变量中可以找到的;也可以传递带路径的可执行程序
3)execle函数
#include <unistd.h>
int execle(const char* path, const char* arg, ..., char* const envp[]);
// path:带路径的可执行程序
// envp:程序员自己组织环境变量,如果不传入,则认为当前替换后的进程没有环境变量
4)execv函数
#include <unistd.h>
int execv(const char* path, char* const argv[]);
5)execvp函数
#include <unistd.h>
int execvp(const char* file, char* const argv[]);
6)execve函数
#include <unistd.h>
int execve(const char* path, char* const argv[], char* const envp[]);
代码示例:进程替换函数的使用
#include <stdio.h>
#include <unistd.h>
int main()
{
// 函数名中带e的需要自己组织环境变量
char* const envp[] = {"PATH = /bin:/usr/bin", "TERM = console", NULL};
// execl系列函数,参数用列表
execl("/bin/ps", "ps", "-ef", NULL);
execlp("ps", "ps", "-ef", NULL);
execle("/bin/ps", "ps", "-ef", NULL, envp);
// execv系列函数中,参数用数组保存,传递时直接传数组
char* const argv[] = {"ps", "-ef", NULL};
execv("/bin/ps", argv);
execvp("ps", argv);
execve("bin/ps", argv, envp);
return 0;
}