sleep在多进程中的问题
时间:2011-04-03
来源:互联网
本帖最后由 reallyfly_1 于 2011-04-03 19:23 编辑
本想测试一下FORK的使用,写了如下代码,意思是:
1.不断的从父进程创建5个子进程
2.父进程创建完子进程后睡眠3秒
3.睡眠后挂起所有的子进程
但是里面的sleep运行时间总是不对。
sleep的返回值是真正‘睡眠’的时间,但是如下代码在我的机器上返回的总是1。
本人新手,求帮助
#!/usr/bin/perl -w
use strict;
use POSIX 'WNOHANG';
$| = 1;
print "main PID=$$\n";
$SIG{CHLD} = sub{
while((my $kid = waitpid(-1, WNOHANG)) > 0)
{
print "reaped child...\n";
}
};
my $child_1 = 1;
foreach my $i (1..5)
{
if($child_1 > 0)
{
$child_1 = fork();
}
if($child_1 > 0)
{
print "this is father, PID=$$\n";
}elsif($child_1 == 0){
$child_1 = -1;
sleep($i);
print "this is child, PID=$$\n";
}
}
if($child_1 > 0)
{
setpgrp(0,0);
local $SIG{HUP} = 'IGNORE';
print "I am $$, I will kill all, after sleep\n";
my $time = sleep(3);
print "sleep over $time sec, begin\n";
kill('HUP', -$$);
}
本想测试一下FORK的使用,写了如下代码,意思是:
1.不断的从父进程创建5个子进程
2.父进程创建完子进程后睡眠3秒
3.睡眠后挂起所有的子进程
但是里面的sleep运行时间总是不对。
sleep的返回值是真正‘睡眠’的时间,但是如下代码在我的机器上返回的总是1。
本人新手,求帮助
#!/usr/bin/perl -w
use strict;
use POSIX 'WNOHANG';
$| = 1;
print "main PID=$$\n";
$SIG{CHLD} = sub{
while((my $kid = waitpid(-1, WNOHANG)) > 0)
{
print "reaped child...\n";
}
};
my $child_1 = 1;
foreach my $i (1..5)
{
if($child_1 > 0)
{
$child_1 = fork();
}
if($child_1 > 0)
{
print "this is father, PID=$$\n";
}elsif($child_1 == 0){
$child_1 = -1;
sleep($i);
print "this is child, PID=$$\n";
}
}
if($child_1 > 0)
{
setpgrp(0,0);
local $SIG{HUP} = 'IGNORE';
print "I am $$, I will kill all, after sleep\n";
my $time = sleep(3);
print "sleep over $time sec, begin\n";
kill('HUP', -$$);
}
作者: reallyfly_1 发布时间: 2011-04-03
本帖最后由 027xiatian 于 2011-04-05 10:13 编辑
复制代码
elsif的代码执行不到吧(不知道如何标记成红色)
- if($child_1 > 0)
- {
- print "this is father, PID=$$\n";
- }elslif ($child_1 == 0){
- $child_1 = -1;
- sleep($i);
- print "this is child, PID=$$\n";
- }
作者: 027xiatian 发布时间: 2011-04-05
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28