Quoth "sonet" <>:
> Will the win2003 server lock the directory when create and
> read difference files at the same time?
If I have understood your question correctly (will Windows prevent you
from reading a file if a different file in the same directory is open
for writing) then the answer is no. Windows does place a lock on any
file that is open, so if another process is holding *the same* file open
for writing your read (or rather, I would expect, your open) will fail.
> my $JobFileLocation='c:/pjob.sp';
Are you really locating files in the root of the C drive? This is not a
good idea.
> open( my $JobFile, $JobFileLocation );
Use three-arg open (it's safer if the file has an odd name.
Always check if open succeeded.
open( my $JobFile, '<', $JobFileLocation)
or die "can't open '$JobFileLocation': $!";
You don't need those parens, but if you feel happier with them there
that's fine.
> binmode $JobFile;
> $contents = join('',<$JobFile>);
This is an inefficient way of reading a whole file. Perl will first
split the file into lines and then join them back together again.
Setting $/ to undef is better, and better still would be to use the
File::Slurp module from CPAN.
> close($JobFile);
>
> Sometime the line 4 will block. And cpu speed up to 25%(The
> system have 4 cpu). The process stop at line 4 and the system
> is hang.
How do you know it stops at line 4? If that is not the code you are
running, please post your actual code. It prevents people from wasting
time chasing the wrong problem.
> $/=undef;
> $contents = <$JobFile>;
>
> The second method read file is seem fast than the ftrst one.
It will be faster, but I doubt if it would be enough faster that you'd
notice it.
> Need i read files using nonblocking mode? And how to read
> a file in nonblocking mode ?
use Fcntl;
my $flags = fcntl $JobFile, F_GETFL;
fcntl $JobFile, $flags | O_NONBLOCK
or die "can't set non-blocking mode: $!";
or, if you prefer,
use IO::Handle;
$JobFile->blocking(0)
or die "can't set non-blocking mode: $!";
You don't want to do this, though: as a rule, non-blocking mode doesn't
apply to ordinary files, and in any case you *want* to block until all
the data has been read.
Ben
--
#!/bin/sh
quine="echo 'eval \$quine' >> \$0; echo quined"
eval $quine
# []
|