|
Home > Archive > Unix Programming > February 2005 > fopen multi-threading
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
fopen multi-threading
|
|
| soren juhu 2005-02-22, 2: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.
| |
| Christian Panten 2005-02-22, 2: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
| |
| Michael B Allen 2005-02-22, 2: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
| |
| Casper H.S. Dik 2005-02-22, 2: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.
|
|
|
|
|