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 10:41:45 +0800	[thread overview]
Message-ID: <CAHnt0GUmmVsbFN2mrKxbR-_E76BC4x1shiXscqwyvMxgPat3uA@mail.gmail.com> (raw)
In-Reply-To: <CAEEuMqf5cH-bVYSJd0MWTvxSYPwijX6f9GuYc6HnsauD5MK5PQ@mail.gmail.com>

in ANY updates/changes, locking is always needed, to prevent multiple
parties from updating at the same time.   but there is another way:
 lockless updates.   one form done in linux kernel is called RCU:

http://en.wikipedia.org/wiki/Read-copy-update

the logic is whenever someone want to change, just write the changes
somewhere, so that reconstruction of the change is possible through reading
the changes + existing data.   (Oracle database, and indeed any database
does that too.).   so if multiple CPU want to write to the same place, then
u still need per-CPU locks for classic RCU:

http://lwn.net/Articles/305782/

But for reader, there is no need to lock:  just go ahead and read - if u
read AFTER the update has started, then u will be reading the older copy,
and the last reader will then kick off the merging of the older copy +
newer updates.

http://lwn.net/2001/features/OLS/pdf/pdf/read-copy.pdf

http://lwn.net/Articles/262464/

http://lwn.net/Articles/263130/  (see the picture here)

but these locking are done at the low level - harddisk is data block level.


For vfs_read() -  its purpose is to read...and it does not prevent u from
writing!!! yes, everything is left to the user at the userspace
level...locking/unlocking.   because it is done at the FILE level, and so
if u have multiple reads and then someone come in and write....yes, there
will be corruption.   but that is the logic corruption, not the
hardware/datablocks corruption, which the kernel aimed to protect.

On Tue, Jan 29, 2013 at 11:35 PM, Karaoui mohamed lamine <moharaka@gmail.com
> wrote:

> Hello,
>
> I was looking at how a syscall read/write was done, and i found this :
>
>    ....
>    loff_t pos = file_pos_read(f.file);
>    ret = vfs_read(f.file, buf, count, &pos);
>    file_pos_write(f.file, pos);
>    fdput(f);
>    ...
>
> My questions are :
>
> Where did the locking go? I would have imaginated something like :
>
>    ....
>    *lock(f);*
>    loff_t pos = file_pos_read(f.file);
>    ret = vfs_read(f.file, buf, count, &pos);
>    file_pos_write(f.file, pos);
>    fdput(f);
>    *unlock(f);*
>    ...
>
> If multiple threads try to read/write at the same time, they could
> read/write at the same offset ?
>
> If my understanding are correct, is this POSIX compliant ?
>
>
> thanks.
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>


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

      parent reply	other threads:[~2013-02-07  2:41 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
2013-02-06 22:58 ` Jimmy Pan
2013-02-07  2:41 ` Peter Teoh [this message]

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=CAHnt0GUmmVsbFN2mrKxbR-_E76BC4x1shiXscqwyvMxgPat3uA@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.