home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / Documents / FAQ / Unix / Unix.faq3 < prev    next >
Text File  |  1993-01-27  |  27KB  |  615 lines

  1. Archive-name: unix-faq/part3
  2. Version: $Id: part3,v 1.4 92/03/19 14:07:42 tmatimar Exp $
  3.  
  4. These four articles contain the answers to some Frequently Asked
  5. Questions often seen in comp.unix.questions and comp.unix.shell.
  6. Please don't ask these questions again, they've been answered plenty
  7. of times already - and please don't flame someone just because they may
  8. not have read this particular posting.  Thank you.
  9.  
  10. These articles are divided approximately as follows:
  11.  
  12.       1.*) General questions.
  13.       2.*) Relatively basic questions, likely to be asked by beginners.
  14.       3.*) Intermediate questions.
  15.       4.*) Advanced questions, likely to be asked by people who thought
  16.        they already knew all of the answers.
  17.  
  18. This article includes answers to:
  19.  
  20.       3.1)  How do I find out the creation time of a file?
  21.       3.2)  How do I use "rsh" without having the rsh hang around
  22.               until the remote command has completed?
  23.       3.3)  How do I truncate a file?
  24.       3.4)  Why doesn't find's "{}" symbol do what I want?
  25.       3.5)  How do I set the permissions on a symbolic link?
  26.       3.6)  How do I "undelete" a file?
  27.       3.7)  How can a process detect if it's running in the background?
  28.       3.8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  29.       3.9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  30.               programs from a shell script or in the background?
  31.       3.10) How do I find out the process ID of a program with a particular
  32.             name from inside a shell script or C program?
  33.       3.11) How do I check the exit status of a remote command
  34.             executed via "rsh" ?
  35.       3.12) Is it possible to pass shell variable settings into an awk program?
  36.       3.13) How do I get rid of zombie processes that persevere?
  37.       3.14) How do I get lines from a pipe as they are written instead of
  38.             only in larger blocks.
  39.  
  40.     If you're looking for the answer to, say, question 3.5, and want to skip
  41.     everything else, you can search ahead for the regular expression "^5)".
  42.  
  43. While these are all legitimate questions, they seem to crop up in
  44. comp.unix.questions on an annual basis, usually followed by plenty
  45. of replies (only some of which are correct) and then a period of
  46. griping about how the same questions keep coming up.  You may also like
  47. to read the monthly article "Answers to Frequently Asked Questions"
  48. in the newsgroup "news.announce.newusers", which will tell you what
  49. "UNIX" stands for.
  50.  
  51. With the variety of Unix systems in the world, it's hard to guarantee
  52. that these answers will work everywhere.  Read your local manual pages
  53. before trying anything suggested here.  If you have suggestions or
  54. corrections for any of these answers, please send them to to
  55. tmatimar@nff.ncl.omron.co.jp.
  56.  
  57. 1)  How do I find out the creation time of a file?
  58.  
  59.     You can't - it isn't stored anywhere.  Files have a last-modified
  60.     time (shown by "ls -l"), a last-accessed time (shown by "ls -lu")
  61.     and an inode change time (shown by "ls -lc"). The latter is often
  62.     referred to as the "creation time" - even in some man pages -  but
  63.     that's wrong; it's also set by such operations as mv, ln,
  64.     chmod, chown and chgrp.
  65.  
  66.     The man page for "stat(2)" discusses this.
  67.  
  68. 2)  How do I use "rsh" without having the rsh hang around until the
  69.     remote command has completed?
  70.  
  71.     (See note in question 2.7 about what "rsh" we're talking about.)
  72.  
  73.     The obvious answers fail:
  74.             rsh machine command &
  75.     or      rsh machine 'command &'
  76.  
  77.     For instance, try doing   rsh machine 'sleep 60 &'
  78.     and you'll see that the 'rsh' won't exit right away.
  79.     It will wait 60 seconds until the remote 'sleep' command
  80.     finishes, even though that command was started in the
  81.     background on the remote machine.  So how do you get
  82.     the 'rsh' to exit immediately after the 'sleep' is started?
  83.  
  84.     The solution - if you use csh on the remote machine:
  85.  
  86.         rsh machine -n 'command >&/dev/null </dev/null &'
  87.  
  88.     If you use sh on the remote machine:
  89.  
  90.         rsh machine -n 'command >/dev/null 2>&1 </dev/null &'
  91.  
  92.     Why?  "-n" attaches rsh's stdin to /dev/null so you could run the
  93.     complete rsh command in the background on the LOCAL machine.
  94.     Thus "-n" is equivalent to another specific "< /dev/null".
  95.     Furthermore, the input/output redirections on the REMOTE machine
  96.     (inside the single quotes) ensure that rsh thinks the session can
  97.     be terminated (there's no data flow any more.)
  98.  
  99.     Note: The file that you redirect to/from on the remote machine
  100.     doesn't have to be /dev/null; any ordinary file will do.
  101.  
  102.     In many cases, various parts of these complicated commands
  103.     aren't necessary.
  104.  
  105. 3)  How do I truncate a file?
  106.  
  107.     The BSD function ftruncate() sets the length of a file.  Xenix -
  108.     and therefore SysV r3.2 and later - has the chsize() system call.
  109.     For other systems, the only kind of truncation you can do is
  110.     truncation to length zero with creat() or open(..., O_TRUNC).
  111.  
  112. 4)  Why doesn't find's "{}" symbol do what I want?
  113.  
  114.     "find" has a -exec option that will execute a particular
  115.     command on all the selected files. Find will replace any "{}"
  116.     it sees with the name of the file currently under consideration.
  117.  
  118.     So, some day you might try to use "find" to run a command on every
  119.     file, one directory at a time.  You might try this:
  120.  
  121.     find /path -type d -exec command {}/\* \;
  122.  
  123.     hoping that find will execute, in turn
  124.  
  125.     command directory1/*
  126.     command directory2/*
  127.     ...
  128.  
  129.     Unfortunately, find only expands the "{}" token when it appears
  130.     by itself.  Find will leave anything else like "{}/*" alone, so
  131.     instead of doing what you want, it will do
  132.  
  133.     command {}/*
  134.     command {}/*
  135.     ...
  136.  
  137.     once for each directory.  This might be a bug, it might be a feature,
  138.     but we're stuck with the current behaviour.
  139.  
  140.     So how do you get around this?  One way would be to write a
  141.     trivial little shell script, let's say "./doit", that
  142.     consists of
  143.     
  144.     command "$1"/*
  145.  
  146.     You could then use
  147.  
  148.     find /path -type d -exec ./doit {} \;
  149.  
  150.     Or if you want to avoid the "./doit" shell script, you can use
  151.  
  152.     find /path -type d -exec sh -c 'command $0/*' {} \;
  153.  
  154.     (This works because within the 'command' of "sh -c 'command' A B C ...",
  155.      $0 expands to A, $1 to B, and so on.)
  156.  
  157.     or you can use the construct-a-command-with-sed trick
  158.  
  159.     find /path -type d -print | sed 's:.*:command &/*:' | sh
  160.  
  161.     If all you're trying to do is cut down on the number of times
  162.     that "command" is executed, you should see if your system
  163.     has the "xargs" command.  Xargs reads arguments one line at a time
  164.     from the standard input and assembles as many of them as will fit into
  165.     one command line.  You could use
  166.  
  167.     find /path -print | xargs command
  168.  
  169.     which would result in one or more executions of
  170.  
  171.     command file1 file2 file3 file4 dir1/file1 dir1/file2
  172.  
  173.     Unfortunately this is not a perfectly robust or secure solution.
  174.     Xargs expects its input lines to be terminated with newlines, so it
  175.     will be confused by files with odd characters such as newlines
  176.     in their names.
  177.  
  178. 5)  How do I set the permissions on a symbolic link?
  179.  
  180.     Permissions on a symbolic link don't really mean anything.  The
  181.     only permissions that count are the permissions on the file that
  182.     the link points to.
  183.  
  184. 6)  How do I "undelete" a file?
  185.  
  186.     Someday, you are going to accidentally type something like "rm * .foo",
  187.     and find you just deleted "*" instead of "*.foo".  Consider it a rite
  188.     of passage.
  189.  
  190.     Of course, any decent systems administrator should be doing regular
  191.     backups.  Check with your sysadmin to see if a recent backup copy
  192.     of your file is available.  But if it isn't, read on.
  193.  
  194.     For all intents and purposes, when you delete a file with "rm" it is
  195.     gone.  Once you "rm" a file, the system totally forgets which blocks
  196.     scattered around the disk comprised your file.  Even worse, the blocks
  197.     from the file you just deleted are going to be the first ones taken
  198.     and scribbled upon when the system needs more disk space.  However,
  199.     never say never.  It is theoretically possible *if* you shut down
  200.     the system immediately after the "rm" to recover portions of the data.
  201.     However, you had better have a very wizardly type person at hand with
  202.     hours or days to spare to get it all back.
  203.  
  204.     Your first reaction when you "rm" a file by mistake is why not make
  205.     a shell alias or procedure which changes "rm" to move files into a
  206.     trash bin rather than delete them?  That way you can recover them if
  207.     you make a mistake, and periodically clean out your trash bin.  Two
  208.     points:  first, this is generally accepted as a *bad* idea.  You will
  209.     become dependent upon this behaviour of "rm", and you will find
  210.     yourself someday on a normal system where "rm" is really "rm", and
  211.     you will get yourself in trouble.  Second, you will eventually find
  212.     that the hassle of dealing with the disk space and time involved in
  213.     maintaining the trash bin, it might be easier just to be a bit more
  214.     careful with "rm".  For starters, you should look up the "-i" option
  215.     to "rm" in your manual.
  216.  
  217.     If you are still undaunted, then here is a possible simple answer.  You
  218.     can create yourself a "can" command which moves files into a
  219.     trashcan directory. In csh(1) you can place the following commands
  220.     in the ".login" file in your home directory:
  221.  
  222.     alias can    'mv \!* ~/.trashcan'       # junk file(s) to trashcan
  223.     alias mtcan    'rm -f ~/.trashcan/*'       # irretrievably empty trash
  224.     if ( ! -d ~/.trashcan ) mkdir ~/.trashcan  # ensure trashcan exists
  225.  
  226.     You might also want to put a:
  227.  
  228.     rm -f ~/.trashcan/*
  229.  
  230.     in the ".logout" file in your home directory to automatically empty
  231.     the trash when you log out.  (sh and ksh versions are left as an
  232.     exercise for the reader.)
  233.  
  234.     MIT's Project Athena has produced a comprehensive
  235.     delete/undelete/expunge/purge package, which can serve as a
  236.     complete replacement for rm which allows file recovery.  This
  237.     package was posted to comp.sources.misc (volume 17, issue 023-026)
  238.  
  239. 7)  How can a process detect if it's running in the background?
  240.  
  241.     First of all: do you want to know if you're running in the background,
  242.     or if you're running interactively? If you're deciding whether or
  243.     not you should print prompts and the like, that's probably a better
  244.     criterion. Check if standard input is a terminal:
  245.  
  246.         sh: if [ -t 0 ]; then ... fi
  247.         C: if(isatty(0)) { ... }
  248.  
  249.     In general, you can't tell if you're running in the background.
  250.     The fundamental problem is that different shells and different
  251.     versions of UNIX have different notions of what "foreground" and
  252.     "background" mean - and on the most common type of system with a
  253.     better-defined notion of what they mean, programs can be moved
  254.     arbitrarily between foreground and background!
  255.  
  256.     UNIX systems without job control typically put a process into the
  257.     background by ignoring SIGINT and SIGQUIT and redirecting the standard
  258.     input to "/dev/null"; this is done by the shell.
  259.  
  260.     Shells that support job control, on UNIX systems that support job
  261.     control, put a process into the background by giving it a process group
  262.     ID different from the process group to which the terminal belongs.  They
  263.     move it back into the foreground by setting the terminal's process group
  264.     ID to that of the process.  Shells that do *not* support job control, on
  265.     UNIX systems that support job control, typically do what shells do on
  266.     systems that don't support job control.
  267.  
  268. 8)  Why doesn't redirecting a loop work as intended?  (Bourne shell)
  269.  
  270.     Take the following example:
  271.  
  272.     foo=bar
  273.  
  274.     while read line
  275.     do
  276.         # do something with $line
  277.         foo=bletch
  278.     done < /etc/passwd
  279.  
  280.     echo "foo is now: $foo"
  281.  
  282.     Despite the assignment ``foo=bletch'' this will print ``foo is now: bar''
  283.     in many implementations of the Bourne shell.  Why?
  284.     Because of the following, often undocumented, feature of historic
  285.     Bourne shells: redirecting a control structure (such as a loop, or an
  286.     ``if'' statement) causes a subshell to be created, in which the structure
  287.     is executed; variables set in that subshell (like the ``foo=bletch''
  288.     assignment) don't affect the current shell, of course.
  289.  
  290.     The POSIX 1003.2 Shell and Tools Interface standardization committee
  291.     forbids the behaviour described above, i.e. in P1003.2 conformant
  292.     Bourne shells the example will print ``foo is now: bletch''.
  293.  
  294.     In historic (and P1003.2 conformant) implementations you can use the
  295.     following `trick' to get around the redirection problem:
  296.  
  297.     foo=bar
  298.  
  299.     # make file descriptor 9 a duplicate of file descriptor 0 (stdin);
  300.     # then connect stdin to /etc/passwd; the original stdin is now
  301.     # `remembered' in file descriptor 9; see dup(2) and sh(1)
  302.     exec 9<&0 < /etc/passwd
  303.  
  304.     while read line
  305.     do
  306.         # do something with $line
  307.         foo=bletch
  308.     done
  309.  
  310.     # make stdin a duplicate of file descriptor 9, i.e. reconnect it to
  311.     # the original stdin; then close file descriptor 9
  312.     exec 0<&9 9<&-
  313.  
  314.     echo "foo is now: $foo"
  315.  
  316.     This should always print ``foo is now: bletch''.
  317.     Right, take the next example:
  318.  
  319.     foo=bar
  320.  
  321.     echo bletch | read foo
  322.  
  323.     echo "foo is now: $foo"
  324.  
  325.     This will print ``foo is now: bar'' in many implementations,
  326.     ``foo is now: bletch'' in some others.  Why?
  327.     Generally each part of a pipeline is run in a different subshell;
  328.     in some implementations though, the last command in the pipeline is
  329.     made an exception: if it is a builtin command like ``read'', the current
  330.     shell will execute it, else another subshell is created.
  331.  
  332.     POSIX 1003.2 allows both behaviours so portable scripts cannot depend
  333.     on any of them.
  334.  
  335. 9)  How do I run 'passwd', 'ftp', 'telnet', 'tip' and other interactive
  336.     programs from a shell script or in the background?
  337.  
  338.     These programs expect a terminal interface.  Shells makes no special
  339.     provisions to provide one.  Hence, such programs cannot be automated
  340.     in shell scripts.
  341.  
  342.     The 'expect' program provides a programmable terminal interface for
  343.     automating interaction with such programs.  The following expect
  344.     script is an example of a non-interactive version of passwd(1).
  345.  
  346.         # username is passed as 1st arg, password as 2nd
  347.         set password [index $argv 2]
  348.         spawn passwd [index $argv 1]
  349.         expect "*password:"
  350.         send "$password\r"
  351.         expect "*password:"
  352.         send "$password\r"
  353.         expect eof
  354.  
  355.     expect can partially automate interaction which is especially
  356.     useful for telnet, rlogin, debuggers or other programs that have no
  357.     built-in command language.  The distribution provides an example
  358.     script to rerun rogue until a good starting configuration appears.
  359.     Then, control is given back to the user to enjoy the game.
  360.  
  361.     Fortunately some programs have been written to manage the connection
  362.     to a pseudo-tty so that you can run these sorts of programs in a script.
  363.  
  364.     To get expect, email "send pub/expect/expect.shar.Z" to
  365.     library@cme.nist.gov or anonymous ftp same from durer.cme.nist.gov.
  366.  
  367.     Another solution is provided by the pty 4.0 program, which runs a
  368.     program under a pseudo-tty session and was posted to comp.sources.unix,
  369.     volume 25.  A pty-based solution using named pipes to do the same as
  370.     the above might look like this:
  371.  
  372.     #!/bin/sh
  373.     /etc/mknod out.$$ p; exec 2>&1
  374.     ( exec 4<out.$$; rm -f out.$$
  375.     <&4 waitfor 'password:'
  376.         echo "$2"
  377.     <&4 waitfor 'password:'
  378.         echo "$2"
  379.     <&4 cat >/dev/null
  380.     ) | ( pty passwd "$1" >out.$$ )
  381.  
  382.     Here, 'waitfor' is a simple C program that searches for
  383.     its argument in the input, character by character.
  384.  
  385.     A simpler pty solution (which has the drawback of not
  386.     synchronizing properly with the passwd program) is
  387.  
  388.     #!/bin/sh
  389.     ( sleep 5; echo "$2"; sleep 5; echo "$2") | pty passwd "$1"
  390.  
  391. 10) How do I find out the process ID of a program with a particular
  392.     name from inside a shell script or C program?
  393.  
  394.     In a shell script:
  395.  
  396.     There is no utility specifically designed to map between program names
  397.     and process IDs.  Furthermore, such mappings are often unreliable,
  398.     since it's possible for more than one process to have the same name,
  399.     and since it's possible for a process to change its name once it
  400.     starts running.  However, a pipeline like this can often be used to
  401.     get a list of processes (owned by you) with a particular name:
  402.  
  403.         ps ux | awk '/name/ && !/awk/ {print $2}'
  404.  
  405.     You replace "name" with the name of the process for which you are
  406.     searching.
  407.  
  408.     The general idea is to parse the output of ps, using awk or grep or
  409.     other utilities, to search for the lines with the specified name on
  410.     them, and print the PID's for those lines.  Note that the "!/awk/"
  411.     above prevents the awk process for being listed.
  412.  
  413.     You may have to change the arguments to ps, depending on what kind of
  414.     Unix you are using.
  415.  
  416.     In a C program:
  417.  
  418.     Just as there is no utility specifically designed to map between
  419.     program names and process IDs, there are no (portable) C library
  420.     functions to do it either.
  421.  
  422.     However, some vendors provide functions for reading Kernel memory; for
  423.     example, Sun provides the "kvm_" functions, and Data General provides
  424.     the "dg_" functions.  It may be possible for any user to use these, or
  425.     they may only be useable by the super-user (or a user in group "kmem")
  426.     if read-access to kernel memory on your system is restricted.
  427.     Furthermore, these functions are often not documented or documented
  428.     badly, and might change from release to release.
  429.  
  430.     Some vendors provide a "/proc" filesystem, which appears as a
  431.     directory with a bunch of filenames in it.  Each filename is a number,
  432.     corresponding to a process ID, and you can open the file and read it
  433.     to get information about the process.  Once again, access to this may
  434.     be restricted, and the interface to it may change from system to
  435.     system.
  436.  
  437.     If you can't use vendor-specific library functions, and you don't have
  438.     /proc, and you still want to do this completely in C, you are going to
  439.     have to do the grovelling through kernel memory yourself.  For a good
  440.     example of how to do this on many systems, see the sources to
  441.     "ofiles", available in the comp.sources.unix archives.
  442.     (A package named "kstuff" to help with kernel grovelling was posted
  443.     to alt.sources in May 1991 and is also available via anonymous ftp as
  444.     usenet/alt.sources/articles/{329{6,7,8,9},330{0,1}}.Z from
  445.     wuarchive.wustl.edu.)
  446.  
  447. 11) How do I check the exit status of a remote command
  448.     executed via "rsh" ?
  449.  
  450.     This doesn't work:
  451.  
  452.     rsh some-machine some-crummy-command || echo "Command failed"
  453.  
  454.     The exit status of 'rsh' is 0 (success) if the rsh program
  455.     itself completed successfully, which probably isn't what
  456.     you wanted.
  457.  
  458.     If you want to check on the exit status of the remote program, you
  459.     can try using Maarten Litmaath's 'ersh' script, which was posted to
  460.     alt.sources in January, 1991.  ersh is a shell script that
  461.     calls rsh, arranges for the remote machine to echo the status
  462.     of the command after it completes, and exits with that status.
  463.  
  464. 12) Is it possible to pass shell variable settings into an awk program?
  465.  
  466.     There are two different ways to do this.  The first involves simply
  467.     expanding the variable where it is needed in the program.  For
  468.     example, to get a list of all ttys you're using:
  469.  
  470.      who | awk '/^'"$USER"'/ { print $2 }'                (1)
  471.  
  472.     Single quotes are usually used to enclose awk programs because the
  473.     character '$' is often used in them, and '$' will be interpreted by
  474.     the shell if enclosed inside double quotes, but not if enclosed
  475.     inside single quotes.  In this case, we *want* the '$' in "$USER"
  476.     to be interpreted by the shell, so we close the single quotes and
  477.     then put the "$USER" inside double quotes.  Note that there are no
  478.     spaces in any of that, so the shell will see it all as one
  479.     argument.  Note, further, that the double quotes probably aren't
  480.     necessary in this particular case (i.e. we could have done
  481.  
  482.      who | awk '/^'$USER'/ { print $2 }'                (2)
  483.  
  484.     ), but they should be included nevertheless because they are
  485.     necessary when the shell variable in question contains special
  486.     characters or spaces.
  487.  
  488.     The second way to pass variable settings into awk is to use an
  489.     often undocumented feature of awk which allows variable settings to
  490.     be specified as "fake file names" on the command line.  For
  491.     example:
  492.  
  493.      who | awk '$1 == user { print $2 }' user="$USER" -            (3)
  494.  
  495.     Variable settings take effect when they are encountered on the
  496.     command line, so, for example, you could instruct awk on how to
  497.     behave for different files using this technique.  For example:
  498.  
  499.      awk '{ program that depends on s }' s=1 file1 s=0 file2        (4)
  500.  
  501.     Note that some versions of awk will cause variable settings
  502.     encountered before any real filenames to take effect before the
  503.     BEGIN block is executed, but some won't so neither way should be
  504.     relied upon.
  505.  
  506.     Note, further, that when you specify a variable setting, awk won't
  507.     automatically read from stdin if no real files are specified, so
  508.     you need to add a "-" argument to the end of your command, as I did
  509.     at (3) above.
  510.  
  511. 13) How do I get rid of zombie processes that persevere?
  512.  
  513.     From: jik@pit-manager.MIT.Edu (Jonathan I. Kamens)
  514.     Date: Fri, 17 Jan 92 14:40:09 -0500
  515.  
  516.     Unfortunately, it's impossible to generalize how the death of child
  517.     processes should behave, because the exact mechanism varies over
  518.     the various flavors of Unix.
  519.  
  520.     First of all, by default, you have to do a wait() for child
  521.     processes under ALL flavors of Unix.  That is, there is no flavor
  522.     of Unix that I know of that will automatically flush child
  523.     processes that exit, even if you don't do anything to tell it to do
  524.     so.
  525.  
  526.     Second, under some SysV-derived systems, if you do "signal(SIGCHLD,
  527.     SIG_IGN)" (well, actually, it may be SIGCLD instead of SIGCHLD, but
  528.     most of the newer SysV systems have "#define SIGCHLD SIGCLD" in the
  529.     header files), then child processes will be cleaned up
  530.     automatically, with no further effort in your part.  The best way
  531.     to find out if it works at your site is to try it, although if you
  532.     are trying to write portable code, it's a bad idea to rely on this
  533.     in any case.  Unfortunately, POSIX doesn't allow you to do this;
  534.     the behavior of setting the SIGCHLD to SIG_IGN under POSIX is
  535.     undefined, so you can't do it if your program is supposed to be
  536.     POSIX-compliant.
  537.  
  538.     If you can't use SIG_IGN to force automatic clean-up, then you've
  539.     got to write a signal handler to do it.  It isn't easy at all to
  540.     write a signal handler that does things right on all flavors of
  541.     Unix, because of the following inconsistencies:
  542.  
  543.     On some flavors of Unix, the SIGCHLD signal handler is called if
  544.     one *or more* children have died.  This means that if your signal
  545.     handler only does one wait() call, then it won't clean up all of
  546.     the children.  Fortunately, I believe that all Unix flavors for
  547.     which this is the case have available to the programmer the wait3()
  548.     call, which allows the WNOHANG option to check whether or not there
  549.     are any children waiting to be cleaned up.  Therefore, on any
  550.     system that has wait3(), your signal handler should call wait3()
  551.     over and over again with the WNOHANG option until there are no
  552.     children left to clean up.
  553.  
  554.     On SysV-derived systems, SIGCHLD signals are regenerated if there
  555.     are child processes still waiting to be cleaned up after you exit
  556.     the SIGCHLD signal handler.  Therefore, it's safe on most SysV
  557.     systems to assume when the signal handler gets called that you only
  558.     have to clean up one signal, and assume that the handler will get
  559.     called again if there are more to clean up after it exits.
  560.  
  561.     On older systems, signal handlers are automatically reset to
  562.     SIG_DFL when the signal handler gets called.  On such systems, you
  563.     have to put "signal(SIGCHILD, catcher_func)" (where "catcher_func"
  564.     is the name of the handler function) as the first thing in the
  565.     signal handler, so that it gets reset.  Unfortunately, there is a
  566.     race condition which may cause you to get a SIGCHLD signal and have
  567.     it ignored between the time your handler gets called and the time
  568.     you reset the signal.  Fortunately, newer implementations of
  569.     signal() don't reset the handler to SIG_DFL when the handler
  570.     function is called.  To get around this problem, on systems that do
  571.     not have wait3() but do have SIGCLD, you need to reset the signal
  572.     handler with a call to signal() after doing at least one wait()
  573.     within the handler, each time it is called.
  574.  
  575.     The summary of all this is that on systems that have wait3(), you
  576.     should use that and your signal handler should loop, and on systems
  577.     that don't, you should have one call to wait() per invocation of
  578.     the signal handler.
  579.  
  580.     One more thing -- if you don't want to go through all of this
  581.     trouble, there is a portable way to avoid this problem, although it
  582.     is somewhat less efficient.  Your parent process should fork, and
  583.     then wait right there and then for the child process to terminate.
  584.     The child process then forks again, giving you a child and a
  585.     grandchild.  The child exits immediately (and hence the parent
  586.     waiting for it notices its death and continues to work), and the
  587.     grandchild does whatever the child was originally supposed to.
  588.     Since its parent died, it is inherited by init, which will do
  589.     whatever waiting is needed.  This method is inefficient because it
  590.     requires an extra fork, but is pretty much completely portable.
  591.  
  592. 14) How do I get lines from a pipe as they are written instead of only in
  593.     larger blocks.
  594.  
  595.     From: jik@pit-manager.MIT.Edu (Jonathan I. Kamens)
  596.     Date: Sun, 16 Feb 92 20:59:28 -0500
  597.  
  598.     The stdio library does buffering differently depending on whether
  599.     it thinks it's running on a tty.  If it thinks it's on a tty, it
  600.     does buffering on a per-line basis; if not, it uses a larger buffer
  601.     than one line.
  602.  
  603.     If you have the source code to the client whose buffering you want
  604.     to disable, you can use setbuf() or setvbuf() to change the
  605.     buffering.
  606.  
  607.     If not, the best you can do is try to convince the program that
  608.     it's running on a tty by running it under a pty, e.g. by using the
  609.     "pty" program mentioned in question 3.9.
  610. -- 
  611. Ted Timar - tmatimar@nff.ncl.omron.co.jp
  612. Omron Corporation, Shimokaiinji, Nagaokakyo-city, Kyoto 617,  Japan
  613.  
  614.  
  615.