Directory
How keyboard input is recognized by the system
Keyboard interrupts
Receive Queue
Concept of the terminal
How to achieve multi-terminal
TTY private Receive Queue
Handling of control characters
Incorporating TTY into the file system
Process and TTY bindings
Read and Write objects
Character device files
Task_tty
How keyboard input is recognized by the system
The key signal of the keyboard is processed by 8048 Keyboard encoder and sent to 8042 keyboard controller, 8042 receives and decodes 8048 data passed over. Then 8048 gives 8259A an interrupt signal, indicating that the user has input action on the keyboard. 8259A gives the CPU a signal, and the CPU responds with a call to the keyboard interrupt handler. As to why the CPU can find the keyboard interrupt without mistakenly calling the hard drive interrupt.
- The IDT table has a handler function for each ordinal number, so that the correct handler can be found if the sequence number is correct.
- The sequence number is 8259A, 8259A through the initialization time setting, determines each interrupt pin corresponding sequence number, when the interruption can get the correct serial number.
Keyboard interrupts
What does a keyboard interrupt do for you? It is easy to read the data from the 0x60 port. In addition, if the 0x60 port data is not read out, then the next keystroke on the keyboard can not be received 8042.
Receive Queue
Where does the keyboard interrupt program put the received data? Should be placed in a global variable, because the interrupt handler reads the memory data to be used by other processes. In this case, it is natural to use a circular queue to represent the better. That is, the receiving queue that is often said.
Concept of the terminal
I don't know how other people understand the terminal. My understanding is that there is an input device and the output of the device even if the terminal. Why call TTY, a legacy of history, is said to be the name of a fax printer.
How to achieve multi-terminal
In fact, multi-terminal just for the user, press ALT+F2 to change to a brand-new input window, and then press ALT+F1 and changed back again, it seems really two unrelated monitors.
I used to wonder how this was done. But at that time the study center of gravity was placed on the file system, the energy is limited, there is no further access to information. Just in the process of practice to see the realization of multi-terminal, the author of a theory, suddenly dawned.
Because the function of writing data to the display or our own implementation, as long as we organize the logic, to each terminal planning a different memory, in the call write function when writing to each terminal's own memory range, write full on the rollback, so that the realization of multi-terminal.
Like the example in the book, 32K of video memory, to each terminal points 10K of memory and the remaining. The first terminal writes 0-10k space; the second terminal writes 10-20k space. When we design the TTY structure, we only need to design a console structure to describe each terminal memory, such as the terminal memory start address, the current cursor position.
TTY private Receive Queue
Multiple terminals, then it seems that a keyboard buffer is not enough, for example, this line break which console to change the line? Therefore, the TTY structure needs to add the receiving queue of the respective terminal, read the data from the keyboard receiving queue and put it in its own receiving queue, and give the output function to output.
In the function that handles TTY, we need to take out the data in the keyboard receive queue and put it in the receive queue in the corresponding TTY in the current console.
Handling of control characters
But keyboard keys are not just English letters and numbers so simple, there are a lot of control keys, such as left and right shift, Ctrl, Alt,capslock, Backspace, enter. Need to be handled separately. It takes a little bit of patience to gradually implement them.
Incorporating TTY into the file system
We know the input and output in the front, but only do the input of the keyboard and then print to the different memory area. Doesn't seem to be the same as the Linux we use. It is often used as if the terminal is operated by a file descriptor. This requires that the TTY be included in the file system.
After the TTY is included in the filesystem, the TTY will not be able to play as if it were the first one, and then the keyboard interrupts to receive and then print out. will be subject to the interface provided by the filesystem.
Process and TTY bindings
At this point, the TTY is not independent, it must be explicitly called to perform the terminal read and write operations. In this way, the TTY must be associated with the process, and in the structure of the TTY will be added to the process number that causes the read and write operations, so that the process can be notified after the end of the operation.
Read and Write objects
At this point, the read and write function of TTY is not only to handle the keyboard receiving queue and video memory. It also faces the need for processes.
For a user's read request, the TTY task needs to iterate through the keyboard receive queue, echo back to the console, and put the valid characters in the user-supplied buffer, and read the "\ n" character to send a read-complete message to the process that sent the request.
For the user's writing interest, the TTY task needs to read the characters in the user-supplied buffer to read out and print to the console.
Character device files
What exactly is a character device file?
Assuming you don't have these device files, imagine how to read and write hardware devices like terminals. It is possible for the kernel to provide an interface separately, similar to read_tty (int tty_num,void *read_buffer,int size). Look closely at what is different from normal read-write interfaces. The first parameter is the sequence number of the TTY, not the file descriptor that is commonly referred to. What does that mean? It means that developers have to remember which terminal number No. 0 is, and which terminal 1th is, which is really a headache. If a few more hardware equipment is not very depressed? In addition to a very small number of people with a strong memory, the average person is only good at dealing with meaningful characters. Then the file name is an excellent index, not repeating multiple devices, but also has the significance of identification. Then put the device into the file system, so that the device is used to indicate that the file is the device files AH.
How does the device file work with the file system? Modern file systems are naturally extremely complex and are not mastered by a single person. However, the implementation of a simple file system can be combed smoothly. We think of the file system inside the INODE structure has a i_mode element, this is used to divide the file processing process. If the file's mode indicates a device file, then in fact the file does not have real data on the hard disk, we need to let the file processing flow into the device processing process indicated by mode. For example, like a character device, mode== i_char_special This time we can invoke the processing function of the character device, then which TTY to handle? Above, we can see the TTY serial number when we read the TTY device, which is the subscript of our TTY struct array to indicate which TTY we are working with. So where is this number stored? Have to say that the former operating system designers really fair bet, remember the Inode has a member of I_start_sect? Originally it is used to represent the normal file of the data storage of the starting sector, now used to store the device number! In fact, it is reasonable to think carefully, since this file does not have the real data on the hard disk, then this field is no use, it is not good to move him.
Task_tty
The above summarizes the tasks that TTY will handle and how to associate it with processes and file systems. By this time, in fact, the whole cycle of Task_tty has come out.
- The first is to call the terminal in turn, if it is the current terminal so read the keyboard receive queue to the private buffer of the TTY
- echo the word read in 1 to the console and stores the characters in a buffer provided by the process
- Wait for other processes to send read-write requests
- For a read request, record the process number you want to read into the TTY structure so that the 2nd step
- For write requests, the characters in the buffer supplied by the process are printed sequentially to the console
Understanding of input and output systems