3

For example, a process is listening on some port with block mode, so if the I/O is not ready, the process will be blocked.

while (true)
{
    msg = recv(port, BLOCKING_FLAG); // blocks here
    cout<<msg<<endl;
}

We also know that we can make a process sleep: sleep(1000).

My question is: if such a process is blocking, can I say that the process is suspended? Will the process be swapped out from CPU? Same questions on sleep.

Yves
  • 11,597
  • 17
  • 83
  • 180
  • Related https://stackoverflow.com/questions/35688553/does-a-thread-waiting-on-io-also-block-a-core?rq=1 – Thilo May 04 '18 at 08:59
  • Short answer: yes, the OS knows that this process won't need CPU until the condition is met (input data arrived or sleep time up) – Thilo May 04 '18 at 09:02
  • You could improve this question by replacing every occurrence of "process" in this question with "thread." – Solomon Slow May 04 '18 at 15:44

1 Answers1

6

"Sleeping" -- usually means that the thread is in an explicit sleep(...) call.

"Suspended" -- sometimes is used in a generic way, meaning that the thread is waiting for ...something. Other times, "Suspended" means that some other thread or process explicitly suspended it (e.g., for debugging purposes), and the process/thread will not be able to run again until it is explicitly resumed.

"Blocked" -- is the most generic of the three. Often it merely means that the process/thread is waiting for something. Sometimes it implies that what the thread/process is waiting for is an I/O operation.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • So do all of the three words, "suspended", "sleeping" and "blocked", mean that they stop spending the resource of CPU? – Yves May 07 '18 at 01:18
  • 2
    @Yves, If we're talking about a user-mode thread in a multi-tasking operating system, then for most purposes, the answer is "yes, a blocked/sleeping/suspended thread uses no CPU." One possible, exception though: A high-performance mutex implementation might detect that it is running on a multi-CPU platform, and if so, it might busy-wait for some number of cycles before giving up the CPU. – Solomon Slow May 07 '18 at 15:46
  • Can we suspend a blocking thread? For example when someone is debugging, maybe he wants to suspend all threads. What happens then when a thread gets unblocked? – neoexpert Jul 29 '20 at 21:31
  • @neoexpert, you mean _explicitly_ suspend? Sure. "suspend" in that case could be as simple as setting a flag in the thread state that means, "don't let this one run—not even if it's in the run queue." – Solomon Slow Jul 29 '20 at 21:52
  • @neoexpert, Re, "...when a thread gets unblocked?" In most operating systems, the object that represents a thread can be in exactly one of several different queues. All but one of those are for threads that are waiting for some thing to happen. E.g., For every mutex, there might be a (hopefully short!) queue of threads that are waiting for their turn to acquire the mutex. "Unblocking" a thread simply means moving it from the queue where it was waiting to the "run queue" where it waits for a CPU time slice, or directly onto a CPU if one happens to be available at that moment. – Solomon Slow Jul 29 '20 at 21:56
  • I mean maybe the thread is waiting for a TCP client connection. I would like to suspend it, so if some client is connected, the thread should remain blocked until it gets resumed. – neoexpert Jul 29 '20 at 22:10
  • Ok I solved this for my JVM running in a browser: when a thread gets unblocked it gets immediately suspended in the next instruction. For this the ahead of time Compiler inserts such instruction after every potential blocking method invocation... – neoexpert Jul 30 '20 at 14:18