Operating System - HP-UX
1828406 Members
3256 Online
109977 Solutions
New Discussion

question regarding background process in shell

 
vasundhara
Frequent Advisor

question regarding background process in shell

Hi,

I just want to know the advantages and disadvantages of a process running in back ground. In a script, I found the following piece of code which I did not get the logic behind...

#!/bin/sh
while true do
sleep 5 >/dev/null&
bgpid=$!
wait $bgpid
done

What will be the difference if I write the above code as ...

#!/bin/sh
while true do
sleep 5 >/dev/null
done

any advantage of having sleep in background and waiting for the process to terminate? Will there be any problem if I make sleep in the current shell and remove the wait??? Please clarify...

Regards
VJ.
17 REPLIES 17
Massimo Bianchi
Honored Contributor

Re: question regarding background process in shell

It is an exercise left there :)

There is really no difference in this script, but looks like a test for a real script.

What was its name ?

Base question is: what is the purpose of a script, that do only sleep ??

Massimo

Graham Cameron_1
Honored Contributor

Re: question regarding background process in shell

VJ.

If you rewrite the code as suggested it will behave exactly the same.
The original code runs a background process and then waits for it to complete - absolutely pointless.

The reason to run things in background is so that you can do some other processing while the background job executes. Maybe your original script used to do this but got changed along the way, that's the only explanation I can think of.

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
vasundhara
Frequent Advisor

Re: question regarding background process in shell

Hi,

Thnx alot for the quick responce. My only doubt is will there be aby difference in the utilisation of the resources when a command is run in bg/fg?

Will assign points without fail.

Regards
VJ.
Graham Cameron_1
Honored Contributor

Re: question regarding background process in shell

Can't imagine any resource implications of running a process in background or foreground.
The only difference is that with a background job, the shell continues processing commands (until it does a "wait").
There is the potential to start several background jobs simultaneously, which, if each is resource intensive, could cause problems, but with a trivial thing like sleep, there is no issue.

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Sunil Sharma_1
Honored Contributor

Re: question regarding background process in shell

Hi,

In back ground process are running in less priority then in foreground.

Sunil
*** Dream as if you'll live forever. Live as if you'll die today ***
Massimo Bianchi
Honored Contributor

Re: question regarding background process in shell

From a performance point of view, the background form is a little heavier, because you have to handle the bg....

But we are talking about infinitesimals....

Massimo

John Carr_2
Honored Contributor

Re: question regarding background process in shell

Hi if you put a process into background you can then use the nice command to control the amount of process given to the script.

But as the script only sleeps its not worth the bother.

I dont think you need to send output from sleep to /dev/null as there should be none

John.
Mark Grant
Honored Contributor

Re: question regarding background process in shell

I think the first form is a bug.

As far as I remember, in the shell, "wait" takes a job number not a process ID or it takes no argument thus "waiting" for all child processes to finish.

Having said that, I think the intention is to demonstrate the "wait" command but I think "sleep" is a bad example of where it might be useful. Also, redirecting output to /dev/null seems a bit dramatic to me and very few people would ever bother to do that.
Never preceed any demonstration with anything more predictive than "watch this"
Massimo Bianchi
Honored Contributor

Re: question regarding background process in shell

For Mark: wait needs a PID

the job-id you are mentioning is used with the commands fg and the other job control if any other command :)

Massimo
Mark Grant
Honored Contributor

Re: question regarding background process in shell

Massimo,

I have to say that it would seem to make sense to use a PID bearing in mind that that is what you prbably want, however I just checked man sh-posix and I get this

wait [job]

Never preceed any demonstration with anything more predictive than "watch this"
Massimo Bianchi
Honored Contributor

Re: question regarding background process in shell

Mark: i checked the man page, and it turned out to be wait [pid].


So, i think that there are both the bulti-in command wait and the external command wait..

Look at this:

yomodvts:/var/mail> which wait
/usr/bin/wait
yomodvts:/var/mail> typeset wait
yomodvts:/var/mail> type wait
wait is a shell builtin.
yomodvts:/var/mail> file /usr/bin/wait
/usr/bin/wait: commands text
yomodvts:/var/mail> more /usr/bin/wait
#!/usr/bin/sh
# @(#) $Revision: 72.2 $

# This is the execable version of wait utility implemented
# using posix shell built-in wait command.

wait $@
exit $?



#################################


I didn't ever checked it before ,
sorry :(

We both were perfectly right.

Massimo
Mark Grant
Honored Contributor

Re: question regarding background process in shell

Isn't it nice when we can both be right :)

Never preceed any demonstration with anything more predictive than "watch this"
vasundhara
Frequent Advisor

Re: question regarding background process in shell

Thanks alot for all the responces. I just want to check at least one resouce utilisation factor for both cases mentioned above. For example, how much memory will be used when its run in bg/fg.

Can anyone help me how to find memory used for a particular process???
Michael Schulte zur Sur
Honored Contributor

Re: question regarding background process in shell

Hi,

I use backgound processes in a shell, when there is a chance, that the command issued may hang, like in my case ftp. I wait, see the return codes in the log file and after five minutes I terminate frp and start from scratch.

You can see the size of a process by:
UNIX95= ps -eo pid,ppid,sz

greetings,

Michael
Mark Grant
Honored Contributor

Re: question regarding background process in shell

The difference between these two on memory consumption is going to be so tiny, I can't see how you would measure it. The first one is slightly longer and therefore requires slightly more memory though possibly not and I guess the shell allocates memory is chunks anyway so the difference probably doesn't exist.

I think you need to realise that unix is elegant. The only reason anything runs in the foreground at all is because processes open the terminal as their standard input and the shell thinks it would be a good idea to wait for them to finish. If you use the "&", the shell decides not to wait for it and the standard input is closed.

This means that resource wise, the two are almost identical.
Never preceed any demonstration with anything more predictive than "watch this"
Graham Cameron_1
Honored Contributor

Re: question regarding background process in shell

>>Can anyone help me how to find memory used for a particular process???

Look in the "sz" col of "ps -l"

"man ps" for more info.

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
John Palmer
Honored Contributor

Re: question regarding background process in shell

Actually there is a subtle difference between the two, how the shell handles signals.

If you have the 'sleep' running in the foreground, any kill signal passed to the shell will be processed AFTER sleep exits. In the case where the shell is waiting for the background process signals will be processed IMMEDIATELY.

Of course, this matters only if your script has been written to process signals in some way e.g. trap 'whatever' n

Regards,
John