标签:
introduction to the Linux kernel
1.operating system
1) considered the parts of the system
2) responsible for basic use and administration.
3) includes the kernel and device drivers, boot loader, command shell or other user interface, and basic file and system utilities.
Boot Loader 是在操作系统内核运行之前运行的一段小程序。通过这段小程序,我们可以初始化硬件设备、建立内存空间的映射图,从而将系统的软硬件环境带到一个合适的状态,以便为最终调用操作系统内核准备好正确的环境。
2.Typical components of a kernel:
1) interrupt handlers to service interrupt requests,
2) a scheduler to share processor time among multiple processes,
3) a memory management system to manage process address spaces,
4) and system services such as networking
5) and interprocess communication.
3.kernel typically resides in an elevated(提高的;高尚的;严肃的;欢欣的) system state compared to normal user applications: protected memory space and full access to the hardware-------This system state and memory space is collectively referred to as kernel-space.
4.user applications execute in user-space.They see a subset of the machine’s available resources and can perform certain system functions, directly access hardware, access memory outside of that allotted them by the kernel, or otherwise misbehave.
5.system communicate with the kernel via system calls
6.When hardware wants to communicate with the system, it issues an interrupt that literally interrupts the processor, which in turn interrupts the kernel
7.When an application executes a system call, we say that the kernel is executing on behalf of the application. Furthermore, the application is said to be executing a system call in kernel-space, and the kernel is running in process context.
8.interrupt handlers run in a special interrupt context that is not associated with any process.This special context exists solely to let an interrupt handler quickly respond to an interrupt, and then exit.
9.each processor is doing exactly one of three things at any given moment:
1) In user-space, executing user code in a process
2) In kernel-space, in process context, executing on behalf of a specific process
3) In kernel-space, in interrupt context, not associated with a process, handling an
Interrupt
10.Monolithic(整体的;巨石的,庞大的;完全统一的) Kernel Versus Microkernel Designs
1) We can divide kernels into two main schools of design: the monolithic kernel and the microkernel.
2) Monolithic kernels are implemented entirely as a single process running in a single address space.(Unix kernel)--------- simpler
3) Linux is a monolithic kernel; that is, the Linux kernel executes in a single address space entirely in kernel mode.
11.Linux supports the dynamic loading of kernel modules
1) Linux has symmetrical multiprocessor (SMP) support
2) The Linux kernel is preemptive.
3) Linux takes an interesting approach to thread support
4) Linux provides an object-oriented device model with device classes, hot-pluggable events, and a user-space device filesystem (sysfs).
5) Linux ignores some common Unix features that the kernel developers consider poorly designed, such as STREAMS, or standards that are impossible to cleanly implement.
12.linux kernel Different Nature
1) No libc or Standard Headers
2) No Memory Protection
3) No (Easy) Use of Floating Point
4) Small, Fixed-Size Stack
13.Process:
A process is a program (object code stored on some media) in the midst of execution.
Process =
1) executing program code
2) + a set of resources such as open files and pending(在…期间) signals
3) + internal kernel data + processor state
4) + a memory address space with one or more memory Mappings
5) + one or more threads of execution
6) + a data section containing global variables
14.Linux has a unique implementation of threads: It does not differentiate between
threads and processes.To Linux, a thread is just a special kind of process.
15.process descriptor type : struct task_struct
16.struct task_struct was stored at the end of the kernel stack of each process
17.struct thread_info: Each task’s thread_info structure is allocated at the end of its stack.
18.thread_info has a pointer to the process descriptor
19.fork() system call
creates a new process by duplicating an existing one.The process that calls fork() is the parent, whereas the new process is the child. The fork() system call returns from the kernel twice: once in the parent process and again in the newborn child.
20.Process Descriptor and the Task Structure
The kernel stores the list of processes in a circular doubly linked list called the task list.
21.The process descriptor contains the data that describes the executing program—open files, the process’s address space, pending signals, the process’s state, and much more
22.The task_struct structure is allocated via the slab allocator.
struct task_struct was stored at the end of the kernel stack of each process.
thread_info has a pointer to the process descriptor
23.Process State
1) TASK_RUNNING: the only possible state for a process executing in user-space
2) TASK_INTERRUPTIBLE: sleeping (blocked), waiting for some condition to exist
3) TASK_UNINTERRUPTIBLE: identical to TASK_INTERRUPTIBLE except that it does not wake up and become runnable if it receives a signal
4) __TASK_TRACED: being traced by another process, such as a debugger, via ptrace.
5) __TASK_STOPPED: Process execution has stopped, occurs if the task receives the SIGSTOP, SIGTSTP, SIGTTIN,or SIGTTOU signal or if it receives any signal while it is being debugged.
24.The Process Family Tree
All processes are descendants of the init process, whose PID is one. The kernel starts init in the last step of the boot process.The init process, in turn, reads the system initscripts and executes more programs, eventually completing the boot process.
25.Every process on the system has exactly one parent. every process has zero or more children.
Siblings: Processes that are all direct children of the same parent are called siblings.
26.Process Creation
Unix takes the unusual approach of separating these steps into two distinct functions: fork()and exec()
The first, fork(), creates a child process that is a copy of the current task. It differs from the parent only in its PID (which is unique), its PPID (parent’s PID, which is set to the original process), and certain resources and statistics, such as pending signals, which are not inherited.
The second function, exec(), loads a new executable into the address space and begins executing it.
27.Copy-on-write (or COW) is a technique to delay or altogether prevent copying of the data.
28.To linux, a thread is merely a process that shares certain resources with other processes.
29.kernel threads:
1) standard processes that exist solely in kernelspace.
2) The significant difference between kernel threads and normal processes is that kernel threads do not have an address space.
3) They operate only in kernel-space and do not context switch into user-space.
4) chedulable and preemptable, the same as normal processes.
Such as: the flush tasks and the ksoftirqd(软中断处理线程) task
30.Multitasking operating systems:
cooperative multitasking and preemptive multitasking.
31.Linux, like all Unix variants and most modern operating systems--------->preemptive multitasking
32.Cooperative multitasking: preemption timeslice
33.Processes can be classified as either I/O-bound or processor-bound.
34.I/O-bound:
a process that spends much of its time submitting and waiting on I/O requests.
35.processor-bound:
Most graphical user interface (GUI) applications, for example, are I/O-bound, even if they never read from or write to the disk, because they spend most of their time waiting on user interaction via the keyboard and mouse.
36.O(1) is an example of big-o notation. In short, it means the scheduler can perform its work in constant time, regardless of the size of any inputs.
37.The most notable of these was the Rotating Staircase Deadline scheduler, which introduced the concept of fair scheduling, borrowed from queuing theory, to Linux’s process scheduler.
38.Completely Fair Scheduler, or CFS:O(1) scheduler’s eventual replacement in kernel version 2.6.23
39.Policy:Policy is the behavior of the scheduler that determines what runs when.
40.The scheduling policy in a system must attempt to satisfy two conflicting goals: fast process response time (low latency低等待时间, 短时等待) and maximal system utilization (high throughput).
------employ complex algorithms to determine the most worthwhile process to run while not compromising fairness to other, lower priority, processes.
41.Unix systems tends to explicitly favor I/O-bound processes, thus providing good process response time.
42.Linux, aiming to provide good interactive response and desktop performance, optimizes for process response (low latency), thus favoring I/O-bound processes over processor-bound processors.
43.The Linux kernel implements two separate priority ranges:
1.nice value :
a number from –20 to +19 with a default of 0. Larger nice values correspond to a lower
priority—you are being “nice” to the other processes on the system.
2.the real-time priority:
The values are configurable, but by default range from 0 to 99, inclusive. Opposite from nice values, higher real-time priority values correspond to a greater priority.
44.Timeslice:the numeric value that represents how long a task can run until it is preempted被打断
45.I/O-bound processes do not need longer timeslices (although they do like to run often), whereas processor-bound processes crave long timeslices (to keep their caches hot).
46.Timeslice is sometimes called quantum or processor slice in other systems. Linux calls it timeslice
47.CFS assigns processes a proportion of the processor:On Linux,nice value acts as a weight,nice values (a lower priority) receive a deflationary weight, yielding them a smaller proportion of the processor.smaller nice values (a higher priority) receive an inflationary weight, netting them a larger proportion of the processor.
48.Linux scheduler is modular,called scheduler classes,enable different, pluggable algorithms to coexist, scheduling their own types of processes
49.The Completely Fair Scheduler (CFS) is the registered scheduler class for normal processes, called SCHED_NORMAL in Linux (and SCHED_OTHER in POSIX).
50.the priority is exported to user-space in the form of nice values.
a few question to figure out:
- First, mapping nice values onto timeslices requires a decision about what absolute timeslice to allot each nice value.
- A second problem concerns relative nice values and again the nice value to timeslice mapping.
- Third, if performing a nice value to timeslice mapping, we need the ability to assign an absolute timeslice.
- The fourth and final problem concerns handling process wake up in a priority-based scheduler that wants to optimize for interactive tasks.
51.Fair Scheduling
To calculate the actual timeslice, CFS sets a target for its approximation of the “infinitely small” scheduling duration in perfect multitasking.This target is called the targeted latency.
CFS imposes a floor on the timeslice assigned to each process.This floor is called the minimum granularity.
52.Sleeping and Waking Up
1) Tasks that are sleeping (blocked) are in a special nonrunnable state.
2) without this special state, the scheduler would select tasks that did not want to run or, worse, sleeping would have to be implemented as busy looping.
3) Sleeping: The task marks itself as sleeping, puts itself on a wait queue, removes itself from the red-black tree of runnable, and calls schedule() to select a new process to execute.
4) Waking up: The task is set as runnable, removed from the wait queue, and added back to the red-black tree.
5) two states are associated with sleeping
(Both types of sleeping tasks sit on a wait queue, waiting for an event to occur, and are not runnable.):
A. TASK_INTERRUPTIBLE : ignore signals
B. TASK_UNINTERRUPTIBLE: wake up prematurely and respond to a signal if one is issued
53.Preemption and Context Switching
handled by the context_switch()function defined in kernel/sched.c
1) Calls switch_mm(),switch the virtual memory mapping
2) Calls switch_to(),to switch the processor state
54.Linux provides two real-time scheduling policies, SCHED_FIFO and SCHED_RR.
55.System calls provide a layer between the hardware and user-space processes.
1) First, it provides an abstracted hardware interface for userspace.
2) Second, system calls ensure system security and stability.
3) Finally, a single common layer between user-space and the rest of the system allows for the virtualized system provided to processes
56.how system calls are defined:
1) asmlinkage modifier directive to tell the compiler to look only on the stack for this function’s arguments.
2) For compatibility between 32- and 64-bit systems, system calls defined to return an int in user-space return a long in the kernel.
3) naming convention taken with all system calls in Linux: sys_xxxx()
56.System Call Handler : somehow user-space causes an exception or trap to enter the kernel.
57.Interrupts and Interrupt Handlers
How can the processor work with hardware without impacting the machine’s overall performance?
1.polling. Periodically, the kernel can check the status of the hardware in the system and respond accordingly
2.provide a mechanism for the hardware to signal to the kernel when attention is needed--------->interrupt.
58.interrupt request (IRQ) lines : interrupt values
59.a specific interrupt is associated with a specific device, and the kernel knows this
60.Bottom half
1) generic description: is a generic operating system term referring to the deferred portion of interrupt processing, so named because it represents the second, or bottom, half of the interrupt processing solution.
2)Bottom half also refers to the original deferred work mechanism in Linux.This mechanism is also known as a BH
Currently, three methods exist for deferring work: softirqs, tasklets, and work queues.
Chapters 10 Kernel Synchronization Methods
Chapter 15: The Process Address Space
An individual process’s view of memory is as if it alone has full access to the system’s physical memory.
-
Each process is given a flat 32- or 64-bit address space
-
A memory address in one process’s address space is completely unrelated to that same memory address in another process’s address space
-
Although a process can address up to 4GB of memory (with a 32-bit address space), it doesn’t have permission to access all of it.
-
Process has permission to access intervals of memory addresses. These intervals of legal addresses are called memory areas.The process, through the kernel, can dynamically add and remove memory areas to its address space.
-
Memory areas have associated permissions, such as readable, writable, and executable, that the associated process must respect.
-
If a process accesses a memory address not in a valid memory area, or if it accesses a valid area in an invalid manner, the kernel kills the process with the dreaded “Segmentation Fault” message.
-
Memory areas can contain all sorts of goodies:
-
text section--A memory map of the executable file’s code
-
data section --A memory map of the executable file’s initialized global variables
-
bss section--A memory map of the zero page containing uninitialized global variables
-
A memory map of the zero page used for the process’s user-space stack.
-
An additional text, data, and bss section for each shared library (C library)
-
Any memory mapped files.
-
Any shared memory segments.
-
Any anonymous( 匿名的 ) memory mappings
-
memory areas do not overlap.As you can see,there is a separate memory area for each different chunk of memory in a running process:the stack,object code,global variables,mapped file,and so on.
-
The kernel represents a process’s address space with a data structure called the memory descriptor. <linux/mm_types.h > struct mm_struct
- The memory area structure, vm_area_struct, represents memory areas. It is defined in <linux/mm_types.h> struct vm_area_struct
- The vm_mm field(in struct vm_area_struct ) points to this VMA’s associated mm_struct. Note that each VMA is unique to the mm_struct with which it is associated. Therefore, even if two separate processes map the same file into their respective address spaces, each has a unique vm_area_struct to identify its unique memory area.
Linux snacks from <linux kernel development>
标签:
原文地址:http://www.cnblogs.com/liao123abc/p/4790228.html