Unix/Linux Programming Practice tutorial Chapter 2 Notes

Source: Internet
Author: User

Chapter 6: User Programming: terminal control and Signal

6.1

1. Software Tools: Programs that do not distinguish disk files from Device Files

2. User Program:Specific DevicePrograms

-------- These programs set the Terminal Driver's shortcut keys and output methods.

 

6.2 Terminal Driver Mode

1. When a program is implemented, the driver automatically completes the processing function:

(1) ① standard mode: buffer, edit (delete a character, word, or whole line)Buttons)

② SpecificCharacterProcessing: Convert \ r to \ n, Ctrl-C

③ Others: for example, Echo

(2) ① editedSpecific keyYou can use stty or the system to call tcsetattr to modify the settings in the driver.

(For example, if you do not need to press Ctrl + C to end the program, you can change it to another button to indicate this function .)

② Conclusion: eachProcessing functionsAre controlled by an independent bit. The program can randomly close these processing steps.

 

2. Terminal Driver mode: (1) standard mode: buffering and editing

(2) Non-standard mode: Disable the standard mode. The specific character processing function is still available.

(3) Raw mode: All processing functions are disabled.

6.3 compile a user program play_again.c

Play_again.c logic: Ask questions about User display

Accept Input

If it is Y, 0 is returned; if it is N, 1 is returned.

Play_again0.c

Implement the above functions.

However, no error input is processed.

 

Play_again1.c

Improvement: (1) Disable buffering. Accept and process characters without waiting for carriage return.

(2) set that only one character can be read at a time. Read a character edge and start processing.

----- (Implemented by setting the Terminal Driver function)

Disadvantage: it is annoying to enter an error character and prompt a series of error messages each time.

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

Play_again2.c

Improvement: (1) disable the echo position. (Implemented by setting functions of the Terminal Driver)

(2) the error message is directly or slightly absent. (Delete the error message code)

Disadvantage: The program keeps waiting for the user to enter 'y'/'n' (blocking ). If people leave, they will be blocked all the time.

 

Play_again3.c

Improvement: (1) Use fcntl to disable Blocking Mode

(2) implement with sleep: first check whether there is input. If there is no input, the sleep 2 s will continue the check.

Maxtrie implementation: If three input errors occur, the program ends and exits.

========================================================== ==============

6.4 Signal

6.4.1ctrl + C terminate the currently running program

Procedure: (1) press Ctrl + c

(2) signal system sends SIGINT signal: ① The driver receives the character

② Characters matching vintr and isig are enabled

③ Driver call signal system

④ Signal system sends SIGINT Signal

(3) The process receives the SIGINT signal.

(4) Process Termination

 

6.4.2 what is the signal?

1. What is the signal?

(1) signal: a message composed of a single word.

For example, signals: green lights, stop cards

Message: Go, stop

(2) Each signal has a digital code. (View the signal List)

2. Where does the signal come from?-------------- The signal comes from the kernel.

After a request generates a signal, the kernel sends a signal.

The signal generation request comes from:

(1) User: Enter the buttons

(2) kernel: When an error occurs during execution, the kernel sends an error signal to the process.

(3) process. A process can send signals to another process by calling kill.

One process can communicate with another process through signals.

3. Classification: synchronous signal: the signal generated by an operation of a process.

Asynchronous signal: the signal caused by external events. (For example, user keys)

4. What can signals be used? For example, a signal can kill a process.

 

6.4.3 how processes process signals

1. The process calls the signal () function to tell the kernel how to process the signal.

Result =Signal(INT Sig, void (* Fun) (INT ));

Function: register with the system. (In which process registration is performed, it plays a role in which process .)

After that, as long as the SIG signal occurs, the systemStop the current processAnd call the signal processing function fun.

When the function execution is complete,Continue the original process.------- That is, achieve final disconnection.

Return: Successful return refers to the pointer of the forward processing function.

Error:-1.

 

2. After the process receives the signal, there are three processing methods:

(1) Accept the default processing (generally process extinction) signal (signit,Sig_dfl);

(2) Ignore signal (signit,Sig_ign);

(3) call a function. Signal (signit,Function fun);

Location of signal?

6.7 programming for devices

Three aspects of writing the terminal control program: (1) learning the attributes of the driver and how to control the connection

(2) learning specific application requirements

(3) learning how to handle signals-a form of Interruption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.