OS知識點總結

圖片版在這:http://www.javashuo.com/article/p-wcrjpnzs-ha.htmlhtml

上完5103其實就該總結一下的......仍是懶  (呵node

 


1. 進程棧

函數調用時,函數參數、返回地址、環境、函數內非static的局部變量存入棧。(棧空間是專門留給函數用的)git

程序內全部malloc/new出來的空間、全局變量、全部的static變量存入堆。程序員

(Ref:http://www.javashuo.com/article/p-npysctrh-gx.htmlgithub

堆是從低地址向高地址增加的,棧相反。參考下圖算法

 


2. 內核態和用戶態、system call

User programs are typically executed in user mode. In user mode, program cannot control devices or access memory address directly. All privileged operations should be executed by using system call. A user program executes in the USER mode and it is given limited privileges. Region of memory it can access is restricted. It cannot execute certain instructions such as HALT or setting or changing timers. It cannot directly access any I/O devices or network ports. The primary reason is to protect the system and other user processes and the file system from a misbehaving program.shell

Kernel code is trusted to be safe and correct. This code is executed in Privileged/Supervisor Mode. It can execute any operation (i.e. instruction). A user process executes the kernel code by making a system call.
安全

 

System Call的全過程:服務器

System Call和普通函數調用(procedure call)的對比:網絡

system call:
1. Push all parameters into user mode stack
2. put the code of system call into CPU register (in other words, call this procedure)
3. execute a trap instruction, so it will start executing in kernel mode at a fixed address.
4. kernel code will be dispatched to a system-call handler, and the system-call handler runs in kernel mode.
5. After the kernel code finished, switch back to user mode and return to the instruction following TRAP in system call library procedure.
6. After the system call finished, return to user program.
7. The user program will clean up the parameters of system call in the stack, and user program continues.

procedure call:
1. Push all parameters into user mode stack
2. put the code of procedure call into CPU register (in other words, call this procedure)
3. execute the procedure in user mode
4. After procedure call finished, return to user program.
5. The user program will clean up the parameters of system call in the stack, and user program continues.

The procedure call does not need to switch betweenuser mode and kernel mode, so it will save time

 

Interrupts & exceptions & System calls & traps
Interrupts   source: External devices (eg: finish I/O operation on hard disk). Goal: CPU could work in parallel with devices
Exceptions   source: Errors in CPU when executing instructions. Goal: Handle internal errors in CPU (eg: divided by zero)
System calls   source: Program manually call system call functions provided by operating system. Goal: Execute features which needs OS support (eg: I/O)
Traps   source: TRAP instruction in program. Goal: Transfer from user mode into kernel mode

 

fork()函數

1.Only the thread who called fork() will be forked to new child process. So the child process only have 1 thread.
2.The child process will copy all the memory data from its parent process. Then they will run as independent processes. In modern operating systems, they will do
copy-on-write in fork(), which means both parents and child will receive a read-only copy of the parent's data space, instead of copying the whole data in the first
place. The copy will actually occur when one of them want to modify some data.
3.Based on (2), mutex variables will be copied into child process. Suppose we added locked a mutex before fork(). After fork(), the mutex in the child process will
remain locked, and there will be only one thread in the child process, which means this mutex in child process will never be unlocked(and this is called deadlock). Even if we unlocked mutex in parent process, the one in child process will not be modified.
4.Based on (2), File descriptors will be copied into child process. So they will have the same file pointer, which will point to the same file table. Thus the file status
opened in both processors(include their filename, current file offset) are shared.

 

clone()函數

Linux provides a more powerful function than fork for creating a new process or thread.
It can be used to create a new process as in case of fork.
It can also be used to create a new thread in the address space of the calling process.

 

進程的上下文切換(略)

 

進程和線程的區別

A process represents one single sequential activity. – one execution context (Program counter and stack)

A thread represents one activity -- one context and stack per thread. All Threads of one process can share resources such as memory, open files, and communication channels.

  • 進程是資源分配的獨立單位
  • 線程是資源調度的獨立單位

All threads in a process share the following items:
Address space
Global variables
Open files / resources
Child processes
Signals and signal handlers
Accounting Information

Thread specific items:
Program counter and registers (execution context)
Stack
Execution state (ready, waiting, running)

 

進程之間私有和共享的資源

  • 私有:地址空間、堆、全局變量、棧、寄存器
  • 共享:代碼段,公共數據,進程目錄,進程 ID

線程之間私有和共享的資源

  • 私有:線程棧,寄存器,程序寄存器
  • 共享:堆,地址空間,全局變量,靜態變量

 

協程

進程是資源分配的最小單位,線程是CPU調度的最小單位。而協程能夠理解爲同一個線程經過上下文切換來「超線程」,併發執行兩個工做。比起線程,協程更加輕量,並且多個協程訪問同一資源不須要加鎖(由於本質上仍是在同一個線程內)。

協程的詳細定義能夠參考這裏。另外go語言對協程有很好的支持

 

進程/線程之間的通訊方式總結

進程之間的通訊方式以及優缺點
    管道(PIPE)
        有名管道:一種半雙工的通訊方式,它容許無親緣關係進程間的通訊
            優勢:能夠實現任意關係的進程間的通訊
            缺點:
                長期存於系統中,使用不當容易出錯
                緩衝區有限
        無名管道:一種半雙工的通訊方式,只能在具備親緣關係的進程間使用(父子進程)
            優勢:簡單方便
            缺點:
                侷限於單向通訊
                只能建立在它的進程以及其有親緣關係的進程之間
                緩衝區有限
    信號量(Semaphore):一個計數器,能夠用來控制多個線程對共享資源的訪問
        優勢:能夠同步進程
        缺點:信號量有限
    信號(Signal):一種比較複雜的通訊方式,用於通知接收進程某個事件已經發生
    消息隊列(Message Queue):是消息的鏈表,存放在內核中並由消息隊列標識符標識
        優勢:能夠實現任意進程間的通訊,並經過系統調用函數來實現消息發送和接收之間的同步,無需考慮同步問題,方便
        缺點:信息的複製須要額外消耗 CPU 的時間,不適宜於信息量大或操做頻繁的場合
    共享內存(Shared Memory):映射一段能被其餘進程所訪問的內存,這段共享內存由一個進程建立,但多個進程均可以訪問
        優勢:無須複製,快捷,信息量大
        缺點:
            通訊是經過將共享空間緩衝區直接附加到進程的虛擬地址空間中來實現的,所以進程間的讀寫操做的同步問題
            利用內存緩衝區直接交換信息,內存的實體存在於計算機中,只能同一個計算機系統中的諸多進程共享,不方便網絡通訊
    套接字(Socket):可用於不一樣及其間的進程通訊
        優勢:
            傳輸數據爲字節級,傳輸數據可自定義,數據量小效率高
            傳輸數據時間短,性能高
            適合於客戶端和服務器端之間信息實時交互
            能夠加密,數據安全性強
        缺點:需對傳輸的數據進行解析,轉化成應用級的數據。



線程之間的通訊方式
    鎖機制:包括互斥鎖/量(mutex)、讀寫鎖(reader-writer lock)、自旋鎖(spin lock)、條件變量(condition)
        互斥鎖/量(mutex):提供了以排他方式防止數據結構被併發修改的方法。
        讀寫鎖(reader-writer lock):容許多個線程同時讀共享數據,而對寫操做是互斥的。
        自旋鎖(spin lock)與互斥鎖相似,都是爲了保護共享資源。互斥鎖是當資源被佔用,申請者進入睡眠狀態;而自旋鎖則循環檢測保持者是否已經釋放鎖。
        條件變量(condition):能夠以原子的方式阻塞進程,直到某個特定條件爲真爲止。對條件的測試是在互斥鎖的保護下進行的。條件變量始終與互斥鎖一塊兒使用。
    信號量機制(Semaphore)
        無名線程信號量
        命名線程信號量
    信號機制(Signal):相似進程間的信號處理
    屏障(barrier):屏障容許每一個線程等待,直到全部的合做線程都達到某一點,而後從該點繼續執行。
線程間的通訊目的主要是用於線程同步,因此線程沒有像進程通訊中的用於數據交換的通訊機制
View Code

 

Process Control Block

存儲瞭如下信息:

Process state Number
User ID, accounting info, scheduling priority
Process context
Memory info, page tables
Open files, current /root dir
Pending signals and I/O

 


3. Scheduling Algorithm(略)

 

 


4. 臨界區、信號量、死鎖

進程的幾種狀態:

New              process is being created and initialized
Running       – currently executing
Ready          – waiting to get CPU to become running
Blocked        – waiting for some event, such a I/O completion
Swapped      – partially executed, and its memory image has been stored on the disk
Terminated   – process has completed due to normal or abnormal exit.

sleep:         A process executing a system call may go to sleep to wait for a resource.
wakeup:     When the resource is available it is awakened

注意區分兩個概念:

忙等待(busy-waiting): while(i!=1) {…} 消耗 CPU
阻塞等待(blocked-waiting): 掛起,不消耗 CPU

 

臨界區:同時只有一個進程能夠訪問的資源。爲了互斥訪問臨界資源,每一個進程在進入臨界區以前,須要先進行檢查。

A sequence of code is called CRITICAL SECTION has the following properties: Mutual exclusion Property : At any time, at most one process can be executing the critical section code. In general, updates to shared data structures are performed in a critical section.

 

信號量

The motivation was to avoid busy waiting by blocking a process execution until some condition is satisfied.

  • wait():value--,一個進程想得到臨界區(當value>0時才放行。不然進程睡眠,等待信號量大於 0)    
  • signal():value++,一個進程釋放臨界區
  • value>=0時表示信號量的值。value<0時表示當前等待進入臨界區的進程個數。

Binary Semaphore:value取值只能取0和1。0 表示臨界區已經加鎖,1 表示臨界區解鎖。

 

經典的信號量同步問題

1111111111111

 

死鎖

死鎖的發生條件:

  • 互斥資源
  • 已獲得某資源的進程能夠請求新的資源
  • 已分配到某資源的進程不可被搶佔(eg:刻光盤)
  • 環路等待:A等待B,B等待C,C等待A

預防死鎖的方法

  • 打破互斥條件:改造獨佔性資源爲虛擬資源,大部分資源已沒法改造。
  • 打破不可搶佔條件:當一進程佔有一獨佔性資源後又申請一獨佔性資源而沒法知足,則退出原佔有的資源。
  • 打破佔有且申請條件:採用資源預先分配策略,即進程運行前申請所有資源,知足則運行,否則就等待,這樣就不會佔有且申請。
  • 打破循環等待條件:實現資源有序分配策略,對全部設備實現分類編號,全部進程只能採用按序號遞增的形式申請資源。
  • 銀行家算法

Ref:https://github.com/CyC2018/CS-Notes/blob/master/notes/%E8%AE%A1%E7%AE%97%E6%9C%BA%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%20-%20%E6%AD%BB%E9%94%81.md

銀行家算法:避免死鎖。在進程申請資源時,檢查申請的合理性。只有不致使死鎖的申請才被承認。

 

安全性算法:由銀行家算法改進更通用的安全性算法。用於multiple resource type的狀況。

        Ref:https://blog.csdn.net/Caoyang_He/article/details/80819411

 

管程(monitor)

 


5. 內存管理

Paged memory management:eliminates fragmentation by non-contiguous allocation

  • Physical memory is divided into fixed size blocks– FRAMES.
  • Logical memory (as seen by the programmer) is also dividedinto the same size blocks called – PAGES.
  • PAGE TABLE: Maps logical pages to physical frames.  sizeof(Page)==sizeof(Frame)

頁表使用虛擬地址中的頁號做爲索引,以找相應的物理頁號每一個進程它本身的頁表用來將程序的虛擬地址空映射到主存中。爲了指出頁儲器中的位置,硬件包含個指向頁表首址的寄存器,咱們稱之頁表寄存器 ( page table register

OS中會同時運行不少的進程。進程的地址空間,以及它在主存中能夠訪問的全部數據 在主 的頁表所定系統只是簡單地加載頁表寄存器用來指向它想激活的進程的頁表 而不是保 存整個頁 因爲不一樣進程使用相同的虛擬地址 所以每一個進程有各 的頁表 做系統負責分配物 理主存和更新頁表 所以不一樣進程的虛擬地址空間不會發生衝突

擬存儲器系統必須使用 write-back機制,對存儲器中的頁進行單獨的寫操做(暫時不修改磁盤上對應的頁),而且在該頁被替換出存儲器時再被複制到磁盤中去。爲此,爲了追蹤讀入 主存中 的頁是否被寫過 ,能夠在頁表中增長一 髒位 (dirty bit) 當被指向的這個頁中任何字被寫時就將這一位置1 。修改過的頁也被稱爲dirty page  

 

頁表的結構

程序中使用的地址都是邏輯地址(Page Number和Offset)。CPU想訪問某內存空間時,首先在TLB(位於CPU中,至關於一個頁表的cache)中找頁號對應的頁框號。若是找到了就直接去物理內存讀該頁框的內容。若是沒找到就先進入頁表找,獲得頁框號和物理地址(Frame Number+Offset),再去讀物理內存。

另外,每一個頁表項使用 1 位有效位,就像在 cache 中設計的若是該位爲0,表示該頁不在主存中,就發生次缺頁 若是該位爲1,代表該頁在主存中,能夠直接根據物理頁號去內存中讀取這個頁 。 (《計算機組成與設計——硬件/軟件接口》 P294)

TLB的做用就是避免每次都要查找頁表(這個速度是比較慢的)。TLB 的每一個標記項存放虛頁號的部分,每一個數據項中存放了物理頁號 因爲咱們每次訪問的是 TLB 而不是頁表, TLB 須要包括其餘狀態位,如髒位和引用位。 以下圖,灰色線表示直接經過TLB hit找到內存中的頁面,沒找到的就要去頁表查了。

頁表能夠提供該虛擬地址的物理頁號(能夠用來建立TLB項),或者指出該頁在磁盤上,這時就會發生缺頁 因爲頁表對於每一個虛頁都有 個相應的項並不須要標記 換句話說,不一樣於 TLB, 頁表並不屬於 cache 。在後面FastMATH的例子中能夠更深入的體會到這一點。

各個步驟所需的時間(clock)能夠參考下圖:

 

TLB和cache的關係

TLB也在CPU中,屬於一種特殊的cache。那麼它和普通的L一、L2 Cache有什麼關係呢?

以HI書的圖5-30中介紹的FastMATH芯片爲例。原版的圖比較複雜,簡化一下大概是這樣:

這裏面CPU想訪問一個虛擬地址,而後有這麼幾種可能性(VA: 虛擬地址    PA: 物理地址):

  • 1. (1)TLB沒找到VA->PA,(2)頁表中找到了VA->PA。而後去Cache試圖訪問這個PA,若是有了就完事了,沒有就再去內存讀
  • 2. (1)TLB找到了VA->PA。直接去Cache試圖訪問這個PA,若是有了就完事了,沒有就再去內存讀
  • 3. (1)TLB沒找到VA->PA,(2)頁表裏也沒找到VA->PA。而後要去硬盤的交換區讀相關的VA->PA的記錄,而後這時cache裏是確定沒這個PA(頁面)的,只能取讀內存了

能夠看出:

  • TLB和頁表屬於一類,用於完成VA->PA。
  • Cache和內存屬於一類,用於存儲每一個PA的頁面。
  • CPU只認識VA,因此須要先想辦法轉換成PA才行。這樣就有了前面這套流程

上面這套流程假定了在訪問 cache 以前,全部存儲器地址都被轉換成物理地址。在這種結構中,cache 是按物理地址索引 (physically indexed) 而且是物理標記 ( physically tagged) 的(全部 cache 的索引和標記都用物理地址,而不是虛擬地址) 。 在這個系統中,假定 cache命中,那麼訪問主存的時間要包括對 TLB 訪問和 cache 訪問的時間,固然,這些訪問能夠流水地執行 。這種設計叫作物理尋址的Cache。

另外還有一種虛擬尋址Cache,這裏Cache裏的數據是按虛擬地址索引並訪問的。但這就有個問題了:若是兩個很類似的進程用了某個如出一轍的虛擬地址,那麼存放在cache中就會致使混亂。一個辦法是對不一樣進程搞個別名,但這樣就增長了軟件的複雜度。

這兩種設計觀點經常使用的折中方法是採用虛擬地址索引的 cache-—-有時僅僅使用地址的Page offset(頁內偏移)部分,由 於沒有被轉換,所以其實是物理地址——但使用物理標記。這些採用虛擬索引和物理標記的設計,試圖同時擁有虛擬地址索引 cache 的優越性能以及物理尋址 cache (physical addressed cache) 的簡單結構 。 例如 ,在 這種狀況下就沒有別名的問題 。 要實現這種方法,必須在最小頁大小 、 cac he 大小以及相聯度之間進行謹慎的權衡 。(HI P301,Problem set Q2(b))

 

Page Fault

物理內存空間是有限的,因此引入虛擬內存的概念。當程序試圖訪問的內存地址如今不在物理內存中時,就會發生Page Fault。此時OS得到控制權,並進入TRAP(進入內核態,並由系統將對應的內存頁從虛擬內存載入物理內存)。

操做系統在建立進程的時候一般會在閃存或磁盤上,爲進程中全部的頁(所有虛擬地址空間)預留一塊磁盤空間 。 這一磁盤空間稱爲交換區 (swap space) 。 同時,它也會建立一個數據結構來記錄每一個虛擬頁在磁盤上的存放位置 。 這個數據結構多是頁表中的一部分,也多是輔助數據結構, 尋址方式和頁表同樣 。

Page Fault後的過程以下圖:

 

 

頁面置換算法

內存滿時用於選出要放入虛擬內存的頁面。(略)

 

當頁表太大時須要分紅多級頁表

二級頁表:第二級頁表能夠按須要動態調入,從而節省空間。 

 

Working Set

The memory references generated by a program over some period of time tend to be confined to some small number of pages which reflects its "locality" during that execution phase. Try to maintain the pages which belong to the current working set (locality). Working set is estimated based on a time window of execution instead of some number of memory references. Parameter t defined as a time window. Working-set algorithm removes page p from then working-set if age(p) > t

程序在一段時間內訪問的內存塊集中在特定的幾塊。所以能夠記錄最近(某個time window內)用過的k個Page,保證它們不會被置換出去。

 

分段內存管理(略)

 

分段和分頁的比較

    對程序員的透明性:分頁透明,可是分段須要程序員顯式劃分每一個段。
    地址空間的維度:分頁是一維地址空間,分段是二維的。
    大小是否能夠改變:頁的大小不可變,段的大小能夠動態改變。
    出現的緣由:分頁主要用於實現虛擬內存,從而得到更大的地址空間;分段主要是爲了使程序和數據能夠被劃分爲邏輯上獨立的地址空間而且有助於共享和保護。

頁面置換算法

FIFO:最簡單的......

LRU:Replace 最久沒用過的。注意如何實現LRU算法也是個常常被問的問題

NRU:按如下優先級(順序按timestamp而定)淘汰頁面:沒被訪問過也沒被修改過 > 沒被訪問過但修改過 > 訪問過但沒被修改過 > 訪問過也修改過。

Working Set:爲每一個頁面設置一個R bit(記錄當前時鐘時間間隔內是否訪問過該頁面)和timestamp(記錄上次訪問時間),而後按如下規則淘汰頁面:

                         

                          其中τ是一個參數,表示一個Working set的大小(按時間計算)。R==1表示剛剛用過,確定不能淘汰。若是有頁面知足R==0且timestamp已經在τ以外,直接淘汰它。若是掃描了一圈發現不存在這樣的頁面,就把R==0且timestamp最小(距離如今最久遠)的一個淘汰掉。

Working Set Clock:對基本工做集算法的改進,提升了一點點性能,但意思仍是同樣的

 


6. IO、文件系統

Polling: busy waiting until device finish

Interrupt: 用中斷處理,但頻繁中斷佔用太多 cpu

DMA:傳輸一大段才 interrupt,而不是每一個 word/byte 都中斷
1. CPU sets up the DMA controller register to handler DMA transfer from a disk to memory.
    •Number of bytes to be transferred
    •Memory address where to transfer the data.
2. DMA controller issues a request to disk controller read the next block in its buffer and write to a memory address.
3. Disk controller reads disk block in its buffer, and writes it to memory buffer.
4. After writing the block to the memory, the disk controller 「acks」 the DMA controller, which then decrements the count, increases the memory address, and goes to step 2 if
the count is greater than 0.
5. DMA controller interrupts the CPU when the count reaches 0.

 

Disk && Disk Storage Management(略)

 

Concurrent Access to Files
On Unix system:
    •Writes to an open file are visible immediately to other users that have this file open at the same time.
    •When a process forks a child, a file open at the time of fork is shared with the child. Both parent and the child share the file position pointer

 

inode

Ref:https://blog.csdn.net/xuz0917/article/details/79473562

文件存儲在硬盤上,硬盤的最小存儲單位叫作「扇區」(Sector)。每一個扇區儲存512字節(至關於0.5KB)。操做系統讀取硬盤的時候,不會一個個扇區的讀取,這樣效率過低,而是一次性連續讀取多個扇區,即一次性讀取一個「塊」(block)。這種由多個扇區組成的「塊」,是文件存取的最小單位。「塊」的大小,最多見的是4KB,即連續八個sector組成一個block。

文件數據都儲存在block中,那麼很顯然,咱們還必須找到一個地方儲存文件的「元信息」,好比文件的建立者、文件的建立日期、文件的大小等等。這種儲存文件元信息的區域就叫作inode,中文譯名爲」索引節點「。每個文件都有對應的inode,裏面包含了與該文件有關的一些信息。如圖所示

The addresses of all storage blocks of a file are recorded in one (or more) "index block". This block is maintained on the disk and is accessed through the directory entry. When a file is opened, this block is read into memory. Once this block is in memory, any block of the file can be accessed directly

 

Unix的iNode是一個多級的索引結構。以下圖,

  • The INODE for a file contains up to 10 direct data block addresses.
  • Three pointers for first, second, and third level of indirection.

Suppose that file-block size is 4KB. The first 10 direct pointers will point first 40KB of the file.

Suppose that the first level indirect block has 128 pointers. (設32bit pointer,4KB/32bit=128)
Then, with the block size of 4KB, we can now address additional 128*4KB, i.e. about 0.5 MBytes.

For a file larger than that, the INODE contains the second level indirect blocks. This block has pointers (128) for 128 first level indirect blocks, each of which points to data blocks of 0.5 MBytes. Thus by using the second level indirect pointer, we can now store additional 64 MBytes in the file.(128*128*4KB)

With third level indirect block we get file size close to 8GB. (128*128*128*4KB)

 

文件夾也有inode。訪問文件夾時,先訪問該文件夾的iNode,而後順着iNode找該文件夾所在的block。

 

  Symbolic Linking(符號連接/軟連接) Hard-Link(硬連接)
 

相似Windows的快捷方式,只記錄源文件的位置。

是個獨立的文件,佔用空間

與源文件指向相同的iNode,不佔用空間。

源文件的全部hard link都被刪除時,源文件才真正被刪除。iNode keep count of hard links.

Pros

能夠爲文件夾建立symbolic link

源文件能夠在不一樣的文件系統。

可用來保護源文件

 


7. Security (略)

 

 

 

 

 


8. Virtual Machine(略)

 

 

 

 


 9. 一些補充知識點

Ref:
    https://github.com/CyC2018/CS-Notes/blob/master/notes/%E8%AE%A1%E7%AE%97%E6%9C%BA%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%20-%20%E7%9B%AE%E5%BD%95.md
    https://github.com/huihut/interview#-%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F

 

主機字節序與網絡字節序

主機字節序又叫 CPU 字節序,其不是由操做系統決定的,而是由 CPU 指令集架構決定的。主機字節序分爲兩種:

  • 大端字節序(Big Endian):高序字節存儲在低位地址,低序字節存儲在高位地址
  • 小端字節序(Little Endian):高序字節存儲在高位地址,低序字節存儲在低位地址

各架構處理器的字節序

  • x86(Intel、AMD)、MOS Technology 650二、Z80、VAX、PDP-11 等處理器爲小端序;
  • Motorola 6800、Motorola 68000、PowerPC 970、System/370、SPARC(除 V9 外)等處理器爲大端序;
  • ARM(默認小端序)、PowerPC(除 PowerPC 970 外)、DEC Alpha、SPARC V九、MIPS、PA-RISC 及 IA64 的字節序是可配置的。

 

網絡字節順序是 TCP/IP 中規定好的一種數據表示格式,它與具體的 CPU 類型、操做系統等無關,從而能夠保證數據在不一樣主機之間傳輸時可以被正確解釋。網絡字節順序採用大端(Big Endian)排列方式。

 

大內核和微內核

大內核
大內核是將操做系統功能做爲一個緊密結合的總體放到內核。因爲各模塊共享信息,所以有很高的性能。

微內核
因爲操做系統不斷複雜,所以將一部分操做系統功能移出內核,從而下降內核的複雜性。移出的部分根據分層的原則劃分紅若干服務,相互獨立。在微內核結構下,操做系統被劃分紅小的、定義良好的模塊,只有微內核這一個模塊運行在內核態,其他模塊運行在用戶態。由於須要頻繁地在用戶態和核心態之間進行切換,因此會有必定的性能損失。

 

 

編譯系統

如下是一個 hello.c 程序:

#include <stdio.h> int main() { printf("hello, world\n"); return 0; }

在 Unix 系統上,由編譯器把源文件轉換爲目標文件。

gcc -o hello hello.c

這個過程大體以下:

 

  • 預處理階段:處理以 # 開頭的預處理命令;
  • 編譯階段:翻譯成彙編文件;
  • 彙編階段:將彙編文件翻譯成可重定位目標文件;
  • 連接階段:將可重定位目標文件和 printf.o 等單獨預編譯好的目標文件進行合併,獲得最終的可執行目標文件。

靜態連接

靜態連接器以一組可重定位目標文件爲輸入,生成一個徹底連接的可執行目標文件做爲輸出。連接器主要完成如下兩個任務:

  • 符號解析:每一個符號對應於一個函數、一個全局變量或一個靜態變量,符號解析的目的是將每一個符號引用與一個符號定義關聯起來。
  • 重定位:連接器經過把每一個符號定義與一個內存位置關聯起來,而後修改全部對這些符號的引用,使得它們指向這個內存位置。

目標文件

  • 可執行目標文件:能夠直接在內存中執行;
  • 可重定位目標文件:可與其它可重定位目標文件在連接階段合併,建立一個可執行目標文件;
  • 共享目標文件:這是一種特殊的可重定位目標文件,能夠在運行時被動態加載進內存並連接;

動態連接

靜態庫有如下兩個問題:

  • 當靜態庫更新時那麼整個程序都要從新進行連接;
  • 對於 printf 這種標準函數庫,若是每一個程序都要有代碼,這會極大浪費資源。

共享庫是爲了解決靜態庫的這兩個問題而設計的,在 Linux 系統中一般用 .so 後綴來表示,Windows 系統上它們被稱爲 DLL。它具備如下特色:

  • 在給定的文件系統中一個庫只有一個文件,全部引用該庫的可執行目標文件都共享這個文件,它不會被複制到引用它的可執行文件中;
  • 在內存中,一個共享庫的 .text 節(已編譯程序的機器代碼)的一個副本能夠被不一樣的正在運行的進程共享。

 

 

 

 

 

 

 

 

111

相關文章
相關標籤/搜索