Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   question: threads behaviour (http://www.velocityreviews.com/forums/t866446-question-threads-behaviour.html)

Raphael Bauduin 03-01-2011 08:45 AM

question: threads behaviour
 
Hi,

As ruby threads are so called green threads, what's the behaviour when
one thread is blocked on IO?
If the thread is blocked on a system call, is the whole process, ie
all threads blocked because the kernel scheduler considers the process
as blocked on IO? Are there IOs blocking a thread without blocking the
whole ruby process?

Thanks

Rapha=EBl


me@kmwhite.net 03-01-2011 09:24 AM

Re: question: threads behaviour
 
On Tue, 1 Mar 2011 17:45:26 +0900, Raphael Bauduin <rblists@gmail.com>
wrote:
> Hi,
>
> As ruby threads are so called green threads, what's the behaviour when
> one thread is blocked on IO?
> If the thread is blocked on a system call, is the whole process, ie
> all threads blocked because the kernel scheduler considers the process
> as blocked on IO? Are there IOs blocking a thread without blocking the
> whole ruby process?
>
> Thanks
>
> Raphaël


I'm new to threading, but from what I've been able to find, if the
currently active thread is blocked on IO, it should block the rest of them.
At least, certain circumstances could cause that. According to (the rather
old) post at http://www.justskins.com/forums/when...ck-107159.html
, it's possible to block on IO. My own tests are unable to confirm that,
even for large test sizes. My guess is that in the 7ish years since that
post, many improvements have gone into the interpreter. ;) The post at
http://stackoverflow.com/questions/3...he-main-thread
has more information on ways of using them to not block each other.

http://www.ruby-doc.org/docs/Program...t_threads.html was
where I started for threading, but the only references I see to blocking in
it talk about the Thread.critical= method.

http://stackoverflow.com/questions/5...ng/57802#57802
has a nice synopsis of the threading types between ruby implementations.


Eric Wong 03-01-2011 09:27 AM

Re: question: threads behaviour
 
Raphael Bauduin <rblists@gmail.com> wrote:
> Hi,
>
> As ruby threads are so called green threads, what's the behaviour when
> one thread is blocked on IO?


They're only green threads on Ruby 1.8, 1.9 uses native threads.

> If the thread is blocked on a system call, is the whole process, ie
> all threads blocked because the kernel scheduler considers the process
> as blocked on IO? Are there IOs blocking a thread without blocking the
> whole ruby process?


1.8 will schedule other threads if the blocking IO is for a socket/pipe
and uses the normal Ruby IO interfaces (or the extension author was
careful). Filesystem I/O (including NFS) can block the entire process
in 1.8.

--
Eric Wong


Robert Klemme 03-01-2011 04:19 PM

Re: question: threads behaviour
 
On Tue, Mar 1, 2011 at 10:27 AM, Eric Wong <normalperson@yhbt.net> wrote:
> Raphael Bauduin <rblists@gmail.com> wrote:
>> As ruby threads are so called green threads, what's the behaviour when
>> one thread is blocked on IO?

>
> They're only green threads on Ruby 1.8, 1.9 uses native threads.
>
>> If the thread is blocked on a system call, is the whole process, ie
>> all threads blocked because the kernel scheduler considers the process
>> as blocked on IO? Are there IOs blocking a thread without blocking the
>> whole ruby process?

>
> 1.8 will schedule other threads if the blocking IO is for a socket/pipe
> and uses the normal Ruby IO interfaces (or the extension author was
> careful). =A0Filesystem I/O (including NFS) can block the entire process
> in 1.8.


Where do you take that from? AFAIK internally Ruby 1.8.* uses non
blocking IO calls in combination with select() to be able to let other
threads work while IO is under way.

So: if IO blocks generally other threads continue to work - even on 1.8.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/


Eric Wong 03-01-2011 07:16 PM

Re: question: threads behaviour
 
Robert Klemme <shortcutter@googlemail.com> wrote:
> > 1.8 will schedule other threads if the blocking IO is for a socket/pipe
> > and uses the normal Ruby IO interfaces (or the extension author was
> > careful). *Filesystem I/O (including NFS) can block the entire process
> > in 1.8.

>
> Where do you take that from? AFAIK internally Ruby 1.8.* uses non
> blocking IO calls in combination with select() to be able to let other
> threads work while IO is under way.


Yes, I meant "blocking" from a user-visible perspective, not from a
kernel perspective. If there's an EAGAIN it'll block the current thread
from a user perspective and schedule others using select(), but not
actually block on the syscall that caused EAGAIN.

--
Eric Wong


Robert Klemme 03-01-2011 09:56 PM

Re: question: threads behaviour
 
On 03/01/2011 08:16 PM, Eric Wong wrote:
> Robert Klemme<shortcutter@googlemail.com> wrote:
>>> 1.8 will schedule other threads if the blocking IO is for a socket/pipe
>>> and uses the normal Ruby IO interfaces (or the extension author was
>>> careful). Filesystem I/O (including NFS) can block the entire process
>>> in 1.8.

>>
>> Where do you take that from? AFAIK internally Ruby 1.8.* uses non
>> blocking IO calls in combination with select() to be able to let other
>> threads work while IO is under way.

>
> Yes, I meant "blocking" from a user-visible perspective, not from a
> kernel perspective. If there's an EAGAIN it'll block the current thread
> from a user perspective and schedule others using select(), but not
> actually block on the syscall that caused EAGAIN.


OK, but earlier you said that file system IO can block the entire
process - which is not exactly true and also does not match what you
said now. :-)

Cheers

robert

Eric Wong 03-01-2011 10:13 PM

Re: question: threads behaviour
 
Robert Klemme <shortcutter@googlemail.com> wrote:
> On 03/01/2011 08:16 PM, Eric Wong wrote:
>> Robert Klemme<shortcutter@googlemail.com> wrote:
>>>> 1.8 will schedule other threads if the blocking IO is for a socket/pipe
>>>> and uses the normal Ruby IO interfaces (or the extension author was
>>>> careful). Filesystem I/O (including NFS) can block the entire process
>>>> in 1.8.
>>>
>>> Where do you take that from? AFAIK internally Ruby 1.8.* uses non
>>> blocking IO calls in combination with select() to be able to let other
>>> threads work while IO is under way.

>>
>> Yes, I meant "blocking" from a user-visible perspective, not from a
>> kernel perspective. If there's an EAGAIN it'll block the current thread
>> from a user perspective and schedule others using select(), but not
>> actually block on the syscall that caused EAGAIN.

>
> OK, but earlier you said that file system IO can block the entire
> process - which is not exactly true and also does not match what you
> said now. :-)


Filesystem IO (regular files) doesn't return EAGAIN (at least not on
systems I'm familiar with).

--
Eric Wong


Robert Klemme 03-02-2011 10:54 AM

Re: question: threads behaviour
 
On Tue, Mar 1, 2011 at 11:13 PM, Eric Wong <normalperson@yhbt.net> wrote:
> Robert Klemme <shortcutter@googlemail.com> wrote:
>> On 03/01/2011 08:16 PM, Eric Wong wrote:
>>> Robert Klemme<shortcutter@googlemail.com> =A0wrote:
>>>>> 1.8 will schedule other threads if the blocking IO is for a socket/pi=

pe
>>>>> and uses the normal Ruby IO interfaces (or the extension author was
>>>>> careful). =A0Filesystem I/O (including NFS) can block the entire proc=

ess
>>>>> in 1.8.
>>>>
>>>> Where do you take that from? =A0AFAIK internally Ruby 1.8.* uses non
>>>> blocking IO calls in combination with select() to be able to let other
>>>> threads work while IO is under way.
>>>
>>> Yes, I meant "blocking" from a user-visible perspective, not from a
>>> kernel perspective. =A0If there's an EAGAIN it'll block the current thr=

ead
>>> from a user perspective and schedule others using select(), but not
>>> actually block on the syscall that caused EAGAIN.

>>
>> OK, but earlier you said that file system IO can block the entire
>> process - which is not exactly true and also does not match what you
>> said now. :-)

>
> Filesystem IO (regular files) doesn't return EAGAIN (at least not on
> systems I'm familiar with).


http://www.kernel.org/doc/man-pages/....2.html#ERRORS

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/


Eric Wong 03-02-2011 07:37 PM

Re: question: threads behaviour
 
Robert Klemme <shortcutter@googlemail.com> wrote:
> On Tue, Mar 1, 2011 at 11:13 PM, Eric Wong <normalperson@yhbt.net> wrote:
> > Filesystem IO (regular files) doesn't return EAGAIN (at least not on
> > systems I'm familiar with).

>
> http://www.kernel.org/doc/man-pages/....2.html#ERRORS


From http://www.kernel.org/doc/man-pages/...d.2.html#NOTES

Many file systems and disks were considered to be fast enough
that the implementation of O_NONBLOCK was deemed unnecessary.
So, O_NONBLOCK may not be available on files and/or disks.

From what I've seen, setting O_NONBLOCK doesn't error, but read() never
actually respects it on ext{2,3,4} even when a disk is horribly slow.

--
Eric Wong


Jörg W Mittag 03-02-2011 09:30 PM

Re: question: threads behaviour
 
Eric Wong wrote:
> Raphael Bauduin <rblists@gmail.com> wrote:
>> As ruby threads are so called green threads, what's the behaviour when
>> one thread is blocked on IO?

> They're only green threads on Ruby 1.8, 1.9 uses native threads.


That's not true. The vast majority of Ruby 1.8 implementations use
native threads, and I think there is at least one Ruby 1.9
implementation which uses green threads.

jwm


All times are GMT. The time now is 09:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.