1
# Copyright (C) 2010 Sun Microsystems, Inc. All rights reserved.
2
# Use is subject to license terms.
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 2 of the License.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18
package GenTest::IPC::Process;
23
use if osWindows(), threads;
25
## A Process is a placeholder for an object run in a separate process.
26
## The contract assumes that the objects constructor is run in the
27
## parent process and the fork is done in Process->start and then
28
## obect->run() is invoked.
36
use constant PROCESS_OBJECT => 0;
37
use constant PROCESS_PID => 1;
42
return $class->SUPER::new({
43
'object' => PROCESS_OBJECT},@_);
48
my ($self, @args) = @_;
51
my $thr = threads->create(sub{$self->[PROCESS_OBJECT]->run(@args)});
53
$self->[PROCESS_PID]=$thr->tid();
54
$processes{$thr->tid()} = $self->[PROCESS_OBJECT];
55
say "".(ref $self->[PROCESS_OBJECT])."(".$thr->tid().") started\n";
60
$self->[PROCESS_PID]=$$;
61
$self->[PROCESS_OBJECT]->run(@args);
62
say "".(ref $self->[PROCESS_OBJECT])."($$) terminated normally\n";
65
say "".(ref $self->[PROCESS_OBJECT])."($pid) started\n";
66
$self->[PROCESS_PID] = $pid;
67
$processes{$pid} = $self->[PROCESS_OBJECT];
80
print "".(ref $processes{$pid})."($pid) stopped with status $?\n";
84
map {$pids{$_}=1} @list;
85
while ((keys %pids) > 0) {
88
print "".(ref $processes{$pid})."($pid) stopped with status $?\n";
89
delete $pids{$pid} if exists $pids{$pid};
95
my ($max, @list) = @_;
101
$status = $? if $status < $?;
102
print "".(ref $processes{$pid})."($pid) stopped with status $?\n";
103
last if $status >= $max;
107
map {$pids{$_}=1} @list;
108
while ((keys %pids) > 0) {
111
$status = $? if $status < $?;
112
print "".(ref $processes{$pid})."($pid) stopped with status $?\n";
113
delete $pids{$pid} if exists $pids{$pid};
114
last if $status >= $max;
123
## Not sure yet, but the thread will enevtually die dtogether
124
## with the main program
126
say "Kill ".(ref $processes{$self->[PROCESS_PID]})."(".$self->[PROCESS_PID].")\n";
127
kill(15, $self->[PROCESS_PID]);