I started a java program that simply enters an infinite loop from a unix terminal (let's say terminal window tw1)I opened up a second terminal window (say tw2) and executed ps -ef | grep java command that gave me the following output501 32566 32444   0  2:26PM ttys000    7:43.89 /usr/bin/java com.test.Hello501 32784 31676   0  2:34PM ttys003    0:00.00 grep java1) Does ttys000 identifies tw1 and ttys003 identifies tw2?2) The first line represents a running java process. It is associated with ttys000. So basically tw1 is a controlling terminal of my java process. Is it correct?3) If I run ps -ef command I see a bunch of processes that have ?? in tty column. Does it mean they don't have an opened terminal associated with the process?
Understanding controlling terminal
terminal;process
1.)Yes, although there is more to it.  ttys000 is also a character device sitting in /dev, a user that has permissions to write to the tty group (most users have) can pipe characters into that device and they will appear on the corresponding terminal.  ttys* are not real teletypes though, they're emulated ttys, emulated by your (appropriately named) terminal emulator.I do not have a Mac so I'll use the Linux naming convention for the following example:Open terminal A as user A and find the emulated tty:[userA@terminalA]$ tty/dev/pts/0Open terminal B as user B and do the same:[userB@terminalB]$ tty/dev/pts/3Now redirect a couple of characters from terminal A to terminal B:[userA@terminalA]$ echo Hi there > /dev/pts/3And see them appear on terminal B:[userB@terminalB]$ Hi thereOn a Mac the devices should be /dev/ttys*, I believe.2.)More or less.  The ttys000 itself is just the character device, the actual entity that is controlling your java process is the terminal emulator.  By controlling I mean that it is the parent off your java process.  The parent can interact with its children in an easier fashion than other processes can.Moreover, if certain precautions are not taken (see man nohup for an example of such precaution), the death of the parent process will cause the death of all its children processes.3)The answer by Karlson already explain that the ? means a process not associated with a terminal.Since the terminal itself is just the character device, I believe it is not hard to conclude that it is not necessary for a process to be associated with a terminal device.Closing notesThe actual terminals /dev/tty are hardly used on modern *nix OSes (although they're used ensively during the boot process).  But that does not mean you cannot use the actual terminals.  On a Linux machine (sorry, I have no clue how a Mac performs this) the combination Ctrl + Alt + F1 (and F2, F3, ... up to F7) gives you a real terminal.  One of these real terminals is used to perform the graphical display.Several processes (including graphical applications) on a modern *nix OS are associated with a terminal device because the script that starts them needs to pass extra arguments.  The script fires a shell passes the extra arguments and starts the process.  Such scripts are often causes for confusion.