blocking VS non blocking

Hankyu KimHankyu Kim
2 min read

Blocking

In a blocking scenario, a thread remains idle while it waits for a method call to complete its request to a device.

Blocking Socket

When the receive buffer is full, the TCP sender's send() method becomes blocked, halting communication even though the connection is still active. Similarly, if the receive buffer is empty, the receiver's recv() method will also block until at least one datagram is recieved.

Cons: In a blocking socket setup, you need a separate thread for each network host. Each thread is responsible for data transfer with its designated network host. This approach works well with a small number of connections. However, as the number of connected hosts increases, it requires significantly more memory since each threadโ€™s call stack typically consumes about 1 MB. Additionally, frequent context switching between threads can further degrade performance.

Non-Blocking Socket

Non-blocking sockets, supported by the operating system, allow for more efficient resource use by avoiding the blocking of socket methods. These sockets return immediately when a method is called.

In a non-blocking socket, if the buffer has data, it is retrieved and returned immediately. If the buffer is empty, the method returns a "would block" error code, indicating that the operation cannot be completed at the moment.

In conclusion, non-blocking sockets can handle a large number of socket operations without delays, making them ideal for scenarios where high throughput and minimal latency are critical.

The problem with this non block socket is that if the socket continues to be in a would block state, it will continue to loop that can cause a single CPU core in 100 percentage usage.

To solve this problem, if there is a single change in the state of one of numerous sockets being a would block, a function that informs the situation that resolves the CPU usage congestion is needed.

The function that does this is select() or poll.
If select is called, it returns immediately if there is even one socket capable of I/O processing.
Otherwise block up to 100 milliseconds.

0
Subscribe to my newsletter

Read articles from Hankyu Kim directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hankyu Kim
Hankyu Kim

