Re: [fpc-devel] How to use fpFlock?

2008-05-06 Thread ik
flock in Linux (at least) is blocking by default unless passed with
specific parameter.

You can try also to create a mutex that only when removed you will
access the procedure/function that try to write to the file, and the
process that created the mutex is the only one that can write to that
file.
You of course remove it when you closed the file.

Just an idea...

Ido

On Fri, May 2, 2008 at 5:35 AM, amir [EMAIL PROTECTED] wrote:
 Hi,

  I have many process wanting to write in a file. Each process is going to
 open the file as a writeonly (using Assignfile and rewrite or fpOpen with
 o_WrOnly). But there is a risk that two processes simultaneously trying to
 write a message in the file. I want to use fpflock to avoid this. Is it
 possible? What I done is something like this:

  AssignFile (OutputFile, FileName);
  fpflock (OutputFile, LOCK_EX);
  Rewrite (OutputFile);
  ...

  The description of fpFlock does not  say anything about the default
 behavior of fpFlock (Is it blocking?). What I saw told me that it is not
 blocking.

  Is it the correct way of using fpFlock?


  ___
  fpc-devel maillist  -  fpc-devel@lists.freepascal.org
  http://lists.freepascal.org/mailman/listinfo/fpc-devel




-- 
http://ik.homelinux.org/
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


Re: [fpc-devel] How to use fpFlock?

2008-05-06 Thread amir

So what wrong is with the following code?
When running two instance of this program, *both* print After Flock 
and wait for an input?


uses
 Unix;
var
 InputFile: TextFile;

begin
 AssignFile (InputFile, 'Lock.txt');
 WriteLn ('Before Flock');
 Flush (Output);
 Fpflock (InputFile, LOCK_SH);
 WriteLn ('After Flock');
 Flush (Output);

 Reset (InputFile);
 ReadLn;

 CloseFile (InputFile);

end.

ik wrote:

flock in Linux (at least) is blocking by default unless passed with
specific parameter.

You can try also to create a mutex that only when removed you will
access the procedure/function that try to write to the file, and the
process that created the mutex is the only one that can write to that
file.
You of course remove it when you closed the file.

Just an idea...

Ido

On Fri, May 2, 2008 at 5:35 AM, amir [EMAIL PROTECTED] wrote:
  

Hi,

 I have many process wanting to write in a file. Each process is going to
open the file as a writeonly (using Assignfile and rewrite or fpOpen with
o_WrOnly). But there is a risk that two processes simultaneously trying to
write a message in the file. I want to use fpflock to avoid this. Is it
possible? What I done is something like this:

 AssignFile (OutputFile, FileName);
 fpflock (OutputFile, LOCK_EX);
 Rewrite (OutputFile);
 ...

 The description of fpFlock does not  say anything about the default
behavior of fpFlock (Is it blocking?). What I saw told me that it is not
blocking.

 Is it the correct way of using fpFlock?


 ___
 fpc-devel maillist  -  fpc-devel@lists.freepascal.org
 http://lists.freepascal.org/mailman/listinfo/fpc-devel






  


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


Re: [fpc-devel] How to use fpFlock?

2008-05-06 Thread ik
You should use LOCK_EX instead (exclusive lock rather then shared lock).
From the man file (man 2 flock):

  LOCK_SH   Place a shared lock.  More than one process
may hold a shared lock for a given file at a given time.

  LOCK_EX   Place an exclusive lock.  Only one process may
hold an exclusive lock for a given file at a given time.

  LOCK_UN   Remove an existing lock held by this process.

Ido

On Tue, May 6, 2008 at 9:38 AM, amir [EMAIL PROTECTED] wrote:
 So what wrong is with the following code?
  When running two instance of this program, *both* print After Flock and
 wait for an input?

  uses
   Unix;
  var
   InputFile: TextFile;

  begin
   AssignFile (InputFile, 'Lock.txt');
   WriteLn ('Before Flock');
   Flush (Output);
   Fpflock (InputFile, LOCK_SH);
   WriteLn ('After Flock');
   Flush (Output);

   Reset (InputFile);
   ReadLn;

   CloseFile (InputFile);

  end.



  ik wrote:

  flock in Linux (at least) is blocking by default unless passed with
  specific parameter.
 
  You can try also to create a mutex that only when removed you will
  access the procedure/function that try to write to the file, and the
  process that created the mutex is the only one that can write to that
  file.
  You of course remove it when you closed the file.
 
  Just an idea...
 
  Ido
 
  On Fri, May 2, 2008 at 5:35 AM, amir [EMAIL PROTECTED] wrote:
 
 
   Hi,
  
