Here’s a list of things you need to know about Linux development, and we’ll talk about them later. Welcome to follow my wechat official account: Fensnote

The article directories

      • What does Linux development use?
        • C Language Basics
        • A shell script
        • Learn how to use makefiles
      • What do you know about regular Linux programming? Which ones are commonly used? Which ones are not commonly used?
        • General Linux programming knowledge
          • File IO
          • File and directory operations
          • The standard IO library
          • Process control
          • Thread programming
          • Senior IO
          • Interprocess communication
          • The database
          • A serial port programming,
          • Network programming
        • conclusion

What does Linux development use?

C Language Basics

Linux system kernel is written in C language, so, Linux system development may deal with a lot of system API, need to master the C language foundation, C language is the most basic Linux development language, of course, you can also use C++. Generally, C language is more used for modules that interact with the system. For upper-level business applications, C++ is used for development in order to be efficient. After all, C++ is an object-oriented development language, suitable for large-scale project development, convenient modularization and high code reuse rate.

A shell script

Shell scripts, which use a series of commands in the Linux system, are organically combined to complete the expected functions. The Linux design philosophy of “small is beautiful” is the perfect embodiment of shell scripting. Each command performs only one function. The combination of different commands can achieve complex functions. Scripting (testing, analyzing data, etc.) is an inevitable part of Linux development.

Learn how to use makefiles

The Makefile is a compilation and management script that is necessary for Linux program development. This script contains a lot of things, but it can be used briefly at the beginning. As the project gets bigger and the code files and directories become more complex, the Makefile may become more complicated. Mutual calls or coordination of makefiles involving different directories. For example, in an embedded system, there is usually more than one application, and there may be more than one program. When compiling the whole program, the following contents will be included:

  1. Compilation of individual application modules (need to determine whether the compilation is successful, stop in case of compilation failure, throw an error message)
  2. Application packaging (as a zipped package or partition image)
  3. The default configuration file is packaged
  4. The uboot, kernel, file system, and application program are packed into an image file. The size of the image file corresponds to the size of the Flash partition. If the size is insufficient, fill the image file with empty data and ensure that the image corresponds to the partition.
  5. Automatic version management, after compilation and packaging, you can automatically generate version numbers based on time through scripts.

These things can be quite complicated and require a Makefile with a shell script.

What do you know about regular Linux programming? Which ones are commonly used? Which ones are not commonly used?

General Linux programming knowledge

File IO

On Linux, all resources are accessed as files. The device is abstracted as a device file and stored in the /dev directory. Basic access functions:

The function name Introduction to the How to use it in general
open Open device file To access device files, you must first open the device
close Close device file After file access is complete, this interface needs to be called. If not closed, Fb will be exhausted
read Read the data Read data, such as text file content or camera data
write Write data When writing data to a device, such as through a serial port, you can use write to send data
ioctl IO operation function This function has many functions, known as the glove box ~, can read and write data, do the students should be very familiar with the driver
fcntl Property setting function For devices that have been turned on, set properties. For example, you can set blocking and non-blocking properties for serial port read data
lseek Change offset Generally used to modify the offset of the read/write position of an open file
File and directory operations

Common system programming involves file and directory access, such as reading all file names in a directory, or modifying file names, modifying directories, moving files and so on.

The function name Introduction to the How to use it in general
Stat, fstat, lstat Obtaining file status You can obtain file creation, access time, file size, and so on
access Checking file properties This is used to check whether a file exists
chmod Modifying file Permissions It is used when modifying file access permissions, for example, adding executable permissions
mkdir Create a directory Generally in the program to determine the need to use the directory does not exist, use this function to create
rmdir Delete the directory This command is used only when a directory needs to be deleted
opendir Open directory Before reading directory information, you need to open the directory (class and file access)
readdir Reading directory contents When you’re retrieving a file, you’re reading a list of files in a directory
closedir Close the directory Remember to close the directory after reading
sync Synchronize data to disks This function is called after a file has been modified and data has been written to allow the system to synchronize the data to disk
The standard IO library

More commonly used so-called standard IO library, in fact, is the C language file IO, in addition, here the IO and the previous mentioned “file IO” in the file read and write there is a difference, standard IO is cached, and the Linux system file IO function is no cache. Mainly file manipulation functions:

The function name Introduction to the How to use it in general
fopen Open the file Call fopen to get the file handle before placing the file
fclose Close the file After the file access is complete, close the file; otherwise, memory leaks may occur
fread Read the file Reading file data
fseek Modify the file read/write location Used to modify the position offset of a file read or write
Process control

If you don’t use multiprocess programming, you probably don’t use that much, and I don’t use that much in my code. This knowledge is mainly about process creation, child process management, process exit resource release and so on.

Thread programming

More commonly used on the system, the general running task will be more, thread is more commonly used, generally can be divided into two kinds, one is a long time running task, one is to execute the task to exit. Threading is now supported in C++11, making it very easy to use. Before C++11, for the convenience of use, are the pthrea_ cluster function for the secondary encapsulation.

Senior IO

Often you may be more curious, why so many “IO” related things: file IO, standard file IO, advanced IO! Yes, there are a lot of IO, the first two “IO” are somewhat similar, this advanced IO is a bit different from the first two. The advanced IO is mainly about the IO operation mode: blocking, non-blocking control, the most important is to use SELECT and poll to achieve IO multiplexing, so that the program can be asynchronous execution. The most popular Nginx now uses the epoll model to achieve high concurrency.

Interprocess communication

Interprocess communication is used. There are many open source tools (such as DBUS) for interprocess communication. Here is a brief introduction to the communication mode provided by the system:

The name of the Introduction to the
The pipe Commonly used for communication between parent and child processes, named pipes can cross programs
The message queue Message queues can be typed and are suitable for small, simple communications
A semaphore The PV operation, used for synchronization, can be used with shared memory to tell another thread that it is ready to read when the data is written
The Shared memory Suitable for large data transmission
socket Socke is not only an interface for network communication, but also for local interprocess communication (UNIX SOCKE).This is very common
The database

If you are storing configuration parameters, I would not recommend using a database. Json is fine. Data is used to store regular data for easy data management. In embedded system, sqLite database is used more.

A serial port programming,

More commonly used in Linux, especially do embedded systems, this may be indispensable, more commonly used. In fact, serial programming is very simple, maybe 200 lines of code can set up serial communication. Data communication at the application level is done using write, read, and write functions, but if you want to do it better, you need to take the time to create a software framework, such as asynchronous operations using the aforementioned SELECT (the implementation and serial port packaging will be described in a later article).

Network programming

Super commonly used to believe that embedded devices now basically need network communication function, so, network programming is naturally very commonly used. Special is now the development of the Internet of things, the Internet of everything. Linux network programming, there are a series of API functions, a lot of parameter configuration is also more, at the beginning will let a person very headache, do not know which parameter set what value is appropriate (we are slowly trial and error out). Do not worry about this, I will share with you later I use C++ encapsulated network communication library, including TCP, UPD, UDP multicast, domain socket (local) and so on.

conclusion

I will introduce you later when I have time. Welcome your attention.

Wechat Official Account: