没有释放资源
exit是对_exit进行封装

命令:
echo $?,是对当前进程的返回值

比_exit多一个刷stdio流缓存区
#include
#include
#include
#include
int main(int argc, char const *argv[], char **env)
{
printf("hello world");
_exit(1); //打印不出hello world
//exit(1); //可以打印出hello world
while(1);
return 0;
}
两者区别
小结
退出处理程序
在exit退出后可以自动执行用户注册的退出处理程序
执行顺序与注册顺序相反
函数原型: int atexit (void (funtion)void);
函数原型: int on_exit (vold (*function)int,void *), void *arg);
#include
#include
#include
#include
void my_exit(void)
{
printf("my_exit\n");
}
int main(int argc, char const *argv[], char **env)
{
atexit(my_exit); //会在程序退出前调用my_exit函数
printf("hello world");
//_exit(1); //打印不出hello world
exit(1); //可以打印出hello world
while(1);
return 0;
}

#include
#include
#include
#include
void my_exit(void)
{
printf("my_exit\n");
}
void my_exit2(int ret, void *p)
{
printf("ret = %d\n", ret);
printf("p = %s\n", (char *)p);
}
int main(int argc, char const *argv[], char **env)
{
//atexit(my_exit);
on_exit(my_exit2, "hello");
printf("hello world");
//_exit(1); //打印不出hello world
exit(1); //可以打印出hello world
while(1);
return 0;
}

进程运行终止后,不管进程是正常终止还是异常终止的,必须回收进程所古用的资源

当一个进程退出之后,进程能够回收自己的用户区的资源,但是不能回收内核空间的PCB资源,必须由父进程调用wait或者waitpid函数完成对子进程的回收,避免造成资源浪费。
父进程运行结束时,会负责回收子进程资源。
0、1、 2三个进程:OS启动之后一 直默默运行的进程,直到关机OS结束运行
做内部调度




父进程还没结束,子进程结束,子进程资源没被回收,变成僵尸进程,占用资源

父进程结束,子进程还没结束,会变成孤儿进程,会被托管到PID == 1的下面

等待子进程运行终止

子进程返回状态
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
pid_t pid;
pid = fork();
if (pid < 0)
{
perror("fork error!\n");
exit(1);
}
if (pid == 0) //子进程
{
for (size_t i = 0; i < 3; i++)
{
printf("hello \n");
sleep(1);
}
exit(3);
}
else if (pid > 0) //父进程
{
printf("father hello\n");
int ret;
wait(&ret);
int num = WEXITSTATUS(ret);
printf("子进程全部结束 = %d\n",num);
}
return 0;
}


谈谈你对进程的理解
进程是对程序的统一抽象,因为任务调度,分配资源是按照进程分配的
拥有独立的地址空间,进程的消亡不会对其他进程产生影响
缺点:开销比较大
进程调度:三态、调度算法
进程创建