Chapter--linux Foundation

Source: Internet
Author: User

(i): Linux system root directory structure

The concept of "filesystem" in the Unix-like system contains two meanings, the first is "root file System" and the second is "Storage class file System". The latter concept is basically the same as the Windows operating system, which differs greatly from Windows. He is not used to store actual files. The root file system is abbreviated to ROOTFS, which features:

 1:"文件"不仅是指硬盘上的数据,他还包括任何设备资源.在Unix-like系统中,所有的硬件设备都被看作是文件,"文件"是内核范畴的概念,磁盘,U盘,内存,网络,甚至cpu都被内核抽象成文件.为了区别一般意义上的文件按,内核级文件被称为"设备文件"或"设备虚拟文件".这些设备文件在rootfs中可以被看到.2:并不是所有的目录或文件都对应磁盘上的存储空间.比如,sys,proc,dev这三个目录,他们对应的不是存储空间,而是设备文件,这三个目录中的内容由内核及相应的驱动程序维护.3:存储类我呢见系统不能和rootfs并列存在,而只能挂载到rootfs的一个子目录上4:Unix-like中的"存储类文件系统"内部等同于windows的文件系统,包括文件系统类型.windows系统常见的文件系统类型包括FAT16,FAT32,NTFS,linux中也支持这些文件系统类习惯,但更常见的却是ext2,ext3,ext4,yaffs等.

In a unix-like system, the operating system can have only one root file system, but may contain multiple "storage class file Systems". To perform a mount, unload the storage class file system operation you can use the mount and Umount commands under terminal.

(ii): Linux boot process

From a computer system point of view, the startup process is generally divided into three steps

The first is the boot, the boot is open system start power supply, at this time, the hardware circuit will produce a deterministic reset timing, to ensure that the CPU is the last reset of the device. The reason why the CPU is finally reset is that if the CPU is first reset, when the CPU is reset, The state of the registers inside other hardware may not be ready, such as a disk or memory, and a peripheral hardware initialization error may occur.

When the reset is completed correctly, the CPU starts executing the first instruction, and the memory address of the instruction is fixed, which is specified by the manufacturer of the CPU. Different CPUs may get instructions from different addresses, but this address must be fixed, and the program stored at this fixed address is often referred to as the "Boot Program" ( bootloader) ", its role is to load the real user program.

As for how to load, is a strategic problem, different CPUs will provide different loading methods, such as some through the common port memory, some through the SD card, but also through the RS232 interface. Regardless of which interface is mounted 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 loading complete user program, should jump to where, that is, the user program's execution entrance where

The second step is to execute the kernel program, which is called "User program" in the previous step. Because from a CPU point of view, all programs except bootloader are user programs, but from the software point of view, the user program is divided into "kernel program" and "Application", In this step, the kernel program is executed.

The operations that are performed when the kernel program is initialized include initializing various hardware, including memory, network interface, display, input device, and then establishing various internal data structures that will be used for multi-threaded scheduling and memory management. When the kernel is initialized, Start running the specific application. In general, it is customary to refer to the first application as a "home program".

The third step is to run the home program, such as the Windows system desktop, is a typical home program. This is called the Home program, because it is easy to start other applications through the program.

Let's take a look at the Android boot process.
Most actual arm-based hardware systems will load the program from the 0x00000000 address in the NADN Flash chip. For some small embedded systems, the program at that address is the user program that is ultimately executed, and for Android, The address of the program is not an Android program, but a program called Uboot or FastBoot, its role is to initialize hardware devices, such as network port, sdram,rs232, etc., and provide some debugging functions. When Uboot is loaded, it starts to run, He will usually first detect whether the user has pressed some special keys, these special keys are uboot in the compilation of the pre-agreed, to enter the debug mode. If the user does not press these special buttons, Uboot will load the Linux kernel from NAND flash. The loaded address is pre-agreed when compiling the uboot.

After the Linux kernel is loaded, the kernel initialization process begins, as follows:

(c): Make Script Memo

The Linux system contains an interpreter for make scripts, which can read the contents of making scripts and execute them, making scripts are used for the automatic compilation process, but not that the make script can only have auto-compilation.
The basic syntax for make scripts is as follows:

Target: Condition (prerequest)
(Tab key) command

In this syntax, the target can be either a string name or a specific file name. The condition can be the name of another target, or the name of a specific file. When the make script is executed, the make interpreter checks to see if the file timestamp is the same in the target and file, and if different, The interpreter executes the ' command ' after the TAB key, and the command can be any executable program.

The basic principle of automatic compilation is to use the target file as a "target" and the source file as "condition", so when the source file is modified, the timestamp of the target file is earlier than the source file, and make interprets it to automatically execute the relevant specified command. You can now execute the compiled command as "command" here. So as 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    "nothing"hello.bin: hello.c    "now make hello.bin"    gcc hello.c -o hello.bin.PHONY: hehe: hello.c    "now make he"    gcc hello.c -o hello.bin

This code has the following characteristics:

1:#符号是注释符,可用在代码中任何地方2:$是函数调用符号,info是一个函数名称,作用是输出一段信息.类似的信息输出函数还包括warning,error两个函数,不过error按书执行后会终止执行并退出3:目标定以前不能加任何空格,而命令前面必须以Tab键开始4:.PHONY关键字用于声明一个目标,被.PHONY声明的目标将总是执行其指定的命令,而如果不声明的话,则仅当目标后面的条件变动之后才执行5:命令前面的@符号的作用是,不显示被执行的命令.因为默认情况下,Make解释器在执行命令的时候会打印出执行的命令6:对于hello.bin目标,该目标本身就是一个文件,其依赖的文件是hello.c文件.因此当hello.c文件被修改后,将会执行gcc命令重新对该c文件编译,并输出hello.bin文件.

To execute the above script, you can run the following command:

$make-f Makefile hello

In this command,-f is used to specify the name of the script file to execute. If you do not specify a file name, the interpreter automatically looks for a script file with the name makefile from the current directory.

2: Definition and assignment of variables
Variables in makefile do not need to be defined individually and can be assigned directly, and the common methods of assignment are:

The make interpreter executes a script in two steps:

1: Load other makefile in Makefile and makefile. After all the relevant script files have been loaded, a diagram is created inside the system that describes the dependencies of each makefile.
2: Identify 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, start executing the corresponding command in the Taget.

For variable definitions and assignments, the interpreter chooses whether to assign a value immediately or to delay the assignment depending on the assignment, and the assignment process becomes the unfolding process. The so-called immediate assignment is the assignment of the variable at the time the script is read, and the deferred assignment, which means that the variable is not assigned at the time of the read, only when the script is executed. To assign a value to it. Different assignment methods correspond to the expansion time:

3: Conditional Control statement
The conditional control of make scripts can be divided into two categories, one is processing when the interpreter parses the script file, and the other is handled when the script is executed.
Here is the syntax model for the first type of conditional control statement, as follows:

if-condition   textifthetrueendif//或者if-condition    textifthetrueelse    textifthefalse

Among them, condition can only make two judgments, one is to determine whether the expression is equal, the other is to determine whether the expression is defined, as follows:

1:ifdef var: Determines whether a variable is defined.
2:ifndef var: as opposed to ifdef, determine if the variable is not yet defined
3:ifeq test: To determine if the expression test is equal, the expression can be written as "a" "B" or (a)
4:IFNEQ test, contrary to ifeq

4: Macro Definition
The functions in the make script are divided into three categories by the way they are called:

The first is a built-in function, which is a function defined inside the make interpreter that can be called directly in any script file, in the form of:

$(fname,param...)

FName is the name of the function, param is a parameter, and multiple arguments are separated by commas

The second class is a user-defined, parametric function that is defined using the Define keyword, and the format of the call is:

$(call fname.param...)

Call is the keyword called, fname represents the name of the function, param is a function parameter, and multiple parameters are separated by commas.

The third class is also user-defined, but without parameters, which is called a macro, and its invocation format is:

$(fname)

Neither the call keyword nor the parameter is used.

User functions are defined in the following way:

define fname//各种具体的命令endef

In a custom function, the command money does not need to be tab-key, because when the macro is expanded, the make interpreter automatically adds the TAB key before a row of commands. function Internal Use (N)GenerationTableTunewithLetternumberwhenof theReferencenumber,NGenerationTablefromthennumber, (0) Represents the function name itself, and $ (1) represents the first parameter.

As an example:

define showFirstName  @echo$(1)endef.PHONY: namename:    $(call showFirstName,yuandan,ferr)

The function showfirstname is defined in this code to return the first parameter passed to the caller. Use the make name command to execute the above script and execute the result as follows:

$ make nameyuandan

Common built-in functions:

? (1): String manipulation function

Built-in common string functions such as tables:

(2): File name operation
Common functions for working with file paths, names:

(3): Process Control function

In the C language, If/else is a keyword defined by the syntax itself, and the process control of the script is actually done by the function.

Process Control function Table:

5: Built-in symbols and variables
The make interpreter internally defines special symbols and some special name variables that can be used directly when writing user scripts, without the need to define them.

(1): Built-in symbols

(2): Built-in variables

Because the make script is primarily used for compiling C + + code, it also defines a number of variables that are specifically compiled for C/s + +.

6: Template target

Assume that there is currently a C source project, which contains 3 C source files, the name is F1.C,F2.C,MAIN.C, where F1 and F2 defined two functions, MAIN.C will use the two functions, and then you can write a script file, the pin source code is as follows:

 testtest:  f1.o  f2.o  main.o  gcc-o main.bin  f1.o  f2.o  main.o  F1.O:  f1.c  gcc f1.c  f1.o  F2.O:  f2.c  gcc f2.c  f2.o  MAIN.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. So we use a "template" to define the target, after using the template target, the above script can be simplified to:

OBJ = f1.o f2.o f3.o.PHONY: testtest: $(OBJ)    gcc $(OBJ) -o main.bin%.o: %.c    gcc -c -o [email protected] $<

7: Target-specific variable assignment
In a script file, when you assign a value to a variable, the value of the variable is the same regardless of which target you make, as shown in the following code:

CFLAGS = -c.PHONY: tar1tar1:    gcc $(CFLAGS) main.ctar2 : CFLAGS=tar2:    gcc $(CFLAGS) main.c

Cflags is assigned a value of-C, and its value is valid across the entire script file. For example, when make tar1, the Gcc-c main.c is executed. But in the tar2 target, we want to be able to execute GCC main.c, The cflags variable can therefore be re-assigned in the TAR2 target, which is only valid in the command of the Tar2 target, which is known as the "target-specific" variable assignment. Because the assignment is for that target only.

It is important to note that the assignment of the TAR2 target cannot be directly written in the following form:

tar2 : CFALGS =     gcc $(CFLAGS) main.c

Instead, they must be written separately, that is, the target variable assignment and the target rule are written separately.

Copyright NOTICE: Hello, reprint please leave my blog address, thank you

Chapter--linux Foundation

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.