Operating System - HP-UX
1821638 Members
3127 Online
109633 Solutions
New Discussion юеВ

program in background and foreground

 
Ravi_8
Honored Contributor

program in background and foreground

Hi,

What is the difference between running a program (a shell script or an executable) in the background versus in the foreground? It would be good to know all the technicalities involved.
In our case we are facing a major problem where a client gets an unknown exception(sometime even hang) when the server program is run in foreground and everything works when the server program is run in the background.

Client is an RPC client and we have an HP DCE RPC only configuration.

thanx in advance
never give up
3 REPLIES 3
Elmar P. Kolkman
Honored Contributor

Re: program in background and foreground

For unix, there is no real difference between both modes of a proces. But sometimes a shell sends signals to processes running in foreground when a user does something (think off the ^Z in ksh, for instance, or a ^C in most shells). Per default it is best to run a daemon in background or have the daemon itself force this by starting with a fork and exiting the main process.
Every problem has at least one solution. Only some solutions are harder to find.
Mark Grant
Honored Contributor

Re: program in background and foreground

Ravi,

A process run in the background is only one in which the shell doesn't wait for the execution to finish. As far am I am aware, that is the only difference apart from the fact that processes running in the background, by default, stop if they attempt to read from the terminal.

What exactly do you mean by running it in the foregorund? Do you mean that if you start the application from the shell it just sits there and everntually starts to experience problems or do you mean that "cron" or startup scripts start it and it only works nice if you put a "&" at the end of the command.

Never preceed any demonstration with anything more predictive than "watch this"
Mike Stroyan
Honored Contributor

Re: program in background and foreground

Different people attach different meaning to foreground and background, so "your mileage may vary".

Shells such as posix shell will put background jobs in a different process group. That is discussed in "man setsid", "man setpgid", and "man termio". Processes in a background process group can't read from the controlling terminal. They may also be prevented from writing to the controlling terminal. Only the processes in the foreground process group receive termio signals such as SIGINT and SIGTSTP.

Shells often reduce the priority of background processes by using nice(). See "man 2 nice". That could affect the timing of operations in a troubled program. A coding error could cause a problem when an application runs more quickly, attempting to use some resource before it is ready.