# From /etc/security/limits:
# Sizes are in multiples of 512 byte blocks
# fsize - soft file size in blocks
# To test this program, I set block size via:
# ulimit -f 900
use strict;
use warnings;
$| = 1;
my $bsize = 512;
my $ulimit = `ulimit -f`;
chomp $ulimit;
print "ulimit=$ulimit blocks (block size=$bsize)\n";
my $fname = 'f.tmp';
open my $fh, '>', $fname or die "error: open '$fname': $!";
my $niter = 1000;
my $size = 0;
my $block = 'a' x $bsize;
for my $i (0..$niter) {
$size += $bsize;
print "$i: $size bytes written\n" if $i % 100 == 0;
print $fh $block or die "$i: error in print: $!";
}
close $fh or die "error: close: $!";
After issuing the command:
ulimit -f 900
running the above program f.pl produced the following output:
% perl f.pl
ulimit=900 blocks (block size=512)
0: 512 bytes written
100: 51712 bytes written
200: 102912 bytes written
300: 154112 bytes written
400: 205312 bytes written
500: 256512 bytes written
600: 307712 bytes written
700: 358912 bytes written
800: 410112 bytes written
900: 461312 bytes written
904: error in print: A file cannot be larger than the value set by uli
+mit. at f.pl line 22.
Update: The reason for the program stopping at 904,
rather than 900, is user-space buffering by stdio.
To verify that it stops at 900 with user-space buffering
switched off, I adjusted the above program by adding a
call to autoflush right after the
output file is opened.
Running this adjusted program:
use strict;
use warnings;
use IO::Handle; # <-- Added this line for autoflush
$| = 1;
my $bsize = 512;
my $ulimit = `ulimit -f`;
chomp $ulimit;
print "ulimit=$ulimit blocks (block size=$bsize)\n";
my $fname = 'f.tmp';
open my $fh, '>', $fname or die "error: open '$fname': $!";
$fh->autoflush(); # <-- Added this line to make print autoflush
my $niter = 1000;
my $size = 0;
my $block = 'a' x $bsize;
for my $i (0..$niter) {
print "$i: $size bytes written\n" if $i % 100 == 0;
print $fh $block or die "$i: error in print: $!";
$size += $bsize; # <-- Moved this line to after successful print
}
close $fh or die "error: close: $!";
produced:
ulimit=900 blocks (block size=512)
0: 0 bytes written
100: 51200 bytes written
200: 102400 bytes written
300: 153600 bytes written
400: 204800 bytes written
500: 256000 bytes written
600: 307200 bytes written
700: 358400 bytes written
800: 409600 bytes written
900: 460800 bytes written
900: error in print: A file cannot be larger than the value set by uli
+mit. at f.pl line 23.
Note further that the above was run with the korn shell (ksh).
The ulimit command is a shell builtin and the block size,
on aix at least, seems to be 512 bytes for ksh and 1024 bytes
for bash, as shown below:
% type ulimit
ulimit is a shell builtin.
% ulimit -f 900
% ulimit -f
900
% ksh -c 'ulimit -f'
900
% bash -c 'ulimit -f'
450
|