home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!sdd.hp.com!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.unix.internals
- Subject: Re: How can a Unix process put itself in background?
- Date: 12 Sep 1992 02:49:08 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 32
- Message-ID: <18rlr4INNi23@early-bird.think.com>
- References: <1992Sep11.170405.28836@tandem.com> <18qopsINN12n@early-bird.think.com> <1992Sep11.222546.3878@tandem.com>
- NNTP-Posting-Host: telecaster.think.com
-
- In article <1992Sep11.222546.3878@tandem.com> norcott_bill@tandem.com (Bill Norcott) writes:
- >In article <18qopsINN12n@early-bird.think.com>, barmar@think.com (Barry Margolin) writes:
- >|> You still have to fork first, as the shell (or whatever process started the
- >|> program) waits for the original process to exit.
- >
- >Sure, all UNIX user processes are created by fork(). So the process
- >that wants to put itself into the background was created by fork().
- >And it's parent should be using waidpid() or wait() to wait for the
- >death of the child. But the original question was, how can the guy's
- >existing (child) process put ITSELF in the background. This is where
- >setsid() and setpgid() are used.
-
- A background process is one that runs in parallel with the one that
- originally created it. The child process has to fork() a grandchild, so
- that the child can exit(), which will cause the parent's wait() to return
- and allow it to continue processing. Simply calling setsid() and setpgid()
- won't cause the parent's wait() or waitpid() to return.
-
- The only thing that setsid() and setpgid() do is dissociate the process
- from the original process group. And process groups only affect signal
- handling. For instance, if the child calls setpgid(), and then the phone
- line is hung up, the child process won't receive a SIGHUP because it's no
- longer in the controlling terminal's process group. So, whether it's
- appropriate to call setsid() or setpgid() depends on whether you want the
- background process to be immune to signals from the terminal (depending on
- what you're doing, you may actually want a background process to be killed
- when you hang up -- consider a program like leave(1)).
- --
- Barry Margolin
- System Manager, Thinking Machines Corp.
-
- barmar@think.com {uunet,harvard}!think!barmar
-