Chinaunix

标题: parallel communication [打印本页]

作者: pgy    时间: 2004-08-13 16:50
标题: parallel communication
I am writing a simple demo program in parallel using fork, it print a global
shared var's value in parent process, then decrease it, I expect the
decrement can be seen in the child process, however, the it does not, I am
looking for someone's advice how to implement this program.

here is my demo program,

use POSIX;
my $v=4;
my $pid=fork();
while ($v) {
    if ($pid) {
         print "the v in parent is ",--$v,"\n";
    }
    else {
         print "the v in child is ", --$v, "\n";
    }
}

the output is something like this,
the v in child is 4
the v in child is 3
the v in child is 2
the v in child is 1
the v in parent is 4
the v in parent is 3
the v in parent is 2
the v in parent is 1

while, I hope the total output should be 4 lines, the v's value is from 1-4
is in random order, maybe.
Could anyone help me?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2