父进程等待子进程退出和僵尸进程、孤儿进程、字符化界面控制终端和图形界面伪终端切换

一、什么是僵尸进程?

父进程没有收集子进程的退出状态,子进程就变成了僵尸进程。

用指令   ps -aux|grep 'Z'   来进行查找

二、在Linux中,命令行也是一个进程

shell(壳)来处理用户输入的命令。Shell是一种命令解释器,它接收用户输入的命令,并将其转化为操作系统能够理解的形式,然后将命令发送给相应的程序或进程执行。在Linux中,常见的shell有Bash、Zsh、Fish等。

当我们在命令行中输入一个命令时,shell会将其解析并执行相应的操作。这个过程涉及到创建一个新的进程来执行命令,并将标准输入、输出和错误重定向到合适的地方。当命令执行完毕后,shell会等待下一个命令的输入。

因此,可以说在Linux中,命令行也是一个进程,它充当了shell的角色,负责接收用户输入的命令并进行处理。

三、父进程等待子进程退出

第一种:使用wait()函数

1 #include<stdio.h>
  2 #include<unistd.h>
  3 #include<stdlib.h>
  4 #include<sys/wait.h>
  5 
  6 int main()
  7 {
  8         int status=10;
  9         int cnt=0;
 10         //      pid_t getpid(void);
 11         printf("before fork pid is %d\n",getpid());
 12         //      pid_t fork(void);
 13         pid_t pid;
 14         pid = fork();
 15         printf("after  fork pid is %d\n",getpid());
 16 
 17         if(pid == 0){
 18                 while(1){
 19                         printf("This is child process and pid is %d\n",getpid());
 20                         sleep(1);
 21                         cnt++;
 22                         if(cnt==5){
 23                                 exit(6);
 24                         }
 25                 }
26         }else if(pid > 0){
 27                 wait(&status);
 28                 while(1){
 29                         printf("This is father process and pid is %d\n",getpid());
 30                         printf("PID=%d\n",pid);
 31                         sleep(1);
 32                         printf("data = %d\n",WEXITSTATUS(status));
 33                 }
 34         }
 35         return 0;
 36 }

 第二种:使用waitpid()有笔记

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 #include<stdlib.h>
  4 #include<sys/wait.h>
  5 
  6 int main()
  7 {
  8         int status=10;
  9         int cnt=0;
 10         //      pid_t getpid(void);
 11         printf("before fork pid is %d\n",getpid());
 12         //      pid_t fork(void);
 13         pid_t pid;
 14         pid = fork();
 15         printf("after  fork pid is %d\n",getpid());
 16 
 17         if(pid == 0){
 18                 while(1){
 19                         printf("This is child process and pid is %d\n",getpid());
 20                         sleep(1);
 21                         cnt++;
 22                         if(cnt==5){
 23                                 exit(6);
 24                         }
 25                 }
 26         }else if(pid > 0){
 27         //      wait(&status);
 28 //      pid_t waitpid(pid_t pid, int *wstatus, int options);
 29                 waitpid(pid,&status,WNOHANG);
 30                 while(1){
 31                         printf("This is father process and pid is %d\n",getpid());
 32                         printf("PID=%d\n",pid);
 33                         sleep(1);
 34                         printf("data = %d\n",WEXITSTATUS(status));
 35                 }
 36         }
 37         return 0;
 38 }

 四、什么是孤儿进程

父进程先于子进程退出来,子进程被init = 1收留

具体看这篇文章

Ubuntu上孤儿进程的父进程id输出为什么不是1?_孤儿进程的父进程pid不为1_皮蛋瘦肉粥嗷的博客-CSDN博客

 

 五、字符化界面控制终端和图形界面伪终端切换

init 3 进入字符化控制界面

sudo init 5  退出来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值