1

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 ...

Vineel
  • 436
  • 6
  • 12
hello123
  • 391
  • 4
  • 15
  • A thread waiting for an event (either IO or a signal) won't be scheduled by the OS. That would be a waste of time, the thread would be put back to sleep immediately. The CPU doesn't know anything about async programming, that's an high-level concept for languages that tries to avoid threads in the first place. Async programming is like using cooperative multitasking or the yield syscall. Just with syntax sugar to hide the technical details of implementing a scheduler. The CPU responds to interrupts to get notified of events and events are the low level blocks of async programming. – Margaret Bloom Jun 03 '19 at 13:13
  • @MargaretBloom so from CPU perspective there is not much of a gain by using async programming right? – hello123 Jun 03 '19 at 14:16
  • If by async programming you are referring to the link you posted then answer is that from a CPU perspective there's no such thing as async programming. However those async libraries are implemented on top of the OS "async" API (based on callbacks or events) so are more efficient than blocking a thread and schedule it out. – Margaret Bloom Jun 03 '19 at 14:33

2 Answers2

1

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.

David Haim
  • 25,446
  • 3
  • 44
  • 78
  • Hi David thanks for the response, can you please share some doc around above information. Thanks again. – hello123 Jun 03 '19 at 14:13
  • @hello123, hi, you can google "asynchronous IO" and start researching. I'd be happy if you accept this answer. press on the V sign where the ranking is. – David Haim Jun 03 '19 at 14:27
0

My understanding is asynchronous keyword is used for your program to continue executing instead of waiting for the forked process to complete, even in single core processors, as was the case with early computers we were able to multitask, hence this could be deduced that the resource allocation was done by cpu while trying to be as judicious as it could be, so using async allows your thread of execution to execute without waiting for the blocking task to get completed, otherwise, even though cpu will take turns in executing a thread but since your program is a single thread it will block.

piyush tyagi
  • 105
  • 1
  • 9