- Building the requisite knowledge is a challenge in tackling any technical subject. When explaining how software system works, things can get really complicated. Too much detail bogs down the reader and makes the important stuff difficult to grasp (the human brain just can't process so many new concepts at once), but two little detail leaves the reader in the dark and unprepared for later material.
I've designed most chapters to tackle the most important material first: the basic information that you'll need in order to progress. In places, I've simplified things in order to keep focus. As a chapter progresses, you'll see much more detail, especially in the last few sections. Do you need to know those bits right away? In most cases, no, as I often note. If your eyes start to glaze over when faced with a lot of extra details about stuff that you only just learned, don't hesitate to skip ahead to the next chapter or just take a break. The nitty-gritty will still be there waiting for you.
The Big Picture
Levels and Layers of Abstraction in a Linux System
- The kernel runs in kernel mode, and the user processes run in user mode. Code running in kernel mode has unrestricted access to the processor and main memory.
- The area that only the kernel can access is called kernel space.
- User mode, in comparison, restricts access to a (usually quite small) subset of memory and safe CPU operations.
- User space refers to the parts of main memory that the user processes can access
Hardware: Understanding Main Memory
- main memory is just a big storage area for a bunch of 0s and 1s. Each 0 or 1 is called a bit.
- A CPU is just an operator on memory; it reads its instructions and data from the memory and writes data back out to the memory.
- One of the kernel’s tasks is to split memory into many subdivisions, and it must maintain certain state information about those subdivisions at all times
The kernel is in charge of managing tasks in four general system areas:
- Processes. The kernel is responsible for determining which processes are allowed to use the CPU.
- Memory. The kernel needs to keep track of all memory—what is currently allocated to a particular process, what might be shared between processes, and what is free.
- Device drivers. The kernel acts as an interface between hardware (such as a disk) and processes. It’s usually the kernel’s job to operate the hardware.
- System calls and support. Processes normally use system calls to communicate with the kernel.
- In practice, each process uses the CPU for a small fraction of a second, then pauses; then another process uses the CPU for another small fraction of a second; then another process takes a turn, and so on.
- The act of one process giving up control of the CPU to another process is called a context switch.
- Each piece of time—called a time slice
** The kernel is responsible for context switching
- The CPU (the actual hardware) interrupts the current process based on an internal timer, switches into kernel mode, and hands control back to the kernel.
- The kernel records the current state of the CPU and memory, which will be essential to resuming the process that was just interrupted.
- The kernel performs any tasks that might have come up during the preceding time slice (such as collecting data from input and output, or I/O, operations).
- The kernel is now ready to let another process run. The kernel analyzes the list of processes that are ready to run and chooses one.
- The kernel prepares the memory for this new process, and then prepares the CPU.
- The kernel tells the CPU how long the time slice for the new process will last.
- The kernel switches the CPU into user mode and hands control of the CPU to the process.
- The kernel must have its own private area in memory that user processes can’t access. o Each user process needs its own section of memory.
- One user process may not access the private memory of another process.
- User processes can share memory.
- Some memory in user processes can be read-only.
- The system can use more memory than is physically present by using disk space as auxiliary.
Device Drivers and Management
- The kernel’s role with devices is pretty simple. A device is typically accessible only in kernel mode
System Calls and Support
- fork() When a process calls fork(), the kernel creates a nearly identical copy of the process.
- exec() When a process calls exec(program), the kernel starts program, replacing the current process.
- When you enter ls into a terminal window, the shell that’s running inside the terminal window calls fork() to create a copy of the shell, and then the new copy of the shell calls exec(ls) to run ls.
- As mentioned earlier, the main memory that the kernel allocates for user processes is called user space.
- The Linux kernel supports the traditional concept of a Unix user. A user is an entity that can run processes and own files
- A user is associated with a username
- the kernel does not manage the usernames; instead, it identifies users by simple numeric identifiers called userids
- Users exist primarily to support permissions and boundaries.
Basic Commands and Director Hierarchy
The Bourne Shell: /bin/sh
- Linux uses an enhanced version of the Bourne shell called bash or the “Bourne-again” shell.
- The bash shell is the default shell on most Linux distributions, and /bin/sh is normally a link to bash on a Linux system.
** 大部分都非常熟悉， 只写不太熟悉的**
Standard Input and Standard Output
- diff # diff file1 file2
- file file1 # show file infomation
- find dir -name filename
- head -n file1 tail -n file2
- CTRL-B Move the cursor left
- CTRL-F Move the cursor right
- CTRL-P View the previous command (or move the cursor up)
- CTRL-N View the next command (or move the cursor down)
- CTRL-A Move the cursor to the beginning of the line
- CTRL-E Move the cursor to the end of the line
- CTRL-W Erase the preceding word
- CTRL-U Erase from cursor to beginning of line
- CTRL-K Erase from cursor to end of line
- CTRL-Y Paste erased text (for example, from CTRL-U)
File Modes and Permissions
-rw--r--r-- - ==> Type rw- ==> User Permissions r-- ==> Group Permissins r-- ==> Other permissions chmod 700 file1 ==> User read write execute, Group none permition, Other none permition 7 ==> 111 ==> read write execute r ==> read , w ==> write, x ==> execute chmod u+x file1 // add user execute permission chmod g+x file1 // add group execute permission chmod o+x file1 // add other execute permission chmod go+x file1 chmod +x file1 || chmod a+x file1 // add all execute permission
Archiving and Compressing Files
gzip file // compress file to file.gz gunzip file.gz // uncompress file.gz to file tar cvf achive file1 file2 # pack files # Unlike the zip programs for other operating systems, gzip does not create archives of files; that is, it doesn’t pack multiple files and directories into one fi tar xvf archive.tar # unpack files tar tvf archive.tar # Table-of-Contents Mode # Compressed Archives (.tar.gz) gunzip file.tar.gz tar xvf file.tar # A better way is to combine archival and compression functions with a pipeline. For example, this command pipeline unpacks <file>.tar.gz: The zcat command is the same as gunzip -dc. The -d option decompresses and the -c option sends the result to standard output (in this case, to the tar command). zcat file.tar.gz | tar xvf - # You can use z as an option to automatically invoke gzip on the archive; this works both for extracting an archive (with the x or t modes in tar) and creating one (with c). tar ztvf file.tar.gz