linux基础——进程的退出及资源回收

简介: linux基础——进程的退出及资源回收

文章目录

进程的退出

returen 和 exit

代码示例

注册进程结束调用函数

代码示例(on_exit):

atexit

代码示例(atexit)

进程资源的回收

代码示例

wait回收进程资源

代码示例

waitpid

代码示例

给指定进程发送信号(kill)

僵尸进程

代码示例

在进程的虚拟地址空间加载新的映像

代码示例

使用system启动新的可执行程序

代码示例


进程的退出

returen 和 exit

return只是函数的返回,而exit却是进程的结束。


void exit(int status);


#include <stdlib.h>
void exit(int status);
功能:终止进程
参数:
status:退出状态码。status&0377的值给父进程。
返回值:
永远不返回。


代码示例

  • test.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
  getchar();
  exit(-1);
}


  • 执行结果

20200205205519469.png

注册进程结束调用函数

在进程结束前,可以注册一些函数给进程,在进程结束时会自动调用这些被注册的函数。

on_exit(3)


#include <stdlib.h>
int on_exit(void (*function)(int , void *), void *arg);
功能:注册一个函数给进程,在进程终止的时候调用该函数
参数:
function:指定退出函数的名字
void (*function)(int , void *)
arg:指定退出函数的第二个参数
返回值:
0    成功
非0   错误


代码示例(on_exit):

  • on_exit.c
#include <stdio.h>
#include <stdlib.h>
void doit(int n,void *arg){
  printf("n=%d\targ:%s\n",\
  n,(char *)arg);
  return;
}
int main(void){
  //向进程注册退出函数
  on_exit(doit,"beijing");
  getchar();
  exit(3);
}


  • 执行结果

20200205212023579.png

atexit

atexit(3)


#include <stdlib.h>
int atexit(void (*function)(void));
功能:注册一个函数给进程,在进程终止的时候调用该函数
参数:
function:指定了要注册的函数的名字
返回值:
0    成功
非0   错误


代码示例(atexit)

  • atexit.c
#include <stdio.h>
#include <stdlib.h>
//注册给进程的退出函数
void doit(void){
  printf("hahha....\n");
  return;
}
int main(void){
  //向进程注册一个退出处理函数
  atexit(doit);
  getchar();
  return 0;
}


  • 执行结果

20200205212417685.png

进程资源的回收

在进程退出后,父进程会回收子进程的资源。

使用wait(2)、waitpid(2)系统调用回收子进程的资源。

如果父进程早于子进程结束,那么父进程的子进程的父亲就改变成为init进程,这种进程被成为孤儿进程。


代码示例

  • lonely.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void){
  pid_t pid;
  //创建子进程
  pid=fork();
  if(pid==-1){
  perror("fork");
  return 1;
  }
  if(pid==0){//子进程的代码
  sleep(5);
  printf("child...\n");
  //getchar();
  exit(0);
  }else{//父进程的代码
  printf("parent...\n");
  exit(0);
  }
  return 0;
}


  • 执行结果

20200205213053718.png

wait回收进程资源

#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
功能:等待进程改变状态。
参数:
status:退出状态码的地址。子进程的退出状态存放在这块地址空间里。可以使用一些宏检测退出原因。
WIFEXITED(status)  如果正常死亡,返回真
WEXITSTATUS(status)  返回子进程的退出状态和0377的与,那个值。
WIFSIGNALED(status) 如果子进程被信号终止,返回真
WTERMSIG(status)  检测被几号信号终止。只有上个宏为真的时候,才使用。
返回值:
-1   错误
返回终止的子进程的pid


代码示例

  • wait.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void){
  pid_t pid;
  int s;
  //创建子进程
  pid=fork();
  if(pid==-1){
  perror("fork");
  return 1;
  }
  if(pid==0){
  printf("child pid=%d\n",\
    getpid());
  //sleep(5);
  getchar();
  exit(-1);
  }else{
  //等待子进程的结束
  wait(&s);
  if(WIFEXITED(s)){
    //子进程正常终止
    printf("status:%d\n",     WEXITSTATUS(s));
  }
  //检测子进程是否被信号终止
  if(WIFSIGNALED(s)){
    //输出终止子进程的信号编号
    printf("signum :%d\n",\
    WTERMSIG(s));
  }
  printf("parent...\n");
  }
  return 0;
}


  • 执行结果

20200205214023156.png

waitpid

pid_t waitpid(pid_t pid,int *status,int options);


功能:等待进程改变状态。
参数:
pid:
< -1: pid取绝对值,如果子进程的组id等于这个绝对值,那么这个子进程就被等待。
-1:等待任意子进程
0:等待和当前进程有同一个组id的子进程
> 0   等待子进程的pid是pid参数的子进程。
status:同wait(2)参数的使用
options:
WNOHANG:非阻塞回收。
0    阻塞回收
返回值:
-1   错误  
0   没有子进程退出
回收的子进程的pid


代码示例

  • waitpid.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void){
  pid_t pid;
  int s;
  //创建子进程
  pid=fork();
  if(pid==-1){
  perror("fork");
  return 1;
  }
  if(pid==0){
  printf("child pid=%d\n",\
    getpid());
  //sleep(5);
  getchar();
  exit(-1);
  }else{
  //非阻塞等待子进程的结束
  waitpid(-1,&s,WNOHANG);
  if(WIFEXITED(s)){
    //子进程正常终止
    printf("status:%d\n",     WEXITSTATUS(s));
  }
  //检测子进程是否被信号终止
  if(WIFSIGNALED(s)){
    //输出终止子进程的信号编号
    printf("signum :%d\n",\
    WTERMSIG(s));
  }
  printf("parent...\n");
  }
  return 0;
}


  • 执行结果

