6

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 output

501 32566 32444   0  2:26PM ttys000    7:43.89 /usr/bin/java com.test.Hello
501 32784 31676   0  2:34PM ttys003    0:00.00 grep java

1) 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?

1
  • This is several questions in one, and you don't even need question #3. It is already here.
    – JdeBP
    Jul 5, 2016 at 12:30

1 Answer 1

8

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/0

Open terminal B as user B and do the same:

[userB@terminalB]$ tty
/dev/pts/3

Now redirect a couple of characters from terminal A to terminal B:

[userA@terminalA]$ echo Hi there > /dev/pts/3

And see them appear on terminal B:

[userB@terminalB]$ Hi there

On 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 notes

The 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.

2
  • Character device is something that I hear for the first time... Jul 5, 2016 at 18:20
  • 3
    @user1745356 - All devices on a UNIX system are either: block devices, which read/write blocks of bytes; and character devices which read/write bytes by the stream (they were named during a time that characters were always one byte long). If you do ls -l a character device will show as crwx------ and a block device as brwx------ (similar to what you see with directories: drwx------).
    – grochmal
    Jul 5, 2016 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .