The tty core code tty_io.c contains the following lines of code:
/* * Ok, now we can initialize the rest of the tty devices and can count * on memory allocations, interrupts etc.. */static int __init tty_init(void){ cdev_init(&tty_cdev, &tty_fops); if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) panic("Couldn't register /dev/tty driver\n"); device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); cdev_init(&console_cdev, &console_fops); if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) panic("Couldn't register /dev/console driver\n"); device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, "console");#ifdef CONFIG_VT vty_init(&console_fops);#endif return 0;}
Several system-level terminal devices are created here. They are not real physical devices. Each of them creates these device nodes.
/Dev/tty
The device number (5, 0) is the device/dev/tty, indicating the "control terminal" of the current process, and does not represent any physical terminal, access the device (5, 0) is the "control terminal" that accesses the current process. Every process usually has a "control terminal ". If no, when the first terminal device is enabled, set this terminal device as the "control terminal" of the process (of course, this newly opened terminal must represent a specific terminal device ). If you do not want to use the opened terminal device as the process control terminal, you can add the o_noctty identifier to the flags parameter called by the system.
The process control terminal is stored in task_struct.signal-> TTY.
I personally think that the introduction of/dev/tty (5, 0) is to provide the application layer with a short cut to access the "control terminal" of the current process. We can see the following code in tty_open:
if (device == MKDEV(TTYAUX_MAJOR, 0)) { tty = get_current_tty(); if (!tty) { unlock_kernel(); mutex_unlock(&tty_mutex); return -ENXIO; } driver = tty_driver_kref_get(tty->driver); index = tty->index; filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ /* noctty = 1; */ /* FIXME: Should we take a driver reference ? */ tty_kref_put(tty); goto got_driver; }
If the enabled device node is/dev/tty, operate the current process "control terminal" returned by get_current_tty"
/Dev/tty0
The primary device number is 4, and the secondary device number is 0. Indicates the current virtual console. There is a global fg_console in the kernel that supports the virtual console, indicating the current "foreground console ".
/Dev/console
The primary device number is 5, and the secondary device number is 1. It is used in the external console. When the kernel is installed during initialization, it registers and uses the register_console function as the terminal device of the console, and mounts a console data structure to the console_drivers queue in the kernel.
If the terminal device to be opened is/dev/console, find the first console structure in the console_drivers queue. The device number corresponding to the console is the device Number of the current system console.