20200205214449872.png

给指定进程发送信号(kill)

kill -[信号编号] [进程的pid]


僵尸进程

子进程已经终止,但是父进程还没有回收子进程的资源,这时候的子进程处于僵尸状态,成为僵尸进程。


代码示例

  • zombile.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
int main(void){
  pid_t pid;
  pid=fork();
  if(pid==-1){
  perror("fork");
  return 1;
  }
  if(pid==0){
  exit(0);
  }else{
  sleep(20);
  wait(NULL);
  }
  return 0;
}


在进程的虚拟地址空间加载新的映像

在子进程的虚拟地址空间加载新的影像,需要使用系统提供的一个家族的函数。


execl(3)


#include <unistd.h>
extern char **environ;
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[]);
int execvpe(const   char  *file,  char *const argv[],
                  char *const envp[]);


execve(2)


#include <unistd.h>
int  execve(const  char  *filename, char *const argv[],\
                  char *const envp[]);
相同的exec
l list   
v vector
p PATH    
e 环境变量
返回值:
成功调用永远不返回
-1  错误   errno被设置


代码示例

  • exec.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
char *const ps_argv[]={"ps","-o","pid,ppid,pgrp,comm",NULL};
int main(void){
  pid_t pid;
  //创建子进程
  pid=fork();
  if(pid ==-1){
  perror("fork");
  return 1;
  }
  if(pid==0){
  //加载新映像
  //execl("/bin/ps","ps","-o",\
  "pid,ppid,pgrp,comm",NULL);
  //execlp("ps","ps","-o",\
  "pid,ppid,pgrp,comm",NULL);
  execvp("ps",ps_argv);
  }else{
  wait(NULL);
  }
  return 0;
}


  • 执行结果:

20200205215929431.png

使用system启动新的可执行程序

#include <stdlib.h>
int system(const char *command);
功能:执行一个shell命令
参数:
command:可执行命令
返回值:
-1  错误
返回command的退出状态码。


代码示例

  • system.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
int main(void){
  pid_t pid;
  pid=fork();
  if(pid==-1){
  return 1;
  }
  if(pid==0){
  execl("./myt","myt",NULL);
  //system("myt");
  exit(0);
  }else{
  wait(NULL);
  }
  return 0;
}


  • 执行结果

2020020522040989.png

相关文章
|
2天前
|
存储 Linux Shell
Linux进程概念(上)
冯·诺依曼体系结构概述,包括存储程序概念,程序控制及五大组件(运算器、控制器、存储器、输入设备、输出设备)。程序和数据混合存储,通过内存执行指令。现代计算机以此为基础,但面临速度瓶颈问题,如缓存层次结构解决内存访问速度问题。操作系统作为核心管理软件,负责资源分配,包括进程、内存、文件和驱动管理。进程是程序执行实例,拥有进程控制块(PCB),如Linux中的task_struct。创建和管理进程涉及系统调用,如fork()用于创建新进程。
16 3
Linux进程概念(上)
|
3天前
|
Linux 开发工具
Linux技术资源分享:探索Linux软件包的宝藏:沙皇下载平台体验
Linux技术资源分享:探索Linux软件包的宝藏:沙皇下载平台体验
10 3
|
3天前
|
缓存 监控 安全
Linux top命令详解:持续监听进程运行状态
Linux top命令详解:持续监听进程运行状态
14 3
|
4天前
|
监控 Linux Shell
探索Linux命令nice:优雅地调整进程优先级
`nice`命令在Linux中用于调整进程优先级,影响资源分配。它允许设置-20到19的nice值,数值越低,优先级越高。在数据处理时,使用`nice`可控制任务优先级,避免占用全部CPU资源。例如,`nice -n 10 command`以低优先级启动`command`。注意不要过度使用,应根据系统负载和需求谨慎调整。使用`renice`可改变已运行进程的优先级,生产环境操作需谨慎。
|
7天前
|
Linux 数据处理
深入了解Linux命令kill:终止进程的艺术
**Linux的`kill`命令详解:高效管理进程的工具** `kill`命令在Linux中用于向进程发送信号,如SIGTERM(默认)和SIGKILL,以终止或影响进程行为。它通过进程ID(PID)操作,支持多种信号和选项,如`-l`列出信号,`-9`强制杀进程。例如,`kill 1234`发送TERM信号,`kill -9 1234`发送KILL信号。使用时注意,SIGKILL是不可忽视的,可能导致数据丢失。配合`pgrep`和`pkill`能更灵活管理进程。了解进程依赖和使用其他命令如`ps`和`top`可优化系统资源管理。
|
8天前
|
Linux API
Linux线程总结---线程的创建、退出、取消、回收、分离属性
Linux线程总结---线程的创建、退出、取消、回收、分离属性
|
5天前
|
监控 Linux 程序员
linux查看进程
linux查看进程
|
6天前
|
算法 Linux Shell
c++高级篇(一) —— 初识Linux下的进程控制
c++高级篇(一) —— 初识Linux下的进程控制
|
1天前
|
Linux
Linux如何快速执行历史命令
Linux如何快速执行历史命令
14 8
|
1天前
|
存储 安全 Linux
Linux passwd命令:守护账户安全的密钥
`passwd`命令是Linux中管理用户密码的关键工具,确保数据安全。它用于更改密码,采用加密存储,并有锁定/解锁账号、设置密码策略等功能。参数如`-d`删除密码,`-l`锁定账号,`-u`解锁。最佳实践包括定期更改复杂密码,保护root密码,谨慎使用无密码选项。了解和正确使用passwd是保障系统安全的重要步骤。