Chapter 1-linux basics, Chapter 1-linux
(1): linux system root directory structure
The "File System" concept in Unix-like systems has two meanings: the first is the "root file system" and the second is the "storage file system ". the latter concept is basically equivalent to the windows operating system, while the former is much different from windows, and it is not used to store actual files. the root file system (rootfs) has the following features:
1: "file" refers not only to the data on the hard disk, but also to any device resources. in Unix-like systems, all hardware devices are regarded as files, and "Files" are core concepts, such as disks, USB disks, memory, and network, even the cpu is abstracted into files by the kernel. kernel-level files are called "Device Files" or "device virtual Files" to distinguish files in the general sense ". these device files can be seen in rootfs. 2: Not all directories or files correspond to the storage space on the disk. for example, sys, proc, and dev directories correspond not to buckets but to device files. The contents of these three directories are maintained by the kernel and corresponding drivers. 3: Storage Class I can see that the system cannot coexist with rootfs, but can only be mounted to a sub-directory of rootfs 4: the "storage file system" in Unix-like is equivalent to a windows File System, including the file system type. common File System Types in windows include FAT16, FAT32, NTFS, and linux. These file system types are also supported, but more commonly ext2, ext3, ext4, and yaffs.
In Unix-like systems, an operating system can only have one root file system, but can contain multiple "storage file systems ". you can run the mount and umount commands under terminal to mount and unmount the storage file system.
(2) linux Startup Process
From the computer system perspective, the startup process is generally divided into three steps
The first step is to start the system and start power supply. At this time, the hardware circuit will generate a definite reset sequence to ensure that the CPU is the last reset device. the reason why the CPU is reset at the end is that if the CPU is reset for the first time, when the CPU starts to run after the reset, the internal register status of other hardware may not be ready yet, for example, disks or memory may cause peripheral hardware initialization errors.
After the reset is completed correctly, the CPU starts to execute the first command. The memory address of the command is fixed, which is specified by the CPU maker. different CPUs may obtain commands from different addresses, but the address must be fixed. The programs stored with this fixed address are often called "bootloader )", its role is to load real user programs.
As for how to load, it is a policy problem. Different CPUs provide different loading methods, such as some through common Parallel Memory, some through SD card, and some through RS232 interface. regardless of the interface used on the hardware, the loading process must provide the following information, including:
1: Where to read the user program
2: What is the length of the user program?
3: After the user program is loaded, where should I jump?
The second step is to execute the kernel program. The kernel program mentioned here in the previous step refers to the "user program ". from the CPU point of view, all programs except bootloader are user programs, but from the software point of view, user programs are divided into kernel programs and applications ", in this step, the kernel program is executed ".
Operations performed during kernel program initialization include Initializing Various hardware, including memory, network interfaces, monitors, and input devices, and then establishing various internal data structures, these data structures will be used for multi-thread scheduling and memory management. after the kernel Initialization is complete, it starts to run the specific application. generally, the first application is called the "Home program ".
The third step is to run the Home program. For example, the windows system desktop is a typical Home program. It is called the Home program because it can easily start other applications.
Next, let's take a look at the android startup process.
Most ARM-based hardware systems load programs from the 0x00000000 address in the parallel NADN Flash chip. for some small embedded systems, the program at this address is the user program to be executed. for android, the program at this address is not an android program, a program called uboot or fastboot initializes hardware devices, such as network ports, SDRAM, and RS232, and provides some debugging functions. when the uboot starts running after it is loaded, it usually checks whether the user has pressed some special buttons. These special buttons are pre-defined by uboot during compilation, used to enter the debugging mode. if you do not press these special buttons, uboot will load the Linux kernel from NAND Flash. The address for loading is predefined during uboot compilation.
After the Linux kernel is loaded, the kernel initialization process starts. The process is as follows:
(3) Make script memo
The Linux system contains a Make script interpreter, which can read and execute the Make script. The Make script is mostly used for automatic compilation, but it does not mean that Make scripts can only have automatic compilation.
The basic syntax of the Make script is as follows:
Target: condition (prerequest)
(Press Tab) command
In this syntax, the target can be any string name or a specific file name. the condition can be the name of another target or a specific file. when executing the Make script, the Make interpreter checks whether the timestamp of the object and the file are the same. If different, the interpreter executes the 'COMMAND 'After the Tab key ', the command can be any executable program.
The basic principle of automatic compilation is to use the target file as the "target" and the source file as the "condition". Therefore, after the source file is modified, the timestamp of the target file will be earlier than the source file, as a result, Make will automatically execute the specified "command ". in this case, the compiled command can be used as the "command" here to achieve the purpose of automatic compilation.
1: A simple Makefile file
The source code is as follows:
#FileName Makefile#this file is used for showing how to use makefile$(info start working)hello: hello.c echo "nothing"hello.bin: hello.c @echo "now make hello.bin" gcc hello.c -o hello.bin.PHONY: hehe: hello.c @echo "now make he" gcc hello.c -o hello.bin
This code has the following features:
1: # The symbol is a annotator. It can be used anywhere in the Code. 2: $ is a function call symbol. info is a function name and serves to output a piece of information. similar information output functions include the warning and error functions. However, after an error is executed in a book, it terminates execution and exits. 3: No space can be added before the target is set, the command must start with the Tab key 4 :. the PHONY keyword is used to declare a target. PHONY declares that the target will always execute the specified command. If it is not declared, it will only execute the command after the condition changes after the target. 5: The @ symbol in front of the command is used to execute the command, the executed command is not displayed. by default, the Make interpreter prints the Command 6: for hello. bin target, which is itself a file and the dependent file is hello. c file. therefore, when hello. after the c file is modified, it will execute the gcc command to re-compile the c file and output hello. binfile.
To run the preceding script, run the following command:
$make -f Makefile hello
In this command,-f is used to specify the name of the script file to be executed. If no file name is specified, the interpreter will automatically find the script file named Makefile from the current directory.
2: Definition and value assignment of Variables
Variables in Makefile do not need to be defined separately. values can be assigned directly. Common assignment methods include:
The script execution process of the Make interpreter can be divided into two steps:
1: Load Makefile and other makefiles included in Makefile. After all the relevant script files are loaded, a graph is created in the system, which describes the dependencies of each Makefile.
2: locate all dependencies of the target Based on the target specified by the user, and determine the timestamp of the file in the dependency condition. If the timestamp is newer, execute the corresponding commands in the taget.
For variable definition and assignment, the interpreter selects immediate assignment or delayed Assignment Based on Different assignment methods, and the assignment process becomes an extension process. the so-called immediate value assignment refers to assigning values to the variables when reading the script, while the delayed value assignment refers to not assigning values at the moment when reading the script. This variable is only used when executing the script, to assign a value. the expansion time corresponding to different assignment methods:
3: Conditional Control statement
The condition control of Make scripts can be divided into two types: one is to process when the interpreter parses the script file, and the other is to process when the script is executed.
The following code describes the syntax model of the first type of conditional control statements:
If-condition text if the condition is trueendif // or if-condition text if the condition is trueelse text if the condition is falseendif
Condition can only be used to determine whether an expression is equal or not, and whether the expression is defined as follows:
1: ifdef var: determines whether the variable has been defined.
2: ifndef var: opposite to ifdef, judge whether the variable has not been defined
3: ifeq test: judge whether the expression test is equal. The expression can be written as "a", "B", or (a, B)
4: ifneq test, opposite to ifeq
4: macro definition
Functions in the Make script are divided into three types by calling:
The first class is a built-in function, that is, a function defined in the Make interpreter. It can be called directly in any script file. The Calling format is:
$(fname,param...)
Fname is the name of the function, And param is the parameter. Multiple parameters are separated by commas.
The second type is user-defined. A function with parameters is defined using the define keyword. The call format is:
$(call fname.param...)
Call is the call keyword. fname indicates the function name. param is a function parameter. Multiple parameters are separated by commas.
The third type is user-defined, but does not contain parameters. This type of function is also called a macro. The Calling format is as follows:
$(fname)
Neither the call keyword nor parameter is used.
User functions are defined as follows:
Define fname // various specific commands endef
In User-defined functions, the Tab key is not required for command money, because when the macro is expanded, the Make interpreter automatically adds the Tab key before no command line. (N) represents the parameter used to call the function, and n represents the natural number, (0) indicates the function name, and $ (1) indicates the first parameter.
For example:
define showFirstName @echo $(1)endef.PHONY: namename: $(call showFirstName,yuandan,ferr)
The showFirstName function is defined in this Code. It is used to return the first input parameter to the caller. Use the make name command to execute the above script and the execution result is:
$ make nameyuandan
Common built-in functions:
(1): String operation functions
Built-in common string functions such as tables:
(2) file name Operation
Common functions used to process file paths and names:
(3) process control functions
In C, if/else is the keyword defined by the syntax, and the process control of the script is indeed completed by the function.
Process control function table:
5. built-in symbols and variables
The Make interpreter defines some special symbols and variables with special names. These symbols can be used directly when writing a user script, without the need to define them.
(1): built-in symbols
(2): built-in Variables
The Make script is mainly used to compile the C/C ++ source code. Therefore, it also defines some variables dedicated to C/C ++ compilation.
6: Template target
Assume that there is currently a C source code project, which contains three C source files named f1.c, f2.c, and main. c, where f1 and f2 define two functions, main. c will use these two functions, and then you can write a script file. The script source code is as follows:
.PHONY: testtest: f1.o f2.o main.o gcc -o main.bin f1.o f2.o main.of1.o: f1.c gcc f1.c f1.of2.o: f2.c gcc f2.c f2.omain.o: main.c gcc main.c -c main.o
When the number of source code is large, the definition of the script file becomes cumbersome. in this way, we use a "template" to define the target. After using the template target, the above script can be simplified:
OBJ = f1.o f2.o f3.o.PHONY: testtest: $(OBJ) gcc $(OBJ) -o main.bin%.o: %.c gcc -c -o $@ $<
7: assign values to specific target variables
In a script file, after a variable is assigned a value, the value of the variable is the same no matter which target of Make is used, as shown in the following code:
CFLAGS = -c.PHONY: tar1tar1: gcc $(CFLAGS) main.ctar2 : CFLAGS=tar2: gcc $(CFLAGS) main.c
CFLAGS is assigned-c, and its value is valid throughout the script file. for example, when Make tar1 is executed, it will execute gcc-c main. c. but in the tar2 goal, we want to be able to execute gcc main. c. Therefore, you can assign a value to the CFLAGS variable in the tar2 target. This value is only valid in the command of the tar2 target. This is the so-called "target-specific" variable assignment. because the value is only for this target.
In syntax, you must note that the value assignment to the tar2 object cannot be written in the following form:
tar2 : CFALGS = gcc $(CFLAGS) main.c
Instead, the value assignment of the target variable and the target rule must be written separately.
Copyright Disclaimer: Hello, please leave the address of your blog for reprinting. Thank you.