Hello,

Je suis en train d'apprendre le perl et je ne comprends pourquoi dans ce code que j'ai écris pour tester fork, le père n'écrit jamais que son fils vient de crever.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#!/usr/bin/perl -w
use strict;
use POSIX ":sys_wait_h";
 
my $pid;
my $i=1;
while ($i <= 5) {
	$pid = fork();
	if ($pid != 0) {
	  &father_stuff($pid);
	} else {
	  &child_stuff($i);
	  exit;
	}
	$i++;
}
 
if ($pid != 0) {
	do
	{
                $pid = waitpid(-1,&WNOHANG);
		if ($pid > 0) {
			print ("[FATHER]\t".scalar(localtime())." \tChild <$pid> ended.\n");
		}
	}
	until ($pid == -1);
}
 
sub father_stuff($)
{
	my ($local_pid) = @_;
	print ("[FATHER]\t".scalar(localtime())." \n\tWaiting for my son's <$pid> end...\n");
}
 
sub child_stuff($)
{
	my ($child_number) = @_;
        print ("[CHILD_$child_number]\t".scalar(localtime())." \tEnding here...\n");
}
Merci pour votre aide,
Rithy666.