To generalize further u can safely say that all synchronous operation have to be thread-safe, except for some APIs as listed here:<div><br></div><div><a href="http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_09.html" target="_blank">http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_09.html</a></div>
<div><br></div><div>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.</div>
<div><br></div><div>below discussion is useful (first answer esp):</div><div><br></div><div><a href="http://stackoverflow.com/questions/5268307/thread-safety-of-read-pread-system-calls" target="_blank">http://stackoverflow.com/questions/5268307/thread-safety-of-read-pread-system-calls</a><br>
<div><br></div><div>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.</div>
<div><br><div class="gmail_quote">On Thu, Feb 7, 2013 at 6:23 PM, Peter Teoh <span dir="ltr"><<a href="mailto:htmldeveloper@gmail.com" target="_blank">htmldeveloper@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>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:</div>
<div><br></div><div>See here:</div><div><br></div><div><a href="http://lwn.net/Articles/180387/" target="_blank">http://lwn.net/Articles/180387/</a></div><div><br></div><div>And Linus patch:</div><div><br></div><div><a href="http://lwn.net/Articles/180396/" target="_blank">http://lwn.net/Articles/180396/</a></div>
<div><br></div><div>but my present version (3.2.0) has rcu lock over it (higher performance):</div><div><br></div><div><div> INIT_LIST_HEAD(&f->f_u.fu_list);</div><div> atomic_long_set(&f->f_count, 1);</div>
<div> rwlock_init(&f->f_owner.lock);</div><div> spin_lock_init(&f->f_lock);</div><div> eventpoll_init_file(f);</div><div> /* f->f_version: 0 */</div><div><br></div><br><div class="gmail_quote">
<div><div>
On Thu, Feb 7, 2013 at 4:44 PM, Karaoui mohamed lamine <span dir="ltr"><<a href="mailto:moharaka@gmail.com" target="_blank">moharaka@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div>
<div><br></div>Tahnks guys!<br><br><div class="gmail_quote">2013/1/30 Karaoui mohamed lamine <span dir="ltr"><<a href="mailto:moharaka@gmail.com" target="_blank">moharaka@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
thanks, i think i get it.<br><br><div class="gmail_quote">2013/1/30 <span dir="ltr"><<a href="mailto:Valdis.Kletnieks@vt.edu" target="_blank">Valdis.Kletnieks@vt.edu</a>></span><div><div><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>On Tue, 29 Jan 2013 20:16:26 +0100, you said:<br>
<br>
> Actually my question is :<br>
> Does POSIX specifies the fact that we need to use "lockf" to be able to do<br>
> read/write operation in different offset ? Is'n the kernel supposed to<br>
> ensure this ?<br>
<br>
</div>If you have non-overlapping writes, the kernel will eventually sort it out<br>
for you. If your writes overlap, you'll have to provide your own locking<br>
via lockf() or similar, and synchronization via other methods.<br>
</blockquote></div></div></div><br>
</blockquote></div><br>
<br></div></div><div>_______________________________________________<br>
Kernelnewbies mailing list<br>
<a href="mailto:Kernelnewbies@kernelnewbies.org" target="_blank">Kernelnewbies@kernelnewbies.org</a><br>
<a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
<br></div></blockquote></div><span><font color="#888888"><br><br clear="all"><div><br></div>-- <br>Regards,<br>Peter Teoh
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Regards,<br>Peter Teoh
</div></div>