How to avoid background command crashed in shell?

I have program a shell to start several process.All process are running in
background, but only one process running
in foreground.

run.sh
<
#!/bin/ksh

proc1 &
proc2 &
proc3 &

proc_fore4 #for display info

when I start the shell ,it is ok, all process running rightly.
But when I use “ctrl+c” to terminate the foreground process “proc_fore4”,all
the
processes in the shell running in background are also terminated!
The proc1,proc2,proc3 are all running in background!!!
why does this happen?

if I only start the 3 background processes ,and then use command line to
running the “proc_fore4”.Every thing is ok(the 3 process are running ok
,never terminated), even if I terminate the process “proc_fore4”.

How to avoid the background process terminated when I terminate the
foreground process start in shell?

How to avoid the background process terminated when I
terminate the foreground process start in shell?

This behaviour is dependent upon the shell you are running.
Under bash it will work as you wish, whereas under sh it will
not.

If the programs are written by you, you could add a setsid()
call to the programs you want in the background. This will
detach them from the controlling terminal.

Regards
William Morris

If the programs are written by you, you could add a setsid()
call to the programs you want in the background. This will
detach them from the controlling terminal.

Only if you where not already a session leader, otherwise setsid() returns
with an error. You should fork() first, to guarantee you’re not a session
leader before doing a setsid(). Or you could use the QNX specific method
via procmgr_daemon() to move to the background.

-Adam

It seems that the process running in background is different from the one
running in daemon state.
The process running in background maybe not detached with the controlling
terminal.
But the one running in daemon state must be detached with the controlling
terminal.
Above is right?

I use QNX specific method “procmgr_daemon()”.
if I call the function directly in process,the process running ok.
But if I put the function
procmgr_daemon(EXIT_SUCCESS,PROCMGR_DAEMON_NODEVNULL ) in share lib(I named
it libconproc.so),when I use the lib ,the whole system crashed.It seems that
the function can’t used in share library. In the process,I use some
mechanism ,such as message queue, share memory and so on.I don’t know why
the system crash?

Another question.When a process running in daemon status, can it use open()
or read()/write() function to read/write file?

“Adam Mallory” <amallory@qnx.com> wrote in message
news:asofnl$p1p$1@nntp.qnx.com

snip
If the programs are written by you, you could add a setsid()
call to the programs you want in the background. This will
detach them from the controlling terminal.

Only if you where not already a session leader, otherwise setsid() returns
with an error. You should fork() first, to guarantee you’re not a session
leader before doing a setsid(). Or you could use the QNX specific method
via procmgr_daemon() to move to the background.

-Adam

zhz_zhang <zhz_zhang26@sina.com> wrote in message
news:asp55t$bjj$1@inn.qnx.com

It seems that the process running in background is different from the one
running in daemon state.
The process running in background maybe not detached with the controlling
terminal.
But the one running in daemon state must be detached with the controlling
terminal.
Above is right?

Well you can ensure that you’re detached from the controlling terminal and
not a session leader via fork()/setsid()/fork().

I use QNX specific method “procmgr_daemon()”.
if I call the function directly in process,the process running ok.
But if I put the function
procmgr_daemon(EXIT_SUCCESS,PROCMGR_DAEMON_NODEVNULL ) in share lib(I
named
it libconproc.so),when I use the lib ,the whole system crashed.It seems
that
the function can’t used in share library. In the process,I use some
mechanism ,such as message queue, share memory and so on.I don’t know why
the system crash?

I’m not sure what you mean by “whole system crashed” - you mean the entire
OS is unresponsive or just your software system crashed. I did a quick test
of a process which links in a shared lib, and in the shared lib does the
procmgr_daemon() call; it worked fine as I would expect. Perhaps you should
post a small example that illustrates your problem; it could be you’re not
building your shared lib correctly, or there is a mix of PIC and non PIC
code causing you grief.

Another question.When a process running in daemon status, can it use
open()
or read()/write() function to read/write file?

Yes, just watch out for read/write against stdin/out/err since they status
changes depending on flags you pass to procmgr_daemon().

-Adam