multithreadingcpucpu-usagecpu-cores

Blocking IO-Operation in Single core machine


I am trying to understand what happens to a thread when it is waiting for a http response from remote server.

Let's say at one point in time n processes are running. The OS based on its thread scheduling algorithm will try to run every thread (Let's say on round robin fashion). Let's say one out of n thread has initiated a http request and waiting for response from remote server. Will this thread keep on getting its turn on cpu core? or is there some interrupt sort of mechanism which will notify the thread if it is ready to run? If interrupt sort of mechanism is present, then what is the benefit of using asynchronous programming? at-least from CPU utilization perspective.

Is the above thing language dependent? If yes, what is the difference between java vs nodejs vs python ...


Solution

  • I am trying to understand What happens to a thread when it is waiting for a http response from remote server.

    Well, the thread will wait for the underlying TCP socket to receive data. HTTP is a high level protocol that uses blocking/nonblocking TCP connection. as itself, the thread doesn't wait for an "HTTP response" but rather to some available data for the socket to read.

    Will this thread keep on getting its turn on cpu core?

    If the thread waits for a TCP socket to be readable, the OS doesn't schedule this thread to run until some data is received. then the OS will schedule the thread to run in some point in the future. blocked thread is never schedule to run - the OS doesn't see the reason to do so, considering the fact that the thread has nothing to do.

    Is the above thing dependent on language? if yes what is the difference between java vs nodejs vs python ...

    No. Each OS provides a C/C++ API for application to consume. Windows provides Win32, while Linux provides POSIX. every programming language wraps and binds these APIs and every "high level" call (such as connecting a socket) will eventually call the operating system APIs.