Basic Unix 2. Shifting a process between the foreground and the background

   A foreground process is one that occupies your shell (terminal window),
   meaning that any new commands that are typed have no effect until the
   previous command is finished.  This is as we might expect, but can be
   confusing when we run long lasting programs, such as the 'afni' GUI.

   Note: afni can no longer be used as the example for this, since it creates
         a child process and returns the user to an active command prompt.  So
         suma will now be used for this demonstration.

   Commands and descriptions:

        suma    : AFNI program: the main GUI for viewing surface data
        ctrl-z  : keystroke: suspend the foreground process
        bg      : put the recently suspended process in the background
        &       : run a program in the background to begin with

   help for suma
   help for ctrl-z
   help for bg:     not ready
   help for &:      not ready


   A. Start suma (from any directory).  It will display a random dataset.

         suma                  

      When the suma GUI is opened, it shows us something (a random internal
      dataset of its choosing), which will vary.  But this is about Unix, so
      we do not care what it shows.

      To see that suma is active, use the left mouse button to "grab" the
      surface data and rotate it around.


   B. Try to type commands in the original terminal window.

      Locate the terminal window from which 'suma' was launched.  Try to
      enter some commands into it.

         <Enter>         
         <Enter>         
         ls                    
         pwd                   
         <Enter>         

      We get no prompt and the commands have no effect.

      That is because the 'suma' program is running in the foreground of the
      terminal shell.  The shell is occupied by suma.


   C. Suspend suma.

      In the terminal window, hold the ctrl key and press 'z'.

         ctrl-z                

      The terminal window shows 'Suspended'.  That refers to the suma program,
      meaning that suma is no longer running, but the terminal is available.


   D. Repeat of B: try to type commands in the terminal window.

         <Enter>         
         <Enter>         
         ls                    
         pwd                   

      This time the commands work.  The suma program is no long occupying the
      terminal shell.


   E. Try to use the suma GUI.

      Try to use the left mouse button to grab and rotate the surface again.

      Next grab a different window (maybe the terminal or web browser), move
      it to partially obscure the suma window, and then move it back away.

      The windows basically get erased, maybe leaving copies of the obscuring
      image in the suma window.  The suma image is no longer getting re-drawn
      by the window manager.  This shows us that 'suma' is no longer running
      (since it was suspended).


   F. Put suma into the background, then try to use it again.

      Find the same terminal window again and type the 'bg' command (for
      background).  Watch the suma GUI when you hit Enter.

         bg                    

      Notice that suma started running again, as soon as the 'bg' command was
      entered.  Also, notice that we can still enter commands into the terminal
      window.  So not only is afni running, but we have access to the shell.

   G. Be efficient.  Run 'suma' in the background to begin with.  Then ctrl-z
      and 'bg' would not be needed.

      Start by closing the current suma GUI.  Use the 'x' in the upper corner
      of the window, or hit <Esc> in the suma image window, and click on
      Yes to close (or just hit <Enter>).

      Now re-start suma, putting the & character after the command.

         suma &            

      Now suma is in the background to begin with.  We can use suma and we can
      type commands in the terminal window.



Comment(s):

   c1. After starting some program, it can be suspended and put into the
       background by using first ctrl-z and then the bg command.

   c2. A program can be put directly into the background using '&'.

   c3. If a program terminates quickly, such as 'ls', we do not really care.