如何让所有的 fork(ing) 子进程同时执行?

发布于 2024-10-21 04:37:08 字数 1468 浏览 1 评论 0原文

我有一个进程,我希望主线程运行一个循环并生成 n 个子线程,所有子线程都应休眠 j 秒,然后(或多或少)同时醒来并执行其操作。

我的代码看起来像这样 [根据 Dre 的请求编辑]:

 #THE IDEA HERE IS SOMETIMES I WANT TO HOLD THE 
SMS DELIVERY FOR $smsDelay number of seconds 
    if($smsDelay){
            my $forkPid = fork();
            if($forkPid){
                next;
            }
            elsif($forkPid == 0){
                                #db connection disappears while children wait, so, need to reconnect (probably not the best way to handle THIS either! :)
                $myDbC = DBI->connect([DBLOGIN STUFF]) or myDie("can not connect to db");
                &logData("WAITING $smsDelay SECONDS TO SEND SMS");
                sleep($smsDelay * 1);
                $t = time();
                print "sending SMS";
                &send_sms_message($userPN, $smsText , $smsCampaignId);
                print "SMS sent";       
                my $smsVerification = &getDeliveryStatus($userPN, '.smslog');       
                &logData("SMS delivery for $userPN, filename:$filename. Status = $smsVerification");
                #save mms, sms, response code in db
                &runSQL([SQL HERE]);
                exit; #this should only exit the fork, not the entire process!
            }
        }else{
            #HERE I WOULD SEND SMS IMMEDIATELY WITHOUT DELAY

        }

我看到的是每个子进程都连续执行,但每个子进程在下一个子进程之间等待 j 秒!这不是我想要的(坦率地说,完全令人困惑)。我做错了什么?

TIA

I have a process where I want the main thread to run through a loop and produce n number of children, all of which should sleep for j seconds and then (more or less) simultaneously wake up and do their thing.

My code looks like this [Edited as per Dre's request]:

 #THE IDEA HERE IS SOMETIMES I WANT TO HOLD THE 
SMS DELIVERY FOR $smsDelay number of seconds 
    if($smsDelay){
            my $forkPid = fork();
            if($forkPid){
                next;
            }
            elsif($forkPid == 0){
                                #db connection disappears while children wait, so, need to reconnect (probably not the best way to handle THIS either! :)
                $myDbC = DBI->connect([DBLOGIN STUFF]) or myDie("can not connect to db");
                &logData("WAITING $smsDelay SECONDS TO SEND SMS");
                sleep($smsDelay * 1);
                $t = time();
                print "sending SMS";
                &send_sms_message($userPN, $smsText , $smsCampaignId);
                print "SMS sent";       
                my $smsVerification = &getDeliveryStatus($userPN, '.smslog');       
                &logData("SMS delivery for $userPN, filename:$filename. Status = $smsVerification");
                #save mms, sms, response code in db
                &runSQL([SQL HERE]);
                exit; #this should only exit the fork, not the entire process!
            }
        }else{
            #HERE I WOULD SEND SMS IMMEDIATELY WITHOUT DELAY

        }

What I'm seeing is that each child is executed in succession, but each one waits j seconds IN BETWEEN the next! This is not what I'm after (and frankly totally confusing). What am I doing wrong?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2024-10-28 04:37:08

他们应该同时醒来,我测试时他们也这样做了。我的代码:

print "parent: ".localtime."\n";
for (1..3) {
    my $pid = fork();
    if ($pid == 0){
       sleep(3);
       print "$: ".localtime."\n";
       exit(0);
    }
}

1 while wait != -1;

Unix:

parent: Wed Mar  9 22:21:27 2011
29757: Wed Mar  9 22:21:30 2011
29755: Wed Mar  9 22:21:30 2011
29756: Wed Mar  9 22:21:30 2011

Windows (fork emulation):

parent: Thu Mar 10 01:19:39 2011
-3836: Thu Mar 10 01:19:42 2011
-4600: Thu Mar 10 01:19:42 2011
-4400: Thu Mar 10 01:19:42 2011

我怀疑问题出在你没有显示的代码中。

They should simultaneously wake up, and they do when I tested. My code:

print "parent: ".localtime."\n";
for (1..3) {
    my $pid = fork();
    if ($pid == 0){
       sleep(3);
       print "$: ".localtime."\n";
       exit(0);
    }
}

1 while wait != -1;

Unix:

parent: Wed Mar  9 22:21:27 2011
29757: Wed Mar  9 22:21:30 2011
29755: Wed Mar  9 22:21:30 2011
29756: Wed Mar  9 22:21:30 2011

Windows (fork emulation):

parent: Thu Mar 10 01:19:39 2011
-3836: Thu Mar 10 01:19:42 2011
-4600: Thu Mar 10 01:19:42 2011
-4400: Thu Mar 10 01:19:42 2011

I suspect the problem is in the code you didn't show.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文