fopen multi-threading
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > fopen multi-threading




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    fopen multi-threading  
soren juhu


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-22-05 07:52 AM

Hi all,


I have a multi threaded application in which multiple threads are
running and are moving files from one directory to another directory.
This involves two fopen calls, one to open the existing file in one
directory and another to open a new file in the destination directory.
I have found that sometimes one existing file and one new file in
different threads have same file descriptor. Is this possible ?

TIA
Soren.





[ Post a follow-up to this message ]



    Re: fopen multi-threading  
Christian Panten


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-22-05 07:52 AM

soren juhu wrote:

> Hi all,
>
>
> I have a multi threaded application in which multiple threads are
> running and are moving files from one directory to another directory.
> This involves two fopen calls, one to open the existing file in one
> directory and another to open a new file in the destination directory.
> I have found that sometimes one existing file and one new file in
> different threads have same file descriptor. Is this possible ?

I'm not really sure whether the function fopen is thread-safe. But I have
two solutions you can try.
1. Use the function open() instead of fopen(). open() is one of the
reentrant functions declared in the POSIX.1 standard.
2. You can protect the opening of the files by a mutex. Only one thread can
lock a mutex at the same time. If an other thread wants to lock a locked
mutex it has to wait until the mutex is unlocked. You should use mutexes
only for critical code in which the program does not need much time.

Best regards
Christian





[ Post a follow-up to this message ]



    Re: fopen multi-threading  
Michael B Allen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-22-05 07:52 AM

In article <43845182.0502212112.13a8238a@posting.google.com>,
sacjuhu2002@yahoo.com.sg (soren juhu) wrote:

> Hi all,
>
>
> I have a multi threaded application in which multiple threads are
> running and are moving files from one directory to another directory.
> This involves two fopen calls, one to open the existing file in one
> directory and another to open a new file in the destination directory.
> I have found that sometimes one existing file and one new file in
> different threads have same file descriptor. Is this possible ?

No. Multiple threads share the same file table so it's not possible for
two files opened by the same process to have the same descriptor value.

I suspect your diagnostic method has a concurrency flaw. For example the
moment a file is closed it is possible (and likely) that that value will
be returned by a subsequent open call.

Mike





[ Post a follow-up to this message ]



    Re: fopen multi-threading  
Casper H.S. Dik


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-22-05 07:52 AM

sacjuhu2002@yahoo.com.sg (soren juhu) writes:

>I have a multi threaded application in which multiple threads are
>running and are moving files from one directory to another directory.
>This involves two fopen calls, one to open the existing file in one
>directory and another to open a new file in the destination directory.
>I have found that sometimes one existing file and one new file in
>different threads have same file descriptor. Is this possible ?

No; (well, I'm assuming you close the files so it may just
appear that they have the same file descriptor)

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:38 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register