当前位置: 首页 > wzjs >正文

做网站年赚千万商城网站建设排名

做网站年赚千万,商城网站建设排名,html个人网站案例,汽车网站排名查询1、进程等待 僵尸进程 是一个比较麻烦的问题,如果不对其做出处理,僵尸进程 就会越来越多,导致 内存泄漏 和 标识符 占用问题 进程一旦变成僵尸状态,那就刀枪不入,“杀人不眨眼”的kill -9 也无能为力,因为…
1、进程等待

僵尸进程 是一个比较麻烦的问题,如果不对其做出处理,僵尸进程 就会越来越多,导致 内存泄漏 和 标识符 占用问题

进程一旦变成僵尸状态,那就刀枪不入,“杀人不眨眼”的kill -9 也无能为力,因为谁也没有办法杀死一个已经死去的进程

2、等待原因

子进程运行结束后,父进程没有等待并接收其退出码和退出状态,OS 无法释放对应的 内核数据结构+代码和数据,出现 僵尸进程

为了避免这种情况的出现,父进程可以通过函数等待子进程运行结束,此时父进程属于阻塞状态

注意:

  • 进程的退出状态是必要的
  • 进程的执行结果是非必要的
  • 也就是说,父进程必须对子进程负责,确保子进程不会连累 OS,而子进程执行的结果是否正确,需要我们自行判断
 3.等待函数
3.1 wait()
#include<sys/types.h>
#include<sys/wait.h>
pid_t wait(int*status);
返回值:成功返回被等待进程pid,失败返回-1。
参数:输出型参数,获取子进程退出状态,不关心则可以设置成为NULL

 代码验证:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>void RunChild()
{int cnt = 5;while(cnt){printf("I am child process,pid:%d,ppid:%d\n",getpid(),getppid());sleep(1);cnt--;}
}
int main()
{printf("I am a father process,pid:%d,ppid:%d\n",getpid(),getppid());pid_t id = fork();if(id == 0){RunChild();printf("child quit...\n");exit(123);}sleep(6);pid_t rid = wait(NULL);sleep(2);printf("father quit...\n");return 0;
}

 注意:wait等待的是任意一个进程。 

3.2 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相等的子进程。pid<0,等待的进程错误,比如进程不存在。status:WIFEXITED(status): 若为正常终止子进程返回的状态,则为真。(查看进程是否是正常退出)WEXITSTATUS(status): 若WIFEXITED非零,提取子进程退出码。(查看进程的退出码)options:WNOHANG: 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若正常结束,则返回该子进程的ID。

代码展示:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>void RunChild()
{int cnt = 5;while(cnt){printf("I am child process,pid:%d,ppid:%d\n",getpid(),getppid());sleep(1);cnt--;}
}
int main()
{printf("I am a father process,pid:%d,ppid:%d\n",getpid(),getppid());pid_t id = fork();if(id == 0){RunChild();printf("child quit...\n");exit(123);}sleep(6);pid_t rid = waitpid(-1,NULL,0);//可以匹配任何子进程sleep(2);printf("father quit...\n");return 0;
}

当waitpid函数以 waitpid(-1,NULL,0) 进行传参,效果跟wait是一样的,都是对任意一个进程等待。 

4. 获取子进程status
  • wait和waitpid,都有一个status参数,该参数是一个输出型参数,由操作系统填充。
  • 如果传递NULL,表示不关心子进程的退出状态信息。
  • 否则,操作系统会根据该参数,将子进程的退出信息反馈给父进程。
  • status不能简单的当作整形来看待,可以当作位图来看待,具体细节如下图(只研究status低16比特位)

 代码演示:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>void RunChild()
{int cnt = 5;while(cnt){printf("I am child process,pid:%d,ppid:%d\n",getpid(),getppid());sleep(1);cnt--;}
}
int main()
{printf("I am a father process,pid:%d,ppid:%d\n",getpid(),getppid());pid_t id = fork();if(id == 0){RunChild();printf("child quit...\n");exit(123);}sleep(6);// fatherint status = 0;pid_t rid = waitpid(id,&status,0);if(rid > 0){printf("wait sucess,pid:%d\n",rid);}else{printf("wait failed!\n");}sleep(2);printf("father quit...,child quit code :%d,child signal code:%d\n",(status>>8)&0xFF,status & 0x7F);return 0;
}

 5.阻塞等待

前面我们写的都是阻塞等待的代码,但是都是我们自己手动判断结果,此处还有两个宏,下面使用宏来演示阻塞等待的代码。

(status >> 8) & 0xFF 和 (status & 0x7F) 这两个位运算难记,系统还提供了两个宏来简化代码

