Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to other thread when one thread get blocked?

In Linux, if two threads are created and both of them are running, when one of them calls recv() or any IO syscall that blocks when no data is available, what would happen to the whole process?

Will the other thread block also? I guess this depends on how threading is implemented. If thread library is in user space and kernel totally unaware of the threads within process, then process is the scheduling entity and thus both threads got blocked.

Further, if the other thread doesn't block because of this, can it then send() data via the same socket, which is blocking the recv thread? Duplexing?

Any ideas?

like image 721
Figo Avatar asked Jan 27 '10 22:01

Figo


People also ask

What happens to a thread that gets blocked?

The running thread will block when it must wait for some event to occur (response to an IPC request, wait on a mutex, etc.). The blocked thread is removed from the running array, and the highest-priority ready thread that's at the head of its priority's queue is then allowed to run.

When a thread of a process is blocked than all other threads of the same process will also be blocked?

If one process is blocked, then no other process can execute until the first process is unblocked. While one thread is blocked and waiting, a second thread in the same task can run. Multiple processes without using threads use more resources. Multiple threaded processes use fewer resources.

Can one thread block another thread in Java?

No, this is impossible. It's hard to imagine why you would ever need it. Let's say there was a notifyOtherThreadAndBlock() call. This call would notify the other thread and then block.

Can threads block independently from one another?

Yes, this can happen for any synchronization operation, where one thread needs to wait for another thread to do something.


1 Answers

You're absolutely right that the blocking behavior will depend on if the thread is implemented in kernel space, or in user space. If threading is implemented purely in user space (that is, the kernel is completely uninvolved with the threading), then any blocking entry point into the kernel will need to be wrapped with some non-blocking variant that can simulate blocking semantics to its calling "thread" (e.g. using AIO to send / recv data instead of blocking, and the completion callback makes the thread runnable, again).

In Linux (and every other extant major OS I can think of), threading is implemented at the kernel level, or similar, and a blocking call into the kernel will not cause all other threads to block.

Yes, you can send() to a socket for which another thread is blocked on recv().

like image 96
Aidan Cully Avatar answered Oct 19 '22 22:10

Aidan Cully