File: 004_pid.t

package info (click to toggle)
libposix-atfork-perl 0.04-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: perl: 1,946; ansic: 64; makefile: 2; sh: 1
file content (42 lines) | stat: -rw-r--r-- 776 bytes parent folder | download | duplicates (2)
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
#!perl
use strict;
use warnings;
use Test::More tests => 10;
use Test::SharedFork;
use POSIX::AtFork qw(:all);
use POSIX qw(getpid);

my $prepare = 0;
my $parent  = 0;
my $child   = 0;

my $parent_pid = $$;

pthread_atfork(
    sub { $prepare++ },
    sub { $parent++; is $$, $parent_pid },
    sub { $child++;  is $$, getpid() },
);

my $pid = Test::SharedFork->fork;
die "Failed to fork: $!" if not defined $pid;

if($pid != 0) {
    is $$, $parent_pid;

    is $prepare, 1, '&prepare in parent';
    is $parent,  1, '&parent in parent';
    is $child,   0, '&child in parent';
    waitpid $pid, 0;
    exit;
}
else {
    is $$, getpid();

    is $prepare, 1, '&prepare in child';
    is $parent,  0, '&parent in child';
    is $child,   1, '&child in child';
    exit;
}