Linux/Windows 應用程序開發

1、基礎知識

  雖然寫的都是代碼,可是代碼運行在哪一個級別什麼位置,仍是須要作好定位,這樣才心中有數。Linux和Windows都是宏內核,內核本身管理硬件軟件資源,對外提供「系統調用」給程序員編程。html

  操做系統按照必定的協議把咱們寫的程序加載進內存,爲其分配內存等資源,讓其誕生;而後程序本身管本身,本身運行;最後C/C++/Java的mian函數都能會返回,返回後這個程序就結束了(還可能有其餘方式,進程被迫結束),所佔用的資源(好比內存堆和棧、任務時間片、外設等)返還給了操做系統。linux

1.1 Linux 環境

    1 Linux系統調用

   所謂系統調用是指操做系統提供給用戶程序調用的一組「特殊」接口,用戶程序能夠經過這組「特殊」接口得到操做系統內核提供的服務。程序員

   Linux/Unix 的設計策略將是內核空間與用戶空間隔絕開,使得內核空間對象或資源「不能」被用戶空間直接訪問。用戶能在用戶空間操做用戶本身的對象,若是須要使用內核資源,能夠直接調用系統調用(system call)或者經過「庫(好比C庫),也能夠叫APIs」間接地調系統調用。系統調用像一個屏障,它將內核空間與用戶空間隔開。shell

  由於核心態和用戶態用的是兩個不一樣的棧,切換時須要切換特權級別,而且不一樣的硬件平臺(arm/ibm/intel)有不一樣的硬件棧指針(StackPointer)實現機制,因此係統調用的實現須要用到彙編語言,實現的功能包括切換SP、參數傳遞、上下文切換等。若是提到彙編怎麼經過cpu寄存器傳遞參數,這些細節都有其標準,沒必要深究。若是說起StackPointer,stm32就有兩個獨立的StackPointer(SP),MSP與PSP。須要指出Linux用戶空間、內核空間與中斷是有區別的:用戶空間和內核空間其實看上去就是運行在不一樣處理器特權級別上的普通程序(相對中斷而言),只不過內核空間運行着整個軟件系統的核心LinuxKernel,裏面裝的是正在運行的Linux Objects;中斷程序通常針對硬件,短小而嚴謹,不能睡眠。編程

  Linux系統調用很是精簡(只有250個左右),它繼承了UNIX系統調用中最基本和最有用的部分。這些系統調用按照功能邏輯大體可分爲進程控制、進程間通訊、文件系統控制、存儲管理、網絡管理、套接字控制、用戶管理等幾類。windows

   2 系統庫/通用庫

  這裏系統庫指Linux/Unix之上的通用庫,好比C/C++庫。網絡

  編程人員能夠經過調用C/C++庫繼而調用Linux/Unix的系統調用。固然,這些庫不只僅涉及到操做系統(也就是系統調用),還涉及到與系統資源無關的好比數值計算等庫函數,而這種類型的庫函數能夠在用戶空間執行。less

   3 系統命令

       Shell 實際上也是一個可執行程序,與其餘普通程序並沒有二致,它是經過命令行方式運行其它程序的程序。wordpress

   4 系統內核、系統調用、通用庫、shell、應用程序之間的關係

  見下圖。函數

 

1.2 Windows 環境

  Windows和Linux相似都有一套系統內核對象(或者叫組件、模塊,好比進程、內存、文件系統等)。Windows提供了 Windows APIs -> Microsoft Fundation(MFC) -> [todo]

MFC 是利用面向對象思想的C++語言對 WindowsAPIs 進行的一次封裝。MFC提升了編程的效率,可是下降了靈活度。MFC還有一個莫名其妙的別名,叫 Visual C++,是指「用 Visual Studio IDE 、C++ 和 MFC 技術」開發 windows 程序的意思,側重在「MFC」。這句話出自《深刻淺出MFC》第二版侯俊傑。終於知道多年的VC++是什麼意思了。這個世界太亂了,亂起名,讓人厭惡、不負責任。

《深刻淺出MFC》(第二版)侯俊傑

Winconsole程序是沒有GUI的WinAPIs程序。

 1.3 C/C++/Java 標準庫函數

  我認爲不論是基於操做系統仍是基於裸機,你只要編寫出符合標準庫函數規範的函數便可, 即標準對於如何實現無要求,所以不一樣的實現方式,可能性能上有所差別。

  以malloc爲例,malloc invokes either brk or mmap syscall to obtain memory.

https://www.linuxjournal.com/article/6390

https://sploitfun.wordpress.com/2015/02/11/syscalls-used-by-malloc/amp/

關於malloc in linux 的說明:

When a process needs memory, some room is created by moving the upper bound of the heap forward, using the brk() or sbrk() system calls. Because a system call is expensive in terms of CPU usage, a better strategy is to call brk() to grab a large chunk of memory and then split it as needed to get smaller chunks. This is exactly what malloc() does. It aggregates a lot of smaller malloc() requests into fewer large brk() calls. Doing so yields a significant performance improvement. The malloc() call itself is much less expensive than brk(), because it is a library call, not a system call. Symmetric behavior is adopted when memory is freed by the process. Memory blocks are not immediately returned to the system, which would require a new brk() call with a negative argument. Instead, the C library aggregates them until a sufficiently large, contiguous chunk can be freed at once.

For very large requests, malloc() uses the mmap() system call to find addressable memory space. This process helps reduce the negative effects of memory fragmentation when large blocks of memory are freed but locked by smaller, more recently allocated blocks lying between them and the end of the allocated space. In this case, in fact, had the block been allocated with brk(), it would have remained unusable by the system even if the process freed it.

相關文章
相關標籤/搜索