̽ÌÖLinux»º´æ»úÖÆ£ºÕ¹ÏÖÆäÔË×÷ÔÀíºÍ·ÖÀàµÄÉî²ãÆÊÎö
ÉîÈëÆÊÎöLinux»º´æ»úÖÆ£ºÌ½Ë÷ÆäÊÂÇéÔÀíºÍ·ÖÀà
СÐò£º
Linux×÷Ϊһ¿îÆÕ±éʹÓõIJÙ×÷ϵͳ£¬ÆäÐÔÄÜÓÅ»¯Ò»Ö±ÊÇ¿ª·¢Õß¹Ø×¢µÄÖصãÖ®Ò»¡£¶ø»º´æ»úÖÆ×÷ΪһÖÖÌáÉýϵͳÐÔÄܵÄÒªº¦ÊÖÒÕÖ®Ò»£¬ÔÚLinuxϵͳÖÐÊÎÑÝ×ÅÖ÷Òª½ÇÉ«¡£±¾ÎĽ«ÉîÈëÆÊÎöLinux»º´æ»úÖÆ£¬Ì½Ë÷ÆäÊÂÇéÔÀíºÍ·ÖÀ࣬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢Linux»º´æ»úÖƵÄÊÂÇéÔÀí
Linux»º´æ»úÖÆÔÚÄÚ´æÖÎÀíÖÐÊÎÑÝ×ÅÖ÷Òª½ÇÉ«£¬ÆäÖ÷ÒªµÄÊÂÇéÔÀíÈçÏ£º
»º´æÊý¾ÝµÄ¶ÁÈ¡£º
µ±Ó¦ÓóÌÐòÐèÒª¶ÁÈ¡Ò»¸öÎļþʱ£¬²Ù×÷ϵͳ»áÏȼì²é»º´æÖÐÊÇ·ñÒѾ±£´æ¸ÃÎļþµÄ»º´æÊý¾Ý¡£ÈôÊDZ£´æ£¬ÔòÖ±½Ó´Ó»º´æÖжÁÈ¡Êý¾Ý£¬×èÖ¹ÁË»á¼û´ÅÅ̵ĿªÏú¡£ÈôÊÇ»º´æÖÐûÓиÃÎļþµÄÊý¾Ý£¬Ôò²Ù×÷ϵͳ»á½«Îļþ´Ó´ÅÅ̶ÁÈ¡µ½»º´æÖУ¬²¢·µ»Ø¸øÓ¦ÓóÌÐòʹÓá£
»º´æÊý¾ÝµÄдÈ룺
µ±Ó¦ÓóÌÐòÐèҪдÈëÒ»¸öÎļþʱ£¬²Ù×÷ϵͳ»áÏȽ«Êý¾ÝдÈ뻺´æÖУ¬²¢±ê¼ÇΪ¡°ÔࡱÊý¾Ý¡£Ö»Óе±ÏµÍ³ÄÚ´æȱ·¦»ò¸Ã»º´æÊý¾Ý±»ÆäËûÀú³ÌÐèҪʱ£¬²Ù×÷ϵͳ²Å»á½«¡°ÔࡱÊý¾Ýд»Ø´ÅÅÌ¡£
»º´æÊý¾ÝµÄÖû»£º
µ±ÏµÍ³ÄÚ´æȱ·¦Ê±£¬²Ù×÷ϵͳ»áƾ֤һ¶¨µÄË㷨ѡÔñһЩ»º´æÊý¾Ý¾ÙÐÐÖû»£¬ÒÔ±ãΪеÄÊý¾ÝÌÚ³ö¿Õ¼ä¡£Öû»Ë㷨ͨ³£»áƾ֤»º´æÊý¾ÝµÄ»á¼ûƵÂʺÍÖ÷ÒªÐÔ¾ÙÐÐÆÀ¹ÀºÍÑ¡Ôñ¡£
¶þ¡¢Linux»º´æ»úÖƵķÖÀà
Linux»º´æ»úÖÆƾ֤»º´æÊý¾ÝµÄÀàÐͺÍÓÃ;¿ÉÒÔ·ÖΪÒÔϼ¸Àࣺ
Îļþ»º´æ£¨Page Cache£©£º
Îļþ»º´æÊÇLinuxÖÐ×î³£¼ûµÄÒ»ÖÖ»º´æÀàÐÍ£¬ËüÒÔҳΪµ¥Î»¶ÔÎļþÊý¾Ý¾ÙÐлº´æ¡£µ±Ó¦ÓóÌÐòÐèÒª¶ÁÈ¡Îļþʱ£¬²Ù×÷ϵͳ»áÏȼì²éÎļþ»º´æÖÐÊÇ·ñÒѾ±£´æ¸ÃÎļþµÄÒ³¡£ÈôÊDZ£´æ£¬ÔòÖ±½Ó´Ó»º´æÖжÁÈ¡Êý¾Ý£»ÈôÊDz»±£´æ£¬ÔòÐèÒª´Ó´ÅÅÌÖжÁÈ¡ÎļþÊý¾Ýµ½»º´æÖС£Ò³Ã滺´æ½«ïÔ̶ԴÅÅ̵ĶÁд²Ù×÷£¬´Ó¶øÌá¸ßÎļþ»á¼ûµÄËÙÂÊ¡£
Ŀ¼»º´æ£¨dentry Cache£©£º
Ŀ¼»º´æÖ÷ÒªÓÃÓÚ»º´æÎļþϵͳÖÐĿ¼µÄÏà¹ØÐÅÏ¢£¬ÈçĿ¼µÄinodeºÅ¡¢Ä¿Â¼Ïî¶ÔÓ¦µÄÎļþÃûµÈ¡£Ëü¿ÉÒÔïÔÌÓ¦ÓóÌÐòÔÚÎļþϵͳÖоÙÐÐĿ¼²Ù×÷ʱµÄ¿ªÏú£¬¼ÓËÙÎļþϵͳµÄ»á¼ûËÙÂÊ¡£
¿é»º´æ£¨Buffer Cache£©£º
¿é»º´æÖ÷ÒªÓÃÓÚ»º´æÎļþϵͳÖеĿéÊý¾Ý£¬ÈçÎļþϵͳµÄ³¬µÈ¿é¡¢Ë÷Òý½ÚµãºÍÊý¾Ý¿éµÈ¡£ËüÄܹ»Ìṩ¶Ô´ÅÅ̵ÄËæʱ»ú¼ûÄÜÁ¦£¬´Ó¶øÌá¸ßÎļþϵͳµÄÐÔÄÜ¡£
ÍøÂ绺´æ£¨Socket Buffer Cache£©£º
ÍøÂ绺´æÓÃÓÚ»º´æÍøÂçÊý¾Ý£¬ÈçTCP/IPÐÒéÕ»ÖеÄÊý¾Ý°ü¡¢Ì×½Ó×Ö»º³åÇøµÈ¡£Ëü¿ÉÒÔÓÐÓõØïÔÌÓ¦ÓóÌÐòÓëÍøÂç×°±¸Ö®¼äµÄÊý¾Ý´«Ê俪Ïú£¬Ìá¸ßÍøÂç´«ÊäµÄЧÂÊ¡£
Èý¡¢ Linux»º´æ»úÖƵĴúÂëʾÀý
ÏÂÃæ¸ø³öһЩLinux»º´æ»úÖÆʹÓõÄÏêϸ´úÂëʾÀý£º
Îļþ»º´æ¶ÁÈ¡£º
#include <stdio.h> #include <fcntl.h> #include <unistd.h> int main() { int fd = open("test.txt", O_RDONLY); char buf[1024]; ssize_t n = read(fd, buf, sizeof(buf)); close(fd); return 0; }
µÇ¼ºó¸´ÖÆ
Îļþ»º´æдÈ룺
#include <stdio.h> #include <fcntl.h> #include <unistd.h> int main() { int fd = open("test.txt", O_WRONLY | O_CREAT, 0644); char buf[1024] = "Hello, world!"; ssize_t n = write(fd, buf, sizeof(buf)); close(fd); return 0; }
µÇ¼ºó¸´ÖÆ
Ŀ¼»º´æ¶ÁÈ¡£º
#include <stdio.h> #include <dirent.h> int main() { DIR* dir = opendir("/path/to/dir"); struct dirent* entry; while ((entry = readdir(dir)) != NULL) { printf("%s ", entry->d_name); } closedir(dir); return 0; }
µÇ¼ºó¸´ÖÆ
½áÂÛ£º
ͨ¹ýÉîÈëÆÊÎöLinux»º´æ»úÖÆ£¬ÎÒÃÇÏàʶµ½ËüµÄÊÂÇéÔÀíºÍ·ÖÀࡣͨ¹ýºÏÀíµØʹÓúÍÖÎÀí»º´æ»úÖÆ£¬ÎÒÃÇ¿ÉÒÔÓÐÓõØÌá¸ßϵͳµÄÐÔÄܺÍÏìÓ¦ËÙÂÊ¡£Ï£Íû±¾ÎĶԶÁÕßÃ÷È·Linux»º´æ»úÖƺÍÓ¦ÓóÌÐòÐÔÄÜÓÅ»¯ÓÐËù×ÊÖú¡£
²Î¿¼×ÊÁÏ£º
[1] Understanding the Linux Kernel, Third Edition, O’Reilly
[2] LinuxÄÚºËÔ´´úÂë
[3] https://www.kernel.org/
ÒÔÉϾÍÊÇ̽ÌÖLinux»º´æ»úÖÆ£ºÕ¹ÏÖÆäÔË×÷ÔÀíºÍ·ÖÀàµÄÉî²ãÆÊÎöµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