Thread Problem mit fork() (24 answers)
Opened by bianca at 2015-09-05 20:15

bianca
 2015-09-20 07:51
#182269 #182269
User since
2009-09-13
6977 Artikel
BenutzerIn

user image
Danke dir für die Hilfe!

Caching im Ajax war schon drin und der Header führt zumindest bei mir nicht zum erwarteten Verhalten. Mit diesem Header hab ich in jeweils 10 Versuchen im Firefox garnicht und im IE zweimal das erwartete Verhalten gesehen.

Im IE hatte ich sogar mal eine Mischform, die ich bisher noch nie gesehen habe:
Quote
Polling Ergebnisse:
Initialisierung wird ausgeführt...
1442728063 Hier ist der Vater, terminiere jetzt (0 Sekunden)
Polling wird ausgeführt...
***Z-0-1442728065 ***Z-1-1442728066 ***Z-2-1442728067 ***Z-3-1442728068 ***Z-4-1442728069 ***Z-5-1442728070 ***Z-6-1442728071 ***Z-7-1442728072 (0 Sekunden)
Polling wird ausgeführt...
***Z-0-1442728065 ***Z-1-1442728066 ***Z-2-1442728067 ***Z-3-1442728068 ***Z-4-1442728069 ***Z-5-1442728070 ***Z-6-1442728071 ***Z-7-1442728072 ***Z-8-1442728073 ***Z-9-1442728074 (0 Sekunden)
Fertig

Was kann das sein? Noch eine Idee?

Würdest du es bitte auch nochmal testen. Hier das neue Script:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use JSON;
use Fcntl qw(:DEFAULT);
use IO::Handle;
STDOUT->autoflush(1);

my $pfad = '/pfad_zum_cgi_bin/cgi-bin';

my $t = time();

if (defined $ARGV[0]) {
    unlink("$pfad/test_fork.txt");
    if (sysopen(my $fh,"$pfad/test_fork.txt",O_WRONLY|O_APPEND|O_CREAT)) {
        $fh->autoflush(1);
        for (my $z = 0; $z < 10; $z ++) {
            print $fh "***Z-$z-".time()."\n";
            sleep(1);
        }
        close($fh);
    }
    exit();
}

my $cgi = new CGI;
my $parameter = $cgi->param('parameter');

if (!defined $parameter) {
    print STDOUT CGI->new->header(
        -charset        => 'ISO-8859-15',
        -Expires        => 'now',
        -Cache_Control  => 'no-cache, max-age=0',
    ).<<HTML_TEIL
<!DOCTYPE html>
<html>
    <head>
        <title>fork() Fehler</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <style type="text/css">
            #fehler {
                padding-top:    20px;
                color:          red;
                font-weight:    bold;
            }
            #out {
                padding-top:    20px;
            }
        </style>
    </head>
    <body>
        <div id="fehler"><u>Fehler:</u></div>
        <div id="out"><u>Polling Ergebnisse:</u><br></div>
        <script>
            var ini = true;
            function aktualisieren() {
                document.getElementById('out').innerHTML += (ini ? 'Initialisierung' : 'Polling')+' wird ausgeführt...<br>';
                var datapostobj = {
                    parameter: (ini ? 'ini' : 'poll')
                };
                console.log('Start Ajax mit Parameter "'+datapostobj['parameter']+'"');
                jQuery.ajax({
                    url:        '/cgi-bin/test_fork.pl',
                    data:       datapostobj,
                    dataType:   'json',
                    type:       'POST',
                    cache:      false,
                    success:    function(input) {
                        console.log('Ergebnis ist da');
                        document.getElementById('out').innerHTML += (input['text'] != '' ? input['text'] : 'nix')+'<br>';
                        ini = false;
                        if (input['text'].indexOf('Z-9') < 0) {
                            window.setTimeout(function() { aktualisieren(); },1000);
                        }
                        else {
                            document.getElementById('out').innerHTML += 'Fertig<br>';
                        }
                    },
                    error:      function(xhrobj,textStatus,errorThrown) {
                        jQuery('#fehler').html('Verbindungsfehler textStatus: '+textStatus+' errorThrown: '+errorThrown);
                    }
                });
            }

            aktualisieren();
        </script>
    </body>
</html>
HTML_TEIL
;
}
elsif ($parameter eq 'ini') {
    my $pid = fork();

    # Fehler
    if (!defined $pid) {
        print STDOUT CGI->new->header(
            -charset        => 'ISO-8859-15',
            -Expires        => 'now',
            -Cache_Control  => 'no-cache, max-age=0',
        ).time()." Fehler bei fork()\n";
    }

    # Vater
    elsif ($pid) {
        my %out = (text => time().' Hier ist der Vater, terminiere jetzt ('.(time() - $t)." Sekunden)\n");
        print STDOUT CGI->new->header(
            -charset        => 'ISO-8859-15',
            -Expires        => 'now',
            -Cache_Control  => 'no-cache, max-age=0',
        ).JSON->new->encode(\%out);
    }

    # Sohn
    else {
        close STDIN;
        close STDOUT;
        exec("perl $pfad/test_fork.pl kommandozeile");
        exit();
    }
}
elsif ($parameter eq 'poll') {
    my %out = (text => '');
    if (sysopen(my $fh,"$pfad/test_fork.txt",O_RDONLY)) {
        undef local $/;
        $out{text} = <$fh>;
        close($fh);
    }
    else { $out{text} = "Fehler '$!'"; }
    $out{text} .= ' ('.(time() - $t)." Sekunden)\n";
    print STDOUT CGI->new->header(
        -charset        => 'ISO-8859-15',
        -Expires        => 'now',
        -Cache_Control  => 'no-cache, max-age=0',
    ).JSON->new->encode(\%out);
}

Last edited: 2015-09-20 07:53:16 +0200 (CEST)
10 print "Hallo"
20 goto 10

View full thread Problem mit fork()