All of lore.kernel.org
 help / color / mirror / Atom feed
From: htmldeveloper@gmail.com (Peter Teoh)
To: kernelnewbies@lists.kernelnewbies.org
Subject: thread concurrent file operation
Date: Thu, 7 Feb 2013 21:49:51 +0800	[thread overview]
Message-ID: <CAHnt0GWeic+mNTxOgq9MoTaP8AXcy8gCvPP=hHhN1tqfpghrRA@mail.gmail.com> (raw)
In-Reply-To: <CAHnt0GVgtBz4a1MNubSKxb+Y2xBCmLUav4HxmvJ6n9iiOAU_vg@mail.gmail.com>

To generalize further u can safely say that all synchronous operation have
to be thread-safe, except for some APIs as listed here:

http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_09.html

linux kernel may guarantee thread-safety - but this only apply to
serializing data at the per-syscall level.   Ie, every read() will
complete, before being intercepted by another read() from another thread.
But at the file level u still may get file corruption/file datastructure
mangled if u mixed write/read without properly serialization at the
userspace level.   thus, kernel locking + userspace locking are needed -
for different purpose.

below discussion is useful (first answer esp):

http://stackoverflow.com/questions/5268307/thread-safety-of-read-pread-system-calls

in the kernel for each file descriptor, there is only one single offset
value to indicate the current file pointer position.   so at the userspace
level, different read/write combination will affect the file pointer value
- which explained also why userspace locking (for logical reasons) are
needed.

On Thu, Feb 7, 2013 at 6:23 PM, Peter Teoh <htmldeveloper@gmail.com> wrote:

> Multiple concurrent write() by different thread is possible, as they all
> can share the same file descriptor in a single similar process, and this is
> not allowed.   So nevertheless, the problem you posed is not
> allowed/acceptable by the kernel, so Linus himself fixed it:
>
> See here:
>
> http://lwn.net/Articles/180387/
>
> And Linus patch:
>
> http://lwn.net/Articles/180396/
>
> but my present version (3.2.0) has rcu lock over it (higher performance):
>
>         INIT_LIST_HEAD(&f->f_u.fu_list);
>         atomic_long_set(&f->f_count, 1);
>         rwlock_init(&f->f_owner.lock);
>         spin_lock_init(&f->f_lock);
>         eventpoll_init_file(f);
>         /* f->f_version: 0 */
>
>
>  On Thu, Feb 7, 2013 at 4:44 PM, Karaoui mohamed lamine <
> moharaka at gmail.com> wrote:
>
>>
>> Tahnks guys!
>>
>> 2013/1/30 Karaoui mohamed lamine <moharaka@gmail.com>
>>
>>> thanks, i think i get it.
>>>
>>> 2013/1/30 <Valdis.Kletnieks@vt.edu>
>>>
>>> On Tue, 29 Jan 2013 20:16:26 +0100, you said:
>>>>
>>>> > Actually my question is :
>>>> > Does POSIX specifies  the fact that we need to use "lockf" to be able
>>>> to do
>>>> > read/write operation in different offset ? Is'n the kernel supposed to
>>>> > ensure this ?
>>>>
>>>> If you have non-overlapping writes, the kernel will eventually sort it
>>>> out
>>>> for you.  If your writes overlap, you'll have to provide your own
>>>> locking
>>>> via lockf() or similar, and synchronization via other methods.
>>>>
>>>
>>>
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> Kernelnewbies at kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>>
>>
>
>
> --
> Regards,
> Peter Teoh
>



-- 
Regards,
Peter Teoh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130207/933276dc/attachment.html 

  parent reply	other threads:[~2013-02-07 13:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-29 15:35 thread concurrent file operation Karaoui mohamed lamine
2013-01-29 15:56 ` Tobias Boege
2013-01-29 16:55   ` Valdis.Kletnieks at vt.edu
2013-01-29 17:25   ` Karaoui mohamed lamine
2013-01-29 21:38     ` Valdis.Kletnieks at vt.edu
2013-01-29 16:53 ` Valdis.Kletnieks at vt.edu
     [not found]   ` <CAEEuMqcZFKPnmhbqyzQQJ4DB+tLN=Pbd5fi2oq4qYOeKG6bU7A@mail.gmail.com>
     [not found]     ` <17948.1359482378@turing-police.cc.vt.edu>
     [not found]       ` <CAEEuMqfdTHrYcvFNV72qz9mrw+2b=ABno5tcr0fjY4EAaOkZ-w@mail.gmail.com>
     [not found]         ` <14179.1359507973@turing-police.cc.vt.edu>
     [not found]           ` <CAEEuMqfqgmQr6NA=Akbg9Z48RFNSa5r9bVW7AYH88OEdmJtTwg@mail.gmail.com>
2013-02-07  8:44             ` Karaoui mohamed lamine
2013-02-07 10:23               ` Peter Teoh
2013-02-07 13:49                 ` Karaoui mohamed lamine
2013-02-07 13:49                 ` Peter Teoh [this message]
2013-02-06 22:58 ` Jimmy Pan
2013-02-07  2:41 ` Peter Teoh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHnt0GWeic+mNTxOgq9MoTaP8AXcy8gCvPP=hHhN1tqfpghrRA@mail.gmail.com' \
    --to=htmldeveloper@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.