In addition to security and stability, the Linux operating system is favored by system administrators. Another reason is that it is open-source. As long as the system administrator has this capability
In addition to security and stability, the Linux operating system is favored by system administrators. Another reason is that it is open-source. As long as the system administrator has this capability, the required functions can be developed based on the original features of the operating system. Therefore, if you do not know how to program in a Linux operating system, the system administrator can only say that it is half a system administrator. Recently, a friend of mine tried to design a Linux program and asked me how to do it? Today, I am giving some kind reminders to my friends who want to take this path.
I. pay attention to the differences with the C language.
In the Linux operating system, program development, including embedded development, basically uses the C language development structure. However, note that, the C language used in Linux is very different from other C languages, especially those who have learned C language in school or who have experienced C language development before, now, you need to pay special attention to this issue when you want to develop functions in Linux. for example, the following is the source code of a simple screen welcome interface developed in Linux.
- Int main (int argc, char ** argv)
- Printf ("Welcome! Linux! N );
- Return 0;
Take a closer look at this main function. is it different from the main function in the traditional C language? In terms of format, it seems to be similar, but the C application in the Linux operating system is quite different from other C application programs. take the above main function as an example, it is different. When you take a closer look at the above main function, you will find that in this function, the author uses parameters. This is possible in the C application source code in Linux, but it is not allowed in other C language development environments. That is, parameters cannot be included in the main function. In fact, most Linux operating system commands contain parameters. Parameters such as ls and who commands can be passed in. Therefore, although they are all executed from the main function. However, compared with the traditional C language, C language applications in Linux can include parameters in this function and return values.
For this reason, the first kind of reminder provided by the author is that if you have C language programming or learning experience, it may be helpful for C application development in Linux. After all, there are many similarities in some language structures and command usage. However, before development, it is best to read through the C language programming specifications in the Linux operating system. The important thing to note is the difference in syntax and programming habits from the C language I have learned before. Only in this way can functions or embedded development in a Linux system be avoided, achieving twice the result with half the effort.
2. you must provide detailed error information.
Even programmers who have decades of programming experience may not be careful when writing applications. errors may occur during program running. Or, if you do not provide the appropriate parameters where you need to provide them, the application may run incorrectly. That is to say, users may encounter various situations when using applications. When developing an application, you need to collect the error information and give the detailed and clear feedback to users so that they can see the error information, you will know what causes the problem and how to adjust it.
For example, the system administrator needs to write a file backup program. When the backup program is running, the backup may fail. So what is the possible cause? There may be insufficient user permissions to execute the backup program, and users cannot read the files to be backed up or have no write permission on the target folder. It may also be because the target folder has insufficient capacity to accommodate the files to be backed up. Or it is manually forced to interrupt when the program is running. Remote Backup may also be caused by the network. It can be seen that there are many factors that cause this backup program to fail. When developing this application, the Linux operating system administrator simply reminds you that the file backup has succeeded or failed. If the specific cause is not specified in the case of failure, do you need to confirm and eliminate the cause one by one? This will obviously arouse users' dislike. For this reason, you need to pay attention to the interaction with users when developing applications in Linux. In particular, when an application program is running, you need to explain to the user in detail which stage has encountered a problem. In fact, this is only a few more code, not much difficulty. Taking this backup program for example, reading and saving files are relatively independent tasks. When a problem occurs at any stage, the name of the link is immediately displayed to the user. In this case, the user can quickly locate the cause of the problem based on the error prompt and solve it in time. However, many system program developers do not. When an error occurs in a link in program backup, they simply throw an error command. After receiving this command, the error handler will remind you that the file backup failed. However, if there is any problem, let the user study it on his own.
The author first serves as the system administrator and then enters the field of Linux system programming. For this reason, some ambiguous error messages thrown during the application process or errors such as program running failure that are performed thousands of times are all terrible. Therefore, the second kind reminder I would like to give you is to use the error functions provided by the system to provide detailed error information during program development. It is best to find the cause of the problem as soon as you see the error prompt.
3. improve the readability of the prompt information.
When an application is running, in addition to providing users with detailed error messages, you must also be aware of the readability of the prompt information. In particular, some error messages are thrown at the underlying level of the operating system. In this case, Linux developers are advised to format the information that is hard to read by common users, convert it into information that users can read. In Linux, the strerror function and sys_errlist message string array are provided, which are used to format various types of error messages to improve the readability of error message information. If you use this strerror function, you can convert some error codes that have no actual meaning for the user into language-related error message strings, and return a pointer to this string. Obviously, reading these language error messages is much better than reading the error code.
In addition, if an application needs to call other applications, it is best to notify the user of the application in case of an unexpected situation. For example, the perror function can write language-related messages (including error messages and warning messages) to standard input and output to describe the errors encountered during the call of system functions or library functions. This function outputs parameter strings, error messages, line breaks, and so on. It is important to note that in the parameter string, it is best to bring out the name of the problematic application. Sometimes an application may call other applications, or at the end of an application running, it may call dozens of applications (including system functions and library functions ). At this time, if the system does not tell the user which program or system function is faulty, the user will be tired and cannot start searching for the problem. Even the developer of this application may be at a loss after a while. Therefore, whether it is for yourself or for the user, it is best to include the name or code of the program that causes the application to fail to run in the perror function. This is the best tutorial for program maintenance and debugging.
The preceding three points are the most basic requirements for program development in Linux. I will not talk much about the Linux system architecture, system functions, system interfaces, and so on, because this knowledge is a prerequisite.