From: barmar@think.com (Barry Margolin)
Newsgroups: comp.unix.internals
Subject: Re: Background processes and stdin
Date: 21 Jul 1992 22:48:45 GMT
Organization: Thinking Machines Corporation, Cambridge MA, USA
Distribution: na
NNTP-Posting-Host: telecaster.think.com

In article <1992Jul21.023713.2702@hubcap.clemson.edu> rsaripa@saturn.cs.clemson.edu (Saripalli Ramakrishna) writes:
>	When a background process needs terminal input,it is stopped.
>	The message "Stopped " appears on the output.
>
>	Now,I would like to know why this happens.I think the signal
>	SIGTTIN is sent to the process but why it is sent?.

Here are excerpts from the SunOS termio(4) man page that answer this.

  Process Groups
     A terminal may have a distinguished process group associated
     with  it.   This distinguished process group plays a special
     role in handling signal-generating input characters, as dis-
     cussed  below  in the Special Characters section below.  The
     terminal's process group can can  be  set  only  to  process
     groups that are members of the terminal's session.

     A command interpreter, such as csh(1),  that  supports  "job
     control"  can  allocate  the  terminal to different jobs, or
     process groups, by placing related  processes  in  a  single
     process  group  and  associating this process group with the
     terminal.  A terminal's associated process group may be  set
     or  examined  by  a process with sufficient privileges.  The
     terminal interface aids in this  allocation  by  restricting
     access  to  the  terminal  by  processes that are not in the
     current process group; see Job Access Control below.
...
  Job Access Control
     If a process is  in  the  (non-zero)  distinguished  process
     group of its controlling terminal (if this is true, the pro-
     cess is said to be  a  foreground  process),  then  read(2V)
     operations  are allowed as described below in Input Process-
     ing and Reading Characters.  If a  process  is  not  in  the
     (non-zero)  distinguished  process  group of its controlling
     terminal (if this is true, the process is said to be a back-
     ground  process), then any attempts to read from that termi-
     nal will  typically  send  that  process'  process  group  a
     SIGTTIN  signal.   If  the  process is ignoring SIGTTIN, has
     SIGTTIN blocked, is a member of an orphaned  process  group,
     or  is in the middle of process creation using vfork(2), the
     read will return -1 and set errno to EIO,  and  the  SIGTTIN
     signal  will  not be sent.  The SIGTTIN signal will normally
     stop the members of that process group.

     When the TOSTOP bit is set in the c_lflag field, attempts by
     a  background  process  to write to its controlling terminal
     will typically send that process' process  group  a  SIGTTOU
     signal.   If  the  process  is ignoring SIGTTOU, has SIGTTOU
     blocked, or is in  the  middle  of  process  creation  using
     vfork(),  the process will be allowed to write to the termi-
     nal and the SIGTTOU signal will not be sent. If the  process
     is  orphaned, the write will return -1 and set errno to EIO,
     and the SIGTTOU signal will not be sent. SIGTTOU signal will
     normally  stop  the  members of that process group.  Certain
     ioctl() calls that set terminal parameters  are  treated  in
     this  same  fashion,  except that TOSTOP is not checked; the
     effect is identical to that of terminal writes  when  TOSTOP
     is set.  See IOCTLS.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

barmar@think.com          {uunet,harvard}!think!barmar