利用folk函数进行多线程处理 ### perl

for(1..3){
    $id=fork();
    if ($id==0){
        print "in the chid process, the id is $$\n"; ## you can do your sub jobs here
        exit 0;
    }   
    else {
        $wait=waitpid($pid,0)     #### wait for the child process finishing
        print "in the parent process, the id is $$\n";
    }   
}

当child里面的处理需要花费较长时间,而parent进程瞬时的时候,会让child里面的多个线程几乎同时进行,达到了并行处理的目的。但child的进程存在先后关系,这是和多线程并行不一样的地方。

猜你喜欢

转载自blog.csdn.net/tommy_12345/article/details/82346321
今日推荐