Leser: 1
![]() |
![]() |
9 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use strict;
use warnings;
my $pid = open my $CMD, "| sar -c 1 2 \&>/dev/null" or die $!;
while (1) {
open my $STAT,'<',"/proc/$pid/stat" or die $!;
my $state = (split /\s+/, <$STAT>)[2];
close $STAT;
if ($state eq 'Z') {
close $CMD or die $!;
waitpid($pid,0);
last;
}
sleep 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ cat forked.pl
#!/usr/bin/perl
use strict;
use warnings;
open FH, ">forked.dat" or die $!;
print FH "forked $$\n";
close FH;
print STDERR "This should not appear ($$)\n";
$ perl -wle'
if (my $pid = fork()) {
print "parent $$ forked $pid";
}
elsif (defined $pid) {
close STDERR;
exec qw(perl forked.pl);
}
'
parent 29747 forked 29748
$ cat forked.dat
forked 29748
1
2
3
$proc = Proc::Simple->new();
$status = $proc->start("/bin/echo", "hello", "world");
$pid = $proc->pid;
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
use strict;
use warnings;
# die Prozess-IDs habe ich jetzt mal beispielhaft angelegt
my $self = {};
$self->{23} = {};
$self->{64} = {};
$self->{745} = {};
my @pids = ();
# 1. Beispiel
@pids = keys %$self;
while (@pids) {
for my $pid (@pids) {
@pids = grep { !/^$pid$/ } @pids unless -e "/proc/$pid";
}
}
# 2. Beispiel
@pids = keys %$self;
while (@pids) {
for my $i (reverse 0..$#pids) {
splice(@pids, $i, 1) unless -e "/proc/$pids[$i]";
}
}
# 3. Beispiel
$self->{$_}->{run} = 1 for keys %$self;
my $count = keys %$self;
while ($count) {
for my $pid (sort keys %$self) {
next unless $self->{$pid}->{run};
unless (-e "/proc/$pid") {
$count--;
$self->{$pid}->{run} = 0;
}
}
}
![]() |
![]() |
9 Einträge, 1 Seite |