1

APUE says about sleep()

This function sleep() causes the calling process to be suspended until either

  1. The amount of wall clock time specified by seconds has elapsed.
  2. A signal is caught by the process and the signal handler returns.

Does the first case work by sending some specific signal to the process itself? If yes, what is the signal? alarm() can send signal SIGALARM to the calling process after a specific time period, which is why I wonder if sleep() work in the same way.

Does sleep() change the state of the calling process to the same state as sigsuspend() changes to? Which process state(s) do the two functions change to?

Is it correct that a suspended process can only be waken by a signal? That is the reason why I have the question.

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • I don't know the internals of linux or posix systems, but most operating systems have a ticker and use it for time slicing between equal priority threads that are runnable and also for Sleep() to switch a thread's state to runnable when the specified time has elapsed. – rcgldr Sep 21 '18 at 21:03
  • 2
    Related: https://stackoverflow.com/questions/1719071/how-is-sleep-implemented-at-the-os-level . Essentially, the OS just doesn't schedule the process until N seconds has passed (or a signal is pending) – that other guy Sep 21 '18 at 21:25

1 Answers1

2

Linux kernel has absolutely no needs in using user signals for change the state of the process. So, a signal emitting is performed only for meet a requirement on a user library's function.

As documentation for sleep function doesn't say that any signal is emitted after the end of the sleep, so the kernel doesn't use signals in that case.

Is it correct that a suspended process can only be waken by a signal?

Yes, user code may awoke sleeping process only by sending signal to it. This is true for most non-runnable process states too.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153