Loop without a delay


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop without a delay
# 1  
Old 04-23-2014
Loop without a delay

Hi,

I am trying to understand what would happen if ther is a loop without any delay like sleep statement, I feel that would add a lot of load onto the CPU. Trying to understand how the load is reduced by the introduction of sleep().

Thanks and regards
Zulfi
# 2  
Old 04-23-2014
The kernel gives resources to a process. When other processes want the cpu resource, the looping process loses the cpu - this is called a context switch.

Simply having a forever loop is a waste of resources. Period. If you want to have a process hanging around waiting for some work to do, then there are other better ways. For example, use signals.

If you give us a concrete shell script example we can help clear up your confusion.
# 3  
Old 04-23-2014
Thanks for the reply,
Code:
 
 
filefound=0
 
while [[ $filefound -eq 0 ]]
do
 
if [[ -f abc.dat ]]
then
mv abc.dat xyz.dat
filefound=1
fi
 
done

In the above if the file is not found the loop keeps iterating, adding a sleep command will introduce some wait between iterations. Just wondering if by adding the sleep will add any advantage in terms reducing processor load OR it just doesn't make any difference.

Thanks
Zulfi
# 4  
Old 04-23-2014
Adding a sleep will reduce the overall load on a processor. Even a one second sleep is good enough. The process arranges for a signal to be delivered one second in the future. Then it will call pause() and do nothing until the signal arrives. A second is a long time to a modern cpu. In a small loop like you show the script will run for a few milliseconds then do nothing at all for a full second. So most of the time, this script is doing nothing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Trying to understand the delay

Heyas As you know me, i have scripts for about almost every aspect of my IT life. This time, i'm having issues to figure out why my script to connect to my wifi spots takes so long when started as service. The service file (the after:local-fs.target is for 'home installations'): cat... (3 Replies)
Discussion started by: sea
3 Replies

2. UNIX for Dummies Questions & Answers

Delay with Wget

I have a list of URLs that I need to download in a file. I know I can use the -i option to load the URLs from the file, but how can I make Wget wait a couple secs between each URL download? (4 Replies)
Discussion started by: rlopes
4 Replies

3. Shell Programming and Scripting

delay variable expansion

Hi forum, in my bash script I've many lines executing commands with redirection to log files. ... xyz_cmd 2>&1 > $BASENAME.$LINENO The trailing part of these lines doesn't look nice and I like to put it into a variable. The (not working) idea is something like that ... (3 Replies)
Discussion started by: wolfi089
3 Replies

4. Shell Programming and Scripting

adding delay in script

Hi Expert, I need to run some command 5 times with 5 mins interval whenever one of the application on server hang. Can I use below scripts ? Thanks! ------------- #!/bin/sh command1 sleep 300 command2 sleep 300 command3 sleep 300 command4 sleep 300 command5 sleep 300... (2 Replies)
Discussion started by: skully
2 Replies

5. Shell Programming and Scripting

while loop and delay

Dear all, if we want to run a command every 5 mins to check if the process is working fine or not... like in c, we can use a simple while loop with a delay for 5 mins... how can we accomplish this is solaris 8/9 thanks br/asad (5 Replies)
Discussion started by: asadlone
5 Replies

6. Programming

Delay a process.

How to delay a process. I need to to delay a process from 3sec. At that 3sec other back ground processes also should stop. (just sit 3sec for idle & then starts execution as normally) I use sleep(3)-But it not stop the bg processes I try to use loop but it not gurantee to wait 3sec. ... (2 Replies)
Discussion started by: ugp
2 Replies

7. Solaris

lock time delay

I have a Sol system. The lock timeout is default 15 minutes. I tried to make it longer but cannot by lock -t timeout Anyon can tell me the cmd in solai for this please. A thank in advance (2 Replies)
Discussion started by: part-time-user
2 Replies

8. Programming

Introducing Delay less then a second.

Hi, I have a doubt in introducing a delay in the programs. We know that we do have a sleep() function/api using which we can bring a delay in terms of seconds. A minimum delay can be atleast 1 second. Now I'm bothered about how to introduce a delay that is just less than a second. Like... (3 Replies)
Discussion started by: S.Vishwanath
3 Replies

9. UNIX for Dummies Questions & Answers

Delay in mv

Working on AIX 4.3 I have an active exe that accepts files for processing on our RS6000. Day to day i store these files in a secure place and at the end of the day I mv them one by one. After some reading and ofcourse trial and error i figured out that this helps... mv `ls -l |head -l | awk... (2 Replies)
Discussion started by: buRst
2 Replies

10. UNIX for Dummies Questions & Answers

Login Delay

When telneting to a SUN box and incorrectly spelling your password, there is a delay of approx. 5 seconds. Is there any way to shorten this time? OS solaris 7 (1 Reply)
Discussion started by: SmartJuniorUnix
1 Replies
Login or Register to Ask a Question