I’m trying to see some things about processes in Linux and I have some questions that I hope you could answer for me.
I did this little program to see how it acts:
#!/bin/bash
count=1
while [ true ]
do
echo "Counter $count "
count=$(( $count + 1 ))
done
Is just an infinite loop.
Now, when I execute the program and use the top command in the shell, that process is the one the consumes more CPU resources:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4037 lola 20 0 16880 1248 1028 R 80 0.0 0:33.42 memoryleak.sh
I let the program run for a time, but never exceeds 85% of CPU consumption, why is that? I guess that it is a mechanism of hygiene of the OP, but, if truth, which are the parameters to decide. Even more, the counter is still working, and, for what I see, could still work ad infinitum. Why is the CPU-intensive program not crashing the CPU?
Now, If I interrupt the process -sending a STOP signal-, and do a ps aux I get:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
lola 3896 24.4 0.0 16880 1248 pts/3 T 09:15 0:37 /bin/bash ./cpukilla.sh
Why, after stopped the process, the CPU consumption is still 24%? Should not be zero?
Any assistance would be appreciated.
EDIT: well, sorry for the “confusion” of the memory leak term. Never the less, it would not be technically a memory leak, because the count is consuming much memory without releasing it?
If it never exceeds 85% of the CPU, that’s because the OS has other things calling on its resources. Lots of processes run on a multi-tasking system and it’s the job of the OS to keep them all playing nicely.
In fact, under Linux (for one), if your process consistently uses its entire timeslice (ie, not yielding early), its priority will be downgraded as “punishment”.
A CPU-intensive program won’t crash the operating system simply because the OS knows how to protect itself from such shenanigans 🙂
As to the 24% figure, I’m not sure on that. It may be that the figure is based on program lifetime rather than the last few seconds, in which case it would reduce gradually (a series of
ps -auxstatements ten seconds apart would corroborate or destroy that theory, the former if it keeps reducing, that latter if it jumps up again).The man page doesn’t give a lot of detail on what the
%cpuactually represents but does say:This could be read as being a figure calculated over a time period rather (eg, last five minutes) than instantaneous (last second).