Linux is a free-of-charge and free-propagation Unix-like operating system, which is first used on Computers Based on x86 series CPUs. This system is designed and implemented by thousands of programmers around the world. The purpose is to establish a Unix-compatible product that is free of copyright control from any commercialized software and available worldwide.
We will not introduce the history of the operating system, or the classification of the operating system in terms of the operating system. In other words, the Linux operating system is just a very new operating system. It does not stick to a specific operating system type. In terms of kernel, it is a time-sharing operating system, but also has the characteristics of a real-time operating system. In terms of system, it is a single-core operating system, but also has a modular microkernel feature. It supports various Internet protocols, so it is also a network operating system. It supports large-scale clusters, grid computing, some people have even set up cloud computing, cloud storage and other environments on it, so it is a distributed operating system ......
In any case, the open source nature of Linux determines that all kinds of people in the world can develop and improve it according to their own needs, linux has many features such as high performance, high availability, scalability, and portability.
In today's IT industry, Linux can be seen from embedded systems, mobile phones, and PCs to large-scale clusters, grids, and clouds. I wrote a series of crazy Linux kernel blog posts to let the Chinese compatriots understand the real inside of this great operating system and how great it is.
Linux is an operating system and software. Since a software must follow the characteristics of all software, the essence is algorithm + Data Structure + documentation. This article begins with his architecture and step-by-step entry into it.
1. Linux architecture
The above is a Linux architecture that I think is still perfect. We can see that the applications used by users will eventually access the kernel in the form of interruptions. A detailed description is that the application sends a system call to the kernel, which is a special interrupt. Then the process that contains the program enters the kernel state again, you can access the various functions and data structures provided by the kernel. For a more detailed description, let's talk about some concepts:
"File" and "process" are the two most basic entities and central concepts in the Linux kernel. all operations in the Linux system are based on these two concepts. The core of the entire system consists of the following five parts:
① Virtual File System: file management and disk cache Management (node and space management)
② I/O device management: block Device Drivers (random access devices), raw devices (raw devices, character devices, bare devices)
③ Process Control: scheduling, synchronization, and communication of processes
④ Storage Management: migrates the program between the primary storage and the CPU level-2 storage.
⑤ Internet Protocol Stack: implements various network protocols.
2. Execution of General programs
A process loads the executable file into three regions of the process during the execution of the System Call exec (Exec ("command name", parameter:
· Body area: the body segment of the Executable File
· Data zone: The data identification segment of the executable file
· Stack zone: A New Process Workspace
Stack is an important concept. It is mainly used to pass parameters, protect the field, store the return address, and provide a storage area for local dynamic variables. We will focus on this in the following blog, because the stack is too important.
The workspace of a process running in the kernel state is the kernel stack, and the workspace when running in the user State is the user stack. The kernel stack cannot be used with the user stack.
Let's look at a program. The user typed copy oldfile newfile on the standard terminal. Here, oldfile is an existing file name, while newfile is a new file name. (The variable version is the initialization data, and the Array Buffer is the uninitialized data)
# Include <fcntl. h>
Char buffer [2048];
Int version = 1;
Main (INT argc, char * argv [])/* When the system references main, argc must be provided as */
{/* Parameter, and assign an initial value to each member of the array argv ,*/
Int fdold, fdnew;/* command: argv [0] points to the string copy ;*/
If (argc! = 3)/* argv [1] points to the string oldfile ;*/
{/* Argv [2] points to the string newfile ;*/
Printf ("need 2 arguments for Copy Program/N ");
Exit (1 );
}
Fdold = open (argv [1], o_rdonly);/* Open the source file read-only */
If (fdold =-1)
{
Printf ("cannot open file % s/n", argv [1]);
Exit (1 );
}
Fdnew = creat (argv [2], 0666);/* Create a target file that can be read and written to all users */
If (fdnew =-1)
{
Printf ("cannot create file % s/n", argv [2]);
Exit (1 );
}
Copy (fdold, fdnew );
Exit (0 );
}
Copy (INT old, int new)
{
Int count;
While (COUNT = read (old, buffer, sizeof (buffer)> 0)
Write (new, buffer, count );
}
When the main is called, The argc, argv, fdold, fdnew, and related function address information in the main will be pushed to the stack, when the next function (in this example, the copy function) is encountered, its parameters, variables, and related addresses are also pushed to the stack: (assuming that the program does not enter the stack process in the three if program segments, in fact, there is also a stack pressure process after the IF, we omitted, but do not think it is not)
We can see that Linux processes work in two states: Kernel Mode and user mode ). Therefore, the Linux Kernel stack is separated from the user stack. The user stack stores information about general functions and System Call functions in the program, which is visible to users. The Kernel stack stores functions or data in the kernel, such as getblk functions, it is transparent to users.
So when will the program use the user stack and the kernel stack? In addition, the next loop is decomposed.