I'll base it on the following picture , explain Linux The architecture of (architecture).( This figure is for reference 《Advanced Programming in Unix Environment》)
The innermost layer is the hardware , The outermost layer is a common application for users , for instance firefox browser ,evolution Check email , A computational fluid model and so on . Hardware is the material foundation , And applications provide services . But in between , We have to go through a lot of twists and turns .
Remember Linux start-up .Linux Start with the kernel (kernel), The kernel is a piece of computer program , This program directly manages the hardware , Include CPU、 Memory space 、 Hard disk interface 、 Network interface and so on . All computer operations are passed to the hardware through the kernel .
To make it easier to call the kernel ,Linux Make the function interface of kernel into system call (system call). System calls look like C Functions of language . You can call... Directly in the program .Linux The system has more than 200 such system calls . Users don't need to understand the complex structure of the kernel , You can use the kernel . System call is the smallest functional unit of the operating system . An operating system , And operating system based applications , It is impossible to achieve functions beyond system call . A system call function is like a stroke of a Chinese character . Any Chinese character has to be made up of basic strokes （ spot 、 cross 、 And so on ） constitute . I can't make up strokes .
Enter... On the command line $man 2 syscalls You can see all the system calls . You can also pass $man 2 read To see system calls read() Explanation . In these two commands 2 It means that we are going to 2 class ( System call class ) Query in ( What are the specific classes $man man notice ).
The functions provided by system call are very basic , So it's very cumbersome to use . A simple operation is to allocate memory space to a variable , You need to use multiple system calls .Linux Define some library functions (library routine) To combine system calls into some common functions . The operation of allocating memory above , It can be defined as a library function ( image malloc() A function like this ). Again for instance , While reading the file , The system call requires us to set the buffer we need . I can use Standard IO The read function in the library . This read function is responsible for setting the buffer , Also responsible for using the read system call function . There is no efficiency advantage for machines to use library functions , But it can save programmers from the details . Library functions are like radicals of Chinese characters , It's made up of strokes , But it's easier to form words with radicals , such as " iron ". Of course , You don't have to use library functions at all , And calling system functions directly , It's like “ people ” The words are the same , No radicals .
( actually , An operating system is UNIX System , You have to have some library functions , such as ISO C Standard library ,POSIX Standards, etc .)
shell It's a special application . Many users call it Command line .shell Is a command interpreter (interpreter), When we type in “ls -l” When , It interprets this string as
- Find the file in the default path (/bin/ls),
- Execute the document , With parameters "-l".
Before me use > It means reorientation , use | Said the pipe , through shell explain & perhaps | The meaning of .Shell And then through the system , With the command kernel , Implement specific redirection or pipeline . Before there was a graphical interface ,shell Acts as a user interface , When users want to run certain applications , adopt shell Enter the command , To run the program .shell It's programmable , It can be performed in accordance with shell The text of grammar . This kind of text is called shell Script (script). As you can see in the architecture diagram ,shell Next system call , All kinds of applications , At the same time, there are many of their own gadgets to use .Shell Scripts can be in a few lines , Implement complex functions .
UNIX One of our philosophy is to let each program do a small function independently . and shell Serves as a bridge between these little functions " glue ", So that different programs can have a clear interface ( Text stream ) Working together , So as to enhance the functions of various programs . This is also Linux The old bird encourages the novice to use more shell, One of the reasons for the lack of graphical interfaces .
(shell There are many kinds , The most common is bash, And then there is sh, csh, tcsh, ksh. They come from different times , There are also differences in the functions supported .)
A use bash shell Terminal
One shell Corresponding to a terminal (terminal). Once upon a time , The terminal is a hardware device , Used to input and display output . Now , Because of the popularity of graphical interfaces , The terminal is often like the above picture , It's a graphical window . You can input or output text through this window . This text is passed directly to shell Analyze and explain , And then execute .
Last , We go into general applications . An application is a program , It can
- Call system functions directly
- Call library function
- function shell Script
These applications can be developed in multiple languages . The most common is C Language .
Linux Use the kernel to realize the dialogue between software and hardware .
Through this interface called by the system ,Linux Separate the upper application from the lower kernel , Hiding the underlying complexity , It also improves the portability of the upper application .
Library functions use system calls to create modular functions ,
Shell Provides a user interface , And let's take advantage of shell Script with the syntax of , To integrate programs .