When I do 'top' , I see my perl script process in 'S' state. Is there a way to know, what is making this perl process go in sleep state and ways to wake it up? Any way to debug to give more insights on lines in perl script making it go in 'S' state.
2 Answers
A process in S
state is usually in a blocking system call, such as reading or writing to a file or the network, or waiting for another called program to finish.
You can use strace -p <pid>
to find out which system call is currently happening. It will produce output like
write(1, "foobar"..., 4096
which means that the process is trying to write 4096 bytes starting with "foobar" to stdout (fd #1) but whatever it has been redirected into is busy and the output buffer is full.
Processes go to sleep states when they are waiting for something, usually I/O.
Your process will be in S
state when it is doing reads and possibly writes that are blocking. Can also happen while waiting on semaphores or other synchronization primitives.
You can't "wake it up" - it will only proceed when the data/resource it is waiting for becomes available.
This is all normal and expected, and not usually a problem. Typically, this "program" run on the command line with no file:
while (<>) { print; }
will spend most of its time in sleep state, which is good - you don't want it to waste CPU while it's waiting for user input.
If you think this is a problem, try changing the way you do your I/O (reading larger chunks, in nice multiples of the underlying device's block size, doing memory mapped I/O, etc.).

- 52,586