-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockarea.pl
165 lines (132 loc) · 4.02 KB
/
lockarea.pl
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/perl -w
# lock area - demo record locking with fcntl
use strict;
my $FORKS = shift || 1;
my $SLEEP = shift || 1;
use Fcntl;
use POSIX qw(:unistd_h);
use Errno;
my $COLS = 80;
my $ROWS = 23;
open(FH, "+> /tmp/lkscreen") or die $!;
select(FH);
$| = 1;
select STDOUT;
# clear screen
for (1 .. $ROWS) {
print FH " " x $COLS, "\n";
}
my $progenitor = $$;
fork() while $FORKS-- > 0;
print "hello from $$\n";
if ($progenitor == $$) {
$SIG{INT} = \&infanticide;
} else {
$SIG{INT} = sub { die "goodbye from $$" };
}
while (1) {
my $line_num = int rand($ROWS);
my $line;
my $n;
seek(FH, $n = $line_num * ($COLS + 1), SEEK_SET) or next;
my $place = tell(FH);
my $him;
next unless defined ($him = lockplace(*FH, $place, $COLS));
read(FH, $line, $COLS) == $COLS or next;
my $count = ($line =~ /(\d+)/) ? $1 : 0;
$count++;
seek(FH, $place, 0) or die $!;
my $update = sprintf($him ? "%6d: %d ZAPPED %d" : "%6d: %d was just here", $count, $$, $him);
my $start = int(rand($COLS - length($update)));
die "XXX"if $start + length($update) > $COLS;
printf FH "%*.*s\n", -$COLS, $COLS, " " x $start - $update;
unlockplace(*FH, $place, $COLS);
sleep $SLEEP if $SLEEP;
}
due "NOT REACHED";
# lock ($handle, $offset, $timeout) - get an fcntl lock
sub lockplace {
my ($fh, $start, $till) = @_;
my $lock = struct_flock(F_WRLCK, SEEK_SET, $start, $till, 0);
my $blocker = 0;
unless (fcntl($fh, F_SETLK, $lock)) {
die "F_SETLK $$ @_ : $!" unless $!{EAGAIN} || $!{EDEADLK};
fcntl($fh, F_GETLK, $lock) or die "F_GETLK $$ @_: $!";
$blocker = (struct_fluck($lock))[-1];
$lock = struct_flock(F_WRLCK, SEEK_SET, $start, $till, 0);
unless (fcntl($fh, F_SETLKW, $lock)) {
warn "F_SETLKW $$ @_: $!\n";
return; # undef
}
}
return $blocker;
}
# unlock($handle, $offset, $timeout) - release fcntl lock
sub unlockplace {
my ($fh, $start, $till) = @_;
my $lock = struct_flock(F_UNLCK, SEEK_SET, $start, $till, 0);
fcntl($fh, F_SETLK, $lock) or die "F_UNLCK $$ @_: $!";
}
BEGIN {
my $FLOCK_STRUCT = "s s l l i";
sub linux_flock {
if (wantarray) {
my ($type, $whence, $start, $len, $pid) = unpack($FLOCK_STRUCT, $_[0]);
} else {
my ($type, $whence, $start, $len, $pid) = @_;
return pack($FLOCK_STRUCT, $type, $whence, $start, $len, $pid);
}
}
}
BEGIN {
my $FLOCK_STRUCT = "s s l l s s";
sub sunos_flock {
if (wantarray) {
my ($type, $whence, $start, $len, $pid, $xxx) = unpack($FLOCK_STRUCT, $_[0]);
return ($type, $whence, $start, $len, $pid);
} else {
my ($type, $whence, $start, $len, $pid) = @_;
return pack($FLOCK_STRUCT, $type, $whence, $start, $len, $pid, 0);
}
}
}
BEGIN {
my $FLOCK_STRUCT = "ll ll i s s";
sub bsd_flock {
if (wantarray) {
my ($xxstart, $start, $xxlen, $len, $pid, $type, $whence) = unpack($FLOCK_STRUCT, $_[0]);
return ($type, $whence, $start, $len, $pid);
} else {
my ($type, $whence, $start, $len, $pid) = @_;
my ($xxstart, $xxlen) = (0,0);
return pack($FLOCK_STRUCT, $xxstart, $start, $xxlen, $len, $pid, $type, $whence);
}
}
}
BEGIN {
for ($^O) {
*struct_flock = do {
/bsd/ && \&bsd_flock
||
/linux/ && \&linux_flock
||
/sunos/ && \&sunos_flock
||
die "unknown operating system $%^O, bailing out";
};
}
}
BEGIN {
my $called = 0;
sub infanticide {
exit if $called++;
print "$$: Time to die, kiddies.\n" if $$ == $progenitor;
my $job = getpgrp();
$SIG{INT} = "IGNORE";
kill -2, $job if $job;
1 while wait > 0;
print "$$: My turn\n" if $$ == $progenitor;
exit;
}
}
END { &infanticide }