我如何等待子进程? [英] How do I wait for the child process ?

查看:40
本文介绍了我如何等待子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一种情况需要处理以下情况。

main()必须等待孩子完成或main()必须在3秒后杀死

的孩子并退出。


/ *假设每个星期都被宣布为* /


time(& start);

while(childpid!= wait(& status))

{

time(& ;结束);

diff = difftime(结束,开始);

fprintf(stderr," \ n%ld",diff);

if(diff> = 3.00)

{

/ *杀死孩子* /

退出(0);

}

}

}

即使孩子服用的时间超过3秒,但这似乎不是

工作。知道为什么吗?


谢谢。

解决方案

文章< 11 ** ********************@o13g2000cwo.googlegroups .com> ;,

< co ******* @ gmail.com> ;写道:

我有一种情况,我必须处理以下情况。
主()必须等待孩子完成或主()必须杀死
孩子3秒后退出。
/ *假设everythign被宣布为* /
时间(& start);
while(childpid!=等待(& status))


等待是无条件的。

{
时间(& end);
diff = difftime(结束,开始);




所以你所有的计算都有时间,直到你获得状态

报告。这并不等于限制你想要的时间

等待报告。


限制你想等待的时间一份报告,你可能希望

调查闹钟()。


另外,你可能希望考虑到
$下的事实b $ b在某些情况下,你得到的报告不会是

孩子已经完成,而是孩子已经停止

(例如,SIGSTOP,如果用户^ Z''d在一个bourne shell中

衍生物。)

-

我想确保[一个用户]无法通过......在没有点击微软广告的情况下获得在线

体验

- 史蒂夫鲍尔默[微软首席执行官]


2005年3月28日10:57:06 -0800,在comp.lang.c中, co * ******@gmail.com

写道:



我有一个情况我必须处理的地方以下场景。
main()必须等待孩子完成或者main()必须在3秒后杀死孩子并退出。




等待它的朋友不是C的一部分,但通常是

unix操作系统的一部分。你将在comp.unix.programmer中得到更好的回复。


-

马克麦金太尔

CLC FAQ< http ://www.eskimo.com/~scs/C-faq/top.html>

CLC自述文件:< http://www.ungerhu.com/jxh/clc.welcome.txt> ;


---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全面隐私= ----


28 2005年3月19:03:05 GMT,在comp.lang.c,
ro *** ***@ibd.nrc-cnrc.gc.ca (Walter Roberson)写道:

等待是无条件的。


和offtopic在这里。

所以你所有的计算都有时间,直到你获得状态
报告。这与限制你想等待报告的时间完全不同。




你绝对没有办法知道,因为你甚至不知道

wait()对那个人的操作系统做了什么。我授予你,它可能会在unix上做它所做的事情

但是它可能不会。


请将人们重定向到正确的组,并且如果您/必须/

回答,请说明您猜测。

-

Mark McIntyre

CLC常见问题< http://www.eskimo.com/~scs/C-faq/top.html>

CLC自述文件:< http://www.ungerhu.com /jxh/clc.welcome.txt>


---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全使用网新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----


Hi,

I have a situation where I have to handle the following scenario.
The main() must wait for child to complete or the main() must kill the
child after 3 seconds and exit.

/* Assume everythign is declared */

time(&start);
while ( childpid != wait(&status) )
{
time(&end);
diff = difftime(end,start);
fprintf(stderr, "\n%ld",diff);
if (diff >= 3.00)
{
/* Kill child */
exit(0);
}
}
}
Even though the child is taking more than 3 seconds, this doesn''t seem
to work. Any idea why ?

Thanks.

解决方案

In article <11**********************@o13g2000cwo.googlegroups .com>,
<co*******@gmail.com> wrote:

I have a situation where I have to handle the following scenario.
The main() must wait for child to complete or the main() must kill the
child after 3 seconds and exit. /* Assume everythign is declared */ time(&start);
while ( childpid != wait(&status) )
Wait is unconditional.
{
time(&end);
diff = difftime(end,start);



So all you are calculating there is the time until you did get a status
report. That''s not at all the same as limiting the time you want to
wait for a report.

To limit the time you want to wait for a report, you may wish
to investigate alarm().

Also, you may wish to take into account the fact that under
some circumstances, the report you get will not be that the
child has completed, but rather that the child has stopped
(e.g., SIGSTOP such as if the user ^Z''d in a bourne shell
derivative.)
--
"I want to make sure [a user] can''t get through ... an online
experience without hitting a Microsoft ad"
-- Steve Ballmer [Microsoft Chief Executive]


On 28 Mar 2005 10:57:06 -0800, in comp.lang.c , co*******@gmail.com
wrote:

Hi,

I have a situation where I have to handle the following scenario.
The main() must wait for child to complete or the main() must kill the
child after 3 seconds and exit.



wait and its friends are not part of C, but are typically part of the
unix OS. You will get a better response in comp.unix.programmer.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


On 28 Mar 2005 19:03:05 GMT, in comp.lang.c ,
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:

Wait is unconditional.
and offtopic here.
So all you are calculating there is the time until you did get a status
report. That''s not at all the same as limiting the time you want to
wait for a report.



you have absolutely no way to know that, since you don''t even know
what wait() does on that guy''s OS. I grant you, it may do what it does
on unix but then again it may not.

Please, do redirect people to the right group, and if you /must/
answer, make it clear that you''re guessing.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


这篇关于我如何等待子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