This is rather a beginner question. I wonder what happens when sleep command is triggered from a bash script, for example. That job would still running, with less priority?
Asked
Active
Viewed 6,172 times
1
muru
- 193,181
- 53
- 473
- 722
whitenoisedb
- 713
- 4
- 12
- 18
-
I mean, in general, when our script is an infinite loop and we call sleep 120 (for example). What are the CPU decisions on that script? I think it should have less priority, because the script is like paused. – whitenoisedb Sep 16 '14 at 22:34
-
Ah, gotcha. Please [edit] your question to make that clearer (I will delete my comment), I thought you were under the impression that you could use `sleep` to target other processes. – terdon Sep 16 '14 at 22:39
1 Answers
7
Typically, sleep is the GNU sleep program. This program calls the GNU function xnanosleep, which in turn calls the Linux nanosleep system call. And:
nanosleep() suspends the execution of the calling thread until either
at least the time specified in *req has elapsed, or the delivery of a
signal that triggers the invocation of a handler in the calling thread
or that terminates the process.
So the process is, by definition, not running (whatever maybe the priority), but suspended.
Testing this out:
sleep 10 & ps aux | grep $!
[1] 25682
muru 25682 0.0 0.0 7196 620 pts/13 SN 04:10 0:00 sleep 10
The process state is SN:
S interruptible sleep (waiting for an event to complete)
N low-priority (nice to other users)
Ignore the N, I'd guess that's how it was when it started out. So the effective state of sleep is interruptible sleep.
-
1Thanks for your clear answer. It was a good approach to check the process state while running it and see what those letters mean. – whitenoisedb Sep 17 '14 at 00:35