I have many process wanting to write in a file. Each process is going
 to
   open the file as a writeonly (using Assignfile and rewrite or fpOpen
 with
   o_WrOnly). But there is a risk that two processes simultaneously trying
 to
   write a message in the file. I want to use fpflock to avoid this. Is it
   possible? What I done is something like this:
  
AssignFile (OutputFile, FileName);
fpflock (OutputFile, LOCK_EX);
Rewrite (OutputFile);
...
  
The description of fpFlock does not  say anything about the default
   behavior of fpFlock (Is it blocking?). What I saw told me that it is not
   blocking.
  
Is it the correct way of using fpFlock?
  
  
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel
  
  
  
 
 
 
 
 

  ___
  fpc-devel maillist  -  fpc-devel@lists.freepascal.org
  http://lists.freepascal.org/mailman/listinfo/fpc-devel




-- 
http://ik.homelinux.org/
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


Re: [fpc-devel] How to use fpFlock?

2008-05-06 Thread amir
I have tried both of them (LOCK_SH and LOCK_EX). But the result was the 
same.


ik wrote:

You should use LOCK_EX instead (exclusive lock rather then shared lock).
From the man file (man 2 flock):

  LOCK_SH   Place a shared lock.  More than one process
may hold a shared lock for a given file at a given time.

  LOCK_EX   Place an exclusive lock.  Only one process may
hold an exclusive lock for a given file at a given time.

  LOCK_UN   Remove an existing lock held by this process.

Ido

On Tue, May 6, 2008 at 9:38 AM, amir [EMAIL PROTECTED] wrote:
  

So what wrong is with the following code?
 When running two instance of this program, *both* print After Flock and
wait for an input?

 uses
  Unix;
 var
  InputFile: TextFile;

 begin
  AssignFile (InputFile, 'Lock.txt');
  WriteLn ('Before Flock');
  Flush (Output);
  Fpflock (InputFile, LOCK_SH);
  WriteLn ('After Flock');
  Flush (Output);

  Reset (InputFile);
  ReadLn;

  CloseFile (InputFile);

 end.



 ik wrote:



flock in Linux (at least) is blocking by default unless passed with
specific parameter.

You can try also to create a mutex that only when removed you will
access the procedure/function that try to write to the file, and the
process that created the mutex is the only one that can write to that
file.
You of course remove it when you closed the file.

Just an idea...

Ido

On Fri, May 2, 2008 at 5:35 AM, amir [EMAIL PROTECTED] wrote:


  

Hi,

 I have many process wanting to write in a file. Each process is going


to


open the file as a writeonly (using Assignfile and rewrite or fpOpen


with


o_WrOnly). But there is a risk that two processes simultaneously trying


to


write a message in the file. I want to use fpflock to avoid this. Is it
possible? What I done is something like this:

 AssignFile (OutputFile, FileName);
 fpflock (OutputFile, LOCK_EX);
 Rewrite (OutputFile);
 ...

 The description of fpFlock does not  say anything about the default
behavior of fpFlock (Is it blocking?). What I saw told me that it is not
blocking.

 Is it the correct way of using fpFlock?


 ___
 fpc-devel maillist  -  fpc-devel@lists.freepascal.org
 http://lists.freepascal.org/mailman/listinfo/fpc-devel








  

 ___
 fpc-devel maillist  -  fpc-devel@lists.freepascal.org
 http://lists.freepascal.org/mailman/listinfo/fpc-devel






  


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


[fpc-devel] How to use fpFlock?

2008-05-05 Thread amir

Hi,

I have many process wanting to write in a file. Each process is going to 
open the file as a writeonly (using Assignfile and rewrite or fpOpen 
with o_WrOnly). But there is a risk that two processes simultaneously 
trying to write a message in the file. I want to use fpflock to avoid 
this. Is it possible? What I done is something like this:


AssignFile (OutputFile, FileName);
fpflock (OutputFile, LOCK_EX);
Rewrite (OutputFile);
...

The description of fpFlock does not  say anything about the default 
behavior of fpFlock (Is it blocking?). What I saw told me that it is not 
blocking.


Is it the correct way of using fpFlock?


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel