How ruby โ€‹โ€‹single os thread is not blocked when copying a file?

My assumptions:

  • MRI ruby โ€‹โ€‹1.8.X does not have its own threads, but green threads.

  • The OS does not know about these green threads.

  • issuing an IO-heavy operation should suspend the entire process until the correct I / O interrupt is returned.

With this, I created one that does the following:

  • a stream begins that prints "works!". every second.

  • issues an IO request to copy a large (1gb) file to the "main" stream.

Now, one could assume that, being green threads invisible to the OS, it will transfer the entire process to a โ€œblockedโ€ queue and โ€œwork!โ€. the green thread will not be executed. Surprisingly, this works: S

Does anyone know what is going on there? Thanks.

+4
source share
2 answers

When MRI 1.9 is triggered, it generates two native threads. One thread for the virtual machine, the other for signal processing. Rubinis uses this strategy, as does the JVM. Pipes can be used to transfer any information from other processes.

As for the FileUtils module, cd , pwd , mkdir , rm , ln , cp , mv , chmod , chown and touch all, to one degree or another, transferred to third-party OS utilities using the internal API of the StreamUtils submodule, while the second stream remains to wait signal from an external process. Since these methods are thread safe enough, there is no need to block the interpreter, and therefore the methods do not block each other.

Edit:

MRI 1.8.7 is pretty smart and knows that when Thread expects some external event (for example, a browser to send an HTTP request), Thread can be launched and woken up when data is detected - Evan Phoenix from Engine Yard in Ruby, Concurrency and you

The implementation of the base implementation for FileUtils did not change the meaning of 1.8.7 from viewing the source. 1.8.7 also uses the sleep timer thread to wait for an IO response. The main difference in 1.9 is the use of native threads, not green threads. Also significantly improved source code stream.

By thread safety, I mean that, since there is nothing in common between the processes, there is no reason to block the global interpreter. There is a misconception that Ruby "blocks" when performing certain tasks. Whenever the thread should block, i.e. Waiting without using any processor, Ruby just schedules another thread. However, in certain situations, for example, in a server rack that uses 20% of the processor, waiting for a response, it may be advisable to unlock the interpreter and allow parallel threads to process other requests while waiting. These threads, in a sense, work in parallel. GIL unlocked rb_thread_blocking_region API. Here is a good post in this thread .

+3
source

There is no operation to copy atom core files. These are fairly short reads and writes that enter and exit the kernel.

As a result, the process is constantly gaining control. Signals are transmitted.

Green threads work by connecting a Ruby-level thread manager to low-level input / output and signal reception. As long as these hooks periodically monitor the control, green streams will act just like more parallel streams.

Unix initially had a completely unknown, but very simple abstract machine model for a user work environment.

Over the years, concurrency support in general, and threads in particular, have been added in stages in two different ways.

  • Many small kludges have been added to check if I / O blocks a failure (followed by a restart), if I / O is blocked, interrupt tty's slow input / output for signals, but then transparently return to it, etc. When the Unix APIs were merged, each kludge existed in more than one form. Lots of options. 1.
  • Direct thread support was also added in the form of several processes visible in the kernel that share the address space. These threads are dangerous and not subject to testing , but are widely supported and used. Basically, programs do not crash. Over time, hidden errors become visible as the hardware supports a more true concurrency value. I do not care at all that Ruby does not fully support this nightmare.

1. The good thing about standards is that there are so many.

+4
source

All Articles