WIFEXITED(status) 判断进程退出情况,当宏为真时,表示进程正常退出
WEXITSTATUS(status) 相当于 (status >> 8) & 0xFF,直接获取退出码

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>void RunChild()
{int cnt = 5;while(cnt){printf("I am child process,pid:%d,ppid:%d\n",getpid(),getppid());sleep(1);cnt--;}
}int main()
{printf("I am father process,pid:%d,ppid:%d\n",getpid(),getppid());pid_t id = fork();if(id == 0){// child RunChild();  printf("child quit...\n");exit(123);}sleep(6);// fatherint status = 0;pid_t rid = waitpid(id,&status,0);if(rid > 0){if(WIFEXITED(status))printf("child quit success,child exit code:%d\n",WEXITSTATUS(status));else printf("child quit unnormal!\n");printf("wait success\n");}else {printf("wait failed!\n");}sleep(3);printf("father quit, status: %d, child quit code : %d, child quit signal: %d\n", status, (status>>8)&0xFF, status & 0x7F);return 0;
}

6.非阻塞等待

非阻塞等待就是父进程到达waitpid等待函数处,如果子进程还在运行,那么父进程先不等子进程退出,直接往下面运行,通过while循环实现轮询检查,这样一来使得等待子进程退出的时间,父进程可以做自己的事情,实现了非阻塞等待。

 wait函数只能是一个阻塞等待的函数,如果子进程没有退出,父进程就会在一直在wait调用处等待子进程。

只有 waitpid函数,通过options 参数,可以实现非阻塞等待

//options 参数
WNOHANG

//比如
waitpid(id, &status, WNOHANG);

 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若正常结束,则返回该子进程的ID。 

waitpid() 函数返回值

  • pid_t > 0 : 等待成功,子进程退出,并且父进程回收成功。
  • pid_t = 0 : 检测成功,但是子进程没有退出,需要下一次进行重复等待。
  • pid_t < 0 : 等待失败。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h> //进程等待相关函数头文件int main()
{//演示 waitpid()pid_t id = fork();  //创建子进程if(id == 0){int time = 9;int n = 0;while(n < time){printf("我是子进程,我已经运行了:%d秒 PID:%d   PPID:%d\n", n + 1, getpid(), getppid());sleep(1);n++;}exit(244);  //子进程退出}int status = 0; //状态pid_t ret = 0;while(1){ret = waitpid(id, &status, WNOHANG); //参数3 设置为非阻塞状态if(ret == -1){printf("进程等待失败!进程不存在!\n");break;}else if(ret == 0){ printf("子进程还在运行中!\n");printf("我可以干一些其他任务\n");sleep(3);}else{printf("进程等待成功,子进程已被回收\n");//通过 status 判断子进程运行情况if(WIFEXITED(status)){printf("子进程正常退出,退出码:%d\n", WEXITSTATUS(status));break;}else{printf("子进程异常退出,code dump:%d   退出信号:%d\n", (status >> 7) & 1, (status & 0x7F));break;}}}return 0;
}


文章转载自:

http://9QZ54mR4.fgLxh.cn
http://pQvUrJQ0.fgLxh.cn
http://zQqVhzbi.fgLxh.cn
http://qOzg97O0.fgLxh.cn
http://tR8KZGsz.fgLxh.cn
http://7EokBQ52.fgLxh.cn
http://gOcUOI4j.fgLxh.cn
http://UDqJReTd.fgLxh.cn
http://3cjUm3Gz.fgLxh.cn
http://iboXHKqB.fgLxh.cn
http://mr3Ej6Sa.fgLxh.cn
http://vRBTF92s.fgLxh.cn
http://QSZ1QQTm.fgLxh.cn
http://EvVsSfrm.fgLxh.cn
http://BJ6H2D6e.fgLxh.cn
http://7uiJnHhu.fgLxh.cn
http://sdPCidvA.fgLxh.cn
http://nADwHVU3.fgLxh.cn
http://TqJGXete.fgLxh.cn
http://u0c8OSWX.fgLxh.cn
http://UElop6yI.fgLxh.cn
http://xuVKZhaA.fgLxh.cn
http://hYbTBtGx.fgLxh.cn
http://0aiLVyhx.fgLxh.cn
http://BfBlFM7D.fgLxh.cn
http://ZVLnyW6t.fgLxh.cn
http://Pxo9uvgn.fgLxh.cn
http://FvdnmaFp.fgLxh.cn
http://u50CHfxn.fgLxh.cn
http://qwVovJmz.fgLxh.cn
http://www.dtcms.com/wzjs/679782.html

相关文章:

  • 做h5的图片网站中国免费网站服务器免费下载
  • 网站开发维护合同样板网站设计 培训
  • 深圳专门网站建设延安网站建设报价
  • 国内老牌的网站制作cloudflare cdn
  • wp建站优化内蒙古自治区建设厅官方网站
  • 建设网站需要会什么从做网站可以用现在的名称吗
  • 沈阳网官方网站多个网站一个域名
  • 黄冈网站推广在线观看wordpress 发布
  • 可以做签名链接的网站完整网页制作教程
  • 网站建设太金手指六六十八园区网络建设方案
  • app模板网站兰州公司做网站
  • 做网站框架张家口建设厅官方网站
  • 宜春做网站公司潍坊网站建设熊掌号
  • 珠海网站建站模板网站建设问题整改报告
  • 零基础网站建设入门到精通视频教程重庆模板网站建站
  • 门户网站什么意思举例子东城企业网站开发
  • 找网络公司做网站要注意这4个细节适合做网站背景的图片
  • 襄阳做网站多少钱英文外贸发布网站
  • 网站建设中 模板 下载网站维护的具体问题
  • 福田企业建站推广服务公司长沙天津网站建设
  • 建设银行 福建分行招聘网站网站建设员岗位职责
  • 公司网站制作教学二手交易网站开发的
  • 物流网站的建设方案固定ip做网站路由设置
  • 网站添加微信支付功能网站技术解决
  • 网站域名及空间购买河北邯郸有什么好玩的地方
  • wordpress网站微信支付北湖区网站建设
  • 可以自己买个服务器做网站吗seo优化一般优化哪些方面
  • 做网站购买域名网站建设期的网站案例
  • 电商网站建设解决方案慈溪企业网站seo
  • 建设银行光明支行网站企业建站用什么软件