๐˜ ๐˜ฉ๐˜ข๐˜ท๐˜ฆ ๐˜ข ๐˜ฌ๐˜ฆ๐˜ฆ๐˜ฏ ๐˜ช๐˜ฏ๐˜ต๐˜ฆ๐˜ณ๐˜ฆ๐˜ด๐˜ต ๐˜ข๐˜ฏ๐˜ฅ ๐˜ฑ๐˜ข๐˜ด๐˜ด๐˜ช๐˜ฐ๐˜ฏ ๐˜ง๐˜ฐ๐˜ณ ๐˜ข๐˜ถ๐˜ต๐˜ฐ๐˜ฏ๐˜ฐ๐˜ฎ๐˜ฐ๐˜ถ๐˜ด ๐˜ฅ๐˜ณ๐˜ช๐˜ท๐˜ช๐˜ฏ๐˜จ ๐˜ต๐˜ฆ๐˜ค๐˜ฉ๐˜ฏ๐˜ฐ๐˜ญ๐˜ฐ๐˜จ๐˜บ. ๐˜ˆ๐˜ถ๐˜ต๐˜ฐ๐˜ฏ๐˜ฐ๐˜ฎ๐˜ฐ๐˜ถ๐˜ด ๐˜ค๐˜ข๐˜ณ๐˜ด ๐˜ฐ๐˜ง๐˜ง๐˜ฆ๐˜ณ ๐˜ญ๐˜ช๐˜ฎ๐˜ช๐˜ต๐˜ญ๐˜ฆ๐˜ด๐˜ด ๐˜ด๐˜ค๐˜ฆ๐˜ฏ๐˜ข๐˜ณ๐˜ช๐˜ฐ๐˜ด ๐˜ง๐˜ฐ๐˜ณ ๐˜ค๐˜ฐ๐˜ฏ๐˜ท๐˜ฆ๐˜ฏ๐˜ช๐˜ฆ๐˜ฏ๐˜ค๐˜ฆ ๐˜ข๐˜ฏ๐˜ฅ ๐˜ฒ๐˜ถ๐˜ข๐˜ญ๐˜ช๐˜ต๐˜บ-๐˜ฐ๐˜ง-๐˜ญ๐˜ช๐˜ง๐˜ฆ ๐˜ช๐˜ฎ๐˜ฑ๐˜ณ๐˜ฐ๐˜ท๐˜ฆ๐˜ฎ๐˜ฆ๐˜ฏ๐˜ต๐˜ด. ๐˜›๐˜ฉ๐˜ฆ๐˜บ ๐˜ค๐˜ข๐˜ฏ ๐˜ฑ๐˜ณ๐˜ฐ๐˜ท๐˜ช๐˜ฅ๐˜ฆ ๐˜ช๐˜ฏ๐˜ฅ๐˜ฆ๐˜ฑ๐˜ฆ๐˜ฏ๐˜ฅ๐˜ฆ๐˜ฏ๐˜ค๐˜ฆ ๐˜ต๐˜ฐ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฆ๐˜ญ๐˜ฅ๐˜ฆ๐˜ณ๐˜ญ๐˜บ ๐˜ข๐˜ฏ๐˜ฅ ๐˜ฑ๐˜ฉ๐˜บ๐˜ด๐˜ช๐˜ค๐˜ข๐˜ญ๐˜ญ๐˜บ ๐˜ฅ๐˜ช๐˜ด๐˜ข๐˜ฃ๐˜ญ๐˜ฆ๐˜ฅ ๐˜ช๐˜ฏ๐˜ฅ๐˜ช๐˜ท๐˜ช๐˜ฅ๐˜ถ๐˜ข๐˜ญ๐˜ด. ๐˜๐˜ฐ๐˜ณ ๐˜ช๐˜ฏ๐˜ด๐˜ต๐˜ข๐˜ฏ๐˜ค๐˜ฆ, ๐˜ช๐˜ง ๐˜บ๐˜ฐ๐˜ถ๐˜ณ ๐˜ค๐˜ฉ๐˜ช๐˜ญ๐˜ฅ๐˜ณ๐˜ฆ๐˜ฏ ๐˜ง๐˜ฐ๐˜ณ๐˜จ๐˜ฆ๐˜ต ๐˜ต๐˜ฉ๐˜ฆ๐˜ช๐˜ณ ๐˜ฃ๐˜ข๐˜ต๐˜ฉ๐˜ช๐˜ฏ๐˜จ ๐˜ด๐˜ถ๐˜ช๐˜ต๐˜ด ๐˜ข๐˜ฏ๐˜ฅ ๐˜ต๐˜ฐ๐˜ฐ๐˜ต๐˜ฉ๐˜ฃ๐˜ณ๐˜ถ๐˜ด๐˜ฉ๐˜ฆ๐˜ด ๐˜ข๐˜ต ๐˜ด๐˜ถ๐˜ฎ๐˜ฎ๐˜ฆ๐˜ณ ๐˜ค๐˜ข๐˜ฎ๐˜ฑ, ๐˜ต๐˜ฉ๐˜ฆ ๐˜ค๐˜ข๐˜ณ ๐˜ค๐˜ฐ๐˜ถ๐˜ญ๐˜ฅ ๐˜ฃ๐˜ณ๐˜ช๐˜ฏ๐˜จ ๐˜ต๐˜ฉ๐˜ฆ๐˜ฎ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฎ๐˜ช๐˜ด๐˜ด๐˜ช๐˜ฏ๐˜จ ๐˜ช๐˜ต๐˜ฆ๐˜ฎ๐˜ด. ๐˜ ๐˜ฐ๐˜ถ ๐˜ค๐˜ข๐˜ฏ ๐˜ฆ๐˜ท๐˜ฆ๐˜ฏ ๐˜ด๐˜ฆ๐˜ฏ๐˜ฅ ๐˜บ๐˜ฐ๐˜ถ๐˜ณ ๐˜ฅ๐˜ฐ๐˜จ ๐˜ต๐˜ฐ ๐˜ข ๐˜ท๐˜ฆ๐˜ต๐˜ฆ๐˜ณ๐˜ช๐˜ฏ๐˜ข๐˜ณ๐˜บ ๐˜ข๐˜ฑ๐˜ฑ๐˜ฐ๐˜ช๐˜ฏ๐˜ต๐˜ฎ๐˜ฆ๐˜ฏ๐˜ต ๐˜ถ๐˜ด๐˜ช๐˜ฏ๐˜จ ๐˜ข๐˜ถ๐˜ต๐˜ฐ๐˜ฏ๐˜ฐ๐˜ฎ๐˜ฐ๐˜ถ๐˜ด ๐˜ค๐˜ข๐˜ณ๐˜ด. ๐˜ ๐˜ข๐˜ฎ ๐˜ฑ๐˜ข๐˜ด๐˜ด๐˜ช๐˜ฐ๐˜ฏ๐˜ข๐˜ต๐˜ฆ ๐˜ข๐˜ฃ๐˜ฐ๐˜ถ๐˜ต ๐˜ค๐˜ฐ๐˜ฏ๐˜ต๐˜ณ๐˜ช๐˜ฃ๐˜ถ๐˜ต๐˜ช๐˜ฏ๐˜จ ๐˜ต๐˜ฐ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ณ๐˜ฆ๐˜ด๐˜ฆ๐˜ข๐˜ณ๐˜ค๐˜ฉ ๐˜ข๐˜ฏ๐˜ฅ ๐˜ฅ๐˜ฆ๐˜ท๐˜ฆ๐˜ญ๐˜ฐ๐˜ฑ๐˜ฎ๐˜ฆ๐˜ฏ๐˜ต ๐˜ฐ๐˜ง ๐˜ข๐˜ถ๐˜ต๐˜ฐ๐˜ฏ๐˜ฐ๐˜ฎ๐˜ฐ๐˜ถ๐˜ด ๐˜ฅ๐˜ณ๐˜ช๐˜ท๐˜ช๐˜ฏ๐˜จ ๐˜ต๐˜ฆ๐˜ค๐˜ฉ๐˜ฏ๐˜ฐ๐˜ญ๐˜ฐ๐˜จ๐˜บ, ๐˜ฌ๐˜ฆ๐˜ฆ๐˜ฑ๐˜ช๐˜ฏ๐˜จ ๐˜ช๐˜ฏ ๐˜ฎ๐˜ช๐˜ฏ๐˜ฅ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฑ๐˜ฐ๐˜ด๐˜ด๐˜ช๐˜ฃ๐˜ช๐˜ญ๐˜ช๐˜ต๐˜ช๐˜ฆ๐˜ด ๐˜ช๐˜ต ๐˜ฉ๐˜ฐ๐˜ญ๐˜ฅ๐˜ด. ๐˜ž๐˜ช๐˜ต๐˜ฉ ๐˜ฎ๐˜บ ๐˜ด๐˜ฌ๐˜ช๐˜ญ๐˜ญ๐˜ด ๐˜ข๐˜ฏ๐˜ฅ ๐˜ฌ๐˜ฏ๐˜ฐ๐˜ธ๐˜ญ๐˜ฆ๐˜ฅ๐˜จ๐˜ฆ, ๐˜ ๐˜ข๐˜ช๐˜ฎ ๐˜ต๐˜ฐ ๐˜ค๐˜ฐ๐˜ฏ๐˜ต๐˜ณ๐˜ช๐˜ฃ๐˜ถ๐˜ต๐˜ฆ ๐˜ต๐˜ฐ ๐˜ข ๐˜ด๐˜ถ๐˜ด๐˜ต๐˜ข๐˜ช๐˜ฏ๐˜ข๐˜ฃ๐˜ญ๐˜ฆ ๐˜ง๐˜ถ๐˜ต๐˜ถ๐˜ณ๐˜ฆ ๐˜ต๐˜ณ๐˜ข๐˜ฏ๐˜ด๐˜ฑ๐˜ฐ๐˜ณ๐˜ต๐˜ข๐˜ต๐˜ช๐˜ฐ๐˜ฏ ๐˜ด๐˜บ๐˜ด๐˜ต๐˜ฆ๐˜ฎ. ๐˜ ๐˜ข๐˜ฎ ๐˜ค๐˜ถ๐˜ณ๐˜ณ๐˜ฆ๐˜ฏ๐˜ต๐˜ญ๐˜บ ๐˜ธ๐˜ฐ๐˜ณ๐˜ฌ๐˜ช๐˜ฏ๐˜จ ๐˜ช๐˜ฏ ๐˜ข๐˜ถ๐˜ต๐˜ฐ๐˜ฏ๐˜ฐ๐˜ฎ๐˜ฐ๐˜ถ๐˜ด ๐˜ฅ๐˜ณ๐˜ช๐˜ท๐˜ช๐˜ฏ๐˜จ ๐˜ด๐˜ช๐˜ฎ๐˜ถ๐˜ญ๐˜ข๐˜ต๐˜ฐ๐˜ณ ๐˜ค๐˜ฐ๐˜ฎ๐˜ฑ๐˜ข๐˜ฏ๐˜บ ๐‘ด๐‘ถ๐‘น๐‘จ๐‘ฐ. ๐˜—๐˜ญ๐˜ฆ๐˜ข๐˜ด๐˜ฆ ๐˜ง๐˜ฆ๐˜ฆ๐˜ญ ๐˜ง๐˜ณ๐˜ฆ๐˜ฆ ๐˜ต๐˜ฐ ๐˜ฎ๐˜ฆ๐˜ด๐˜ด๐˜ข๐˜จ๐˜ฆ ๐˜ฎ๐˜ฆ. ๐˜'๐˜ฎ ๐˜ญ๐˜ฐ๐˜ฐ๐˜ฌ๐˜ช๐˜ฏ๐˜จ ๐˜ง๐˜ฐ๐˜ณ๐˜ธ๐˜ข๐˜ณ๐˜ฅ ๐˜ต๐˜ฐ ๐˜ด๐˜ฉ๐˜ข๐˜ณ๐˜ฆ ๐˜ข๐˜ฏ๐˜ฅ ๐˜ค๐˜ฐ๐˜ฏ๐˜ฏ๐˜ฆ๐˜ค๐˜ต. ๐˜›๐˜ฉ๐˜ข๐˜ฏ๐˜ฌ ๐˜บ๐˜ฐ๐˜ถ! ๐˜๐˜ข๐˜ฏ๐˜ฌ๐˜บ๐˜ถ ๐˜’๐˜ช๐˜ฎ