在Ubuntu中的wait()函数

在Ubuntu中的wait()函数,第1张

概述在Ubuntu中的wait()函数

我正在学习Ubuntu中的进程及其行为,但在wait()中有点困惑。 所以我的问题是:

如何将statment while(wait(NulL)> 0); 工作中?

wait()中的NulL的目的是什么?

我已经看到terminal中的输出,但是父母正在执行并且让孩子执行wait()函数。 父执行不应该停止? 这里是代码:

int main(int argc,char *argv[]) { pID_t childID; if(argc!=2) { printf("Please provIDe an argument in terminal! n"); return 0; } int i,n=atoi(argv[1]); for(i=0;i<n;i++) { childID=fork(); if(childID==0) { printf("InsIDe Child process! n My ID is %dn",getpID()); break; // creating fan process structure } else if(childID>0) { printf("InsIDe Parent Process! n My ID is %dn",getpID()); } } while(wait(NulL)>0); printf("After While Statment!n My ID is %dn My parent ID is %dn Child ID is %dn",getpID(),getppID(),childID);

}

我知道这是一个非常蹩脚的问题,但这是人们学习的方式:)

实时追踪日志摘录

稍后执行脚本的命令

如何杀死僵尸进程使用wait()

有没有办法使PowerShell等待安装完成?

WaitpID块永远

谢谢

如何使主窗口等待,直到一个新打开的窗口closures在C#WPF中?

如何在linux中高效地等待RS232的CTS或DSR?

如何使用linux`perf`工具来生成“Off-cpu”configuration文件

僵尸程序在父母去世之后在哪里?

为什么wait4()被waitpID()取代

如何将statment while(wait(NulL)> 0); 工作中?

函数wait ,等待任何子进程的终止,如果成功,则返回终止子进程的标识符。如果没有子进程,则返回-1。

在你的代码中,父进程基本上等待所有已经创建的子进程的终止。

对于任何一个孩子来说,这个调用将立即返回-1,因为他们还没有创建任何进程。

wait()中的NulL的目的是什么? 如果你看到等待的原型,就是这样,

pID_t wait(int *status);

父进程可以获取变量状态下的子进程的退出状态(我们需要传递wait函数中的整数地址,更新该整数),然后使用WEXITSTATUS宏来提取该值。

通过NulL(我们通过NulL,因为变量是指针类型)的意思是,程序员对这个值不感兴趣,他正在通知这个等待调用。

我稍微修改了你的代码,解释了等待函数的返回值和使用非NulL参数。 所有的孩子现在正在返回一个值(循环索引我),这是由父母提取。

PL。 看看这是否有帮助,

#include<stdio.h> #include<sys/types.h> #include<sys/wait.h> #include<stdlib.h> int main(int argc,char *argv[]) { pID_t childID; int ret; int status; if(argc!=2) { printf("Please provIDe an argument in terminal! n"); return 0; } int i,n=atoi(argv[1]); for(i=0;i<n;i++) { childID=fork(); if(childID==0){ printf("InsIDe Child process! n My ID is %dn",getpID()); break; // creating fan process structure } else if(childID > 0){ printf("InsIDe Parent Process! n My ID is %dn",getpID()); } } //The line below,will be immediatly false for all the children while ((ret= wait(&status))>0) { printf("After While My ID is %d and my child with pID =%d exiting with return value=%dn",ret,WEXITSTATUS(status)); } //The if below will be true for the children only if ((0 > ret) && (childID == 0)){ printf("Child with ID %d exiting and my parent's ID is %dn",getppID()); exit(i); } else{ printf("Parent Finally Exitingn"); } }

总结

以上是内存溢出为你收集整理的在Ubuntu中的wait()函数全部内容,希望文章能够帮你解决在Ubuntu中的wait()函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1232134.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存