prime_main v0.3.9

让输入信息更加简洁易懂了:
# ./prime_main
step:115
step:116
step:117
step:118
step:119
step:120
step:121
step:122
step:123
step:124
step:125
Parent: Calling Child to finish job
Child:  Saving step data before exit
Child:  Saved!
Parent: All job finish
Parent: Exiting
Child:  Exiting

以下是程序:

#!/usr/bin/perl -w
#use strict;
# file:prime_main
# main program of prime_project.
# by:leon 05/10/26 v0.3.9

use Socket;
use IO::Handle;
use read_write_db qw(read_db write_db);
use cal_prime qw(cal_prime_by_step);

# Bidirectional communication using socketpair
socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or  die "socketpair: $!";
CHILD->autoflush(1);
PARENT->autoflush(1);

unless ($pid = fork) {
    # This is Child,for calculate prime
    die "cannot fork: $!" unless defined $pid;
    close CHILD;

    # Child ignore "ctrl+c"
    $SIG{INT} = ‘IGNORE’;

    # Read date from database for prepare calculate
    local (@prime_n_array,$prime_step_id,@new_prime_id_array,@new_prime_n_array);
    read_db(*prime_n_array,*prime_step_id);

    while (1) {
        print PARENT "go_on?\n";
        chomp($line = <PARENT>);

        if ($line eq "go_on") {
            # Parent didn’t catch "ctrl+c" so we go on next step calculate

            #$i = 0;
            #while (++$i<150) {
            # Go into a calculate prime step
            print "step:$prime_step_id\n";
            cal_prime_by_step(*prime_n_array,*prime_step_id,*new_prime_id_array,*new_prime_n_array);
            #print "new_id:    @new_prime_id_array\n";
            #print "new_n:    @new_prime_n_array\n";
            write_db(*new_prime_id_array,*new_prime_n_array,*prime_step_id);
            #}
            #last;
        } elsif ($line eq "exit") {
            # Parent catch "ctrl+c" so we stop
            print "Child:    Saving step data before exit\n";
            write_db(*new_prime_id_array,*new_prime_n_array,*prime_step_id);
            print "Child:    Saved!\n";
            print PARENT "child_finish\n";
            last;
        } else {
            # For Extensions
            print "Child:    I don’t understand What Parent say.\n";
            exit;
        }
    }

    print "Child:    Exiting\n";

    close PARENT;
    exit;
} else {
    # This is Parent,for control Child
    close PARENT;

    # Catch ctrl+c,but don’t exit directly.Parent send a msg
    # to Child then exit after Child finish ending work
    $SIG{INT} = sub {$int_count++};

    # When Child finish a step and ask Parent if go on or do something else
    # Parent responses a answer
    while (chomp($line = <CHILD>)) {
        # Parent catch "ctrl+c" and ask Child to exit.
        if ($int_count) {
            # Now we should ignore ctrl+c!
            $SIG{INT} = ‘IGNORE’;

            # Do some ending job
            print "Parent:    Calling Child to finish job\n";
            print CHILD "exit\n";
            chomp($line = <CHILD>);
            if ($line eq "child_finish") {
                print "Parent:    All job finish\n";
                print "Parent:    Exiting\n";
                last;
            } else {
                print "Parent:    I don’t understand What Child say.\n";
            }
        }

        if ($line eq "go_on?") {
            print CHILD "go_on\n";
        } else {
            # For Extensions
            print "Parent:    I don’t understand What Child say.\n";
        }
    }

    close CHILD;
    waitpid($pid,0);
}

Leave a Reply