ºìÁªLinuxÃÅ»§
Linux°ïÖú

FreeBSDϵͳ±à³Ì

·¢²¼Ê±¼ä:2007-06-16 00:36:47À´Ô´:ºìÁª×÷Õß:linuxguy
²Î¿¼Á´½Ó£º
¡¾FreeBSD system programming ¡¿ÖÐÎÄ·­Òë¼Æ»®¼°ËùÓÐÒìÒåÌá½»´¦




FreeBSDϵͳ±à³Ì



Copyright(C) 2001¡«2006 Nathan Boeger ºÍMana Tominaga °æȨËùÓУ¬¼òÌåÖÐÎÄ°æÓÉChinaUnixÂÛ̳Ìṩ·­Òë¡£

Ŀ¼
µÚÒ»ÕÂ: FreeBSDµÄMake
µÚ¶þÕÂ: BSD×Ô¾Ù
µÚÈýÕÂ: ½ø³ÌºÍÄں˷þÎñ
µÚËÄÕÂ: ¸ß¼¶½ø³Ì¿ØÖƺÍÐźÅ
µÚÎåÕÂ: »ù±¾I/O
µÚÁùÕÂ: ¸ß¼¶I/O
µÚÆßÕÂ: ½ø³Ì×ÊÔ´ºÍϵͳÏÞÖÆ
µÚ°ËÕÂ: FreeBSD 5.x


µÚÒ»Õ FreeBSDµÄmake



ÒëÕߣºÓêË¿·çƬ@chinaunix.net



1.1 FreeBSDµÄmake

×÷Ϊ³£Óõĺͻù±¾µÄUnixÈí¼þ¿ª·¢¹¤¾ß£¬makeÊÇÒ»¸ö¿ÉÒÔ¸ú×ÙÈ«²¿µÄÎļþÒÀÀµ¹ØϵµÄ·Ç³£ºÃµÄ²¾¼Ç¹¤¾ß³ÌÐò¡£Òª¹ÜÀíÒÀÀµ¹ØϵÕâÑùµÄÏîĿϸ½Ú³£³£ÐèÒª»¨·ÑºÜ¶àµÄʱ¼ä£¬ÉõÖÁ»áÍÏÑÓ¿ª·¢½ø¶È¡£µ±¶à¸ö¿ª·¢ÈËÔ±ºÏ×÷Ò»¸öÏîÄ¿µÄʱºò£¬ÒÀÀµ¹ØϵµÄ¸ú×پͿÉÄܱäµÃÏ൱À§ÄÑÁË¡£ÊÂʵÉÏ£¬ÕýÈ·µØʹÓÃmake¿ÉÒÔ°ïÖúÎÒÃǼӿìÓ¦ÓóÌÐòµÄ¿ª·¢£¬´Ó¶øÌá¸ßÉú²úЧÂÊ¡£

ËäÈ»make×î³õµÄÉè¼ÆÊÇÓÃÀ´¶ÔÓ¦ÓóÌÐò°æ±¾¹¹½¨µÄά»¤¹ý³Ì½øÐйÜÀíµÄ£¬ÎÒÃÇʵ¼ÊÉÏ»¹¿ÉÒÔͨ¹ý´´½¨Ò»ÏµÁеĻùÓÚÄ¿±êÒÀÀµ¹ØϵµÄUnix shellÃüÁîÀ´ÈÃmakeÍê³É¶àÖÖ¶àÑùµÄ¶îÍ⹤×÷¡£ÕâЩÒÀÀµ¹Øϵ¿ÉÒÔÓúܶàÖÖ·½Ê½¶¨Òå----°üÀ¨ÐèÒª½øÐбàÒëµÄÔ´Îļþ¡¢ËùÐèµÄ¿âÎļþ¡¢shellÃüÁîÒÔ¼°ÆäËüµÄÄ¿±ê¡£

makeÓжàÖÖ·ç¸ñµÄ°æ±¾£¬ÆäÖаüÀ¨GNU makeºÍSystem V make¡£²¢²»ÊÇÔÚÿ¸ömake°æ±¾Öж¼ÓÐÎÒÃǽÓÏÂÀ´ÌÖÂÛµÄÄÇЩÌØÐÔ£¬¾ßÌåʹÓÃÄĸö°æ±¾ÍêÈ«È¡¾öÓÚÄãµÄ¸öÈËϲºÃ¡£ÎÒÃǽ«Ö÷Òª¹Ø×¢¸úËæFreeBSDÒ»Æð·¢²¼µÄmake£¨Ò²½Ð×öbmake»òpmake£©£¬ÓÈÆäÊÇÈçºÎͨ¹ýËüÀ´±àÒëºÍ¸üÐÂFreeBSDϵͳ£¬Ò²¾ÍÊÇËùνµÄmake world¡£ËäÈ»ÎÒÃǹØ×¢µÄÊÇFreeBSD make£¬µ«ÎÒÃÇÔÚÕâÀïÌÖÂÛµÄËùÓж«Î÷¶ÔÓÚ¸÷ÖÖBSD°æ±¾À´Ëµ¶¼ÊÇÊÊÓõġ£

ÎÒÃÇÊ×ÏȻὲÊöÒ»¸öMakefileµÄ»ù±¾Îļþ²¼¾ÖºÍÓï·¨¡£Èç¹ûÕâ¶ÔÓÚÄãÀ´ËµÌ«¼òµ¥ÁË£¬ÄÇÄã¿ÉÒÔÖ±½ÓÌøµ½±¾Õ½áÊø´¦µÄʾÀý²¿·ÖÈ¥ÔĶÁ¡££¨×¢Ò⣬ÎÒÃǸø³öµÄ´úÂëʾÀýÖ»ÓÃÓÚÑÝʾÎÒÃǹØÓÚmakeÄ¿±êºÍÒÀÀµ¹ØϵµÄÌÖÂÛ£¬ËüÃDz¢²»Ò»¶¨ÊÇ¿ÉÒÔÔËÐеĴúÂë¡££©

µ±È»£¬ºÍÆäËü¹¤¾ß³ÌÐòÒ»Ñù£¬×ʼӦ¸ÃÏÈÈ¥¿´¿´man page£¬ÒÔ¶ÔmakeÌṩµÄÃüÁîÐÐÑ¡ÏîµÄ¸ÅÒªºÍϸ½ÚÓÐÒ»¸öÕýʽµÄÁ˽⡣ͬʱ£¬ºÍÆäËü¹¤¾ß³ÌÐòÒ»Ñù£¬Ñ§Ï°makeµÄ×îºÃ·½·¨¾ÍÊÇʹÓÃËü¡£´´½¨Ò»Ð©Ð¡Ð͵ÄÔ´Îļþ£¨¿ÉÒÔʹÓÃÈκÎÓïÑÔ£©£¬È»ºó³¢ÊÔһЩÏÂÃæ¸ø³öµÄÀý×Ó¡£ÎÒÃÇÏ£Íû¶ÁÍê±¾ÕÂÖ®ºóÄã³ýÁËÀí½âmakeµÄÓï·¨¹æÔòÖ®Í⣬»¹ÖªµÀËüÊÇÈçºÎ¹¤×÷µÄ¡£

1.2 Makefile²¼¾Ö

×ܵÄ˵À´£¬ÄãʹÓÃmakeµÄ·½Ê½¾ÍÊÇÈÃËüÈ¥¶ÁÒ»¸öMakefile£¬ÄãÐèÒªÔÚMakefileÀïÖ¸¶¨Ò»¸öÄ¿±ê¼°ÆäÒÀÀµ¹Øϵ¡£ÔÚÔËÐеÄʱºò£¬make»á°´Ë³ÐòËÑË÷Ãû×ÖΪMakefile»òmakefileµÄÎļþ¡£Õâ¸öMakefileͨ³£ÊÇ·ÅÔÚÒ»¸ö¹¤³ÌµÄ¸ùĿ¼Ïµģ¬Èç¹ûÏëÖ¸¶¨ÆäËüµÄMakefile£¬¿ÉÒÔÔÚÃüÁîÐÐÉÏÓÃ-f (filename)µÄÑ¡Ïî¸ø³ö¡£


CODE:[Copy to clipboard]make -f OtherMakefile
1.3 Óï·¨

Ò»¸öMakefileµÄ½á¹¹ÓÉËĸö»ù±¾ÐÐ×é³É£¬ËüÃǶ¼¿ÉÒÔͨ¹ýÔÚÐÐβÌí¼Ó¡®\¡¯×Ö·ûÀ´À©Õ¹µ½ÏÂÒ»ÐУ¨ºÍshell±à³ÌÏàËÆ£©¡£×¢ÊÍÊÇÒÔ¡®#¡¯ºÅ¿ªÊ¼µÄ£¬ÖÁÐÐβ½áÊø¡£


CODE:[Copy to clipboard]########################################
# Simple Makefile with comment example #
########################################

# when run, it will just echo hello
all:
echo "hello"
ҪʹÓÃmakeÀ´±àÒëÒ»¸ö¹¤³Ì£¬Ê×ÏÈÐèҪȷ¶¨ÔÚÄãµÄµ±Ç°¹¤×÷Ŀ¼ÖÐÒÑÓÐÒ»¸öÕýÈ·µÄMakefile£¬È»ºóÔÙͨ¹ýÏÂÁÐÃüÁîÖ®Ò»À´Ê¹ÓÃmake£º


CODE:[Copy to clipboard]bash$ make

bash$ make all

bash$ make
1.4 Ä¿±ê

ÓÃÀ´Ö¸¶¨Ä¿±êµÄ·½Ê½ÓкܶàÖÖ£¬²»¹ý×î³£ÓõľÍÊÇÓÃÄ¿±êÎļþ»òÒ»¸ö¹¤³ÌµÄÃû×Ö¡£¹¤³ÌÃû×Ö²»Ó¦µ±°üº¬Óпոñ»ò±êµã·ûºÅ£¬²»¹ýÕâÖ»ÊǸö¹ßÀý¶øÒÑ£»ÉÙÁ¿µÄ¿Õ¸ñºÍ±êµã·ûºÅÒ²ÊÇÔÊÐíµÄ¡£Õâ¸öÃû×Ö±ØÐëдÔÚÒ»¸öÐÂÐеĿªÍ·£¬±ØÐëÒÔµ¥Ã°ºÅ£¨:£©¡¢Ë«Ã°ºÅ£¨::£©»ò¸Ð̾ºÅ£¨!£©ÈýÕßÖ®Ò»½áÊø¡£


CODE:[Copy to clipboard]myprog:


another::


sample!

ÔÚÕâЩĿ±êÃû×ÖÖ®ºóÊÇËùÐèµÄÒÀÀµÌõ¼þ£¬°üÀ¨Ãû×Ö¡¢±äÁ¿ÒÔ¼°ÆäËüµÄÄ¿±êµÈµÈ¡£Èç¹ûÄãµÄÒÀÀµÌõ¼þÌ«¶àµÄ»°£¬¿ÉÒÔÓÃÒ»¸ö¡®\¡¯ºÍÒ»¸önewlineÀ´½«ËüÃÇ·Ö¿ª¡£ËùÓеÄÒÀÀµÌõ¼þ¶¼±ØÐëMakefileÄÚ¶¨Òå»òÕß´æÔÚÓÚij¸öÍⲿÎļþÖУ¬·ñÔòmake½«ÎÞ·¨ÖªµÀÈçºÎÈ¥Íê³ÉÒÀÀµ²Ù×÷¡£

һЩʾÀýÈçÏ£º


CODE:[Copy to clipboard]all: driver.cpp network_class.cpp file_io_class.cpp network_libs.cpp file_io_libs.cpp

all: myprog.o

myprog.o:
ÉÏÀýÖУ¬allºÍmyprog.oÊÇÒªmakeµÄÄ¿±ê¡£×¢Òâmyprog.o¼ÈÊÇÒ»¸öÄ¿±êÓÖÊÇÒ»¸öÒÀÀµÌõ¼þ¡£makeÊ×ÏȻᵽmyprog.oÄǶù£¬Ö´ÐÐËüµÄÃüÁȻºó·µ»Øµ½allÄǶù£¬ÔÙÖ´ÐÐËüµÄÃüÁî¡£ÕâÖÖ²Ù×÷ÐòÁÐÊÇmakeµÄ¹¦ÄÜ»ù´¡¡£

°´ÕÕ¹ßÀý£¬all:Ä¿±êÊÇÄãµÄÄ¿±êÖеÄ×î¸ßÕߣ¬ÕâÒâζ×Åmake½«´ÓÕâ¶ù¿ªÊ¼È¥Ñ°ÕÒÒªÍê³Éall:Ä¿±ê¶¼ÐèÒªÄÄЩ¶«Î÷¡£²»¹ýall:Ä¿±ê²¢²»ÊDZØÐèµÄ£¬Èç¹ûûÓеĻ°£¬make¾Í»á¼òµ¥µØÑ¡ÔñËùÓÐÁгöµÄÄ¿±êÖеĵÚÒ»¸ö£¬Ö»¶ÔÆäʵʩ²Ù×÷£¬³ý·ÇÄãÔÚÃüÁîÐÐÉÏÖ¸¶¨ÁËij¸öÄ¿±ê¡£¶ÔÓÚÄÇЩÓÐÒ»¸öºËÐĵÄÓ¦ÓóÌÐòÐèҪά»¤ºÍ¹¹½¨µÄ¹¤³ÌÀ´Ëµ£¬ÎÒÃǽ¨ÒéÄãʹÓÃall:Ä¿±ê£»ÕâÊÇÒ»¸öͨÓõĹßÀý£¬ÓÐÖúÓÚ±ÜÃâ´íÎóºÍ²»±ØÒªµÄÈÎÎñ¡£

ÉÏÀýËùʾµÄÒÀÀµÐòÁÐÖ»ÊǺܼòµ¥µÄÒ»¸ö¡£ÏÂÃæÊÇÒ»¸ö¸üΪ¸´ÔÓºÍÁé»îµÄÒÀÀµÐòÁУ¬ÎÒÃÇûÓиø³öÓÃÓÚ¾ßÌåÄ¿±êµÄÃüÁ


CODE:[Copy to clipboard]all: myprog.o lib

lib: lex

lex:

myprog.o: app.h
×¢Ò⣬ÔÚÕâ¸öÀý×ÓÖУ¬all:Ä¿±êÓÐÁ½¸öÒÀÀµÌõ¼þ£ºmyprog.oºÍlib¡£ÕâÁ½¸öÒÀÀµÌõ¼þ±¾ÉíÓÖ¶¼ÊÇÄ¿±ê£¬make½«Ê×ÏÈÈ¥±àÒëmyprog.o¡£ÔÚmake±àÒëmyprog.oµÄʱºò£¬»á·¢ÏÖÓÐÒ»¸öºÍapp.hµÄÒÀÀµ¹Øϵ¡£app.h²¢Ã»ÓÐÔÚÕâ¸ömakefileÀﶨÒ壬µ«app.hÈ´ÊÇÒ»¸öÔÚµ±Ç°Ä¿Â¼ÖеÄÍ·Îļþ¡£

ÓÃÓÚmyprog.oµÄÃüÁîÍê³ÉÖ®ºó£¬make¼´·µ»Øµ½all:´¦£¬¼ÌÐø´¦ÀíÏÂÒ»¸öÒÀÀµÌõ¼þ£¬ÔÚ´ËÀýÖÐÊÇlib¡£ÒÀÀµÌõ¼þlib±¾ÉíÒ²ÓÐÒ»¸öÒÀÀµÌõ¼þlex£¬ËùÒÔmakeÔÚÍê³Élib֮ǰ»áÏÈÈ¥Íê³Élex:¡£

×¢Ò⣺ÕýÈçÄãËù¿´µ½µÄ£¬ÕâЩÒÀÀµ¹Øϵ¿ÉÄÜ»á·Ç³£³¤£¬»òÕßǶÌ׵úÜÉî¡£Èç¹ûÄãÓÐÒ»¸öºÜ´óµÄMakefile£¬ÄÇÒ»¶¨ÒªºÃºÃµØ×é֯һϣ¬°ÑÄ¿±êµÄ˳ÐòŪºÃ¡£

1.5 ÇóÖµ¹æÔò

ÒÀÀµ¹ØϵÊÇ°´ÕÕÒÀÀµÓÚÄ¿±êÃû×Ö½áÊø·ûºÅµÄÑϸñ¹æÔòÀ´ÇóÖµµÄ¡£Ò»µ©makeÈÏΪÂú×ã¹æÔò£¬Ëü½«Í¨¹ýÖ´ÐÐÏàÓ¦µÄÃüÁîÀ´´´½¨Ìض¨µÄÄ¿±ê£¨±ÈÈç±àÒë¸ÃÄ¿±ê£©¡£ÀýÈ磬ʹÓõ¥Ã°ºÅ:¿ÉÒÔÈÃÄã¶ÔÐèÒª½øÐбàÒëµÄÄ¿±ê½øÐиüΪ¾«Ï¸µÄ¿ØÖÆ¡£Ò²¾ÍÊÇ˵£¬Äã¿ÉÒÔÖ¸¶¨Ä³¸öÌض¨µÄÄ¿±êÎļþÿ´Î¶¼ÐèÒªÖØбàÒë»òÕß½öµ±ËüµÄÔ´Îļþ¹ýʱ֮ºó²Å±àÒë¡£ÕâЩ¹æÔò¶¼ÊÇ»ùÓÚÄ¿±êÃû×ֵĽáÊø·ûºÅµÄ£¬ÈçÏ£º

Èç¹ûÄ¿±êÃû×ÖÒÔµ¥Ã°ºÅ£¨:£©½áÊø£¬Ëü½«¸ù¾ÝÒÔÏÂÁ½¸ö¹æÔòÀ´´´½¨£º



Èç¹ûÄ¿±êÉÐδ´æÔÚ£¬¾ÍÏñÎÒÃÇÔÚÉÏÃæ¾ÙµÄÀý×ÓÀïµÄall:Ò»Ñù£¬make¾Í»á´´½¨Ëü¡£

Èç¹ûÈÎÒâÒ»¸öÔ´Îļþ¾ßÓбȵ±Ç°Ä¿±ê¸üеÄʱ¼ä´Á¡£ÔÚÉÏÀýÖÐÈç¹ûapp.h»òmyprog.c¾ßÓиüеÄʱ¼ä´Á£¬myprog.o¾Í»á±»make¡£ÕâÖÖÇé¿öÖ»Ðè¼òµ¥µØÓÃÒ»ÏÂtouchÃüÁî¼´¿É³öÏÖ




CODE:[Copy to clipboard] touch myprog.c
Èç¹ûÄ¿±êÃû×ÖÒÔ˫ðºÅ£¨::£©½áÊø£¬Ëü½«¸ù¾ÝÒÔÏÂÈý¸ö¹æÔòÀ´´´½¨£º



Èç¹ûÈÎÒâÒ»¸öÔ´Îļþ¾ßÓбȵ±Ç°Ä¿±ê¸üеØʱ¼ä´Á¡£

¸ÃÄ¿±ê²»´æÔÚ¡£

¸ÃÄ¿±êûÓÐÓëÖ®¹ØÁªµÄÔ´Îļþ¡£



Èç¹ûÄ¿±êÃû×ÖÒÔ¸Ð̾ºÅ£¨!£©½áÊø£¬Ö»Òªmake°ÑËüËùÐèµÄÈ«²¿ÒÀÀµÌõ¼þ¶¼´´½¨Íê±Ï¾Í»áÀ´´´½¨Ëü¡£

ÄãÖ»ÄÜÔÚÄ¿±ê»òÔ´ÎļþµÄ×îºóÒ»¸ö×é³É²¿·ÖÖÐʹÓÃͨÅä±í´ïʽ?¡¢*ºÍ[]£¬¶øÇÒÖ»ÄÜÓÃÓÚÃèÊöÒѾ­´æÔÚµÄÎļþ¡£±ÈÈ磺


CODE:[Copy to clipboard]myprog.[oc]
¶øʹÓû¨À¨ºÅ{}µÄ±í´ïʽÔò²»Ò»¶¨·ÇµÃÃèÊöÒѾ­´æÔÚµÄÎļþ¡£±ÈÈ磺


CODE:[Copy to clipboard]{mypgog,test}.o

# the expression above would match myprog.o test.o only
×îºóÐèҪעÒâÒ»µã£º¿É±ä±í´ïʽÊÇ°´ÕÕĿ¼˳ÐòÀ´´¦ÀíµÄ£¬¶ø·Ç×Öĸ˳Ðò£¬¾Í¸úÔÚshell±í´ïʽÖÐÒ»Ñù¡£ÀýÈ磬Èç¹ûÄãµÄÄ¿±êÓÐijЩ»ùÓÚ×Öĸ˳ÐòµÄÒÀÀµÌõ¼þ£¬ÏÂÃæÕâ¸ö±í´ïʽ¿ÉÄܾͲ»¶ÔÁË£º


CODE:[Copy to clipboard]{dprog,aprog,bprog,cprog}.cpp
1.6 ±äÁ¿

makeÄܹ»Ê¹ÓñäÁ¿ÕâÒ»µãÊǷdz£ÖØÒªµÄ¡£ÀýÈ磬ÄãÓÐÒ»¸öÃû×ÖΪa.cµÄÔ´Îļþ£¬ÓÉÓÚijÖÖÔ­ÒòÄãÏë°ÑËüµÄÃû×ָijÉb.c¡£Í¨³£Çé¿öÏ£¬ÄãµÃ°ÑÄãµÄmakefileÀïµÄÿ¸öa.cµÄʵÀý¶¼¸Ä³Éb.c¡£µ«ÊÇ£¬Èç¹ûÄãд³ÉÒÔÏ·½Ê½£º


CODE:[Copy to clipboard]MYSRC = a.c
ÄãÖ»ÐèÒª°ÑÕâÒ»ÐиüгÉеÄÃû×Ö¼´¿É£¬ÈçÏ£º


CODE:[Copy to clipboard]MYSRC = b.c
ÄãÒò´Ë¶ø½ÚÊ¡ÁËʱ¼ä£¬ÕâÒ²ÕýÊÇmakeµÄÊ×Òª½ÇÉ«£ºÏîÄ¿¹ÜÀí¡£

±äÁ¿¿ÉÓÃ$(<±äÁ¿Ãû×Ö>)»ò¾ÍÓÃÒ»¸ö$À´ÒýÓ㬵«ºóÕßδ±»¹ã·ºÊ¹Óã¬Òò´Ë½¨Òé±ðÄÇÑùд¡£


CODE:[Copy to clipboard]$(GCC) = /usr/local/bin/gcc
makeÓÐËÄÖÖ²»Í¬ÀàÐ͵ıäÁ¿£¬ÏÂÃ潫°´ÕÕ±»ËÑË÷µÄ˳ÐòÁгöËüÃÇ¡££¨makeµÄËÑË÷½«Ò»Ö±½øÐе½·¢ÏÖij¸öÊýÖµµÄµÚÒ»¸öʵÀýΪֹ¡££©



¾Ö²¿±äÁ¿£ºÕâЩÊǸ³¸øÌض¨Ä¿±êµÄÊýÖµ¡£

ÃüÁîÐУºÃüÁîÐбäÁ¿ÊÇÔÚÃüÁîÐÐÉÏ´«¸ømakeµÄ¡£

È«¾Ö±äÁ¿£ºÈ«¾Ö±äÁ¿ÊÇÔÚ¸ÃMakefile»òÈκÎËù°üº¬µÄMakefileÄÚ¸³ÖµµÄ¡£ÄãÔÚÒ»¸öMakefileÄÚ×î³£¿´µ½µÄ¾ÍÊÇÕâЩ±äÁ¿¡£

»·¾³±äÁ¿£º»·¾³±äÁ¿ÊÇÔÚMakefileÖ®Í⣬Ҳ¾ÍÊÇÔËÐÐmakeµÄshellÀïÉèÖõġ£



ÕâЩ±äÁ¿¿ÉÒÔÔÚMakefileÀïÓÃÒÔÏÂÎåÖÖ²Ù×÷·û½øÐж¨Ò壺




µÈºÅ¡°=¡±ÊÇ×î³£ÓõIJÙ×÷·û£¬ÕâºÍshellµÄÇé¿öÊÇÀàËƵġ£ÊýÖµ±»Ö±½Ó¸³¸øÁ˱äÁ¿¡£ÀýÈ磺


CODE:[Copy to clipboard]VAR = < value >
¼ÓºÅµÈºÅ¡°+=¡±µÄÒâ˼ÊǸ½¼Ó¸³Öµ£¬Í¨¹ý°ÑËù¸³ÊýÖµ¸½¼Óµ½µ±Ç°ÊýÖµµÄºóÃæÀ´Íê³É¶Ô±äÁ¿µÄ¸³Öµ¡£ÀýÈ磺


CODE:[Copy to clipboard]VAR += < value to append >
ÎʺŵȺš°?=¡±µÄÒâ˼ÊÇÌõ¼þ¸³Öµ£¬½öµ±¸Ã±äÁ¿´Óδ¸³ÖµÊ±²Å½øÐи³Öµ¡£Ìõ¼þ¸³ÖµÔÚÏòÒ»¸ö×Ö·û´®ÊýÖµÌí¼Óǰ׺µÄʱºò·Ç³£ÓÐÓá£ÀýÈ磺


CODE:[Copy to clipboard]VAR ?= < value if unset >
ðºÅµÈºÅ¡°:=¡±µÄÒâ˼ÊÇÀ©Õ¹¸³Öµ£¬ÔÚ¸³ÖµÇ°»á¶ÔËù¸³ÊýÖµ½øÐÐÀ©Õ¹£»Í¨³£ÕâÖÖÀ©Õ¹ÊÇÔÚËù¸³±äÁ¿±»ÒýÓõÄʱºò²Å½øÐеġ£ÀýÈ磺


CODE:[Copy to clipboard]VAR := < value to expand >
¸Ð̾ºÅµÈºÅ¡°!=¡±µÄÒâ˼ÊÇshellÃüÁֵ¡£ÔÚÃüÁî±»À©Õ¹²¢·¢¸øshellÖ´ÐÐÍê±ÏÖ®ºó£¬½«ÃüÁî½á¹û¸³¸ø±äÁ¿¡£½á¹ûÖеÄnewline×Ö·û½«±»¿Õ¸ñÈ¡´ú¡£ÀýÈ磺


CODE:[Copy to clipboard]VAR != < shell command to execute >


×¢Ò⣺ÓÐЩ±äÁ¿ÊÇÔÚÍⲿµÄϵͳ¼¶µÄMakefileÄÚ¶¨ÒåµÄ£¨ÔÚ/etc/make.conf»ò/etc/defaults/make.confÖУ©£¬Èç¹ûÄãÔÚÉèÖû·¾³±äÁ¿·½ÃæÓöµ½ÁËÎÊÌ⣬¾ÍÈ¥¼ì²éÒ»ÏÂϵͳ¼¶ÎļþÀïµÄÉèÖá£

Ò»¸öÍêÕûµÄÀý×ÓÈçÏ£º


CODE:[Copy to clipboard] #####################
# Example make file #
#####################
CURDIR != pwd
CFLAGS ?= -g
CFLAGS += -Wall -O2

all:
echo $CFLAGS
#######################

bash$ CFLAGS="-g -Wall" make
ÔÚÉÏÀýÖУ¬CURDIR±»ÉèÖóÉÁËshellÃüÁîpwdµÄ½á¹û¡££¨×¢Ò⣬ÕâÖÖ¸³Öµ²¢²»ÐèÒªbacktickÃüÁ¼´' '£©¡££©CFLAGSÈç¹û´ÓδÉèÖ㬻áÊ×Ïȱ»ÉèÖóÉ-g£¬È»ºó²»¹ÜËüµÄµ±Ç°ÖµÊÇʲô£¬¶¼»á±»¸½¼ÓÉÏ-Wall -O2¡£

1.7 ÃüÁî

Èç¹ûûÓÐÃüÁÄÇmakeʲô¶¼²»ÊÇ£¬Ö»ÓаÑÃüÁî¸æËßmakeËü²ÅÄÜÍê³ÉËüµÄ¹¤×÷¡£makeÖ»ÄÜÔËÐÐÕâЩÃüÁȻºó»ùÓÚshellµÄÍ˳ö״̬ÅжÏÕâЩÃüÁîÊÇ·ñ³É¹¦¡£Òò´Ë£¬Èç¹ûÃüÁîʧ°Ü£¬shell·µ»ØÒ»¸ö´íÎó£¬make½«»áÒò´íÎó¶øÍ˳ö²¢Óڸô¦ÖÕÖ¹¡£make¿ÉÒԾͱ»¿´×÷ÊÇÁíÍâÒ»¸öÃüÁî----³ýÁËÔËÐÐÆäËüÃüÁîÖ®Í⣬ËüºÍÄÇЩÃüÁûÓÐʲôʵ¼ÊµÄ½»»¥¡£

ÃüÁî±ØÐëÓëÒ»¸öÄ¿±êÏà¹ØÁª£¬ÈκÎÒ»¸öÄ¿±ê¶¼¿ÉÒÔÓжà¸öÃüÁî¡£ÀýÈ磺


CODE:[Copy to clipboard]# example for an all-install target

all-install:
$(CC) $(CFLAGS) $(MYSRC)
cp $(MYPROG) $(INSTALL_DIR)
echo "Finished the build and install"
ÿ¸öÃüÁ±ØÐëÔÚÒ»¸öÄ¿±êÖ®ºóÒÔÐÂÐпªÊ¼£¬ÔÚʵ¼ÊÃüÁîÆðʼλÖÃ֮ǰ±ØÐëÒªÓÐÒ»¸ötab¼ü£¬ÈçÉÏËùʾ¡£

¶ÔÓÚ´ó¶àÊýÇé¿ö¶øÑÔ£¬MakefileÀïµÄÃüÁîÖ»ÒªÊǸöÓÐЧµÄshellÃüÁî¾ÍÐУ¬ÃüÁ¾­³£»á°üÀ¨±äÁ¿¡£ÀýÈ磺


CODE:[Copy to clipboard]CPP = -g++
CFLAGS = -Wall -O2

myprog.o:
$(CPP) $(CFLAGS) -c myprog.c
ÏÂÃæÕâ¸öÀý×Ó¸æËßmakeʹÓøø¶¨Öµ±àÒëmyprog.c¡£ÕâЩÃüÁî¿ÉÄÜ»á±ÈÒ»ÐÐÒª³¤£¬ËüÃÇÒ²¿ÉÒÔ±»Ð´À´Íê³ÉÆäËüµÄÈÎÎñ¡£ÕâÊǷdz£ÖØÒªµÄ£¬ÒòΪ±àÒëÆ÷¿ÉÒÔ½ÓÊÜÏ൱¶àµÄÃüÁîÐÐÑ¡Ïî¡¢»·¾³ÉèÖÃÒÔ¼°¶¨ÒåµÈµÈ£¬±ÈÈ磺


CODE:[Copy to clipboard]CLFAGS = $(LINK_FLAGS) $(LINK_LIBS) $(OTHER_LIBS) \
$(OPTIMIZER_FLAGS) $(DEFINES) $(NO_KERNEL) $(OBJS) \
$(CPU_TYPE_FLAGS) $(USE_MY_MALLOC) $(UDEFINES) \
$(SRC_FILE) $(OTHER_SRC_FILES)
ÏÂÃæÕâ¸öÀý×Ó¸æËßmakeɾ³ýËùÓеÄobjectÎļþ¡¢coreÎļþÒÔ¼°Ó¦ÓóÌÐò±¾Éí£¬È»ºó°ÑlogÎļþÒÆ×ߣ¬ÕâЩ²Ù×÷¶¼¿ÉÒԺܷ½±ãµÄÍê³É¡£


CODE:[Copy to clipboard]CPP = -g++
CFLAGS = -Wall -O2
APP = myapp
DATE != date +%m%d%y_%H_%M
LOG = debug


myprog.o:
$(CPP) $(CFLAGS) -c myprog.c
rm -f *.o *.core $(APP)
mv $(LOG).log $(LOG)_$(DATE).log

clean:
rm -f *.o *.core $(APP)
mv $(LOG).log $(LOG)_$(DATE).log
µ«ÊÇ£¬Èç¹û²¢²»´æÔÚlogÎļþ£¬make¾Í»áÒò´íÎó¶øÍ˳ö¡£ÎªÁ˱ÜÃâÕâÖÖÇé¿ö£¬¿ÉÒÔÔÚÃüÁî֮ǰ¼ÓÉÏ¡°-¡±ºÅ¡£Í¨¹ýÔÚÃüÁîÇ°Ãæ¼ÓÉϼõºÅ£¬Äã¾Í¸æËßÁËmakeºöÂÔÖ´ÐÐÃüÁîʱÓöµ½µÄ´íÎó¡££¨²»¹ýmakeÈÔÈ»»á´òÓ¡³ö´íÎóÐÅÏ¢¡££©Òò´Ë£¬ÔÚij¸öÃüÁî³öÏÖ´íÎóÖ®ºómakeÈÔ½«¼ÌÐø¡£ÀýÈ磺


CODE:[Copy to clipboard]clean:
-rm -f *.o *.core $(APP)
-mv $(LOG).log $(LOG)_$(DATE).log
Õ⽫ʹµÃmakeºöÂÔµôrmºÍmvÃüÁî¿ÉÄÜÓöµ½µÄ´íÎó¡£

Ä㻹¿ÉÒÔÈÃmake½ûÖ¹µô¡°echo¡±Ö®ÀàµÄÃüÁîµÄÊä³ö¡£echoÊ×ÏȸæËßmake´òÓ¡³ö°üÀ¨echoÓï¾äÔÚÄÚµÄÕû¸öÃüÁȻºóÔÙÖ´ÐÐÃüÁî²¢½«×Ö·û´®´òÓ¡µ½ÆÁÄ»ÉÏ£º


CODE:[Copy to clipboard] echo $(SOME_DEFINED_STRING)
Òª±ÜÃâÕâÖÖÇé¿ö£¬¿ÉÒÔÔÚechoÃüÁî֮ǰ¼ÓÉÏ¡°@¡±·ûºÅ£¬Õ⽫¸æËßmakeÖ»´òÓ¡×Ö·û´®£¬±ÈÈ磺


CODE:[Copy to clipboard] @echo $(SOME_DEFINED_STRING)
¡°-¡±ºÅºÍ¡°@¡±ºÅ¶¼¼È¿ÉÓÃÓÚ±äÁ¿£¬ÓÖ¿ÉÓÃÓÚÐÎΪ×Ö·û´®µÄÃüÁµ«ÐèҪȷ¶¨Äã¶Ô±äÁ¿ÃüÁîµÄÒýÓÃÊÇÕýÈ·µÄ¡£ÏÂÀýÑÝʾÁËÈçºÎ¶ÔÃüÁîʹÓÃ@²Ù×÷·û£º


CODE:[Copy to clipboard]ECHO = echo
MESSAGE = "Print this message"

msg::
@$(ECHO) $(MESSAGE)
1.8 Ìõ¼þÓï¾ä£¨#if£¬#ifndefµÈµÈ£©

Èç¹ûÄã¶ÔCºÍC++±È½ÏÊìϤ£¬ÄÇÄã¿Ï¶¨ÖªµÀÌõ¼þÔ¤´¦ÀíÃüÁî¡£¹¦ÄÜ·±¶àµÄmakeÒ²ÓÐÒ»¸öÀàËƵÄÌØÐÔ¡£Ìõ¼þÓï¾äʹÄã¿ÉÒÔÑ¡ÔñMakefileÀïµÄÄĸö²¿·ÖÐèÒª±»´¦Àí¡£ÕâЩÌõ¼þÓï¾ä×î¶à¿ÉÒÔǶÌ×30²ã£¬²¢ÇÒ¿ÉÒÔ·ÅÔÚMakefileÀïµÄÈκεط½¡£Ã¿ÌõÓï¾ä¶¼±ØÐëÒÔÒ»¸öÔ²µã£¨.£©¿ªÊ¼£¬¶øÇÒÌõ¼þÓï¾ä¿é±ØÐëÒÔ.endif½áÊø¡£

Ìõ¼þÓï¾äÔÊÐíʹÓÃÂß¼­²Ù×÷·û£¬±ÈÈçÂß¼­AND¡°&&¡±¡¢Âß¼­OR¡°||¡±£¬ÕûÌõÓï¾ä»¹¿ÉÒÔÓá°!¡±²Ù×÷·ûÈ¡·´¡£¡°!¡±²Ù×÷·û¾ßÓÐ×î¸ßÓÅÏȼ¶£¬ÆäºóÒÀ´ÎÊÇÂß¼­ANDºÍÂß¼­OR¡£À¨ºÅ¿ÉÒÔ±»ÓÃÀ´Ö¸¶¨ÓÅÏȼ¶Ë³Ðò¡£¹ØϵÔËËã·ûÒ²ÊÇ¿ÉÒÔʹÓõģ¬±ÈÈç¡°>¡±¡¢¡°>=¡±¡¢¡°<¡±¡¢¡°<=¡±¡¢¡°==¡±ºÍ¡°!=¡±¡£ÕâЩ²Ù×÷·û¿É±»ÓÃÓÚÊ®½øÖƺÍÊ®Áù½øÖƵÄÊýÖµ¡£¶ÔÓÚ×Ö·û´®Ôò¿ÉÒÔʹÓá°==¡±ºÍ¡°!=¡±²Ù×÷·û¡£Èç¹ûûÓиø³ö²Ù×÷·û£¬ÄÇô½«»á°ÑÊýÖµºÍ0½øÐбȽϡ£

ÔÚÏÂÃæµÄÀý×ÓÖУ¬ÔÚ¶ÔVER±äÁ¿½øÐи³ÖµÖ®ºó¶ÔÌõ¼þ½øÐвâÊÔ¡£×¢Ò⣬Èç¹ûVER±äÁ¿Î´±»¸³Öµ£¬Ôò×îºóµÄ.elseÌõÄ¿½«±»ÇóֵΪÕ棬TAG½«±»¸³³É2.4_stableµÄÖµ¡£


CODE:[Copy to clipboard].if $(VER) >= 2.4
TAG = 2.4_current
.elif $(VER) == 2.3
TAG = 2.3_release
.else
TAG = 2.4_stable
.endif
Ìõ¼þÓï¾ä¿ÉÓÃÓÚ²âÊÔ±äÁ¿£¬Ò²¿ÉÒÔÓÃÓÚÏÂÃæÕâÖÖº¯Êý·ç¸ñµÄ±í´ïʽ¡£

ÕâЩÓ÷¨ÖÐÓÐЩÊÇÓмòдÐÎʽµÄ¡£³öÓÚ¼æÈÝÐÔ·½ÃæµÄ¿¼ÂÇ£¬ÎÒÃÇÁгöÁ˼òдÐÎʽ¡£·Ç¼òдÐÎʽÒâ˼¸üΪȷ¶¨£¬Ò²¸üÈÝÒ×±»Àí½â£¬µ«ÐèÒªÄãÇÃÈë¸ü¶àµÄ×Ö·û¡£

ÔÚʹÓüòдÐÎʽµÄʱºòÊDz»±ØʹÓÃÀ¨ºÅµÄ¡£´ËÍ⣬¼òдÐÎʽ»¹¿ÉÒÔºÍif/elseÓï¾äÒÔ¼°ÆäËüµÄ¼òдÐÎʽ»ìºÏÔÚÒ»Æð£º

make( < arg > ) short hand [ .ifmake, .ifnmake, .elifmake, .elifnmake ]

ÔÚÉÏÀýÖУ¬make½«ÒÔÒ»¸öÄ¿±êÃû×Ö×÷ΪËüµÄ²ÎÊý¡£Èç¹ûÕâ¸öÄ¿±êÒÑÔÚÃüÁîÐÐÉϸø³ö£¬»òÕßËü¾ÍÊÇȱʡ½øÐÐmakeµÄÄ¿±ê£¬ÄÇô¸ÃÖµ¾ÍΪÕæ¡£ÏÂÀýÖн«¸ù¾Ýmake()±í´ïʽµÄ¹æÔò¸øCFLAGS¸³Öµ£º


CODE:[Copy to clipboard].if make(debug)
CFLAGS += -g
.elif make(production)
CFLAGS += -O2
.endif
ÏÂÃæÊÇÓüòдÐÎʽ±íʾµÄÏàͬ´úÂ룺


CODE:[Copy to clipboard].ifmake debug
CFLAGS += -g
.elifmake production
CFLAGS += -O2
.endif
target( < arg > )

ÕâÖÖÐÎʽ½«ÒÔÒ»¸öÄ¿±êÃû×Ö×÷Ϊ²ÎÊý¡£½öµ±¸ÃtargetÒѱ»¶¨Òåʱ¸ÃÖµ²ÅΪÕæ¡£Õâ¸ö±í´ïʽûÓмòдÐÎʽ¡£ÀýÈ磺


CODE:[Copy to clipboard].if target(debug)
FILES += $(DEBUG_FILES)
.endif
ÔÚÉÏÀýÖУ¬Èç¹ûdebugÄ¿±ê·µ»ØÕæµÄ»°¾Í»á¶ÔFILES±äÁ¿½øÐи½¼Ó²Ù×÷¡£

empty ( < arg > )

ÕâÖÖÐÎʽÒÔÒ»¸ö±äÁ¿Îª²ÎÊý£¬²¢ÔÊÐíʹÓÃÐÞÊηû¡£µ±±äÁ¿±»À©Õ¹Ö®ºóÊÇÒ»¸ö¿Õ×Ö·û´®Ê±¸ÃֵΪÕæ¡£Õâ¸ö±í´ïʽûÓмòдÐÎʽ¡£´ËÍâÐèҪעÒâµÄÊÇ£¬ÄãÔÚʹÓÃÕâ¸ö±í´ïʽµÄʱºò²¢²»ÐèÒª¶ÔÊýÖµ½øÐÐÒýÓ㬼ÇסÊÇVAR¶ø²»ÊÇ$(VAR)¡£ÀýÈ磺


CODE:[Copy to clipboard].if empty (CFLAGS)
CFLAGS = -Wall -g
.endif
defined( < arg > ) short hand [ .ifdef , .ifndef , .elifdef, elifndef ]

ÏÂÃæÕâ¸öÀý×Ó²ÉÓÃÒ»¸ö±äÁ¿×÷Ϊ²ÎÊý¡£½öµ±±äÁ¿Òѱ»¶¨Òåʱ¸ÃֵΪÕæ¡£


CODE:[Copy to clipboard].if defined(OS_VER)
.if $(OS_VER) == 4.4
DIRS += /usr/local/4.4-STABLE_src
.endif
.else
DIRS += /usr/src
.endif
ÏÂÃæÊǼòдÐÎʽ£º


CODE:[Copy to clipboard].ifdef OS_VER
. if $(OS_VER) == 4.4
DIRS += /usr/local/4.4-STABLE_src
. endif
.else
OS_VER = 3.2
DIRS += /usr/src
.endif
ÕýÈçÄãËù¿´µ½µÄ£¬makeÔÊÐíǶÌ×µÄÌõ¼þºÍdefine±í´ïʽ¡£ºÍC²»Í¬µÄÊÇ£¬Äã²»ÄܶÔifÓï¾äºÍ±äÁ¿¸³ÖµÓï¾ä½øÐÐËõ½ø¡£Èç¹ûÏëÈÃÄãµÄÌõ¼þÓï¾ä¿é¸üÇåÎúÒ»µãµÄ»°£¬Äã¿ÉÒÔÔÚÔ²µãÖ®ºó¡¢if֮ǰ¼ÓһЩ¿Õ¸ñ¡£Ê¾ÀýÈçÏ£º


CODE:[Copy to clipboard].if $(DEBUG) == 1
$(CFLAGS) = -g
. ifndef $(DEBUG_FLAGS)
$(FLAGS) = $(DEBUG_FLAGS)
. endif
.endif
exists( < arg > )

ÏÂÃæµÄÀý×ÓÑÝʾÁËÈçºÎʹÓÃexists£¬ÒÔ¼°ÈçºÎ¸øÒ»¸öÄ¿±êÌí¼ÓÌõ¼þÓï¾ä¡£Èç¹û´æÔÚtmpĿ¼£¬make½«ÔËÐÐ-rm tmp/*.oÃüÁî¡£ÕýÈçÄãÔÚÕâ¸öÀý×ÓÖп´µ½µÄ£¬.ifÓï¾ä½öÔÚcleanÄ¿±êÖб»ÇóÖµ£»ËùÔËÐеÄÃüÁî±ØÐë×ñ´Ó³£¹æµÄÃüÁîÓï·¨¡£


CODE:[Copy to clipboard]clean:
-rm -f *.o *.core
.if exists(tmp)
-rm tmp/*.o
.endif
1.9 ϵͳMakefiles£¬Ä£°åÒÔ¼°.includeÖ¸Áî

CµÄÒ»¸öÖØÒªÌØÐÔ¾ÍÊÇËüµÄ¼òµ¥Ã÷Á˵ÄÔ¤´¦ÀíÖ¸ÁҲ¾ÍÎÒÃdz£ËµµÄ#include¡£Õâ¸öÌØÐÔÒ²ÔÚmakeÖÐʵÏÖÁË¡£Ëù²»Í¬µÄÊÇ£¬Èç¹ûÄãÏë°üº¬ÁíÍâÒ»¸öMakefile£¬ÄÇÄãµÃÔÚÎļþĩβ°üº¬Ëü£¬¶ø²»ÊÇÏñCÄÇÑùÔÚÎļþÍ·°üº¬£¬ÕâÊÇÒòΪ±äÁ¿ÊÇ°´Ë³Ðò½øÐи³ÖµµÄ¡£MakefileÖÐÓÖÓÐMakefile¿ÉÄÜ»áÔì³É»ìÏý£¬²»¹ý°üº¬Ò»¸öMakefileµÄ»ù±¾Ó﷨ȴÊǺܼòµ¥µÄ¡£×¢Ò⣬ÔÚincludeµ¥´ÊÇ°Ãæ±ØÐëÒª¼ÓÒ»¸öÔ²µã£¨.£©¡£»ù±¾Óï·¨ÈçÏ£º


CODE:[Copy to clipboard].include file name
Èç¹ûÒªÖ¸¶¨Ò»¸öλÓÚϵͳmakeĿ¼ÖеÄMakefile£¬¿ÉÒÔʹÓüâÀ¨ºÅ£º


CODE:[Copy to clipboard].include
Èç¹ûËùÖ¸¶¨µÄÎļþλÓÚµ±Ç°Ä¿Â¼»òÕßÊÇÓÉ-IÃüÁîÐÐÑ¡ÏîÖ¸¶¨µÄĿ¼ÖУ¬ÔòʹÓÃË«ÒýºÅ£¬ÕâºÍCµÄ#includeÊÇÀàËƵģº


CODE:[Copy to clipboard].include "file name"
ÔÚÏÂÃæµÄÀý×ÓÖУ¬Èç¹ûÒÑÔÚproject.mkÖж¨ÒåÁËCFLAGS±äÁ¿£¬Ëü½«±»ÕâÀïµÄÐÂÉùÃ÷ËùÈ¡´ú¡£µ±°üº¬¶à¸öMakefileʱÕâ¾Í¿ÉÄÜÔì³ÉһЩÂé·³¡£


CODE:[Copy to clipboard].include "../project.mk"

CFLAGS = -g
FreeBSDϵͳÓкܶàµÄϵͳMakefile¿É¹©°üº¬£¬ÓëÖ®¶ÔÓ¦µÄÊÇÍê³É¸÷ÖÖÈÎÎñµÄÀý³Ì¡£ÔÚ´ó¶àÊýµÄFreeBSDϵͳÖУ¬ÕâЩMakefileλÓÚ/usr/share/mk/Ŀ¼Àï¡£³ý´ËÖ®Í⣬»¹ÓÐÒ»¸ö/etc/defaults/make.conf£¬ËüÊÇ¿ÉÒÔ±»/etc/make.confÈ¡´úµÄ¡££¨Ï¸½ÚÇë²Î¼ûman make.conf¡££©

ÏÂÃæÊÇÒ»¸ö³£ÓõÄMakefileµÄ¼òµ¥ÁÐ±í¡£Èç¹ûÄãÕý×¼±¸½øÐдóÁ¿µÄÄں˱à³Ì»òÓ¦ÓóÌÐòÒÆÖ²£¬ÄǾͺúÃÀûÓÃMakefile°É¡£ÕâЩMakefileµÄÐÎʽΪbsd..mk£¬ÆäÖбíʾÕâЩMakefileµÄÓÃ;¡£



bsd.dep.mk£ºÕâÊÇÒ»¸öÓÃÀ´´¦ÀíMakefileÒÀÀµ¹ØϵµÄ·Ç³£ÓÐÓõİüº¬Îļþ¡£

bsd.port.mk£ºÕâÊÇÒ»¸öÔÚ¹¹½¨Ó¦ÓóÌÐòµÄportsµÄʱºòʹÓõİüº¬Îļþ¡£


ʹÓÃ.includeÖ¸ÁîµÄºÃ´¦¾ÍÊÇÄã¿ÉÒÔ°ÑÄãµÄ¹¤³ÌµÄMakefile·Ö³ÉºÜ¶àµÄƬ¶Ï¡£±ÈÈ磬ÄãµÄ¹¤³Ì¿ÉÒÔÓÐÒ»¸öÖ÷Makefile£¬ÓÉËùÓÐÆäËüµÄ×ÓMakefile½øÐаüº¬¡£Õâ¸öÖ÷Makefile¿ÉÒÔ°üº¬±àÒëÆ÷±äÁ¿¼°ÆäËùÐèµÄÑ¡Ïî¡£ÕâÑùÒ»À´£¬¾Í²»±ØÔÚÿ¸öMakefileÀﶼָ¶¨±àÒëÆ÷ºÍËùÐèÑ¡ÏîÁË£¬¶Ô±àÒëÆ÷Ãû×ÖµÄÒýÓÃÒ²Òò´Ë¶øµÃµ½Á˼ò»¯¡£ÕâЩ¹«¹²Ê¹ÓõÄƬ¶ÏÖ®ºó»¹¿ÉÒÔÓÃÓÚÆäËüµÄMakefile£¬¶Ô±àÒë³ÌÐòµÈÀý³ÌµÄÐÞ¸ÄÒ²½«ËæÖ®ÔÚËùÓеÄMakefileÖÐÉúЧ¡£

1.10 ¸ß¼¶Ñ¡Ïî

¸ß¼¶µÄmakeÑ¡ÏîÖ÷ҪΪÁËÔö¼ÓÁé»îÐÔ¡£ÎÒÃǽ¨ÒéÄã×ÐϸÔĶÁÒ»ÏÂmakeµÄman page£¬ÒÔ±ã¶ÔmakeÓÐÒ»¸öÉîÈëµÄÁ˽⡣ÏÂÃæÕâЩѡÏîÊÇÎÒÃÇ×ʹÓõġ£

¾Ö²¿±äÁ¿

make¿ÉÒÔʹÓÃרÃŶ¨ÒåµÄ¾Ö²¿±äÁ¿£¬Æ䷶Χ½öÏÞÓÚµ±Ç°Ä¿±ê¡£ÏÂÃæÁгöÁËÆ߸öÕâÑùµÄ¾Ö²¿±äÁ¿£¬Í¬Ê±»¹¸ø³öÁËËüÃǵÄSystem V¼æÈݵÄÀϵıíʾÐÎʽ¡££¨²»½¨ÒéʹÓÃSystem VµÄÀϵıíʾÐÎʽ£¬Ö®ËùÒÔÁгöËüÃÇ£¬Ö»ÊdzöÓÚÏòºó¼æÈݵĿ¼ÂÇ¡££©

Õâ¸ö±äÁ¿¾ÍÊÇÄ¿±êµÄÃû×Ö£º


CODE:[Copy to clipboard].TARGET
old style notation: '@'
Õâ¸ö±äÁ¿°üº¬Á˵±Ç°Ä¿±êµÄÈ«²¿Ô´ÎļþµÄÁÐ±í£º


CODE:[Copy to clipboard].ALLSRC
old style notation: '>'
Õâ¸ö±äÁ¿Êǵ±Ç°Ä¿±êËùÒþº¬µÄÔ´Îļþ¡£ËüµÄÊýÖµÊÇÕâ¸öÄ¿±êµÄÔ´ÎļþµÄÃû×ֺͷ¾¶¡££¨Ê¾Àý¼ûÏÂÃæµÄ.SUFFIX²¿·Ö¡££©


CODE:[Copy to clipboard].IMPSRC
old style notation: '<'
Õâ¸ö±äÁ¿±£´æµÄÊÇÒѾ­±»È·¶¨Îª¹ýÆÚµÄÔ´ÎļþµÄÁÐ±í£º


CODE:[Copy to clipboard].OODATE
old style notation: '?'
Õâ¸ö±äÁ¿µÄÖµÊDz»°üÀ¨ºó׺ºÍ·¾¶µÄÎļþÃû£º


CODE:[Copy to clipboard].PREFIX
old style notation: '*'
Õâ¸ö±äÁ¿µÄÖµÊǵµ°¸ÎļþµÄÃû×Ö£º


CODE:[Copy to clipboard].ARCHIVE
old style notation: '!'
Õâ¸ö±äÁ¿µÄÖµÊǵµ°¸³ÉÔ±µÄÃû×Ö£º


CODE:[Copy to clipboard].MEMBER
old style notation: '%'
µ±ÔÚÒÀÀµ¹ØϵÐÐÖÐʹÓÃÕâЩ¾Ö²¿±äÁ¿µÄʱºò£¬Ö»ÓÐ.TARGET¡¢.PREFIX¡¢.ARCHIVEºÍ.MEMBER¾ßÓиÃÄ¿±êµÄÖµ¡£

ÁíÍ⻹ÓÐÒ»¸ö·Ç³£²»´íµÄÖ¸Á


CODE:[Copy to clipboard].undef
Õâ»áÈÃÄã·Ç³£·½±ãµØÈ¡Ïû¶ÔÒ»¸ö±äÁ¿µÄ¶¨Òå¡£×¢Ò⣬ֻÓÐÈ«¾Ö±äÁ¿²ÅÄÜÈ¡Ïû¶¨Òå¡£ÀýÈ磺


CODE:[Copy to clipboard].ifdef DEBUG
.undef RELEASE
.endif
1.11 ת»»¹æÔò£¨ºó׺¹æÔò£©

ת»»¹æÔò¹æ¶¨ÁËÈçºÎÈ¥´´½¨Ò»¸öÄ¿±ê¡£Äã¿ÉÒÔÀûÓÃÕâЩ¹æÔò£¬Ê¡ÏÂΪÿ¸öÄ¿±êÎļþ±àд¹æÔòµÄʱ¼ä¡£Óï·¨ºÜ¼òµ¥£º.SUFFIXES: (suffix list)

×¢Ò⣬¿ÉÄÜÓжà¸ö²»Í¬µÄºó׺¹²ÏíÏàͬµÄת»»ºó׺£¨.cpp¡¢.cc¡¢.c¶¼¿ÉÒÔת»»³Éa.o£©¡£Èç¹ûûÓÐÁгöºó׺£¬make½«°Ñ֮ǰµÄËùÓкó׺¶¼É¾³ý¡£Õâ¿ÉÄÜ»áÔÚÄãÓжà¸ö.SUFFIXES¹æÔòµÄʱºòÔì³ÉºÜ´óµÄ»ìÂÒ¡£ÎÒÃǽ¨ÒéÄãֻʹÓÃÒ»¸ö¹æÔò£¬²¢°ÑËü·ÅÔÚMakefileµÄµ×²¿¡£°Ñ.SUFFIXESÓï¾ä¿éÖÐÁгöµÄ¶«Î÷ºÍtarget½øÐбȽϣ¬Äã¾ÍÈÝÒ×Àí½âËüµÄ½á¹¹ÁË¡£


CODE:[Copy to clipboard].SUFFIXES: .cpp .c .o .sh

.c.o:
$(CC) $(CFLAGS) -c ${.IMPSRC}

.cpp.o:
$(CPP) $(CXXFLAGS) -c ${.IMPSRC}

.sh:
cp ${.IMPSRC} ${.TARGET}
chmod +x ${.TARGET}
ÉÏÃæ¸ø³öµÄÕâЩ¹æÔò½«»á±àÒëCºÍC++Ô´Îļþ¡£²»¹ý¶ÔÓÚ.sh:¹æÔò¶øÑÔ£¬ËüÒ²¿ÉÒÔ¸æËßmakeÈ¥´´½¨ÏàÓ¦µÄshell½Å±¾¡£×¢Ò⣬Èç¹ûÏëÔÚÕâ¸öÀý×ÓÖÐÁгöij¸öshell½Å±¾×÷ΪĿ±ê£¬ÄǾͲ»ÄÜÌí¼Ó.shÀ©Õ¹Ãû£¬²»¹ýÄǸöshell½Å±¾±¾ÉíÈ´±ØÐëÊÇ´øÓÐ.shºó׺µÄ¡£

Èç¹ûÎÒÃǰѲ»´ø.shºó׺µÄinstall_scriptÁгöÀ´×÷Ϊһ¸öÒÀÀµÌõ¼þ£¬ÄǾͱØÐë´æÔÚÒ»¸ö´øÓÐ.shºó׺µÄshell½Å±¾£¬Ãû×ÖΪinstall_script.sh¡£Ò²¾ÍÊÇ˵£¬Èç¹ûij¸öÎļþ¿ÉÒÔ±»ÁгöÀ´×÷Ϊһ¸öÄ¿±ê£¬ÄÇôÔÚÕâ¸öÎļþµÄ´æÔÚÆڼ䣬½öµ±makeÈÏΪÄǸöÄ¿±êºÍ¸ÃÎļþ±ÈÆðÀ´¹ýÁËÆÚÖ®ºó²Å»áÈ¥´´½¨Ëü£¬make²¢²»»áÈ¥´´½¨Õâ¸öÎļþ¡£Ê¾Àý¼ûÏ£»¸ü¶àµÄÐÅÏ¢Çë²Î¼ûapps.hµÄÀý×Ó£º


CODE:[Copy to clipboard]all: install_script $(OBJS)
$(CPP) $(CFLAGS) -o $(APP) $(OBJS) $(LINK)
1.12 ÓÐÓõÄÃüÁîÐÐÑ¡Ïî

ÏÂÃæ¸ø³öÁËһЩ·Ç³£ÈÝÒ×ѧϰºÍʹÓõÄÃüÁîÐÐÑ¡Ïî¡£Õâ²¢²»ÊÇÒ»¸öÍêÕûµÄÁÐ±í£¬ÒªÏëÖªµÀÆäËüµÄÑ¡ÏµÃÈ¥¿´man page¡£

-D

Õâ¸öÑ¡ÏÔÚÃüÁîÐÐÉ϶¨ÒåÒ»¸ö±äÁ¿£¬Èç¹ûÄãµÄMakefileÀïÃæÓÐ.ifdefÓï¾ä£¬ÄÇôʹÓÃÕâ¸öÑ¡Ïî¾Í·Ç³£·½±ãÁË¡£ÀýÈ磺


CODE:[Copy to clipboard].ifdef DEBUG
CFLAGS += -g -D__DEBUG__
.endif
ÓÚÊÇ£¬µ±ÄãÔËÐÐÃüÁîmake -D DEBUGµÄʱºò£¬make¾Í»áÕýÈ·µÄÉèÖÃCFLAGS£¬ÔÚ±àÒëÄãµÄÓ¦ÓóÌÐòµÄʱºòÒ²°Ñµ÷ÊÔÓï¾ä±à½øÈ¥¡£

-E < variable name to override >

Õâ¸öÑ¡ÏÓû·¾³±äÁ¿µÄֵȡ´úMakefileÖиø±äÁ¿¸³µÄÖµ¡£ÔÚʹÓÃÕâ¸öÑ¡Ïî֮ǰ£¬ÐèÒªÏÈÉèÖÃÄãµÄshellÖеĻ·¾³±äÁ¿¡£ÀýÈ磺


CODE:[Copy to clipboard]bash $ CFLAGS="-O2 -Wall" make -E CFLAGS
-e

ºÍ´óдÐÎʽÀàËÆ£¬-e½«Óû·¾³±äÁ¿È¡´úMakefileÖеÄËùÓбäÁ¿¡£Èç¹ûij¸ö±äÁ¿Ã»Óж¨ÒåÏàÓ¦µÄ»·¾³±äÁ¿£¬Ôò°´Õý³£·½Ê½¸³Öµ¡£

-f

Õâ¸öÑ¡ÏîʹÄã¿ÉÒÔÔÚÃüÁîÐÐÉÏÖ¸¶¨Makefile£¬ÕâÒ»µãÔÚÄãÐèÒª¶à¸öMakefileʱºÜÓÐÓá£ÀýÈ磺


CODE:[Copy to clipboard]bash$ make -f Makefile.BSD
-j < number of max_jobs >

Õâ¸öÑ¡ÏîʹÄã¿ÉÒÔÖ¸¶¨makeÄܹ»ÅÉÉú³ö¶àÉÙ¸öjobÀ´¡£Ò»°ãÀ´ËµmakeÖ»»áÅÉÉú³öÒ»¸ö£¬µ«¶ÔÓÚÒ»¸ö·Ç³£´óµÄ¹¤³Ì¶øÑÔ£¬Èç¹ûÏëÈÃÄãµÄÓ²¼þÎᄀÆäÓõĻ°£¬ÄǾÍÖ¸¶¨4¸ö°É£¬ÈçÏ£º


CODE:[Copy to clipboard]make -j 4 world
Èç¹û³¬¹ýÁË4¸ö£¬ÓÐʱ·´¶ø»áÑÓ³¤Ö´ÐÐʱ¼ä£¬²»¹ýÓÐЩÈ˵¹ÊÇ¿ÉÄܾõµÃCPU±»Áù¸ö»ò¸ü¶àµÄjobÕÛÌÚµÄÑù×ÓºÜÓÐȤ¡£

-n

Õâ¸öÑ¡Ïî¶ÔÓÚMakefileµÄµ÷ÊÔºÜÓÐÓã¬Ëü¿ÉÒÔÈÃÄã¿´µ½make¾¿¾¹»áÖ´ÐÐÄÄЩÃüÁµ«ÓÖ²»»áʵ¼ÊÈ¥Ö´ÐÐËüÃÇ¡£¶ÔÓÚÓкܶàÃüÁîµÄ´óÐ͹¤³Ì£¬×îºÃÊÇ°ÑÊä³öÖض¨Ïòµ½Ò»¸öÍⲿÎļþÖУ¬·ñÔò¾Í»áÓкÆÈçÑ̺£µÄÃüÁîÏÔʾ³öÀ´¡£Ê¾ÀýÈçÏ£º


CODE:[Copy to clipboard]bash $ make -n >> make_debug.log 2>&1
-V < variable name >

Õâ¸öÑ¡Ï»ùÓÚÈ«¾ÖµÄÉÏÏÂÎÄ´òÓ¡±äÁ¿µÄÖµ¡£ÓëÇ°Ò»¸öÑ¡ÏîÀàËÆ£¬makeÒ²²»»áÈ¥¹¹½¨ÈκÎÄ¿±ê¡£Äã¿ÉÒÔÔÚÃüÁîÐÐÉÏÖ¸¶¨¶à¸ö-VÑ¡ÏÈçÏ£º


CODE:[Copy to clipboard]make -V CFLAGS -V LINK -V OBJS
1.13 Ò»¸ö×îºóµÄÀý×Ó

ÏÂÃæÁгöµÄMakefileÊÇÒ»¸ö¿ÉÒÔÖظ´Ê¹ÓõÄMakefileµÄÀý×Ó¡£ÔÚÄã°üº¬ÁËËüÖ®ºó£¬Ëü¾ÍÖªµÀ´ÓÁгöµÄ.SUFFIXES¹æÔòÖеÃÖªÈçºÎÈ¥±àÒëC++Ô´Îļþ¡£Ëü»¹ÖªµÀÈçºÎÈ¥°²×°Ó¦ÓóÌÐòºÍÇå³ý¿ª·¢Ä¿Â¼¡£ÕâÏÔÈ»²¢²»ÊÇÒ»¸öºÜºÃÀí½âµÄMakefile£¬µ«ËüÈ´ÊÇÒ»¸öºÜºÃµÄ´´½¨Í¨ÓÃÄ£°å·ç¸ñµÄMakefileµÄ·¶Àý£¬ÕâÖÖMakefile°üº¬ÁËÄÇЩÓÃÓÚ¿ª·¢µÄ¹«¹²Àý³Ì¡£Õâ²»Ö»ÊǽÚÊ¡ÁËÔÚ´´½¨Ã¿¸öMakefileʱÖظ´ÊäÈëÕâЩ¹«¹²¹æÔòµÄʱ¼ä£¬Ëü»¹ÄÜÈÿª·¢ÈËÔ±Öظ´Ê¹ÓÃÒÑÖªµÄºÃÀý³Ì¡£


CODE:[Copy to clipboard]########################################################
#
# FILE: Makefile
#
# AUTHOR: Nathan Boeger
#
# NOTES:
# This is a generic Makefile for *BSD make, you will
# need to customize the listed variables below inside
# the Makefile for your application.
#
# INSTALL_DIR = name of the directory that you want to install
# this applicaion (Ex: /usr/local/bin/ )
#
# APP = name of the application
#
# C_SRC = C source files (Ex: pstat.c )
#
# CPP_SRC = CPP source files (Ex: node.cpp)
#
#
# $Id: ch01.html,v 1.5 2004/08/10 14:41:39 nathan Exp $
#########################################################

# Make the OBJ's from our defined C & C++ files
.ifdef CPP_SRC
OBJS = ${CPP_SRC:.cpp=.o}
.endif

.ifdef C_SRC
OBJS += ${C_SRC:.c=.o}
.endif

# define the Compiler. The compiler flags will be appended to
# if defined, else they are just assigned the values below
CPP = g++
CFLAGS += -Wall -Wmissing-prototypes -O
LINK += -lc

# Add a debug flag.
.ifdef DEBUG
CFLAGS += -g
.endif

# Targets
all: ${OBJS}
$(CPP) $(CFLAGS) -o $(APP) ${OBJS} $(LINK)

depend:
$(CPP) -E -MM ${C_SRC} ${CPP_SRC} > .depend

#######################################################
#
# INSTALL SECTION
#
# install will copy the defined application (APP) into the
# directory INSTALL_DIR and chmod it 0755
# for more information on install read MAN(1) install
########################################################
install: all
install -b -s $(APP) $(INSTALL_DIR)

clean
rm -f $(APP) *.o *.core

# SUFFIX RULES
.SUFFIXES: .cpp .c .o

.c.o:
$(CPP) $(CFLAGS) -c ${.IMPSRC}
.cpp.o:
$(CPP) $(CFLAGS) -c ${.IMPSRC}
ÏÂÃæ¸ø³öµÄMakefileÊÇÐèÒªÄãÔÚ¹¤³ÌĿ¼ÄÚ²¿´´½¨µÄ£º


CODE:[Copy to clipboard]#######################################################
# PROJECT Makefile
#
# This is what the programs makefile would look like
# These are the only variables you will need to define
######################################################

APP = myapp
C_SRC = debug_logger.c
CPP_SRC = myapp.cpp base_classes.cpp
INSTALL_DIR = /usr/local/bin/

# And include the template Makefile, make sure its
# path is correct.

.include "../../bsd_make.mk"
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 7 ÌõÆÀÂÛ

  1. linuxguy ÓÚ 2007-06-16 00:42:35·¢±í:

    :0)1 :0D1 :0D1

  2. linuxguy ÓÚ 2007-06-16 00:41:36·¢±í:

    µÚ°ËÕÂ FreeBSD 5.x
    ÒëÕß:FinalBSD

    8.1 ¡¡FreeBSD 5.x
    ¡¡¡¡2003Äê1Ô·¢²¼µÄFreeBSD-5.x ·ÖÖ§£¬ÊÇFreeBSDÏîÄ¿µÄÒ»¸öÖØÒªµÄÀï³Ì±®¡£ÔÚ½ü3ÄêµÄ¿ª·¢ÖУ¬FreeBSD²»¹ÜÊÇÔÚÄں˻¹ÊÇ»ù±¾ÏµÍ³É϶¼ÓÐÁËÐí¶àµÄ±ä»¯¡£´ó²¿·ÖµÄÕâЩ±ä»¯»áÓ°Ï쵽ϵͳ¹ÜÀíÔ±£¬FreeBSD ±à³ÌÕßÔò²»»á£¬Òò´ËÒ²²»»áÓ°Ïìµ½´ËÊéÖÐÌÖÂÛµÄÈκÎÒ»²¿·Ö¡£Ò»Ð©ÀýÍ⽫ÔÚÏÂÃæ½øÐвûÊö¡£

    8.2 ¡¡Æô¶¯²¼¾Ö(Boot Layout)
    ¡¡¡¡µÚÒ»¸ö±ä»¯ÊÇÆô¶¯ÎļþµÄ×éÖ¯·½Ê½¡£FreeBSD 5.xϵͳÒѽ«ËùÓеÄÄ£¿éºÍÄÚºËÎļþ¶¼ÒƵ½/bootĿ¼Ï¡£ºÍÀÏ°æ±¾µÄFreeBSDÒ»Ñù£¬Ò»Ð©ºÍϵͳÆô¶¯Ïà¹ØµÄÅäÖÃÎļþҲλÓÚ´ËĿ¼Ï¡£Õâ¸ö¿´ÆðÀ´ºÜСµÄ±ä»¯Êµ¼ÊÉÏÌṩÁ˸ü¶àµÄ±ãÀû£¬ÒòΪÏÖÔÚ£¬ÔÚ²»Í¬µÄÉ豸¼ä·ÖÀë/ºÍ/boot(°üº¬ËùÓеÄÄں˺ÍÄÚºËÄ£¿é)Ê®·ÖÈÝÒס£

    8.3 Devfs
    ¡¡¡¡FreeBSD 5.xÖУ¬ÎÒÃÇ×îϲ»¶µÄÌØÐÔÊÇdevfs.֮ǰµÄ°æ±¾£¬/dev±»ÈûÂúÁ˳¬¹ý1000¸öÎļþ¡£É豸½ÚµãºÍ´ó¶àÊýÖ§³ÖµÄÉ豸¶¼ÓÐÒ»Ïî×÷ΪÎļþ±£´æÔÚ¸ÃĿ¼Ï¡£ÈçÄãËùÏ룬Õâ¸öĿ¼±äµÃ·Ç³£µÄ´ó²¢ÇÒ°üº¬ÁËÐí¶à²»±ØÒªµÄÎļþ¡£±ÈÈ磬һ¸öÓÐIDEÉ豸µÄϵͳ»áÔÚ´ËĿ¼Ï°üº¬SCSIÉ豸Îļþ£¬ÄÄÅÂÊǸÃϵͳûÓÐÈκÎSCSIÉ豸¡£devfsÖÐÕâÒ»Çбײ¡¶¼Ö®Ç°½«²»¸´´æÔÚ.
    ÏÖÔÚ/devϽö½ö°üº¬ÄÇЩÕæʵ´æÔÚµÄÉ豸Ïʵ¼ÊÉÏ£¬FreeBSD 5.x²¢²»ºÍ֮ǰ°æ±¾Ò»Ñù½«/devµ±×÷µ½ÎļþϵͳµÄÒ»¸ö¹ÒÔص㣬²¢³Æ֮Ϊdevfs¡£

    ¡¡¡¡devfsÎļþϵͳºÍprocÎļþϵͳÏà½ü¡£¶þÕ߶¼ÊǹÒÔص㣬¸Ã¹ÒÔصã°üº¬ÔÚÓ²ÅÌÉϲ»´æÔÚµÄÎļþ¡£ÕâЩÎļþÓÉÄں˴´½¨²¢ÇÒ½öÒÔÎļþ·½Ê½³öÏÖ¡£ÊÂʵÉÏ£¬ËüÃÇÊÇÔÚϵͳÆô¶¯ºó±»½¨Á¢µÄ¡£DevfsÌṩÁ˸ü¶àµÄ±ãÀûÐÔ£¬ÒòΪÄãÄÜÖ§³Ö¶à¸ödevfs¹ÒÔص㡣±ÈÈ磬Èç¹ûÄãÏëchroot»òÕßjailÒ»¸ö½ø³Ì£¬Äã²»ÐèÒªÊÖ¶¯´´½¨/devĿ¼£¬È¡¶ø´úÖ®µÄÊÇ£¬Äã¿ÉÒÔ¼òµ¥µÄΪÄãµÄ½ø³Ì´´½¨Ò»¸öеĹÒÔص㲢¹ÒÔØdevfs.

    ¡¡¡¡devfsµÄÁíÒ»¸öÓŵãÊÇ¿ÉÒÔ¸æËßÄãϵͳÕæʵ´æÔÚÄÄЩÉ豸¡£ÄãÒª×öµÄ½ö½öÊÇcdµ½/devĿ¼ȻºóÁгöÄÇЩÎļþ¡£Õâ¾ÍºÜ·½±ãÓû§µÃµ½ÏµÍ³ÉÏËùÓеÄÉ豸ÁÐ±í£¬¸üÖØÒªµÄÊÇ£¬¼ì²âµ½ÄÄЩÉ豸¡£

    8.4 ¡¡a.out
    ¡¡¡¡FreeBSD 5.xϵÁÐÒѾ­ÔÚ»ù±¾ÏµÍ³ÖÐÈ¥³ýÁ˶Ôa.out¶þ½øÖƸñʽµÄÖ§³Ö¡£µ«ÊÇÄãÈÔÈ»¿ÉÒÔ¼ÓÈëa.out¶þ½øÖÆÖ§³Ö¡£ÕâÊÇÒòΪa.outÊÇÖÖÏ൱Àϵĸñʽ£¬²¢ÇÒÏÖÔÚ¶¼»áÓÅÏÈÑ¡ÔñеÄELF¸ñʽ¡£ELF¸ñʽ¸üÁé»î¶øÇÒÄ¿Ç°±»¹ã·ºÊ¹Óá£

    8.5 ¡¡gcc-3.2¹¤¾ßÁ´
    ¡¡¡¡FreeBSD 5.x ÏÖÔÚʹÓÃgcc-3.2 ¹¤¾ßÁ´×÷Ϊ»ù±¾ÏµÍ³¡£ÕâÊǸöºÜÖØÒªµÄ¸Ä±ä£ºgcc-3.x¸ü½Ó½üISO£¬²¢ÇÒËüµÄC++ ABI¸üÎȶ¨¡£È»¶ø£¬Õâ»òÐí»á¸øһЩÈË´øÀ´Âé·³¡£ËûÃDZàдµÄһЩ³ÌÐòÔÚʹÓÃgcc-3.x½øÐбàÒëÇ°Ò²ÐíÐèÒª½øÐиüС£Èç¹ûÄãʹÓÃflex»òÕßyacc£¬ÇëÈ·¶¨ÄãʹÓõÄÊÇ×îеİ汾£¬»òÕßΪÄ㵱ǰµÄ°æ±¾´òºÃ²¹¶¡£¬ÒòΪÒѾ­È·ÈÏÖªµÀËûÃǻᵼÖÂÎÊÌâ³öÏÖ¡£

    8.6¡¡ SMPng
    ¡¡¡¡FreeBSD 5.x ÒѾ­¸Ä½øΪ֧³ÖSMPµÄϵͳÁË£¬ÕâÒ»¸Ä½ø¶¼À´×ÔÓÚÎÒÃdz£ËµµÄSMPng(ÏÂÒ»´úSMP).¾¡¹Ü֮ǰ°æ±¾Ò²Ö§³ÖSMP£¬µ«ÊÇÐÔÄÜÓдýÌá¸ß¡£

    8.7¡¡Äں˵÷¶ÈʵÌå(KSE)
    ¡¡¡¡ÁíÒ»¸öеÄÌØÐÔÊÇÄں˵÷¶ÈʵÌå(KSE).KSEÊǸöÄÚºËÖ§³ÖµÄÏß³Ìϵͳ£¬ºÍScheduler ActivationsÔÚ¸ÅÄî¸ÅÄîÉϺܽӽü¡£ÌرðµÄ£¬ÔÚÄں˶ˣ¬KSEÔÚÓÚ¶ÔFreeBSDµÄµ÷¶ÈµÄÐ޸ġ£²¢ÇÒÔÚÓû§¶ËʹÓõÄÊÇPOSIXÏß³ÌʵÏÖ·½Ê½£¬ÕâÖÖ·½Ê½»áÀûÓÃÄÚºËÌṩµÄ¶îÍ⹤¾ß¡£È»¶ø£¬Äã²»ÐèÒªÅäÖÃÈκÎÌØÊâµÄÄں˲ÎÊý£¬¾Í¿ÉÒÔ±àÒëµÃµ½Ò»¸ö¾ßÓÐKSEÏà¹ØÐ޸ĵÄÄںˡ£

    ¡¡¡¡ÎªÁËÔÚÓ¦ÓóÌÐòÖÐʹÓÃKSE£¬Äã¿ÉÒÔʹÓÃlibpthreadsÀ´Á´ÈëÖ®¡£libpthreadsĬÈϲ¢Ã»±àÒë½øϵͳ£¬ËùÒÔÄãÊ×ÏÈÐèÒªÔÚϵͳÉÏ°²×°ºÃlibpthreads¡£È»ºó£¬ÔÚËüµÄmakefileÖУ¬½«-pthreadÑ¡Ïî¸ÄΪ-lpthread²¢ÖØÐÂÁ´Èë(relink).

    8.8¡¡Ð¡½á
    ¡¡¡¡FreeBSDÔÚ¼¸ÄêµÄ·¢Õ¹ÖÐÒѾ­±äµÃ³ÉÊ죬²¢ÇÒÏÖÔÚÊÇÒ»¸ö¿ÉÓõķdz£Îȶ¨µÄ²Ù×÷ϵͳÁË¡£ÓÐÁËSMPµÄÔöÇ¿Ö§³ÖºÍÄÚºËỊ̈߳¬FreeBSD»áÒ»Èç¼ÈÍùµÄÌṩǿÎȶ¨ÐԺ͸ßÐÔÄÜ¡£ÒѾ­¼ÓÈëÁ˶ÔһЩÐÂƽ̨µÄÖ§³Ö£¬±ÈÈçSparc64ºÍia64.ÕâЩеÄƽ̨»á°ïÖúBSD·¢Ðаæ¶àÄêÀ´¼ÌÐøÌṩ¸ßÖÊÁ¿µÄ¿ªÔ´Ñ¡Ôñ¡£

  3. linuxguy ÓÚ 2007-06-16 00:40:29·¢±í:

    ¡¡µÚÆßÕ ½ø³Ì×ÊÔ´ºÍϵͳÏÞÖÆ
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÒëÕߣºFinalBSD
    7.1¡¡½ø³Ì×ÊÔ´ºÍϵͳÏÞÖÆ
    ¡¡¡¡ÎªÁËÖ§³Ö¶àÓû§Í¬Ê±µÇ¼ÒÔ¼°¶à¸öÓ¦ÓÃÁ¬½Ó£¬BSD UNIXϵͳ¸øϵͳ¹ÜÀíÔ±ÌṩÁË¿ØÖÆϵͳ×ÊÔ´µÄÐí¶à·½·¨¡£ÕâÖÖ×ÊÔ´ÏÞÖÆ°üÀ¨CPUʱ¼ä¡¢ÄÚ´æʹÓÃÁ¿ÒÔ¼°´ÅÅÌʹÓÃÁ¿¡£×ÊÔ´¿ØÖÆÔÊÐíÄãµ÷Õûϵͳµ½×î¼ÑµÄʹÓÃÂÊ¡£UNIXµÄÔçÆÚ°æ±¾ÖУ¬Ò»Ð©ÔÚ±àÒëʱÉèÖõÄϵͳÏÞÖÆÈç¹ûÐèÒªÐ޸ģ¬ÔòÐèÒªÖØбàÒëÕû¸öϵͳ¡£È»¶ø£¬Èç¹û²¢·ÇËùÓеÄÔËÐÐÖеÄϵͳ×ÊÔ´¶¼ÐèÒªÖØбàÒëÕû¸öϵͳ£¬ÄÇôÏÖ´úµÄBSDϵͳ¿ÉÒÔµ÷Õû´ó¶àÊýÕâЩ×ÊÔ´µÄÏÞÖÆ¡£
    ¡¡¡¡±¾Õ²ûÊöºÍ½ø³ÌÏà¹ØµÄÏÞÖÆ£¬°üÀ¨ÏµÍ³¶ËºÍÓû§Ê¹Óõġ£ÎÒÃǽ«»á¿´µ½ÈçºÎ·¢ÏÖÕâЩÏÞÖÆÒÔ¼°ÔõôÐÞ¸ÄÖ®£¬»¹½«²ûÊö½ø³ÌÊÇÈçºÎ²éѯËüµÄ×ÊԴʹÓÃÂÊ¡£
    7.2 ¡¡È·¶¨ÏµÍ³ÏÞÖÆ
    getrlimit£¬setrlimit
    getrlimitÔÊÐíÒ»¸ö½ø³Ì²éѯËùÊܵĵÄϵͳÏÞÖÆ.ÕâЩϵͳÏÞÖÆͨ¹ýÒ»¶ÔÓ²/ÈíÏÞÖƶÔÀ´Ö¸¶¨¡£µ±Ò»¸öÈíÏÞÖƱ»³¬¹ýʱ£¬½ø³Ì»¹¿ÉÒÔ¼ÌÐø£¬µ±È»ÕâÈ¡¾öÓÚÏÞÖƵÄÀàÐÍ£¬Í¬Ê±Ò»¸öÐźŻᷢË͸ø½ø³Ì¡£ÁíÒ»·½Ã棬½ø³Ì²»¿ÉÒÔ³¬¹ýËüµÄÓ²ÏÞÖÆ¡£ÈíÏÞÖÆÖµ¿ÉÒÔ±»½ø³ÌÉèÖÃÔÚλÓÚ0ºÍ×î´óÓ²ÏÞÖƼäµÄÈÎÒâÖµ¡£Ó²ÏÞÖÆÖµ²»Äܱ»Èκνø³Ì½µµÍ£¬½ö½ö³¬¼¶Óû§¿ÉÒÔÔö¼ÓÖ®¡£
    CODE:[Copy to clipboard]#include
    #include
    #include
    int getrlimit(int resource, struct rlimit *rlp);
    int setrlimit(int resource, const struct rlimit *rlp);
    getrlimitºÍsetrlimit¶¼Ê¹ÓÃÏÂÃæµÄÊý¾Ý½á¹¹:
    CODE:[Copy to clipboard]struct rlimit {
    rlim_t rlim_cur;
    rlim_t rlim_max;
    };
    ÎÒÃÇÀ´¿´Ã¿¸ö³ÉÔ±±äÁ¿¡£rlim_curΪָ¶¨µÄ×ÊÔ´Ö¸¶¨µ±Ç°µÄϵͳÈíÏÞÖÆ¡£rlim_max½«ÎªÖ¸¶¨µÄ×ÊÔ´Ö¸¶¨µ±Ç°µÄϵͳӲÏÞÖÆ¡£
    getrlimitºÍsetrlimitº¯ÊýµÄµÚÒ»¸ö²ÎÊýÊÇ×ÊÔ´²ÎÊý¡£Õâ¸ö²ÎÊýÓÃÀ´Ö¸¶¨½ø³Ì»ñÈ¡ÐÅÏ¢µÄÄǸö×ÊÔ´¡£¿ÉÄܵÄ×ÊÔ´ÖµÁÐÓÚÏÂÃæ¡£ÄãÒ²¿ÉÒÔÔÚ/usr/include/sys/resource.hÖÐÕÒµ½ËüÃÇ£º
    CODE:[Copy to clipboard]#define RLIMIT_CPU 0 /* cpu time in milliseconds */
    RLIMIT_CPU×ÊÔ´ÏÞÖÆÖ¸¶¨Ò»¸ö½ø³Ì¿ÉÒÔÈ¡µÃCPUÖ´ÐÐÈÎÎñµÄºÁÃëÊý¡£Ò»°ãµØ£¬Ò»¸ö½ø³Ì½ö½öÓÐÒ»¸öÈíÏÞÖƶøûÓÐÓ²ÏÞÖÆ¡£Èç¹û³¬³öÈíÏÞÖÆ£¬½ø³Ì»áÊÕµ½Ò»¸öSIGXCPUÐźš£
    CODE:[Copy to clipboard] #define RLIMIT_FSIZE 1 /* maximum file size */
    RLIMIT_FSIZEÏÞÖÆÖ¸¶¨Ò»¸ö½ø³Ì¿ÉÒÔ´´½¨µÄ×î´óÎļþ´óС£¬ÒÔ×Ö½ÚΪµ¥Î»¡£±ÈÈ磬Èç¹ûRLIMIT_FSIZEÉèÖÃΪ0£¬ÄÇô½ø³Ì½«¸ù±¾²»ÄÜ´´½¨Îļþ¡£Èç¹û½ø³Ì³¬³ö´ËÏÞÖÆ£¬¾Í»á·¢³öSIGFSZÐźš£
    CODE:[Copy to clipboard]#define RLIMIT_DATA 2 /* data size */
    RLIMIT_DATA ÏÞÖÆÖ¸¶¨Ò»¸ö½ø³ÌÊý¾Ý¶Î¿ÉÕ¼¾ÝµÄ×î´ó×Ö½ÚÖµ¡£Ò»¸ö½ø³ÌµÄÊý¾Ý¶Î¾ÍÊÇ·ÅÖö¯Ì¬ÄÚ´æµÄÒ»¸öÇøÓò(C/C++ÖÐÓÃmalloc()·ÖÅäµÄÄÚ´æ)¡£Èç¹û³¬³öÏÞÖÆ£¬·ÖÅäÐÂÄÚ´æµÄ²Ù×÷½«»áÔ⵽ʧ°Ü¡£
    CODE:[Copy to clipboard]#define RLIMIT_STACK 3 /* stack size */
    RLIMIT_STACKÏÞÖÆÖ¸¶¨½ø³ÌÕ»¿ÉÕ¼¾ÝµÄ×î´ó×Ö½ÚÊý¡£Ò»µ©³¬³öÓ²ÏÞÖÆ£¬½ø³Ì»áÊÕµ½SIGSEVÐźš£
    CODE:[Copy to clipboard]#define RLIMIT_CORE 4 /* core file size */
    RLIMIT_COREÏÞÖÆÖ¸¶¨Á˽ø³Ì¿ÉÒÔ´´½¨µÄ×î´ócoreÎļþµÄ´óС¡£Èç¹û´ËÏÞÖÆÉèΪ0£¬½«²»ÄÜ´´½¨¡£ÁíÍ⣬µ±´ïµ½´ËÏÞÖÆʱ£¬ËùÓÐÕýÔÚдcoreÎļþµÄ½ø³Ì¶¼½«±»Öжϡ£
    CODE:[Copy to clipboard]#define RLIMIT_RSS 5 /* resident set size */
    RMIMIT_RSSÏÞÖÆÁ˽ø³ÌµÄ³£×¤¼¯´óС(resident set size)¿ÉÕ¼¾ÝµÄ×î´ó×Ö½ÚÊý.Õâ¸ö½ø³ÌµÄ³£×¤¼¯ºÍ½ø³ÌËùʹÓõÄÎïÀíÄÚ´æÊýÓйء£
    CODE:[Copy to clipboard] #define RLIMIT_MEMLOCK 6 /* locked-in-memory address space */
    RLIMIT_MEMLOCKÏÞÖÆÖ¸¶¨Á˽ø³Ì¿ÉÒÔʹÓÃϵͳµ÷Óõ½mlock½øÐÐËø¶¨µÄ×î´ó×Ö½ÚÊý¡£
    CODE:[Copy to clipboard] #define RLIMIT_NPROC 7 /* number of processes */
    RLIMIT_NPROC ÏÞÖÆÖ¸¶¨ÁËÒ»¸öÖ¸¶¨Óû§¿ÉÒÔ¿ªÆôµÄ×î¶à²¢·¢½ø³ÌÊý¡£ÕâÀïµÄÓû§ÊÇͨ¹ý½ø³ÌÀ´È·¶¨µÄÓÐЧÓû§ID.
    CODE:[Copy to clipboard]#define RLIMIT_NOFILE 8 /* number of open files */
    RLIMIT_NOFILE ÏÞÖÆÖ¸¶¨Á˽ø³Ì¿ÉÒÔ´ò¿ªµÄ×î¶àÎļþÊý¡£
    CODE:[Copy to clipboard] #define RLIMIT_SBSIZE 9 /* maximum size of all socket buffers */
    RLIMIT_SBSIZEÏÞÖÆÖ¸¶¨Óû§ÔÚÈκÎʱ¿Ì¿ÉʹÓõÄmbufsÊý¡£¿ÉÒԲ鿴socket manÒ³À´»ñµÃmbufsµÄ¶¨Òå¡£
    CODE:[Copy to clipboard]#define RLIMIT_VMEM 10 /* virtual process size (inclusive of mmap) */
    RLIMIT_VMEMÏÞÖÆ˵Ã÷Ò»¸ö½ø³ÌµÄÓ³ÉäµØÖ·¿Õ¼ä¿ÉÒÔÕ¼¾ÝµÄ×Ö½ÚÊý¡£Èç¹û³¬³öÏÞÖÆ£¬·ÖÅ䶯̬ÄÚ´æºÍµ½mmapµÄµ÷Óûáʧ°Ü¡£
    CODE:[Copy to clipboard]#define RLIM_INFINITY
    RLIM_INFINITYºêÓÃÀ´È¥³ý¶ÔÒ»¸ö×ÊÔ´µÄÏÞÖÆ¡£»»¾ä»°Ëµ£¬½«Ò»¸ö×ÊÔ´µÄÓ²ÏÞÖÆÉèÖÃΪRLIM_INFINITY½«»áµ¼Ö´ËÖÖ×ÊÔ´µÄʹÓÃûÓÐÈκÎϵͳÏÞÖÆ¡£ ½«ÈíÏÞÖÆÉèÖÃΪRLIM_INFINITY½«»á×èÖ¹½ø³ÌÊÕµ½ÈκÎÈíÏÞÖƾ¯¸æ¡£Èç¹ûÄãµÄ½ø³Ì²»ÏëΪÄÇЩ»áµ¼Ö½ø³ÌÔÚ³¬¹ýÈíÏÞÖÆʱ·¢ËÍÐźŵÄ×ÊÔ´ÉèÖÃÒ»¸öÐźŴ¦ÀíÆ÷£¬Õâ¸ö²ÎÊý½«±äµÃ·Ç³£ÓÐÓá£
    Èç¹ûʹÓÃÁËgetrlimit²ÎÊý£¬ÄÇôµÚ¶þ¸ö²ÎÊýÐèÒªÉèÖÃΪһ¸öµ½rlimit½á¹¹µÄÓÐЧָÕ롣Ȼºógetrlimit»á½«Êʵ±µÄÏÞÖÆÖµ·ÅÈë´Ë½á¹¹¡£ÁíÍ⣬ÔڸıäÏÞÖÆʱ£¬setrlimit»áʹÓÃÔÚµÚ¶þ¸ö²ÎÊýÖÐÉèÖÃÖµ¡£½«ÖµÉèÖÃΪ0½«»á×èֹʹÓôË×ÊÔ´¡£½«ÖµÉèÖÃΪRLIM_INFINITY»á³ýÈ¥¶Ô¸Ã×ÊÔ´µÄËùÓÐÏÞÖÆ¡£ÕâЩº¯Êý¶¼ÔÚÖ´Ðгɹ¦ºó¶¼·µ»Ø0£¬·´Ö®Îª-1.ÓÐÈκδíÎó²úÉú£¬ÕâЩº¯Êý»áÏàÓ¦µÄÉèÖÃerrno¡£
    getpagesizeº¯Êý
    CODE:[Copy to clipboard]#include
    int getpagesize(void);
    ¡¡¡¡ÔÚ½éÉÜgetrusageº¯ÊýÇ°£¬ÎÒÃÇÐèÒªÌÖÂÛÒ»ÏÂgetpagesizeº¯Êý¡£Ò»Ð©½ø³Ì״̬ ÊǸù¾ÝʹÓõķÖÒ³(pages)À´ÏÔʾµÄ¡£·ÖÒ³µÄÄÚ´æ½ö½öÊÇÒ»¶ÎÄڴ棬ͨ³£Îª4096×Ö½Ú×óÓÒ¡£µ«ÊÇ·ÖÒ³´óСȴǧ²îÍò±ð£¬²¢ÇÒËü²»»á¹Ì¶¨±àÈë(hard-coded)ÄãµÄϵͳ¡£È¡¶ø´úÖ®µÄÊÇ£¬ÒªÈ·¶¨±¾µØϵͳµÄ·ÖÒ³´óС(pagesize)ÐèҪʹÓÃgetpageseziº¯Êý¡£getpagesizeµÄ·µ»ØÖµ¾ÍÊÇÿ¸ö·ÖҳʹÓõÄ×Ö½ÚÊý¡£
    7.3 È·¶¨½ø³Ì×ÊԴʹÓÃÁ¿
    getrusageº¯Êý
    ÏÖÔÚÎÒÃÇÖªµÀÈçºÎ²é¿´ÏµÍ³ÏÞÖÆ£¬ÎÒÃÇ»¹ÐèÒªÖªµÀÈçºÎÈ·¶¨µ±Ç°½ø³Ì×ÊÔ´µÄʹÓÃÁ¿¡£getrusageº¯Êý¾ÍÊÇÓÃÓÚ´ËÄ¿µÄ¡£´Ëº¯ÊýºÜÈÝÒ×±»Àí½â¡£Ò»¸ö½ø³Ì¿ÉÒÔÈ·¶¨ËüµÄÄÚ´æʹÓÃÁ¿¡¢CPUÖ´ÐÐʱ¼ä¡¢ÉõÖÁ¿É»ñµÃÆä×Ó½ø³ÌµÄÏà¹ØÐÅÏ¢¡£Òò´Ë£¬getrusageº¯ÊýµÄÒ»¸öÓÃ;¾ÍÊÇ°ïÖúϵͳ±ÜÃâÌÓÒݽø³Ì(runaway)µÄ³öÏÖ¡£ÌÓÒݽø³ÌÖ¸µÄÊÇÄÇЩ²»ÊÜϵͳ¿ØÖƵĽø³Ì£¬ËüÃÇ»òÕßʹÓÃÁ˹ý¶àµÄCPU(±ÈÈçÑ­»·µ÷ÓÃ)¡¢»òÕßÊdz¬¹ýÁËÄÚ´æʹÓÃÁ¿µÄÏÞÖÆ(µ¼ÖÂÄÚ´æй©)¡£
    CODE:[Copy to clipboard] #include
    #include
    #include

    #define RUSAGE_SELF 0
    #define RUSAGE_CHILDREN -1

    int getrusage(int who, struct rusage *rusage);
    getrusageº¯ÊýÓÐÁ½¸ö²ÎÊý¡£µÚÒ»¸ö²ÎÊý¿ÉÒÔÉèÖÃΪRUSAGE_SELF»òÕßRUSAGE_CHILDREN¡£Èç¹ûÉèÖóÉRUSAGE_SELF£¬ÄÇô½«»áÒÔµ±Ç°½ø³ÌµÄÏà¹ØÐÅÏ¢À´Ìî³ärusage(Êý¾Ý)½á¹¹¡£·´Ö®£¬Èç¹ûÉèÖóÉRUSAGE_CHILDREN£¬ÄÇôrusage½á¹¹ÖеÄÊý¾Ý¶¼½«Êǵ±Ç°½ø³ÌµÄ×Ó½ø³ÌµÄÐÅÏ¢¡£

    rusage(Êý¾Ý)½á¹¹¶¨ÒåÔÚ/usr/include/sys/resource.hÖС£Ëüº¬ÓÐÒÔϳÉÔ±±äÁ¿£º
    CODE:[Copy to clipboard]struct rusage {
    struct timeval ru_utime; /* user time used */
    struct timeval ru_stime; /* system time used */
    long ru_maxrss; /* max resident set size */
    long ru_ixrss; /* integral shared memory size */
    long ru_idrss; /* integral unshared data */
    long ru_isrss; /* integral unshared stack */
    long ru_minflt; /* page reclaims */
    long ru_majflt; /* page faults */
    long ru_nswap; /* swaps */
    long ru_inblock; /* block input operations */
    long ru_oublock; /* block output operations */
    long ru_msgsnd; /* messages sent */
    long ru_msgrcv; /* messages received */
    long ru_nsignals; /* signals received */
    long ru_nvcsw; /* voluntary context switches */
    long ru_nivcsw; /* involuntary " */
    };
    ÎÒÃÇÀ´Ïêϸ·ÖÎöÿһ¸ö³ÉÔ±±äÁ¿¡£
    ru_utime,ru_stime
    ru_utimeºÍru_stime³ÉÔ±±äÁ¿°üº¬ÁËÔÚÓû§Ä£Ê½ºÍϵͳģʽÖÐÖ´ÐÐʱ¼äµÄ×ܺ͡£ËüÃǶ¼Ê¹ÓÃtimeval½á¹¹£¨Çë²é¿´Ç°Ò»ÕÂÀ´Á˽â´Ë½á¹¹¡££©
    ru_maxrss
    ru_maxrss±£´æÁ˳£×¤¼¯µÄÄÚ´æʹÓÃÊý¡£ÆäÖµ¸ù¾ÝÄÚ´æ·ÖÒ³µÄʹÓÃÀ´È·¶¨¡£
    ru_ixrss
    ru_ixrssÖµÖ¸Îı¾¶Î(text segment)ʹÓõÄÄÚ´æÊý³ËÒÔÖ´ÐеδðÊý¡£
    ru_idrss
    ru_idrss ÖµÖ¸½ø³ÌËùʹÓõÄ˽ÓÐÄÚ´æÊý£¨KB£©³ËÒÔÖ´ÐеδðÊýÀ´¡£
    ru_isrss
    ru_isrss ָջʹÓõÄÄÚ´æÊý(KBΪµ¥Î»)³ËÒÔÖ´ÐеδðÊý¡£
    ru_minflt
    ru_minfltÖµÖ¸²»ÐèÒªI/OµÄҳȱʧÊý¡£Ò³È±Ê§·¢ÉúÔÚÄÚºËÐèÒªµÃµ½Ò»¸öÄÚ´æÒ³ÒÔ¹©½ø³Ì·ÃÎÊʱ¡£
    ru_majflt
    ru_majfltÖµÖ¸ÐèÒªI/OµÄҳȱʧÊý¡£Ò³È±Ê§·¢ÉúÔÚÄÚºËÐèÒªµÃµ½Ò»¸öÄÚ´æÒ³ÒÔ¹©½ø³Ì·ÃÎÊʱ¡£
    ru_nswap
    ÓÐʱ£¬Ò»¸ö½ø³Ì»á±»µ÷³öÄڴ棬ÒÔÌṩ¿Õ¼ä¸øÆäËû½ø³ÌʹÓá£ru_nswapÖ¸µÄ¾ÍÊÇÒ»¸ö½ø³Ì½«Òªµ÷³öÄÚ´æµÄ´ÎÊý¡£
    ru_inblock
    ru_inblock Ö¸ÎļþϵͳÐèҪΪһ¸ö¶ÁÇëÇóÖ´ÐÐÊäÈë²Ù×÷µÄ´ÎÊý¡£
    ru_oublock
    ru_oublockÖ¸ÎļþϵͳÐèҪΪһ¸öдÈëÇëÇóÖ´ÐÐÊä³ö²Ù×÷µÄ´ÎÊý¡£
    ru_msgsnd
    ru_msgsndÖ¸·¢Ë͵ÄIPCÐÅÏ¢×ÜÊý
    ru_msgrcv
    ru_msgrcvÖ¸ÊÕµ½µÄIPCÐÅÏ¢×ÜÊý¡£
    ru_nsignals
    ru_nsignalsÖ¸½ø³ÌÊÕµ½µÄÐźÅ×ÜÊý¡£
    ru_nvcsw
    Ò»¸ö½ø³ÌÖ÷¶¯ÉÏÏÂÎÄÇÒ»ì×ÜÊý¡£Ö÷¶¯ÉÏÏÂÎÄÇл»·¢ÉúÔÚÒ»¸ö½ø³Ì·ÅÆúËüµÄCPU·Öʱʱ¡£Í¨³£·¢ÉúÔÚÒ»¸ö½ø³ÌµÈ´ýij¿ÉÓÃ×ÊԴʱ¡£
    ru_nivcsw
    ru_nivcsw°üº¬ÁËÒò¸ßÓÅÏȼ¶½ø³ÌÔËÐе¼ÖµÄÉÏÏÂÎÄÇл»×ÜÊý¡£
    7.4 С½á
    ±¾ÕÂÖ÷Òª²ûÊöÁ˳ÌÐòÈçºÎµÃµ½ÏµÍ³ÏÞÖÆ¡£ÕâЩÏÞÖƲ»Ó¦¸Ã¹Ì¶¨Ð´ÈëÄãµÄ´úÂ롣ȡ¶ø´úÖ®µÄÊÇ£¬ÄãµÄ³ÌÐòÓ¦¸ÃʹÓÃÕâЩ½Ó¿Ú¡£ÕâÊÇÒòΪÕâЩÏÞÖÆÊÇÓëƽ̨Ïà¹ØµÄ£¬ÉõÖÁ²»Í¬ÏµÍ³¼ä¶¼»áÓвî±ð¡£±ÈÈ磬ϵͳ¹ÜÀíÔ±¿ÉÒÔµ÷ÕûÔÊÐí´ò¿ªµÄ×î´óÎļþÊý£¬Òò´ËÈç¹ûÄãÐèÒªÔö¼Ó»òÕß¼õСÕâ¸öÖµ£¬¾Í²»Äܽ«Öµ4096¹Ì¶¨Ð´Èë³ÌÐòÖС£ÁíÒ»¸öÀý×ÓÊÇ·ÖÒ³´óС¡£Ò»Ð©64λϵͳʹÓÃ8096×÷ΪĬÈϵķÖÒ³´óС£¬µ«ÊÇ´ó¶àÊýµÄ32λϵͳ½«´ËĬÈÏÖµÉèÖÃλ4096¡£ÔÙ´ÎÇ¿µ÷£¬ÕâÊǸö¿Éµ÷ÕûµÄ²ÎÊý£¬²»Ó¦¸ÃʹÓÃÒ»¸ö¹Ì¶¨µÄÖµ¡£
    ÎÒÃÇ»¹Ñ§Ï°ÁËÒ»¸ö³ÌÐòÈçºÎµÃµ½Æä×ÊÔ´µÄµ±Ç°Ê¹ÓÃÁ¿£¬»òÕ߲鿴Æä×Ó½ø³ÌµÄ×ÊԴʹÓÃ×´¿ö¡£Ê¹ÓÃÕâЩ½Ó¿Ú¿ÉÒÔ°ïÖúÎÒÃǼì²âÉõÖÁ±ÜÃâ³öÏÖʧ¿ØµÄ½ø³Ì¡£ËüÃÇÓÃÔÚ³ÌÐòÊÔͼ³¬Ô½ÆäÈí/Ó²ÏÞÖÆʱµ÷ÊÔ´íÎóÒ²·Ç³£²»´í¡£

  4. linuxguy ÓÚ 2007-06-16 00:40:01·¢±í:

    µÚÁùÕ ¸ß¼¶I/O
    6.1 ¸ß¼¶I/OºÍ½ø³Ì×ÊÔ´
    ÕýÈçÎÒÃÇÔÚÇ°ÃæÕ½ÚÖп´µ½µÄ£¬³ÌÐò¿ÉÒÔͬʱ´ò¿ª¶à¸öÎļþÃèÊö·û¡£ÕâЩÎļþÃèÊö·û²¢²»Ò»¶¨¾ÍÊÇÎļþ£¬»¹¿ÉÒÔÊÇfifo¡¢pipe»òÕßsocket¡£ÓÚÊÇ£¬ÈçºÎ¸´ÓÃÕâЩ´ò¿ªµÄÃèÊö·û¾ÍºÜÖØÒªÁË¡£ÀýÈ磬¿¼ÂÇÒ»¸ö¼òµ¥µÄÓʼþÔĶÁ³ÌÐò£¬±ÈÈçpine¡£ËüÏÔȻӦµ±ÔÊÐíÓû§ÔÚ¶ÁдemailµÄͬʱҲÄÜÈ¥¼ì²éÊÇ·ñÓÐÐÂÓʼþ¡£Õâ¾ÍÒâζ×ÅÔÚÈÎÒ»¸ø¶¨Ê±¿Ì¶¼ÖÁÉÙÄܹ»½ÓÊÕÁ½¸öÀ´Ô´µÄÊäÈ룺һ¸öÀ´Ô´ÊÇÓû§£¬ÁíÒ»¸öÊÇÓÃÀ´¼ì²éÐÂÓʼþµÄÃèÊö·û¡£´¦ÀíÃèÊö·ûµÄ¸´ÓÃÊǸö¸´ÔÓµÄÎÊÌâ¡£Ò»ÖÖ·½·¨ÊÇ°ÑËùÓдò¿ªµÄÃèÊö·û¶¼±ê¼ÇΪ·Ç×èÈûµÄ£¨O_NONBLOCK£©£¬È»ºóÔÚËüÃÇÖ®ÖÐÑ­»·£¬Ö±µ½ÕÒµ½Ò»¸ö¿ÉÒÔ½øÐÐI/O²Ù×÷µÄÃèÊö·ûΪֹ¡£ÕâÖÖ·½·¨µÄÎÊÌâÊdzÌÐò»áÒ»Ö±ÔÚÑ­»·£¬Èç¹û³¤Ê±¼äÄÚûÓÐI/O¿ÉÓ㬽ø³Ì¾Í»áÒ»Ö±Õ¼¾ÝCPU¡£µ±Óжà¸ö½ø³ÌÔÚÒ»×éºÜÉÙµÄÃèÊö·ûÉÏÑ­»·Ê±£¬ÄãµÄCPUµÄ¸ºÔؾͻá¶ñ»¯¡£
    ÁíÒ»ÖÖ·½·¨¾ÍÊÇÉèÖÃÐźŴ¦ÀíÆ÷È¥²¶»ñI/O±äΪ¿ÉÓõÄʼþ£¬È»ºó¾ÍÈýø³Ì½øÈëÐÝÃß״̬¡£Èç¹ûÄãÖ»´ò¿ªÁËÉÙÁ¿µÄÃèÊö·û£¬¶øÇÒ²¢²»¾­³£ÇëÇóI/OµÄ»°£¬ÕâÖÖ·½·¨´ÓÀíÂÛÉÏ¿´µ¹ÊDz»´í¡£ÓÉÓÚ½ø³ÌÒѾ­ÐÝÃߣ¬¾Í²»»áÔÙÕ¼ÓÃCPU£¬½öµ±I/O¿ÉÓÃʱËü²Å»Ö¸´Ö´ÐС£È»¶ø£¬ÕâÖÖ·½·¨µÄÎÊÌâÔÚÓÚÐźŴ¦ÀíµÄ¿ªÏúÓеã´ó¡£±ÈÈçÒ»¸öweb·þÎñÆ÷£¬Ã¿·ÖÖÓÊÕµ½100¸öÇëÇó£¬ÄǾͼ¸ºõÒ»Ö±¶¼ÔÚ²¶»ñÐźš£Ã¿ÃëÖÓ²¶»ñÉÏ°Ù¸öÐźŵĿªÏúÊÇÏ൱´óµÄ£¬²»µ¥Êǽø³Ì£¬¶ÔÓÚÄں˷¢ËÍÐźŵĿªÏú¶øÑÔÒ²ÊÇÒ»ÑùµÄ¡£
    µ½Ä¿Ç°ÎªÖ¹£¬ÎÒÃÇ¿´µ½µÄÁ½ÖÖÑ¡Ôñ¶¼ÓÐÏÞÖÆ£¬Ð§ÂÊÒ²²»¸ß£¬ËüÃÇÐèÒª½â¾öµÄ¹²Í¬ÎÊÌâ¾ÍÊǽø³ÌÐèÒªÖªµÀI/O¾¿¾¹Ê²Ã´Ê±ºòÄÜÓã¿È»¶ø£¬Õâ¸öÐÅϢʵ¼ÊÉÏÖ»ÓÐÄں˲ÅÄÜÊÂÏÈÖªµÀ£¬ÒòΪÊÇÄÚºËÔÚ×îÖÕ´¦ÀíϵͳÖеÄËùÓдò¿ªµÄÃèÊö·û¡£ÀýÈ磬µ±Ò»¸ö½ø³Ìͨ¹ýfifoÏòÁíÒ»¸ö½ø³Ì·¢ËÍÊý¾ÝµÄʱºò£¬·¢Ëͽø³Ì»áµ÷ÓÃwrite£¬ÕâÊÇÒ»¸öϵͳµ÷Óã¬Òò´Ë»á½øÈëÄںˡ£ÔÚ·¢ËÍ·½µÄwriteϵͳµ÷ÓÃÖ´ÐÐÍê±Ï֮ǰ½ÓÊÕ·½¶Ô´ËÊÇÒ»ÎÞËùÖªµÄ¡£ÓÚÊǾÍÒý³öÁËÒ»¸ö¸üºÃµÄ¸´ÓÃÎļþÃèÊö·ûµÄ·½·¨£ºÓÉÄÚºËÀ´Ìæ½ø³Ì¹ÜÀíÃèÊö·û¡£»»¾ä»°Ëµ£¬¾ÍÊÇ°ÑÒ»¸ö´ò¿ªÃèÊö·ûµÄÁ´±í·¢Ë͸øÄںˣ¬È»ºóµÈ´ý£¬Ö±µ½Äں˷¢ÏÖij¸ö»ò¶à¸öÃèÊö·ûÒѾ­×¼±¸ºÃÁË»òÕßÒѾ­³¬Ê±ÁËΪֹ¡£
    Õâ¾ÍÊÇselect()¡¢poll()ºÍkqueue()½Ó¿Ú²ÉÓõķ½·¨¡£Í¨¹ýÕâЩ½Ó¿Ú£¬Äں˾ͻá¹ÜÀíÎļþÃèÊö·û£¬µ±I/O¿ÉÓÃʱ¾ÍÈ¥»½Ðѽø³Ì¡£ÕâЩ½Ó¿ÚÇÉÃîµØ´¦ÀíÁËÉÏÊöÎÊÌâ¡£½ø³Ì²»±ØÔÙÔÚ´ò¿ªµÄÎļþÃèÊö·ûÖÐÑ­»·£¬Ò²²»±ØÔÙÈ¥ÉèÖÃÐźÅÁË¡£µ«½ø³ÌÔÚʹÓÃÕâЩº¯ÊýµÄʱºò»¹ÊÇ»á²úÉúÒ»µãСÎÊÌâ¡£ÕâÊÇÒòΪI/O²Ù×÷ÊÇÔÚ´ÓÕâЩ½Ó¿Ú·µ»ØÖ®ºó²ÅÈ¥Ö´Ðеġ£ËùÒÔËüÖÁÉÙÐèÒªÁ½¸öϵͳµ÷ÓòÅÄÜÍê³ÉÆä²Ù×÷¡£ÀýÈ磬ÄãµÄ³ÌÐòÓÐÁ½¸öÓÃÓÚ¶ÁµÄÃèÊö·û¡£Äã¶ÔËüÃÇʹÓÃselect£¬È»ºóµÈ´ýËüÃÇÖ±ÖÁÓÐÊý¾Ý¿É¶Á¡£Õâ¾ÍÐèÒª½ø³ÌÊ×Ïȵ÷ÓÃselect£¬ÔÚselect·µ»ØÖ®ºó£¬¾Í¶Ô¸ÃÃèÊö·ûµ÷ÓÃread¡£¸üÃîµÄÊÇ£¬Ä㻹¿ÉÒÔ¶ÔËùÓдò¿ªµÄÃèÊö·ûÖ´ÐÐÒ»¸öÕûÌåµÄread¡£Ò»µ©ÆäÖÐÓÐij¸öÃèÊö·û×¼±¸ºÃ¶ÁÖ®ºó£¬read¾Í»á·µ»Ø£¬²¢°ÑÊý¾Ý·ÅÔÚ»º³åÇøÖУ¬Í¬Ê±»¹»á¸ø³öÒ»¸ö±êʶ£¬ÓÃÀ´Ö¸Ê¾Õâ¸öÊý¾ÝÊÇ´ÓÄĸöÃèÊö·û¶Á½øÀ´µÄ¡£
    6.2 select
    ÎÒÊ×ÏÈÒª½²µÄ½Ó¿ÚÊÇselect()¡£¸ñʽÈçÏ£º
    CODE:[Copy to clipboard]int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
    ´«¸øselectµÄµÚÒ»¸ö²ÎÊýÒѾ­Ôì³ÉÁ˶àÄêµÄ»ìÂÒ¡£nfds²ÎÊýµÄÕýÈ·Ó÷¨ÊÇ°ÑËüÉè³ÉÎļþÃèÊö·ûµÄ×î´óÖµ¼Ó1¡£»»¾ä»°Ëµ£¬Èç¹ûÄãÓÐÒ»×éÎļþÃèÊö·û{0,1,8}£¬nfds²ÎÊý¾ÍÓ¦µ±±»ÉèÖóÉ9£¬ÒòΪÄãµÄÃèÊö·ûµÄ×î´óֵΪ8¡£ÓÐЩÈË´íÎóµØÒÔΪÕâ¸ö²ÎÊýµÄÒâ˼ÊÇÎļþÃèÊö·ûµÄ×ÜÊý¼Ó1£¬¶ÔÓÚÎÒÃǵÄÀý×Ó¶øÑÔ¾ÍÊÇ4¡£¼Çס£¬Ò»¸öÎļþÃèÊö·ûÖ»ÊÇÒ»¸öÕûÊý¶øÒÑ£¬ËùÒÔÄãµÄ³ÌÐò¾ÍÐèÒªÖ¸³öÄãËùÏëÒªÔÚÆäÉÏselectµÄ×î´óµÄÃèÊö·ûÖµ¡£
    select½ÓÏÂÀ´»á°´Ë³ÐòÕë¶ÔËùÓÐÉÐδÍê³ÉµÄ¶Á¡¢Ð´ÒÔ¼°Òì³£Ìõ¼þ¼ì²éÆäÓàµÄÈý¸ö²ÎÊý£¬readfds¡¢writefdsºÍexceptfds¡££¨ÏêϸÐÅÏ¢Çë²Î¼ûman(2) select£©¡£×¢Ò⣬Èç¹ûreadfds¡¢writefdsºÍexecptfdsÖÐûÓÐÉèÖÃÃèÊö·û£¬ÄÇô´«¸øselectµÄ¶ÔÓ¦²ÎÊýÓ¦µ±±»ÉèÖóÉNULL¡£
    readfds¡¢writefdsºÍexecptfds²ÎÊýͨ¹ýÒÔÏÂ4¸öºê½øÐÐÉèÖá£
    FD_ZERO(&fdset);
    FD_ZEROºêÓÃÀ´¶ÔÖ¸¶¨µÄÃèÊö·û¼¯ºÏÖеÄbit½øÐÐÇåÁã¡£ÓÐÒ»µãÐèÒªÌرð×¢Ò⣺ֻҪʹÓÃselect£¬¾ÍÓ¦µ±µ÷ÓÃÕâ¸öºê£»·ñÔòselectµÄÐÐΪ½«ÊDz»¿ÉÔ¤ÖªµÄ¡£
    FD_SET(fd, &fdset);
    FD_SETºêÓÃÓÚÏòÒ»×鼤»îµÄÃèÊö·ûÖÐÌí¼ÓÒ»¸öÃèÊö·û¡£
    FD_CLR(fd, &fdset);
    FD_CLRºêÓÃÓÚ´ÓÒ»×鼤»îµÄÃèÊö·ûÖÐɾ³ýÒ»¸öÃèÊö·û¡£
    FD_ISSET(fd, &fdset);
    FD_ISSETºêÊÇÔÚselect·µ»ØÖ®ºóʹÓõģ¬ÓÃÓÚ²âÊÔij¸öÃèÊö·ûÊÇ·ñÒÑ×¼±¸ºÃ½øÐÐI/O²Ù×÷¡£
    selectµÄ×îºóµÄ²ÎÊýÊÇÒ»¸ö³¬Ê±Öµ¡£Èç¹û³¬Ê±Öµ±»ÉèÖÃΪNULL£¬Ôò¶ÔselectµÄµ÷Óý«ÒÔ²»È·¶¨µÄ·½Ê½±»×èÈû£¬Ö±ÖÁij¸ö²Ù×÷ÒÑ×¼±¸ºÃΪֹ¡£Èç¹ûÄãÐèÒªÒ»¸öÈ·¶¨µÄ³¬Ê±Ê±¼ä£¬ÄÇô³¬Ê±Öµ¾ÍµÃÊÇÒ»¸ö·Ç¿ÕµÄtimeval½á¹¹Ìå¡£timeval½á¹¹ÌåÈçÏ£º
    CODE:[Copy to clipboard] struct timeval {
    long tv_sec; /* seconds */
    long tv_usec; /* microseconds */
    };
    Èç¹ûselectµ÷Óóɹ¦£¬½«·µ»Ø×¼±¸ºÃµÄÃèÊö·ûµÄÊýÄ¿¡£Èç¹ûselectÒòΪ³¬Ê±¶ø·µ»Ø£¬Ôò·µ»ØֵΪ0¡£Èç¹ûÓдíÎó·¢Éú£¬Ôò·µ»Ø-1£¬Í¬Ê±»áÏàÓ¦µØÉèÖÃerrno¡£
    6.3 poll
    ÎÒÃÇÔÚÕâÀï¶ÔI/OµÄÌÖÂÛÖ÷ÒªÊÇÕë¶ÔBSDµÄ¡£System VÖ§³ÖÒ»ÖÖÌØÊâÀàÐ͵ÄI/O£¬¼´ËùνµÄSTREAMS¡£ºÍsocketÒ»Ñù£¬STREAMSÒ²¾ßÓÐÓÅÏȼ¶ÊôÐÔ£¬ÕâÖÖÊôÐÔÓÐʱҲ±»³ÉΪÊý¾Ý´ø¡£Êý¾Ý´ø¿ÉÓÃÀ´¸øSTREAMSÖеÄÌض¨Êý¾ÝÉèÖýϸߵÄÓÅÏȼ¶¡£BSD×î³õ²¢²»Ö§³ÖÕâÒ»ÌØÐÔ£¬²»¹ýÓÐЩÈËÌí¼ÓÁËSystem V·ÂÕ湦ÄÜ£¬¿ÉÒÔ¶ÔijЩÀàÐÍÌṩ֧³Ö¡£ÓÉÓÚÎÒÃDz¢²»¹Ø×¢System V£¬Òò´ËÎÒÃÇÖ»»áÒýÓÃÊý¾Ý´ø»òÊý¾ÝÓÅÏȼ¶´øµÄ¸ÅÄî¡£ÏêϸÐÅÏ¢Çë²Î¼ûSystem V STREAMS¡£
    pollº¯ÊýºÍselectºÜÏàËÆ£º
    CODE:[Copy to clipboard] int poll(struct pollfd *fds, unsigned int nfds, int timeout);
    ºÍÔ­²úÓÚBSDµÄselect²»Í¬£¬pollÊÇÓÉSystem V Unix´´½¨µÄ£¬ÔÚÔçÆÚµÄBSD°æ±¾Öв¢²»Ö§³ÖËü¡£Ä¿Ç°Ö÷Á÷BSDϵͳÖж¼ÒѾ­Ö§³ÖpollÁË¡£
    ºÍselectÏàËÆ£¬pollÒ²ÊÇÔÚÒ»×é¸ø¶¨µÄÎļþÃèÊö·ûÉϽøÐи´Óá£ÔÚÖ¸¶¨ÕâЩÃèÊö·ûµÄʱºò£¬Äã±ØÐëʹÓÃÒ»¸ö½á¹¹ÌåÊý×飬ÆäÖÐÿ¸ö½á¹¹Ìå´ú±íÒ»¸öÎļþÃèÊö·û¡£ºÍselectÏà±È£¬pollµÄºÃ´¦¾ÍÊÇÄã¿ÉÒÔÅжÏһЩºÜº±¼ûµÄÌõ¼þ£¬¶øselectÔòÎÞ·¨×öµ½¡£ÕâЩÌõ¼þÊÇPOLLERR¡¢POLLHUPºÍPOLLNVAL£¬ÎÒÃÇÉÔºóÌÖÂÛ¡£¾¡¹Ü¶ÔÓÚÑ¡Ôñselect»¹ÊÇpollµÄÎÊÌâÒѾ­ÓÐÁËÏ൱¶àµÄÌÖÂÛ£¬µ«ÕâÔںܴó³Ì¶ÈÉÏ»¹ÊÇÈ¡¾öÓÚÄãµÄ¸öÈË°®ºÃ¡£pollËùʹÓõĽṹÌåÊÇpollfd½á¹¹Ì壬ÈçÏ£º
    CODE:[Copy to clipboard] struct pollfd {
    int fd; /* which file descriptor to poll */
    short events; /* events we are interested in */
    short revents; /* events found on return */
    };
    fd
    fd³ÉÔ±ÓÃÓÚÖ¸¶¨ÄãÏëÒªpollµÄÎļþÃèÊö·û¡£Èç¹ûÄãÏëɾ³ýÒ»¸öÃèÊö·û£¬ÄǾͰÑÄǸöÃèÊö·ûµÄfd³ÉÔ±ÉèÖóÉ-1¡£Í¨¹ýÕâÖÖ·½·¨£¬Äã¿ÉÒÔ±ÜÃâ¶ÔÕû¸öÊý×é½øÐлìÏ´£¬Í¬Ê±»¹¿ÉÒÔÇå³ýrevents³ÉÔ±ÖÐÁгöµÄËùÓÐʼþ¡£
    events, revents
    events³ÉÔ±ÊÇÒ»¸öbitÑÚÂ룬ÓÃÓÚÖ¸¶¨Õë¶ÔÖ¸¶¨ÃèÊö·ûËù¹ØÐĵÄʼþ¡£revents³ÉÔ±Ò²ÊÇÒ»¸öbitÑÚÂ룬µ«ËüµÄÖµÊÇÓÉpollÉèÖõģ¬ÓÃÓڼǼÔÚÖ¸¶¨ÃèÊö·ûÉÏ·¢ÉúµÄʼþ¡£ÕâЩʼþµÄ¶¨ÒåÈçÏ£º
    CODE:[Copy to clipboard] #define POLLIN 0x0001
    POLLINʼþ±íÃ÷ÄãµÄ³ÌÐò½«Ñ¡Ôñ¸ÃÃèÊö·ûÉϵĿɶÁÊý¾Ýʼþ¡£×¢Ò⣬´Ë´¦µÄÊý¾Ý²»°üÀ¨¸ßÓÅÏȼ¶Êý¾Ý£¬±ÈÈçsocketÉϵĴøÍâÊý¾Ý¡£
    CODE:[Copy to clipboard] #define POLLPRI 0x0002
    POLLPRIʼþ±íÃ÷ÄãµÄ³ÌÐò×¼±¸Ñ¡Ôñ¸ÃÃèÊö·ûÉϵÄÈκθßÓÅÏȼ¶Ê¼þ¡£
    CODE:[Copy to clipboard] #define POLLOUT 0x0004
    #define POLLWRNORM POLLOUT
    POLLOUTºÍPOLLWRNOMRʼþ±íÃ÷ÄãµÄ³ÌÐòÏëÖªµÀʲôʱºò¿ÉÒÔ¶ÔÒ»¸öÃèÊö·ûÖ´ÐÐд²Ù×÷ÁË¡£ÔÚFreeBSDºÍOpenBSDÉÏÕâÁ½¸öʼþÊÇÏàͬµÄ£»Äã¿ÉÒÔÔÚÄãµÄϵͳͷÎļþ£¨/usr/include/poll.h£©ÖвéÖ¤ÕâÒ»µã¡£´Ó¼¼Êõ½Ç¶ÈÀ´Ëµ£¬ËüÃÇÖ®¼äµÄÇø±ðÔÚÓÚPOLLWRNOMR½öµ±Êý¾ÝÓÅÏÈ´øµÈÓÚ0µÄʱºò²ÅÈ¥¼ì²âÊÇ·ñ¿ÉÒÔ½øÐÐд²Ù×÷¡£
    CODE:[Copy to clipboard] #define POLLRDNORM 0x0040
    POLLRDNORMʼþ±íÃ÷ÄãµÄ³ÌÐò×¼±¸Ñ¡Ôñ¸ÃÃèÊö·ûÉϵij£¹æÊý¾Ý¡£×¢Ò⣬ÔÚijЩϵͳÉÏ£¬Õâ¸öʼþÖ¸¶¨µÄ²Ù×÷ºÍPOLLINÍêÈ«Ò»Ñù¡£µ«ÔÚNetBSDºÍFreeBSDÉÏ£¬Õâ¸öʼþºÍPOLLIN²¢²»Ïàͬ¡£Í¬Ñù£¬ÇëÈ¥²é¿´ÄãµÄϵͳͷÎļþ£¨/usr/include/poll.h£©¡£ÑϸñµØ˵£¬POLLRDNORM½öµ±Êý¾ÝÓÅÏÈ´øµÈÓÚ0µÄʱºò²ÉÈ¡¼ì²âÊÇ·ñ¿ÉÒÔ½øÐжÁ²Ù×÷¡£
    CODE:[Copy to clipboard] #define POLLRDBAND 0x0080
    POLLRDBANDʼþ±íÃ÷ÄãµÄ³ÌÐòÏëÖªµÀʲôʱºòÄܹ»ÒÔÒ»¸ö·Ç0µÄÊý¾Ý´øÖµ´Ó¸ÃÃèÊö·û¶ÁÊý¾Ý¡£
    CODE:[Copy to clipboard] #define POLLWRBAND 0x0100
    POLLWRBANDʼþ±íÃ÷ÄãµÄ³ÌÐòÏëÖªµÀʲôʱºòÄܹ»ÒÔÒ»¸ö·Ç0µÄÊý¾Ý´øÖµÏò¸ÃÃèÊö·ûдÊý¾Ý¡£
    רÓÃÓÚFreeBSDµÄÑ¡Ïî
    ÏÂÃæµÄÑ¡ÏîÊÇרÓÃÓÚFreeBSDµÄ£¬ÖªµÀµÄºÍʹÓõÄÈ˶¼²»ÊÇÌ«¶à¡£µ«ËüÃÇ»¹ÊÇÖµµÃÌáһϣ¬ÒòΪËüÃÇ¿ÉÒÔÌṩ¸ü¶àµÄÁé»îÐÔ¡£ÕâЩ¶¼ÊÇеÄÑ¡Ïpoll²¢²»±£Ö¤Äܹ»¼ì²âÕâЩÌõ¼þ£¬¶øÇÒËüÃÇÖ»ÄÜÓÃÓÚUFSÎļþϵͳ¡£Èç¹ûÄãµÄ³ÌÐòÐèÒª¼ì²âÕâЩÀàÐ͵Äʼþ£¬ÄÇ×îºÃʹÓÃkqueue½Ó¿Ú£¬ÎÒÃǽ«ÔÚÉÔºó½éÉÜ¡£
    CODE:[Copy to clipboard] #define POLLEXTEND 0x0200
    Èç¹ûÎļþÒѾ­±»Ö´ÐУ¬ÔòÉèÖÃPOLLEXTENDʼþ¡£
    CODE:[Copy to clipboard] #define POLLATTRIB 0x0400
    Èç¹ûÓÐÈÎÒ»ÎļþÊôÐÔ·¢Éú¸Ä±ä£¬ÔòÉèÖÃPOLLATTIBʼþ¡£
    CODE:[Copy to clipboard] #define POLLNLINK 0x0800
    Èç¹ûÎļþ±»ÖØÃüÃû¡¢É¾³ý»ò½â³ýÁ´½Ó£¬ÔòÉèÖÃPOLLNLINKʼþ¡£
    CODE:[Copy to clipboard] #define POLLWRITE 0x1000
    Èç¹ûÎļþÄÚÈݱ»Ð޸ģ¬ÔòÉèÖÃPOLLWRITEʼþ¡£
    ÏÂÃæµÄʼþ²¢²»ÊÇpollfd events³ÉÔ±µÄÓÐЧ±êÖ¾£¬pollÒ²½«ºöÂÔËüÃÇ¡£ËüÃÇÊÇÔÚpollfd reventsÖзµ»ØµÄ£¬ÓÃÓÚ±íÃ÷·¢ÉúÁËij¸öʼþ¡£
    CODE:[Copy to clipboard] #define POLLERR 0x0008
    POLLERRʼþ±íÃ÷ÓдíÎó·¢Éú¡£
    CODE:[Copy to clipboard] #define POLLHUP 0x0010
    POLLHUP±íÃ÷ÔÚ¶ÔÓ¦µÄSTREAMSÉÏ·¢ÉúÁ˹ÒÆðʼþ¡£POLLHUPºÍPOLLOUTÊÇ»¥³âʼþ£¬ÒòΪһ¸ö·¢ÉúÁ˹ÒÆðµÄSTREAMS¾Í²»ÔÙÊÇ¿ÉдµÄÁË¡£
    CODE:[Copy to clipboard] #define POLLNVAL 0x0020
    POLLNVAL±íÃ÷¶ÔpollµÄÇëÇóÊÇÎÞЧµÄ¡£
    pollµÄ×îºóÒ»¸ö²ÎÊýÊdz¬Ê±Öµ¡£¿ÉÒÔͨ¹ýÕâ¸ö²ÎÊý¸æËßpollÒ»¸öÒÔ΢ÃëΪµ¥Î»µÄ³¬Ê±Öµ¡£Èç¹û°Ñ³¬Ê±ÖµÉèÖÃΪ-1£¬poll¾Í»á×èÈû£¬Ö±ÖÁËùÇëÇóµÄʼþ·¢ÉúΪֹ¡£Èç¹û³¬Ê±ÖµÉèÖÃΪ0£¬Ôòpoll½«Á¢¼´·µ»Ø¡£
    Èç¹û¶ÔpollµÄµ÷Óóɹ¦£¬Ôò·µ»ØÒ»¸öÕýÕûÊý¡£Õâ¸öÕýÕûÊýµÄÖµ±íʾÓжàÉÙ¸öÃèÊö·û·¢ÉúÁËʼþ¡£Èç¹û³¬Ê±£¬poll½«·µ»Ø0¡£Èç¹ûÓдíÎó·¢Éú£¬pollÔò»á·µ»Ø-1¡£
    6.4 kqueue
    µ½Ä¿Ç°ÎªÖ¹£¬pollºÍselectÒѾ­ÊÇÏ൱²»´íµÄ¸´ÓÃÎļþÃèÊö·ûµÄ·½·¨ÁË¡£µ«ÎªÁËʹÓÃÕâÁ½¸öº¯Êý£¬ÄãÐèÒª´´½¨Ò»¸öÃèÊö·ûµÄÁ´±í£¬È»ºó°ÑËüÃÇ·¢Ë͸øÄںˣ¬ÔÚ·µ»ØµÄʱºòÓÖÒªÔٴβ鿴Õâ¸öÁ´±í¡£Õâ¿´ÉÏÈ¥ÓеãЧÂʵÍÏ¡£Ò»¸ö¸üºÃһЩµÄÄ£ÐÍÊÇ°ÑÃèÊö·ûÁ´±í½»¸øÄںˣ¬È»ºó¾ÍµÈ´ý¡£Ò»µ©ÓÐij¸ö»ò¶à¸öʼþ·¢Éú£¬Äں˾ͰÑÒ»¸öÖ»°üº¬Óз¢ÉúÁËʼþµÄÃèÊö·ûµÄÁ´±í֪ͨ¸ø½ø³Ì£¬Óɴ˱ÜÃâÁËÿ´Îº¯Êý·µ»ØµÄʱºò¶¼ÒªÈ¥±éÀúÕû¸öÁ´±í¡£¾¡¹Ü¶ÔÓÚÖ»´ò¿ªÁ˼¸¸öÃèÊö·ûµÄ½ø³Ì¶øÑÔÕâµã¸Ä½øËã²»µÃʲô£¬µ«¶ÔÓÚÄÇЩ´ò¿ªÁ˼¸Ç§¸öÎļþÃèÊö·ûµÄ³ÌÐòÀ´Ëµ£¬ÕâÖÖÐÔÄܸĽø¾ÍÏ൱ÏÔÖøÁË¡£Õâ¾ÍÊÇkqueueµ®Éú±³ºóµÄÖ÷ҪĿµÄ¡£Í¬Ê±£¬Éè¼ÆÕß»¹Ï£Íû½ø³ÌÄܹ»¼ì²â¸ü¶àÀàÐ͵Äʼþ£¬±ÈÈçÎļþÐ޸ġ¢Îļþɾ³ý¡¢ÐźŽ»¸¶»òÕß×Ó½ø³ÌÍ˳ö£¬²¢Ìṩһ¸ö°üº¬ÁËÆäËüÈÎÎñµÄÁé»îµÄº¯Êýµ÷Óᣴ¦ÀíÐźš¢¸´ÓÃÎļþÃèÊö·û¡¢ÒÔ¼°µÈ´ý×Ó½ø³ÌµÈ²Ù×÷¶¼¿ÉÒÔ·â×°µ½Õâ¸öµ¥Ò»µÄkqueue½Ó¿ÚÖУ¬ÒòΪËüÃǶ¼ÊÇÔڵȴýij¸öʼþµÄ·¢Éú¡£
    ÁíÒ»¸öÉè¼Æ¿¼ÂǾÍÊÇÈçºÎÈÃÒ»¸ö½ø³ÌºÁÎÞ¸ÉÈŵØʹÓöà¸ökqueueʵÀý¡£ÈçÄãËù¼û£¬½ø³Ì¿ÉÒÔÉèÖÃÒ»¸öÐźŴ¦ÀíÆ÷£¬µ«ÊÇ£¬µ±´úÂëÖеÄÆäËü²¿·ÖÒ²Ï벶»ñÄǸöÖ¸¶¨ÐźŵÄʱºò¸ÃÔõô°ì£¿»òÕß¿¼ÂǸü»µµÄÇé¿ö£¬±ÈÈçÒ»¸ö¿âº¯Êý¶ÔÄãµÄ³ÌÐòÏëÒª²¶»ñµÄÐźÅÉèÖÃÁËÐźŴ¦ÀíÆ÷µÄʱºò£¿ÒªÏëͨ¹ýµ÷ÊÔÀ´ÕÒ³öÄãµÄ³ÌÐòΪʲôûÓÐÖ´ÐÐÄãËùÉèÖõÄÐźŴ¦ÀíÆ÷¿ÉÄÜÒª»¨·Ñ¼¸¸öСʱµÄʱ¼ä¡£²»¹ýÒ»°ã˵À´£¬ÕâЩÇé¿ö²¢²»»á¾­³£·¢Éú¡£ºÃµÄ³ÌÐòÔ±Ó¦¸Ã±ÜÃâÔڿ⺯ÊýÖÐÉèÖÃÐźŴ¦ÀíÆ÷¡£¶ÔÓÚ´óÐ͵ġ¢¸´ÔӵijÌÐòÀ´Ëµ£¬ÕâЩÇé¿ö¾ÍºÜÄѱÜÃâÁË£¬ËùÒÔΪÁ˸üÍêÃÀÒ»µã£¬ÎÒÃÇÓ¦µ±Äܹ»¼ì²âÕâЩʼþ£¬¶økqueue¾Í¿ÉÒÔ¡£
    kqueue APIÓÉÁ½¸öº¯Êýµ÷ÓúÍÒ»¸ö¸¨ÖúÉèÖÃʼþµÄºê×é³É¡£ÕâЩº¯Êý½«ÔÚÏÂÃæ½øÐмòÒª½éÉÜ¡£
    CODE:[Copy to clipboard] int kqueue(void);
    kqueueº¯ÊýÆô¶¯Ò»¸öеÄkqueue¡£Èç¹ûµ÷Óóɹ¦£¬·µ»ØÖµ½«ÊÇÒ»¸öÓÃÀ´ºÍд´½¨µÄkqueue½»»¥µÄÃèÊö·û¡£Ã¿¸ökqueue¶¼ÓÐÒ»¸öÓëÖ®¹ØÁªµÄΨһµÄÃèÊö·û¡£Òò´Ë£¬Ò»¸ö³ÌÐò¿ÉÒÔͬʱ´ò¿ª¶à¸ökqueue¡£kqueueÃèÊö·ûµÄÐÐΪºÍ³£¹æÎļþÃèÊö·ûÀàËÆ£ºËüÃÇÒ²¿ÉÒÔ±»¸´Óá£
    ×îºóÒ»µã£¬ÕâЩÃèÊö·ûÊDz»Äܱ»fork´´½¨µÄ×Ó½ø³Ì¼Ì³ÐµÄ¡£Èç¹û×Ó½ø³ÌÊÇͨ¹ýrforkµ÷Óô´½¨µÄ£¬ÄǾÍÐèÒªÉèÖÃRFFDG±êÖ¾£¬ÒÔÃâÕâЩÃèÊö·û±»×Ó½ø³Ì¹²Ïí¡£Èç¹ûkqueueº¯Êýʧ°Ü£¬½«·µ»Ø-1£¬Í¬Ê±ÏàÓ¦µÄÉèÖÃerrno¡£
    CODE:[Copy to clipboard] int kevent(int kq, const struct kevent *changelist, int nchanges, struct kevent *eventlist, int nevents, const struct timespec *timeout);
    keventº¯ÊýÓÃÓÚºÍkqueueµÄ½»»¥¡£µÚÒ»¸ö²ÎÊýÊÇkqueue·µ»ØµÄÃèÊö·û¡£changelist²ÎÊýÊÇÒ»¸ö´óСΪnchangesµÄkevent½á¹¹ÌåÊý×é¡£changelist²ÎÊýÓÃÓÚ×¢²á»òÐÞ¸Äʼþ£¬²¢ÇÒ½«ÔÚ´Ókqueue¶Á³öʼþ֮ǰµÃµ½´¦Àí¡£
    eventlist²ÎÊýÊÇÒ»¸ö´óСΪneventsµÄkevent½á¹¹ÌåÊý×é¡£keventͨ¹ý°Ñʼþ·ÅÔÚeventlist²ÎÊýÖÐÀ´Ïòµ÷Óýø³Ì·µ»Øʼþ¡£Èç¹ûÐèÒªµÄ»°£¬eventlistºÍchangelist²ÎÊý¿ÉÒÔÖ¸Ïòͬһ¸öÊý×é¡£×îºóÒ»¸ö²ÎÊýÊÇkeventËùÆÚ´ýµÄ³¬Ê±Ê±¼ä¡£Èç¹û³¬Ê±²ÎÊý±»Ö¸¶¨ÎªNULL£¬kevent½«×èÈû£¬Ö±ÖÁÓÐʼþ·¢ÉúΪֹ¡£Èç¹û³¬Ê±²ÎÊý²»ÎªNULL£¬Ôòkevent½«×èÈûµ½³¬Ê±ÎªÖ¹¡£Èç¹û³¬Ê±²ÎÊýÖ¸¶¨µÄÊÇÒ»¸öÄÚÈÝΪ0µÄ½á¹¹Ì壬kevent½«Á¢¼´·µ»ØËùÓе±Ç°ÉÐδ´¦ÀíµÄʼþ¡£
    keventµÄ·µ»ØÖµÖ¸¶¨ÁË·ÅÔÚeventlistÊý×éÖеÄʼþµÄÊýÄ¿¡£Èç¹ûʼþÊýÄ¿³¬¹ýÁËeventlistµÄ´óС£¬¿ÉÒÔͨ¹ýºóÐøµÄkeventµ÷ÓÃÀ´»ñµÃËüÃÇ¡£ÔÚ´¦ÀíʼþµÄ¹ý³ÌÖз¢ÉúµÄ´íÎóÒ²»áÔÚ»¹ÓпռäµÄÇ°Ìáϱ»·Åµ½eventlist²ÎÊýÖС£´øÓдíÎóµÄʼþ»áÉèÖÃEV_ERRORλ£¬ÏµÍ³´íÎóÒ²»á±»·Åµ½data³ÉÔ±ÖС£¶ÔÓÚÆäËüµÄËùÓдíÎ󶼽«·µ»Ø-1£¬²¢ÏàÓ¦µØÉèÖÃerrno¡£
    kevent½á¹¹ÌåÓÃÓÚºÍkqueueµÄͨÐÅ¡£FreeBSDÉϵÄÍ·ÎļþλÓÚ/usr/include/sys/event.h¡£ÔÚÕâ¸öÎļþÖÐÓжÔkevent½á¹¹ÌåµÄÉùÃ÷£¬ÒÔ¼°ÆäËüµÄһЩѡÏîºÍ±êÖ¾¡£ºÍselectºÍpoll±ÈÆðÀ´£¬kqueue»¹Ï൱µÄÄêÇᣬËùÒÔËüÒ»Ö±¶¼ÔÚ·¢Õ¹ºÍÌí¼ÓеÄÌØÐÔ¡£Çë²é¿´ÄãµÄϵͳͷÎļþÒÔÈ·¶¨ÈκÎеĻòÕßÌض¨ÓÚϵͳµÄÑ¡Ïî¡£
    ԭʼµÄkevent½á¹¹ÌåµÄÉùÃ÷ÈçÏ£º
    CODE:[Copy to clipboard] struct kevent {
    uintptr_t ident;
    short filter;
    u_short flags;
    u_int fflags;
    intptr_t data;
    void *udata;
    };
    ÏÖÔÚ£¬ÈÃÎÒÃÇÀ´¿´¿´¸÷¸ö³ÉÔ±£º
    ident
    ident³ÉÔ±ÓÃÓÚ´æ´¢kqueueµÄΨһ±êʶ¡£»»¾ä»°Ëµ£¬Èç¹ûÄãÏë¸øÒ»¸öʼþÌí¼ÓÒ»¸öÎļþÃèÊö·ûµÄ»°£¬ident³ÉÔ±¾ÍÓ¦µ±±»ÉèÖóÉÄ¿±êÃèÊö·ûµÄÖµ¡£
    filter
    filter³ÉÔ±ÓÃÓÚÖ¸¶¨ÄãÏ£ÍûÄÚºËÓÃÓÚident³ÉÔ±µÄ¹ýÂËÆ÷¡£
    flags
    flags³ÉÔ±½«¸æËßÄÚºËÓ¦µ±¶Ô¸ÃʼþÍê³ÉÄÄЩ²Ù×÷ºÍ´¦ÀíÄÄЩ±ØÒªµÄ±êÖ¾¡£ÔÚ·µ»ØµÄʱºò£¬flags³ÉÔ±¿ÉÓÃÓÚ±£´æ´íÎóÌõ¼þ¡£
    fflags
    fflags³ÉÔ±ÓÃÓÚÖ¸¶¨ÄãÏëÈÃÄÚºËʹÓõÄÌض¨ÓÚ¹ýÂËÆ÷µÄ±êÖ¾¡£ÔÚ·µ»ØµÄʱºò£¬fflags³ÉÔ±¿ÉÓÃÓÚ±£´æÌض¨ÓÚ¹ýÂËÆ÷µÄ·µ»ØÖµ¡£
    data
    data³ÉÔ±ÓÃÓÚ±£´æÈκÎÌض¨ÓÚ¹ýÂËÆ÷µÄÊý¾Ý¡£
    udata
    udata³ÉÔ±²¢²»ÓÉkqueueʹÓã¬kqueue»á°ÑËüµÄÖµ²»¼ÓÐ޸ĵØ͸´«¡£Õâ¸ö³ÉÔ±¿É±»½ø³ÌÓÃÀ´·¢ËÍÐÅÏ¢ÉõÖÁÊÇÒ»¸öº¯Êý¸øËü×Ô¼º£¬ÓÃÓÚһЩÒÀÀµÓÚʼþ¼ì²âµÄ³¡ºÏ¡£
    kqueue ¹ýÂËÆ÷
    ÏÂÃæÁгöµÄÊÇkqueueʹÓõĹýÂËÆ÷¡£Ä³Ð©¹ýÂËÆ÷»áÓÐרÓÃÓÚËüµÄ±êÖ¾¡£ÕâЩ±êÖ¾ÊÇÔÚkevent½á¹¹ÌåµÄfflags³ÉÔ±ÖÐÉèÖõġ£
    CODE:[Copy to clipboard] #define EVFILT_READ (-1)
    EVFILT_READ¹ýÂËÆ÷ÓÃÓÚ¼ì²âʲôʱºòÊý¾Ý¿É¶Á¡£keventµÄident³ÉÔ±Ó¦µ±±»Éè³ÉÒ»¸öÓÐЧµÄÃèÊö·û¡£¾¡¹ÜÕâ¸ö¹ýÂËÆ÷µÄÐÐΪºÍselect»òÕâpollºÜÏñ£¬µ«Ëü·µ»ØµÄʼþ½«ÊÇÌض¨ÓÚËùʹÓõÄÃèÊö·ûµÄÀàÐ͵ġ£
    Èç¹ûÃèÊö·ûÒýÓõĴò¿ªÎļþÊÇÒ»¸övnode£¬¸Ãʼþ¾Í±íÃ÷¶ÁÈ¡Æ«ÒÆÁ¿ÉÐδµ½´ïÎļþĩβ¡£data³ÉÔ±±£´æµÄÊǵ±Ç°¾àÎļþĩβµÄÆ«ÒÆÁ¿£¬Õâ¿ÉÒÔÊǸºÖµ¡£Èç¹ûÃèÊö·ûÒýÓõÄÊÇÒ»¸öpipe»òÕßfifo£¬ÄÇô¹ýÂËÆ÷½«ÔÚÓÐʵ¼ÊÊý¾Ý¿É¶Áʱ·µ»Ø¡£data³ÉÔ±±£´æµÄÊǿɹ©¶ÁÈ¡µÄ×Ö½ÚÊýÄ¿¡£EV_EOF bitÓÃÓÚ±íʾÊÇÄĸöдÈëÕ߹رÕÁËÁ¬½Ó¡££¨¹ØÓÚʹÓÃsocketʱEVFILT_READµÄÐÐΪϸ½ÚÇë²Î¼ûkqueueµÄÊÖ²áÒ³¡££©
    CODE:[Copy to clipboard] #define EVFILT_WRITE (-2)
    EVFILT_WRITE¹ýÂËÆ÷ÓÃÓÚ¼ì²âÊÇ·ñ¿ÉÒÔ¶ÔÃèÊö·ûÖ´ÐÐд²Ù×÷¡£Èç¹ûÃèÊö·ûÒýÓõÄÊÇÒ»¸öpipe¡¢fifo»òÕßsocket£¬Ôòdata³ÉÔ±½«´æÓÐд»º³åÇøÖпÉÓõÄ×Ö½ÚÊýÄ¿¡£EV_EOF bit±íʾ¶ÁÈ¡·½ÒѾ­¹Ø±ÕÁËÁ¬½Ó¡£Õâ¸ö±êÖ¾¶ÔÓÚ´ò¿ªµÄÎļþÃèÊö·ûÎÞЧ¡£
    CODE:[Copy to clipboard] #define EVFILT_AIO (-3)
    EVFILT_AIOÓÃÓÚÒì²½I/O²Ù×÷£¬ÓÃÓÚ¼ì²âºÍaio_errorϵͳµ÷ÓÃÏàËƵÄÌõ¼þ¡£
    CODE:[Copy to clipboard] #define EVFILT_VNODE (-4)
    EVFILT_VNODE¹ýÂËÆ÷ÓÃÓÚ¼ì²â¶ÔÎļþϵͳÉÏÒ»¸öÎļþµÄijÖָĶ¯¡£°Ñident³ÉÔ±ÉèÖóÉÒ»¸öÓÐЧµÄ´ò¿ªÎļþÃèÊö·û£¬ÓÃfflags³ÉÔ±Ö¸¶¨Ëù¹ØÐĵÄʼþ¡£·µ»Øʱ£¬fflags³ÉÔ±½«º¬ÓÐËù·¢ÉúʼþµÄ±ÈÌØÑÚÂë¡£ÕâЩʼþÈçÏ£º
    CODE:[Copy to clipboard] #define NOTE_DELETE 0x0001
    NOTE_DELETE fflag±íʾ½ø³ÌÏëÖªµÀ¸ÃÎļþºÎʱ±»É¾¡£
    CODE:[Copy to clipboard] #define NOTE_WRITE 0x0002
    NOTE_WRITE fflag±íʾ½ø³ÌÏëÖªµÀ¸ÃÎļþÄÚÈݺÎʱ±»¸Ä±ä¡£
    CODE:[Copy to clipboard] #define NOTE_EXTEND 0x0004
    NOTE_EXTEND fflag±íʾ½ø³ÌÏëÖªµÀ¸ÃÎļþºÎʱ±»À©Õ¹¡£
    CODE:[Copy to clipboard] #define NOTE_ATTRIB 0x0008
    NOTE_ATTRIB fflag±íʾ½ø³ÌÏëÖªµÀ¸ÃÎļþÊôÐÔºÎʱ±»¸Ä±ä¡£
    CODE:[Copy to clipboard] #define NOTE_LINK 0x0010
    NOTE_LINK fflag±íʾ½ø³ÌÏëÖªµÀ¸ÃÎļþµÄÁ´½Ó¼ÆÊýºÎʱ±»¸Ä±ä¡£µ±Îļþͨ¹ýlinkº¯Êýµ÷ÓýøÐÐÓ²Á´½ÓµÄʱºò£¬ËüµÄÁ´½Ó¼ÆÊý¾Í»á¸Ä±ä¡££¨ÏêÇéÇë²Î¼ûman(2) link¡££©
    CODE:[Copy to clipboard] #define NOTE_RENAME 0x0020
    NOTE_RENAME fflag±íʾ½ø³ÌÏëÖªµÀ¸ÃÎļþÊÇ·ñ±»ÖØÐÂÃüÃûÁË¡£
    CODE:[Copy to clipboard] #define NOTE_REVOKE 0x0040
    NOTE_REVOKE fflag±íʾ¶ÔÎļþµÄ·ÃÎʱ»revokeÁË¡£ÏêÇéÇë¼ûman(2) revoke¡£
    CODE:[Copy to clipboard] #define EVFILT_PROC (-5) /* attached to struct proc */
    EVLILT_PROC¹ýÂËÆ÷±»½ø³ÌÓÃÀ´¼ì²â·¢ÉúÔÚÁíÍâÒ»¸ö½ø³ÌÀïµÄʼþ¡£Ëù¹ØÐĽø³ÌµÄPID´æ´¢ÔÚident³ÉÔ±ÖУ¬fflags³ÉÔ±Ôò±»Éè³ÉËù¹ØÐĵÄʼþ¡£·µ»Øʱ£¬Ê¼þ½«±»·ÅÔÚfflags³ÉÔ±ÖС£ÕâЩʼþÓÉÏÂÁÐʼþ°´±ÈÌØORµÄ·½Ê½ÉèÖãº
    CODE:[Copy to clipboard] #define NOTE_EXIT 0x80000000
    NOTE_EXIT fflagÓÃÓÚ¼ì²â¸Ã½ø³ÌºÎʱÍ˳ö¡£
    CODE:[Copy to clipboard] #define NOTE_FORK 0x40000000
    NOTE_FORK fflagÓÃÓÚ¼ì²â¸Ã½ø³ÌºÎʱµ÷ÓÃfork¡£
    CODE:[Copy to clipboard] #define NOTE_EXEC 0x20000000
    NOTE_EXEC fflagÓÃÓÚ¼ì²â¸Ã½ø³ÌºÎʱµ÷ÓÃexecº¯Êý¡£
    CODE:[Copy to clipboard] #define NOTE_TRACK 0x00000001
    NOTE_TRACK fflagÈÃkqueueÈ¥¸ú×ÙÒ»¸ö¿çÔ½forkµ÷ÓõĽø³Ì¡£×Ó½ø³Ì·µ»Øʱ½«ÉèÖÃfflagsÖеÄNOTE_CHILD±êÖ¾£¬¸¸½ø³ÌµÄPID½«·ÅÔÚdata³ÉÔ±ÖС£
    CODE:[Copy to clipboard] #define NOTE_TRACKERR 0x00000002
    µ±ÔÚ¸ú×Ù×Ó½ø³ÌµÄ¹ý³ÌÖÐÓдíÎó·¢Éúʱ£¬¾Í»áÉèÖÃNOTE_TRACKERR fflag¡£ÕâÊÇÒ»¸ö½öÓÃÓÚ·µ»ØµÄfflag¡£
    CODE:[Copy to clipboard] #define NOTE_CHILD 0x00000004
    NOTE_CHILD fflagÔÚ×Ó½ø³ÌÄÚÉèÖá£ÕâÊÇÒ»¸ö½öÓÃÓÚ·µ»ØµÄfflag¡£
    CODE:[Copy to clipboard] #define EVFILT_SIGNAL (-6)
    EVFILT_SIGNAL¹ýÂËÆ÷ÓÃÓÚ¼ì²âÊÇ·ñÓÐÐźŷ¢Ë͸ø¸Ã½ø³Ì¡£Ã¿µ±ÓÐÐźŷ¢ËÍʱÕâ¸ö¹ýÂËÆ÷¾Í»á¼ì²âµ½£¬²¢°Ñ¼ÆÊýÖµ·ÅÔÚdata³ÉÔ±ÖС£Õâ°üÀ¨ÉèÖÃÁËSIG_IGN±êÖ¾µÄÐźš£Ê¼þ½«ÔÚÖ´ÐÐÍê³£¹æµÄÐźŴ¦Àí¹ý³ÌÖ®ºó·Åµ½kqueueÉÏ¡£×¢Ò⣬Õâ¸ö¹ýÂËÆ÷½«ÔÚÄÚ²¿ÉèÖÃEV_CLEAR±êÖ¾¡£
    CODE:[Copy to clipboard] #define EVFILT_TIMER (-7)
    EVFILT_TIMER¹ýÂËÆ÷»á¸økqueue´´½¨Ò»¸ö¶¨Ê±Æ÷£¬ÓÃÓڼǼÏûÊŵÄʼþ¡£Èç¹ûÐèÒªÒ»¸öÒ»´ÎÐԵĶ¨Ê±Æ÷£¬¿ÉÒÔÉèÖÃEV_ONESHOT±êÖ¾¡£Õâ¸ö¶¨Ê±Æ÷ÊÇÔÚident³ÉÔ±ÖÐÖ¸¶¨µÄ£¬data³ÉÔ±ÓÃÀ´Ö¸¶¨ÒÔºÁÃëΪµ¥Î»µÄ³¬Ê±Ê±¼ä¡£·µ»ØÖµ·ÅÔÚdata³ÉÔ±ÖС£×¢Ò⣬Õâ¸ö¹ýÂËÆ÷½«ÔÚÄÚ²¿ÉèÖÃEV_CLEAR±êÖ¾¡£
    kqueue²Ù×÷
    kqueue²Ù×÷ÓÉËùÐèµÄ²Ù×÷ºÍ±êÖ¾ÒÔ±ÈÌØORµÄ·½Ê½½øÐÐÉèÖá£
    CODE:[Copy to clipboard] #define EV_ADD 0x0001
    EV_ADD²Ù×÷ÏòkqueueÌí¼Óʼþ¡£ÓÉÓÚkqueueÖв»ÔÊÐí³öÏÖÖظ´£¬ËùÒÔÈç¹ûÄãÏëÌí¼ÓÒ»¸öÒѾ­´æÔÚµÄʼþµÄ»°£¬ÏÖÓÐʼþ½«±»ÐµÄÌí¼Ó²Ù×÷¸²¸Ç¡£×¢Ò⣬ÔÚÌí¼ÓʼþµÄʱºò£¬ËüÃÇÒѾ­±»Ä¬Èϼ¤»îÁË£¬³ý·ÇÄãÉèÖÃÁËEV_DISABLE±êÖ¾¡£
    CODE:[Copy to clipboard] #define EV_DELETE 0x0002
    EV_DELETE²Ù×÷´ÓkqueueÖÐɾ³ýʼþ¡£
    CODE:[Copy to clipboard] #define EV_ENABLE 0x0004
    EV_ENABLEÓÃÓÚ¼¤»îkqueueÖеÄʼþ¡£×¢Ò⣬ÐÂÌí¼ÓµÄʼþĬÈϾÍÊǼ¤»îµÄ¡£
    CODE:[Copy to clipboard] #define EV_DISABLE 0x0008
    EV_DISABLE½ûÖ¹kqueue·µ»Øij¸öʼþµÄÐÅÏ¢¡£×¢Ò⣬kqueue²¢²»»áɾ³ý¹ýÂËÆ÷¡£
    kqueue²Ù×÷±êÖ¾
    kqueueµÄ²Ù×÷±êÖ¾¶¨ÒåÈçÏ¡£ËüÃǺÍÉÏÃæÁгöµÄ²Ù×÷½áºÏʹÓá£ËüÃÇÊÇͨ¹ýºÍËùÐè²Ù×÷½øÐбÈÌØORÀ´ÉèÖõġ£
    CODE:[Copy to clipboard] #define EV_ONESHOT 0x0010
    EV_ONESHOT±êÖ¾ÓÃÓÚ֪ͨkqueueÖ»·µ»ØµÚÒ»¸ö¡£
    CODE:[Copy to clipboard] #define EV_CLEAR 0x0020
    EV_CLEAR±êÖ¾ÓÃÓÚ֪ͨkqueue£¬Ò»µ©½ø³Ì´ÓkqueueÖлñÈ¡µ½Á˸Ãʼþ¾Í½«¸ÃʼþµÄ״̬¸´Î»¡£
    kqueue·µ»ØÖµ
    ½öÓÃÓÚ·µ»ØµÄÖµÊÇ·ÅÔÚkevent½á¹¹ÌåµÄflags³ÉÔ±Öеġ£ÕâЩֵµÄ¶¨ÒåÈçÏ£º
    CODE:[Copy to clipboard] #define EV_EOF 0x8000
    EV_EOFÓÃÓÚ±íʾÎļþ½áÊøµÄÇé¿ö¡£
    CODE:[Copy to clipboard] #define EV_ERROR 0x4000
    EV_ERRORÓÃÓÚ±íʾÓдíÎó·¢ÉúÁË¡£ÏµÍ³´íÎ󽫱»·Åµ½data³ÉÔ±ÖС£
    6.5 ½áÂÛ
    ±¾ÕÂÑо¿ÁËBSDÖеÄÃèÊö·û¸´Óá£×÷Ϊһ¸ö³ÌÐòÔ±£¬Äã¿ÉÒÔÑ¡ÔñÈý¸ö½Ó¿Ú£ºselect¡¢pollºÍkqueue¡£¶ÔÓÚСÊýÁ¿µÄÃèÊö·ûÀ´Ëµ£¬ÕâÈýÕßµÄÐÔÄܲ¶à£¬µ«Êǵ±ÃèÊö·ûÊýÁ¿ºÜ´óʱ£¬kqueueÔòÊÇ×îºÃµÄÑ¡Ôñ¡£³ý´ËÖ®Í⣬kqueue»¹¿ÉÒÔ¼ì²â±ÈI/Oʼþ¸üΪ·á¸»µÄÌõ¼þ¡£Ëü¿ÉÒÔ¼ì²âÐźš¢ÎļþÐÞ¸ÄÒÔ¼°×Ó½ø³ÌÏà¹ØµÄʼþ¡£ÔÚÏÂÒ»ÕÂÖУ¬ÎÒÃǽ«Õë¶ÔFreeBSD 5.xÖеÄÐÂÌØÐÔ£¬Ñо¿ÆäËüµÄ»ñÈ¡×Ó½ø³ÌÐÅÏ¢ºÍµ±Ç°½ø³Ìͳ¼ÆÐÅÏ¢µÄ·½·¨¡£

  5. linuxguy ÓÚ 2007-06-16 00:39:30·¢±í:

    µÚÎåÕ »ù±¾ÊäÈëÊä³ö
    ÒëÕߣºhorseman
    (¶àлÎÒµÄͬÊÂHUGH CHEN·­Òë5.6,5.7¸ú²¿·Ö5.4,5.5¶ÎÂä)
    5.1 »ù±¾ÊäÈëÊä³ö
    Ò»°ãÀ´Ëµ£¬UnixÐÅ·î¼òµ¥Éè¼ÆµÄÕÜÀí¡£"Ò»Çж¼ÊÇÎļþ"ÊǸöºÜÇ¿´óµÄÌØÕ÷£­£­Õâ¾ÍÒâζ×ÅÄãËù±à¼­µÄÎı¾Îļþ¾ßÓк͵÷Öƽâµ÷Æ÷¡¢´òÓ¡»ú»òÍø¿¨ÏàͬµÄ±à³Ì½Ó¿Ú,¾ÍÏñ±à¼­Îı¾ÎļþÒ»Ñù£¬ÄãÓ¦¸ÃÄܹ»¶ÔËüÃÇ£¨±»±à¼­Îļþ£©Ö´Ðлù±¾µÄ¶Á¡¢Ð´²Ù×÷µÈµÈ¡£¾¡¹ÜÕâ¸öÏë·¨ÏÖÐеÄʵÏÖ²»ÍêÃÀ£¬BSD Unixʵ¼ÊÉÏ×öµ½·Ç³£½Ó½üÁË,ÕâÒ²ÕýÊÇBSDÓÖÒ»¸öÇ¿´óµÄµØ·½£­£­¼ò½à¶øÓÅÑÅ¡£ÓÐЩ²»ÊÇÕæʵµÄÎļþ¶øÊÇÉ豸£¬ËüÃǵÄÈë¿ÚÔÚ/devĿ¼Ï£¬ÓÐЩÉ豸ֻÄÜÓÃÓÚÌØÊâµÄ²Ù×÷£¬Èç¿éµÄ¶Á¡¢Ð´£¬Ò»¸ö¼«¶ËµÄÀý×ÓÊÇÒÔÌ«ÍøÉ豸£¬ÉõÖÁËü(ÒÔÌ«ÍøÉ豸)ÔÚFreeBSD 5֮ǰÔÚ/devÏÂÃæûÓÐÈë¿Ú¡£
    Ò»°ãÀ´Ëµ£¬ÎļþÊǼÆËã»úÉÏÊý¾Ý×î»ù±¾¡¢×î³õ¼¶µÄ±íÏÖÐÎʽ£¬±¾ÖÊÉÏÊÇÊý¾ÝһλһλµÄÏßÐÔÐòÁС£µ±ÓÃexecÃüÁîÖ´ÐбàÒë¹ýµÄ³ÌÐòʱ£¬ÏµÍ³½«°Ñ¶þ½øÖÆÎļþ¶ÁÈëÄڴ棬´úÂ뽫ÔÚ·ÖÅäµ½ÄÚ´æµØÖ·¿Õ¼äºó±»Ö´ÐÐ,³ÌÐò±»¶¨Î»ÔÚʲôλÖøúexecÃüÁîÓйأ¬Ëü¿ÉÒÔÔÚÈí´ÅÅÌ£¬Ó²ÅÌ£¬¹âÇý£¬ÉõÖÁÊǼÓÔصÄÁíÒ»°ë»¹·Ö²¼ÔÚÊÀ½çÆäËû½ÇÂäµÄÍøÂçÎļþϵͳÉÏ;¸ú»ùÓÚÍøÂçÁ´Â··¢ËÍÊý¾ÝÒ»Ñù£¬ÄÚÈݱ»Ë³ÐòµØһλһλµØ¶ÁÈë¡£µ±Ò»¸ö³ÌÐò·¢ËÍÊý¾ÝµÄʱºò£¬Êý¾Ý±¾ÉíÊÇһλһλµÄÏßÐÔÐòÁУ¬ÓÐЩʱºò½Ð×ö¡°Á÷¡±£¬³ÌÐò²»¹ØÐÄËüÊÇ·ñ»ùÓÚÍøÂçÁ´Â··¢ËÍÊý¾Ý£¬Ëü½ö½öдÊý¾Ý£»ÕâÁ½ÖÖ×î»ù±¾µÄ²Ù×÷£¬¶Á¸úд£¬ÊǼÆË㼦Óõ½µÄ×î¶àµÄ¡£
    ±¾Õ½«Éæ¼°×î»ù±¾µÄI/O×Óϵͳ¸ú½ø³Ì×ÊÔ´¡£
    5.2 »ù±¾ÊäÈëÊä³ö
    UNIX½ø³Ì´ò¿ªÎļþµÄʱºò£¬»á±£´æÎļþÃèÊö·ûµÄ²Î¿¼Öµ£¬¸ÃÖµÊÇÒ»¸öÕûÊý£¬²»ÂÛºÎʱÔÚUNIXϵͳÉÏ´´½¨Ò»¸ö½ø³Ì£¬¶¼»á¸øËü3¸öÎļþÃèÊö·û£º
    0 ±ê×¼ÊäÈë
    1 ±ê×¼Êä³ö
    2 ±ê×¼´íÎó
    ÕâЩֵÄܹ»ÓÃÓÚÃèÊöÖնˡ¢ÎļþµÄ¶Áд£¬ÉõÖÁÉèÖÃÆäËû½ø³ÌµÄÃèÊö¡£Ê¹shellÖض¨Ïò, cat /etc/hosts >> hosts.out, shell½«´ò¿ªÎļþhosts.out²¢ÇÒ½«cat /etc/hosts×öΪ×Ô¼ºµÄ²ÎÊýÖ´ÐС£²»¹ÜÔõÑù£¬µ±cat½ø³Ìдµ½±ê×¼Êä³öºó(1)£¬½á¹û²»»á±»ttyµÃµ½£¬¶øÊÇÊä³öµ½hosts.outÎļþ¡££¨cat³ÌÐò¸ù±¾²»ÖªµÀдÈëÁËÎļþϵͳµÄÎļþ£¬»¹ÊÇдÈëµ½±ê×¼Êä³öµÄÎļþÃèÊö·û£¬ÎÒÃǽ«ÔÚÕâÕµĺóÃæÒ»µã¿´µ½¾¿¾¹ÊÇÔõôʵÏֵģ©
    ×î»ù±¾µÄÁ½¸ö²Ù×÷ÃèÊö·ûÊÇOpenºÍCloseº¯Êý
    Openº¯Êý
    CODE:[Copy to clipboard]int open(const char *path, int flags, /* mode */ );,
    ³É¹¦µØµ÷ÓÃÕâ¸öº¯Êýºó,Openº¯Êý½«·µ»ØÎļþ¼°²ÎÊýµÄÃèÊö·û£¬Õâ¸öÕûÐÍÃèÊö·ûÔÚ½ø³ÌÎļþÃèÊö±íÄÚÉú³ÉË÷Òý¡£¸ÃÃèÊö·ûµÄ½á¹¹Äܹ»ÈÃÄÚºËÖªµÀÈçºÎ²Ù×÷Õâ¸öÎļþ¡£ÔÚBSDÉÏ£¬Õâ¸ö½á¹¹½Ð×öfiledesc£¬²¢ÇÒÄܹ»ÔÚ/usr/include/sys/filedesc.hÕâ¸öÍ·ÎļþÖÐÕÒµ½¡£µ±½ø³ÌÒªÔÚÕâ¸öÃèÊö·ûÉÏÖ´ÐÐһЩ²Ù×÷µÄʱºò£¬Ëü½«ÒªÇóÎļþÃèÊö·ûΪ¶Á¡¢Ð´¡¢¿ÉÖ´ÐвÙ×÷Ö¸¶¨Ò»¸öÕûÊýÖµ¡£
    Äں˱£´æÁËËùÓÐÎļþÃèÊö·ûµÄ»ù×¼Öµ,ÕâЩ»ù×¼Öµ½«ÔÚʱ½ø³Ì´ò¿ª¡¢¸´ÖÆËü»òÕßÒѾ­´¦ÓÚ´ò¿ª×´Ì¬µÄÎļþÃèÊö·ûÖ´ÐÐexecµ÷ÓᢿçÔ½forkµÄ¹ý³ÌÖÐÔö¼Ó¡£µ±Õâ¸ö²Î¿¼ÖµÎª0µÄʱºò£¬Îļþ¹Ø±Õ¡£Òâ˼ÊÇ£¬Èç¹ûÄãÓÐÒ»¸ö³ÌÐòÖ´ÐÐfork»òÕßexecµ÷Ó㬲¢ÇÒclose-on-execλûÓÐÖ¸¶¨£¬»ù×¼Öµ½«Ôö¼Ó£»²¢ÇÒµ±Ò»¸öеijÌÐòÖ´ÐÐÁËfork»òÕßexecµ÷Ó㬻ù×¼Öµ½«¼ÌÐøÔö¼Ó¡£ËùÒÔÎļþÒ»Ö±´¦ÓÚ´ò¿ª×´Ì¬£¬Ö±µ½»ù×¼Öµ±»ÉèÖÃΪ0£¬»òÕßÖ±µ½ËùÓеĽø³Ì¹Ø±ÕËü£¬»òÕßÍ˳ö¡£
    Closeº¯Êý
    CODE:[Copy to clipboard]int close(int fd);
    µ±½ø³ÌÏëÒƳý»òÕ߹رÕÒ»¸ö´ò¿ªµÄÎļþÃèÊö·ûµÄʱºò£¬Ëü½«µ÷ÓÃcloseº¯Êý¡£Õ⽫¹Ø±ÕÖ¸¶¨µÄÎļþÃèÊö·û£¬²¢ÇÒ¼õÉÙÎļþÃèÊö·ûµÄ²Î¿¼Öµ¡£Õâ¸ö¹ý³ÌºÜÏñ exit--------µ±Ò»¸ö½ø³ÌÖ´ÐÐexit£¬ËùÓдò¿ªµÄÎļþÃèÊö·û¸ú½ø³ÌÒ»Æð±»×Ô¶¯µØ¼õÉÙ£¬×Ô´ÓÎļþ²Î¿¼ÖµÉèΪ0£¬Äں˽«ÊÍ·ÅËùÓеÄÎļþÈë¿ÚµÄ¸±±¾.
    getdtablesizeº¯Êý
    CODE:[Copy to clipboard]int getdtablesize();
    getdtablesizeº¯Êý·µ»ØÎļþÃèÊö±íµÄ´óС£¬ËüÄÜÓÃ×÷¼ì²éϵͳÏÞÖÆ£¬ÄãÒ²Äܹ»ÓÃÏÂÃæµÄÃüÁî×öÏàͬµÄÊÂÇé¡£
    CODE:[Copy to clipboard]bash$ sysctl kern.maxfilesperproc
    kern.maxfilesperproc: 3722
    ÒÀÀµÓÚÄãµÄϵͳ£¬ÄãÄܹ»ÔÚϵͳÔËÐеÄʱºòµ÷ÕûËü£¬»òÕß´ÓбàÒëÄãµÄÄںˣ¬Õâ¸öº¯Êý²»Äܼì²éµ±Ç°½ø³Ì´ò¿ªÁ˶àÉÙÎļþ£¬£¨¸úgetdtablesizeÒ»Ñù£©½ö½ö·µ»ØÄãµÄ½ø³ÌÄܹ»´ò¿ªÎļþµÄ×î´ó¸öÊý¡£
    fcntlº¯Êý
    CODE:[Copy to clipboard]int fcntl(int fd, int cmd, ...);
    fcntl º¯ÊýÔÊÐí½ø³Ì²Ù×÷ÎļþÃèÊö·û£¬fcntlº¯ÊýÖÁÉÙÐèÒªÖ¸¶¨Á½¸ö²ÎÊý,Ò»¸öÓÐЧµÄÎļþÃèÊö·û£¬Ò»¸öÃüÁî¡£¸ù¾ÝʹÓõÄÃüÁ¾ö¶¨fnctlÊÇ·ñÐèÒªµÚÈý¸ö²ÎÊý£¬ÏÂÃæΪÃüÁ²ÎÊýÒåÁËһЩֵ¡£ÔÚFreeBSDÉÏ£¬ÄãÄÜÔÚ/usr/include/fcntl.hÍ·ÎļþÖÐÕÒµ½ËüÃÇ¡£
    CODE:[Copy to clipboard]#define F_DUPFD 0
    F_DUPFDÓÃ×÷´´½¨Ò»¸öеĺÜÏñÔ­Ð͵ÄÎļþÃèÊö·û£¬£¨ÄãÄܹ»ÓÃdupµ÷ÓÃ×öµ½ÏàͬµÄÊÂÇ飬½«ÔÚÍíЩʱºòÉæ¼°µ½£©£¬µ±³É¹¦Ö´ÐдøF_DUPFD±ê¼ÇµÄfcntlº¯Êý£¬fcntl½«·µ»ØÏÂÃæÊôÐÔÖ®Ò»µÄеÄÎļþÃèÊö·û£º
    ¼ÙÈçÖ¸¶¨Á˵ÚÈý¸ö²ÎÊý£¬ÃèÊö·û·µ»ØµÄÖµ±È×îС¿ÉÓõÄÃèÊö·û,¸ÃÖµµÈÓÚ»òÕ߱ȸø¶¨µÄµÚÈý¸ö²ÎÊýµÄÖµ´óÒ»µã,·µ»ØµÄÃèÊö·û½«²ÎÕÕÖ¸¶¨¸øfcntlµÚÒ»¸ö²ÎÊýµÄÎļþÃèÊö·û
    ¼ÙÈçfcntlÖ¸¶¨µÄÎļþÃèÊö·ûÊÇÒ»¸öÎļþ£¬ÐÂÎļþÃèÊö·û½«ÓÐÏàͬµÄÎļþÆ«ÒÆÁ¿£¬²¢ÇÒÐÂÎļþÃèÊö·û½«ÓÐÏàͬµÄ·ÃÎÊ·½Ê½(È磺O_RDONLY,O_RDWR,o_WRONLY)
    ÐÂÎļþÃèÊö·û½«¹²ÏíÎļþ״̬±ê¼Ç
    ÐÂÎļþµÄ¡°close-on-exec¡±±êÖ¾½«±»¹Ø±Õ£¬¼´ÐµÄÎļþÃèÊö·ûÔÚexecµ÷ÓÃÖ®ºóÈÔ½«±£³Ö´ò¿ª¡£
    F_GETFDÃüÁî
    CODE:[Copy to clipboard]#define F_GETFD 1
    F_GETFD ÃüÁîÓÃ×÷»ñÈ¡"close-on-exec"±ê¼ÇµÄ״̬,¸úFD_CLOSEXECÏàÓëºó·µ»ØֵҪôΪ0£¬ÒªÃ´Îª1¡£Èç¹û·µ»Ø0£¬close-on- exec±ê¼ÇûÓб»ÉèÖùý£¬ÄÇôÎļþÃèÊö·û½«±£³Öµ÷Óý»²æÖ´Ðе÷ÓÃ(so the file descriptor will remain open across exec calls.),¼ÙÈçÊÇ1£¬close-on-exec±êÖ¾±»ÉèÖùý£¬ÎļþÃèÊö·û½«Ôڳɹ¦µ÷ÓÃÒ»¸öexecº¯Êýºó±»¹Ø±Õ¡£
    F_SETFDÃüÁî
    CODE:[Copy to clipboard]#define F_SETFD 2
    F_SETFDÃüÁîÓÃ×÷ÉèÖÃÎļþÃèÊö·ûµÄclose-on-exec±ê־λ¡£µÚÈý¸ö²ÎÊýҪôÊÇFD_CLOEXEC£¬ÉèÖÃclose-on-exec±ê¼Ç£¬ÒªÃ´ÊÇ0£¬È¡Ïûclose-on-exec±ê¼ÇµÄÉèÖá£
    F_GETFLºÍF_SETFLÃüÁî
    CODE:[Copy to clipboard] #define F_GETFL 3
    #define F_SETFL 4
    F_GETFLÃüÁʹfcntl·µ»Øµ±Ç°ÎļþÃèÊö·û״̬±ê¼Ç,µ±·µ»ØÖµ¼ÓÉÏO_ACCMODE(#define O_ACCMODE 0x0003)Äܹ»»ñÈ¡µ½´ò¿ªµÄ·½Ê½£¬F_SETFLÃüÁ¸ù¾ÝµÚÈý¸ö²ÎÊýÉèÖÃÎļþ״̬±ê¼Ç¡£
    ¹«¹²µÄ±ê¼Ç
    ÏÂÃæÕâЩ±ê¼ÇÒ²ÓÃ×÷µ÷ÓÃopen£¬²¢ÇÒÖ»Äܱ»¸úÉÏÆÚÍûµÄ±ê¼Çµ÷ÓÃopenº¯ÊýÉèÖã¬ÕâÊÇ×î³£¼ûµÄ£¬¼ì²éÄãϵͳµÄÍ·ÎļþÄܹ»²éµ½ÄÇЩֵ
    CODE:[Copy to clipboard]#define O_RDONLY 0x0000
    Èç¹ûO_RDONLY±ê¼Ç±»ÉèÖã¬ÄÇôÎļþÖ»ÄÜÒÔÖ»¶Á·½Ê½´ò¿ª¡£×¢ÒâÕâ¸öO_RDONLY±ê¼ÇÖ»ÄÜÔÚ´ò¿ªµÄʱºò±»ÉèÖã¬Ëü²»Äܱ»fnctl¼ÓF_SETFLÃüÁîÉèÖá£
    CODE:[Copy to clipboard]#define O_WRONLY 0x0001
    Èç¹ûO_WRONLY±ê¼Ç±»ÉèÖã¬ÄÇôÎļþÖ»ÄÜÒÔֻд·½Ê½´ò¿ª¡£Õâ¸ö±ê¼ÇÖ»Äܱ»openÉèÖ㬲»Äܱ»fcntl¼ÓF_SETFLÃüÁîÉèÖá£
    CODE:[Copy to clipboard]#define O_RDWR 0x0002
    Èç¹ûO_RDWR±ê¼Ç±»ÉèÖã¬ÄÇôÎļþÒԿɶÁ¿Éд·½Ê½´ò¿ª¡£Õâ¸ö±ê¼ÇÒ²Ö»Äܱ»openµ÷ÓÃÉèÖá£
    CODE:[Copy to clipboard]#define O_NONBLOCK 0x0004
    Èç¹ûO_NONBLOCK±ê¼Ç±»ÉèÖã¬ÎļþÃèÊö·û½«²»±»×èÈû¶ø±»Ö±½Ó·µ»ØÌæ´ú¡£Ò»¸öÀý×ÓÊÇ´ò¿ªtty¡£Èç¹ûÓû§²»ÔÚÖն˵÷ÓÃÀïÊäÈëÈκζ«Î÷£¬read½«±»×èÈû£¬Ö±µ½Óû§ÓÐÊäÈ룬µ±O_NONBLOCK±ê¼Ç±»ÉèÖã¬readµ÷Óý«Ö±½Ó·µ»ØÉèÖõ½EAGAINµÄÖµ
    CODE:[Copy to clipboard]#define O_APPEND 0x0008
    Èç¹ûO_APPEND±ê¼Ç±»ÉèÖã¬Îļþ½«ÒÔ×·¼Ó·½Ê½´ò¿ª£¬²¢ÇÒ½«´ÓÎļþĩβ¿ªÊ¼Ð´Èë¡£
    CODE:[Copy to clipboard]#define O_SHLOCK 0x0010
    Èç¹ûO_SHLOCK±ê¼Ç±»ÉèÖã¬ÎļþÃèÊö·û½«ÔÚÎļþÉÏÉú³ÉÒ»¸ö¹²ÏíËø,ÔÚÎļþÉÏÉèÖÃÁ˹²ÏíËø£¬¶à¸ö½ø³ÌÄÜÔÚͬһ¸öÎļþ¹»Ö´ÐвÙ×÷£¬Îļþ¹²ÏíËøµÄÏêϸÐÅÏ¢£¬¿ÉÓÿ´fnctlº¯ÊýµÄF_GETLK¸úF_SETLÃüÁî¡£
    CODE:[Copy to clipboard]#define O_EXLOCK 0x0020
    Èç¹ûO_EXLOCK±ê¼Ç±»ÉèÖã¬ÎļþÃèÊö·û½«ÔÚÎļþÉÏÉú³ÉÒ»¸ö¿ÉÖ´ÐÐËø£¬Ò»Ñù£¬¸üÏêϸµÄÃèÊö¿ÉÒÔ²ÎÕÕfcntlº¯ÊýµÄF_SETLK¸úF_GETLKÃüÁî¡£
    CODE:[Copy to clipboard]#define O_ASYNC 0x0040
    Èç¹ûO_ASYNC±ê¼Ç±»ÉèÖ㬽ø³Ì¼¯½«±»·¢Ë͵ÄSIGIOÐźÅ֪ͨ£¬ÔÚÎļþÃèÊö·ûºÅµÄIOÊÇ¿ÉÓõġ£ÏêϸµÄÃèÊöÇë²ÎÕÕÐźÅÄÇÒ»Õ¡£
    CODE:[Copy to clipboard]#define O_FSYNC 0x0080
    Èç¹ûO_FSYNC±ê¼Ç±»ÉèÖã¬ËùÓÐдµ½ÎļþÃèÊö·ûµÄд²Ù×÷½«²»±»Äں˻º´æ£¬È¡¶ø´úÖ®µÄÊǽ«±»Ð´µ½½éÖÊ£¬²¢ÇÒËùÓеÄдµ÷Óö¼½«±»×èÈû£¬Ö±µ½ÄÚºËÍê³É£¨Ð´²Ù×÷£©¡£
    CODE:[Copy to clipboard]#define O_NOFOLLOW 0x0100
    Èç¹ûO_NOFOLLOW±ê¼Ç±»ÉèÖ㬼ÙÈçÎļþÊÇÒ»¸ö·ûºÅÁ¬½Ó£¬openµ÷Óý«»áʧ°Ü¡£Èç¹ûÔÚÒ»¸öÓÐЧµÄÎļþÃèÊö·ûÉÏÉèÖÃÁËÕâ¸ö±êÖ¾£¬ÄÇôµ±Ç°Îļþ¾Í²»ÊÇÒ»¸ö·ûºÅÁ¬½Ó¡£
    CODE:[Copy to clipboard]#define O_CREAT 0x0200
    Èç¹ûO_CREAT±ê¼Ç±»ÉèÖ㬼ÙÈçÖ´ÐÐopenµ÷ÓõÄʱºòÎļþ²»´æÔÚ£¬ÄÇôÎļþ¿ÉÓñ»´´½¨¡£(Õâ¸ö´íÎóµÄƴдºÜÓÐȤ£»when one of the original creators of C was asked "What one thing would you change about C?" he replied, "I would change O_CREAT to O_CREATE!", or at least how the rumor goes)
    CODE:[Copy to clipboard]#define O_TRUNC 0x0400
    Èç¹ûO_TRUNC±ê¼Ç±»ÉèÖ㬳ɹ¦µØµ÷ÓÃopenºóÎļþ½«±»½Ø³ý¡£
    CODE:[Copy to clipboard]#define O_EXCL 0x0800
    µ±O_EXCL±ê¼Ç±»ÉèÖ㬼ÙÈçÎļþÒѾ­´æÔÚ£¬openµ÷Óý«²úÉúÒ»¸ö´íÎó¡£
    CODE:[Copy to clipboard]#define F_GETOWN 5
    F_GETOWNÃüÁîÓÃÓÚÃèÊö·û»ñÈ¡µ±Ç°½ø³Ì»òÕß½ø³Ì¼¯ÊÕµ½µÄSIGIOÐźš£Èç¹ûÕâ¸öÖµÊÇÒ»¸öÕýÊý£¬Ëü±íʾһ¸ö½ø³Ì£¬¸ºÊý±íʾһ¸ö½ø³Ì¼¯¡£
    CODE:[Copy to clipboard]#define F_SETOWN 6
    µ±IO¾ÍÐ÷µÄʱºò£¬F_SETOWNÃüÁîÓÃ×÷ÉèÖýø³Ì»òÕß½ø³Ì¼¯Ê¹Æä½ÓÊÕSIGIOÐźţ¬Ö¸¶¨Ò»¸ö½ø³Ì£¬ÓÃÒ»¸öÕýÊý£¨Ò»¸ö½ø³ÌID£©×÷ΪfcntlµÄµÚÈý¸ö²ÎÊý£¬·ñÔò£¬ÓÃÒ»¸ö¸ºÊý×÷ΪfcntlµÄµÚÈý¸ö²ÎÊýÖ¸¶¨½ø³Ì¼¯¡£
    5.3ÎļþÉÏËø
    µ±¶à¸ö½ø³ÌÊÔͼдͬһ¸öÎļþ£¬½«·¢Éúʲô£¿ËüÃÇÏ໥³åÍ»£¬ÒÑÖªµÄÊÂÇéÏñÎļþÉÏËø¡£½á¹û¾ÍÊÇÿ¸öÎļþÃ趼ÓÐ×Ô¼ºµÄÃèÊö·û¸úÆ«ÒÆÁ¿£¬µ±Ã¿¸ö½ø³Ìд×Ô¼ºµÄÎļþʱ£¬Æ«ÒÆÁ¿Ô¤ÏȶÀÁ¢µ¼ÖÂûÓнø³ÌÖªµÀÆäËûµÄ½ø³ÌÒ²ÕýÔÚÖ´ÐÐд²Ù×÷¡£×îºóµÄÎļþ½«ÒòΪ¶à¸ö¶ÀÁ¢Ð´ÎļþµÄ²Ù×÷ʹ»ìºÏºóµÄÎļþ±äµÃÏ൱ÓÚÀ¬»ø£¬Ö±½Ó¸øÎļþÉÏËøÊǽâ¾öÕâ¸öÎÊÌâµÄÒ»ÖÖ·½Ê½¡£ÔÚÈÎÒâʱ¿ÌÖ»ÄÜÈÃÒ»¸ö½ø³ÌÄܹ»Ð´µ½Îļþ£¬ÁíÒ»ÖÖ°ì·¨ÊÇÔÊÐíÔÚÒ»¸ö½Ð×ö¸ß¼¶ÎļþËøµÄschemeÀïµÄÎļþÄÚ²¿½øÐÐÇøÓòËø¶¨¡£ fcntlº¯ÊýÄܹ»ÌṩÕâ¸ö¹¦ÄÜ£¬Í¨³£À´Ëµ£¬ËøÓÐÁ½ÖÖ£¬Ò»ÖÖÊÇд£¬ÁíÒ»ÖÖÊÇд£¬²»Í¬Ö®´¦ÔÚÓÚ¶ÁËø²»»á¸ÉÈÅÆäËü½ø³Ì¶ÁÈ¡Îļþ£¬µ«ÊÇÌض¨µÄÇøÓòÖ»ÄÜÒ»¸öдËø´æÔÚ¡£
    µ±Ê¹ÓùËÎÊËøµÄʱºò£¬ÏÂÃæµÄ½á¹¹ÓÃ×÷fcntlµÄµÚÈý¸ö²ÎÊý¡£
    CODE:[Copy to clipboard] struct flock {
    off_t l_start; /* starting offset */
    off_t l_len; /* len = 0 means until end of file */
    pid_t l_pid; /* lock owner */
    short l_type; /* lock type: */
    short l_whence; /* type of l_start */
    };
    ÈÃÎÒÃǼÌÐøÌÖÂÛÿ¸öÔªËصÄϸ½Ú¡£
    l_start
    ÕâÊÇÒ»¸öÏà¶ÔÓÚl_whence,µ¥Î»Îª×Ö½ÚµÄÆ«ÒÆÁ¿£¬»»¾ä»°Ëµ£¬ÒªÇóµÄλÖÃʵ¼ÊÉÏÊÇl_whence + l_start
    l_len
    ÐèÉèÖÃΪÆÚÍûλÖõij¤¶È£¬µ¥Î»Îª×Ö½Ú£¬Ëø½«´Ól_whence + l_start¿ªÊ¼Ëø¶¨l_len×Ö½Ú£¬Èç¹ûÄãÏëÕû¸öÎļþÓÃÒ»°ÑËø£¬ÄÇôÉ趨l_lenµÄֵΪ0£¬Èç¹ûl_lenµÄÖµÊÇÒ»¸ö¸ºÊý£¬½á¹ûÊDz»¿ÉÔ¤²âµÄ¡£
    l_pid
    ÐèÒªÉèÖÃΪ¹¤×÷ÔÚËøÉϵĽø³ÌµÄID
    l_type
    ÐèÒªÉèÖÃΪÆÚÍûµÄËøµÄÀàÐÍ£¬ÏÂÃæÊÇÄܹ»Ê¹ÓõÄÖµ
    * F_RDLCK - ¶ÁËø¶¨
    * F_WRLCK - дËø¶¨
    * F_UNLCK - ÓÃ×÷Çå³ýËø¶¨
    l_whence
    ÕâÊÇÕâ¸öϵͳµ÷ÓÃÀïÃæ×î»ìÂҵIJ¿·Ö£¬Õâ¸ö×ֶν«¾ö¶¨l_startλÖõÄÆ«ÒÆÁ¿£¬ÐèÒªÉèΪ£º
    * SEEK_CUR - ÔÚµ±Ç°Î»ÖÃ
    * SEEK_SET - ÔÚÎļþ¿ªÊ¼
    * SEEK_END - ÔÚÎļþĩβ
    fcntlµÄÃüÁî
    ÏÂÃæµÄ¿ÉÓÃ×÷fcntlµÄÃüÁî
    CODE:[Copy to clipboard]#define F_GETLK 7
    F_GETLK ÃüÁî³¢ÊÔ¼ì²é·ñÄÜÉÏËø£¬µ±Ê¹ÓÃÕâ¸öÃüÁfnctl½«¼ì²éÊÇ·ñÓÐÏà³åÍ»µÄËø£¬Èç¹û´æÔÚÏà³åÍ»µÄËø£¬fnctl½«¸Äдflock½á¹¹£¬ÓóåÍ»ËøÏûϢͨ¹ý¼ì²é£¬Èç¹ûûÓÐÏà³åÍ»µÄËø£¬ÄÇôÔÚflock½á¹¹×î³õµÄÐÅÏ¢½«±»±£Áô£¬³ý·Çl_type×ֶα»Éè³ÉF_UNLCK
    CODE:[Copy to clipboard]#define F_SETLK 8
    F_SETLKÃüÁîÊÔͼ»ñµÃflock½á¹¹ÃèÊöµÄËø£¬Èç¹ûËø²»±»³ÐÈÏ£¬±¾´Îµ÷Óý«²»±»×èÈû¡£²»¹ÜÔõÑù£¬fcntl½«Ö±½Ó·µ»ØEAGAIN£¬Í¬Ê±½«ÉèÖÃÏàÓ¦µÄerrno£¬µ±flock½á¹¹µÄl_type±»ÉèÖÃΪF_UNLCKʱ£¬ÄãÄܹ»Ê¹ÓÃÕâ¸öÃüÁîÇå³ýÒ»¸öËø¡£
    CODE:[Copy to clipboard]#define F_SETLKW 9
    F_GETLKÃüÁîÊÔͼ»ñµÃflock½á¹¹ÃèÊöµÄËø£¬Ëü½«ÃüÁîfnctl×èÈû£¬Ö±µ½¸³ÓèÒ»¸öËø
    5.4ΪʲôÓÃFLOCK
    ¶Ô´ó²¿·ÖÇé¿öÀ´Ëµ£¬¸ß¼¶ÎļþËø¶¨»úÖÆÊÇÓкô¦µÄ¡£È»¶ø£¬POSIX.1½Ó¿ÚÓм¸¸öȱµã¡£µÚÒ»£¬µ±Ò»¸öÎļþµÄÈκÎÒ»¸öÎļþÃèÊö·û±»¹Ø±Õʱ£¬Óë¸ÃÎļþ¹ØÁªµÄËùÓÐËø¶¨±ØÐ뱻ɾ³ý¡£»»¾ä»°Ëµ£¬Èç¹ûÄãÓÐÒ»¸ö½ø³Ì´ò¿ªÒ»¸öÎļþ£¬½Ó×ÅËüµ÷ÓÃÒ»¸öº¯Êý´ò¿ªÍ¬Ò»¸öÎļþ£¬¶ÁÈ¡È»ºó¹Ø±ÕËü£¬ÕâÑùÏÈÇ°Äã¶ÔÕâ¸öÎļþµÄËùÓÐËø¶¨¶¼»á±»É¾³ý¡£Èç¹ûÄã²¢²»È·¶¨Ò»¸ö¿âÀý³Ì»á×öʲô£¬Õ⽫»áÒýÆðÑÏÖØÎÊÌâ¡£µÚ¶þ£¬Ëø¶¨ÊDz»»á´«µÝ¸ø×Ó½ø³ÌµÄ£¬ËùÒÔÒ»¸ö×Ó½ø³Ì±ØÐë¶ÀÁ¢µØ´´½¨ÊôÓÚËü×Ô¼ºµÄËø¶¨¡£µÚÈý£¬ËùÓÐÔÚÒ»¸öexecµ÷ÓÃ֮ǰ»ñµÃµÄËø¶¨ÔÚÓÉexecÆô¶¯µÄ½ø³ÌÊÍ·Å£¬¹Ø±ÕÕâ¸öÎļþ»ò½áÊø֮ǰ²»»á±»ÊÍ·Å¡£ËùÒÔ£¬Èç¹ûÄãÐèÒªËø¶¨ÎļþµÄijһ²¿·Ö£¬ÄÇôµ÷ÓÃexec²»ÐèÒªÊÍ·ÅËø¶¨»òÕ߹رÕÎļþÃèÊö·û£¬ÄDz¿·ÖÇøÓò½«±»Ëø¶¨Ö±µ½½ø³Ì½áÊø£¬ÄãÏëÒªµÄ»òÐí²»ÊÇÔ¤ÆڵĽá¹û£¬²»ÂÛÈçºÎ£¬BSDµÄÉè¼ÆÕßÓÃÐí¶àflock´´½¨Á˷dz£¼òµ¥µÄÓÅÏȼ¶ÎļþÉÏËøµÄ½Ó¿Ú.
    flockÓÃÓÚËø¶¨Õû¸öÎļþ£¬ÊÇBSDÓÅÏÈÑ¡ÔñµÄ·½·¨£¬¸úfcntl¸ß¼¶Ëø¶¨Ïà·´£¬flock»úÖÆÔÊÐíËø¶¨½øÈë×Ó½ø³Ì£¬Ê¹ÓÃflockµ÷ÓõÄÆäËûºÃ´¦ÊÇ¿ÉÒÔÔÚÎļþ¼¶²¢ÇÒ²»ÔÚÎļþÃèÊö·û¼¶±ðÍê³ÉËø¶¨£¬ÔÚijЩÇé¿öÏ¿ÉÓÃÓÅÏÈÑ¡Ôñ£¬Òâζ×Ŷà¸ö²ÎÕÕͬһ¸öÎļþµÄÎļþÃèÊö·û£¬ÀýÈçDUP()µ÷Ó㬻òÕ߶à¸öOPEN()µ÷Óã¬Ï£Íûÿһ¸ö²ÎÕÕÏàͬµÄÎļþËø£¬´øflockµÄÎļþËø¸úÒ»¸öд¶à¸ö¶ÁµÄfcntlËøºÜÀàËÆ£¬²»ÂÛÈçºÎ£¬µ±ÏÂÃæµÄ²Ù×÷±»¶¨Òå,µ÷ÓÃflockʱ£¬ËøµÄÓÅÏȼ¶Äܹ»±»ÌáÉý£º
    CODE:[Copy to clipboard]#define LOCK_SH 0x01 /* shared file lock */
    LOCK_SH²Ù×÷ÓÃÓÚÔÚÎļþÉÏ£¨ÀàËÆfcntlµÄ¶ÁËø£©´´½¨¹²ÏíËø£¬ÔÚÒ»¸öÎļþÉÏ£¬¶à¸ö½ø³ÌÄܹ»¹²ÏíÒ»°ÑËø¡£
    CODE:[Copy to clipboard]#define LOCK_EX 0x02 /* exclusive file lock */
    LOCK_EX²Ù×÷ÓÃÓÚÔÚÎļþÉÏ´´½¨»¥³âËø£¬µ±»¥³âËøÉúЧʱ£¬ÎļþÉϲ»ÄÜ´æÔÚÆäËûµÄ¹²ÏíËø£¬°üÀ¨Òѹ²ÏíµÄËø¡£
    CODE:[Copy to clipboard]#define LOCK_NB 0x04 /* don't block when locking */
    ʹÓÃÕâ¸ö£¬flockµÄµ÷Óý«×èÈû£¬Ö±µ½Ëø¶¨ÉúЧ£¬²»ÂÛÈçºÎ£¬¼ÙÈçÆÚÍûµÄ²Ù×÷LOCK_NBÊÇORed£¬flockµÄµ÷Óý«ÏòEWOULDBLOCK·µ»Ø´íÎóºÅ³É¹¦£¨0£©»òÕßʧ°Ü£¨1£©.
    CODE:[Copy to clipboard]#define LOCK_UN 0x08 /* unlock file */
    LOCK_UNÓÃÓÚÒƳýÎļþÉϵÄËø
    ͨ¹ýµ÷ÓÃÆÚÍûµÄflock£¬flockËøµÄÓÅÏȼ¶Äܹ»±»ÌáÉý»òÕß½µµÍ¡£Ðµijɹ¦µÄµ÷Óý«Ê¹×î½üÉúЧµÄËøÌæ»»ÏÈÇ°µÄËø¡£
    DUPº¯Êý
    CODE:[Copy to clipboard]int dup(int old);
    Ïñfcntlµ÷ÓÃÄܹ»ÓÃ×÷ΪÏÖÓеÄÎļþÃèÊö·û¸´ÖÆÃèÊö·ûÒ»Ñù£¬dupº¯ÊýÒ²Äܸ´ÖÆÎļþÃèÊö·û,dupµ÷ÓÃÄܹ»·µ»ØÒ»¸ö¸úold²ÎÊýÎÞ·¨Çø±ðµÄеÄÎļþÃèÊö·û£¬Õâ¾ÍÒâζ×ÅËùÓеÄread(),write()¸úlseek()µ÷Óö¼»á²Ù×÷Á½¸öÃèÊö·û£¨¸´ÖƳöÀ´µÄеĸúÔ­À´µÄÃèÊö·û£©,ͬʱ£¬ËùÓÐfcntlµÄÑ¡Ï±£Áô,close on execλ³öÍâ, close on execλ»á±»¹Ø±Õ£¬ËùÒÔÄã¿ÉÒÔ¸´ÖÆÒ»¸öÎļþÃèÊö·û£¬È»ºóÔÊÐí×Ó½ø³ÌÈ¥µ÷ÓÃÒ»¸öexecº¯Êý£¬ÕâÊÇdupº¯Êý·Ç³£ÆÕ±éµÄÒ»¸öÓ÷¨¡£old²ÎÊýÓÃ×÷¸´ÖÆ£¬²¢ÇÒ±ØÐëÊÇÒ»¸öÓÐЧµÄ²ÎÊý£¬ÃèÊöÄ¿±êÃèÊö·û£¬ÐÂÎļþ·ûÖÁÉÙÓ¦¸ÃÊÇûʹÓùýµÄÎļþÃèÊö·û£¬ËüÓɳɹ¦µ÷ÓÃdupº¯Êýºó·µ»Ø¡£ËüÒâζ×ÅÈç¹ûÄã¹Ø±ÕSTDIN_FILENO(¸ÃֵΪ0)ÄÇôdupÖ±½Óµ÷ÓõÄÐÂÎļþÃèÊö·ûµÄÖµ½«ÊÇSTDIN_FILENO,¼ÙÈçdupº¯Êýµ÷ÓÃÒòһЩԭÒòʧ°Ü£¬½«·µ»Ø-1²¢ÇÒÏàÓ¦µØÉèÖÃerrno.
    DUP2º¯Êý
    CODE:[Copy to clipboard]int dup2(int old, int new);
    ³ýÁËnew²ÎÊýÊÇÏ£ÍûµÄÄ¿±êÖµ£¬dup2º¯Êý¸údupº¯ÊýºÜÏàËÆ¡£¼ÙÈçвÎÊýÒѾ­²ÎÕÕÁËÒ»¸öÓÐЧ´ò¿ªµÄÃèÊö·û,²¢ÇÒËüµÄÖµ¸úÀϲÎÊýµÄÖµ²»Ò»Ñù£¬ÄÇôÐÂÎļþÃèÊö·û½«Ïȱ»¹Ø±Õ¡£¼ÙÈçвÎÊýµÈÓÚÀϲÎÊý£¬ÄÇôº¯Êý²»»á×öÈκβÙ×÷¡£³É¹¦µ÷ÓÃDUP2µÄ·µ»ØÖµ½«µÈÓÚвÎÊý¡£¼ÙÈçDUP2µ÷ÓÃʧ°Ü£¬½«·µ»Ø-1²¢ÇÒÏàÓ¦µØÉèÖÃerrno.
    5.5 ½ø³Ì¼äͨÐÅ
    À´×ÔÓÚSystem VµÄÖ÷ÒªµÄ¹¦ÄÜÊÇ»ù±¾µÄ½ø³Ì¼äͨÐÅ£¬»òÕß˵IPC£¬ÕâЩһֱÔÚBSDÀïÃæ±»·Ç³£¹ã·ºµØʹÓá£IPC»úÖÆÔÊÐí³ÌÐòÖ®¼äÏ໥¹²ÏíÊý¾Ý¡£Õâ¸öºÜÏñÎÒÃÇÒѾ­½éÉܹýµÄÖض¨Ïò£¬µ«ÊÇÖض¨ÏòÊǵ¥Ïò´¦Àí¶ø²»ÊÇË«Ïò´¦Àí¡£ÔÚʾ·¶³ÌÐòÖУ¬Öض¨Ïò¿ÉÒÔ¸úÉèÖùýSTDIN_FILENO²ÎÊýµÄCATÃüÁî¹²ÏíÊý¾Ý£¬µ«ÊÇÓÐÒ»¸öÎÊÌ⣺catÃüÁî²»ÄܸúÖض¨ÏòµÄ³ÌÐò¹²ÏíÊý¾Ý£¬¼ÙÉèÒ»¸öËã·¨ÊÇ´ÓÆäËûÎļþÃèÊö·û¶Áµ«ÊÇÓÃopen²»Áé»îµÄʱºò£¬ÎÒÃÇ¿ÉÒÔÐÞ¸ÄËûÃÇʹÆä¿ÉÒÔË«ÏòµØÏ໥¹²ÏíÊý¾Ý¡£BSDÌṩÁ˺ܶà¸üºÃµÄÓÃÓÚ½ø³Ì¼äͨÐŵķ½·¨¡£
    PIPEº¯Êý
    CODE:[Copy to clipboard]int pipe(int *array);
    ͨ¹ý¸ø¶¨Ò»¸öÓÐЧµÄ¶þάÊý×éµ÷ÓÃpipeº¯Êý£¨È磺int array[2]£©£¬pipeº¯Êý½«·ÖÅäÁ½¸öÎļþÃèÊö·û£¬¼ÙÈç³É¹¦£¬Êý×齫°üº¬Á½¸öÓÐÇø±ðµÄ²¢ÇÒÔÊÐíµ¥ÏòͨÐŵÄÎļþÃèÊö·û, ´ò¿ªµÄµÚÒ»¸öÎļþÃèÊö·û(array[0])ÓÃÓÚ¶Á,´ò¿ªµÄÁíÒ»(array[1])¸öÓÃÓÚд£¬ËùÒÔ£¬×Դӳɹ¦µ÷ÓÃpipeº¯Êýºó,ÄãÄÜÔÚÕâÁ½¸öÃèÊö·ûÖ®¼äµÃµ½Ò»¸öµ¥ÏòͨÐÅͨµÀ,µ±Ð´ÈëÆäÖÐÒ»¸öʱ£¬ÄãÒ²½«Äܹ»´ÓÁíÒ»¸ö¶ÁÈ¡Êä³ö,¹ÜµÀº¯Êý±ÈÖض¨ÏòºÃµÄµØ·½ÊÇÄã²»±ØʹÓÃÎļþ,
    ÕâЩÎļþÃèÊö·ûµÄÐÐΪÓëÆÕͨµÄÎļþÃèÊö·ûÍêÈ«Ò»Ñù£¬È»¶øËüÃÇûÓÐÈκÎÎļþÓëÖ®¹ØÁª£¬¹ÜµÀ¹¦ÄÜʹunix shell¼Óµ½ÆäËûÃüÁîµÄ¹ÜµÀ±äµÃºÜÓÐÓÃ,ÀýÈç
    CODE:[Copy to clipboard]bash$ find / -user frankie | grep -i jpg | more
    Õâ¸öÀý×ÓÀïÃ棬findÃüÁÆäÊä³öͨ¹ý¹ÜµÀ´«Ë͸øgrepÃüÁgrepÔÙ½«ÆäÊä³öͨ¹ý¹ÜµÀ´«Ë͸ømoreÃüÁî¡£µ±½¨Á¢Õâ¸ö˳ÐòµÄʱºò£¬shell½«´¦Àí¹ÜµÀʵ¼ÊµÄÉèÖã¬ÕâЩ³ÌÐò±¾ÉíûÓÐдµ½ÆäËû³ÌÐòµÄÏë·¨£¬ÒòΪËûÃÇÕæµÄ²»ÐèÒªÖªµÀ¡£´ÓÕâ¸öÀý×ÓÄãÄÜ¿´¼û×Ô´Óµ÷ÓÃÁ˹ܵÀ£¬Õý³£À´Ëµ½ø³Ì¾Í»áfork, Ö®ºó½ø³Ì¾ÍÄÜͨÐÅ£¬¼ÙÈçÒª´ïµ½Ë«ÏòͨÐŵÄÄ¿±ê£¬ÄãÐèÒª´´½¨Á½¸ö¹ÜµÀ£¬Ò»¸öÓÃÓÚ¸¸½ø³Ìµ½×Ó½ø³ÌµÄͨÐÅ£¬ÁíÒ»¸öÓÃ×÷×Ó½ø³Ìµ½¸¸½ø³ÌµÄͨÐÅ¡£
    ¹ÜµÀÓÐÏÂÃæÁ½ÌõͨÐŹæÔò£º
    1.Èç¹û¹ÜµÀ¶ÁµÄÕâ±ß¹Ø±ÕÁË£¬ÊÔͼдµ½Õâ¸ö¹ÜµÀ½«»áµ¼ÖÂÒ»¸öSIGPIPEÐźŷ¢Ë͸øÊÔͼдµÄ½ø³Ì¡£
    2.Èç¹û¹ÜµÀдµÄÕâ±ß¹Ø±ÕÁË£¬ÊÔͼ´ÓÕâ¸ö¹ÜµÀ¶Á½«µ¼Ö¶Á·µ»Ø0»òÎļþ½áÊø¡£¹Ø±Õд¶ËÊÇ·¢ËÍÎļþ½áÊøµ½¸Ã¹ÜµÀ¶Á¶ËµÄΨһ·½·¨¡£
    ³É¹¦µØµ÷ÓÃpipeº¯Êýºó½«·µ»Ø0£¬¼ÙÈçµ÷ÓÃʧ°Ü£¬½«·µ»Ø-1²¢ÇÒerrno½«±»ÏàÓ¦µØÉèÖã¬
    ±¸×¢£ºÔÚ¸ü¶àµÄÏÖ´úµÄBSDÉÏ£¬µ¥¸öÃèÊö·ûÉÏpipeº¯ÊýÖ§³ÖË«ÏòͨÐÅ£¬²»ÂÛÈçºÎ£¬Õâ¸öÌØÐÔ²»ÊǺÜÇá±ã£¬Ò²ÒòΪÈç´Ë£¬Õâ¸ö·½Ê½²»½¨ÒéʹÓá£
    Mkfifoº¯Êý
    CODE:[Copy to clipboard]int mkfifo(const char *path, mode_t mode);
    ÔÚÏà¹ØµÄ½ø³Ì¼äͨÐÅʱ£¬¹ÜµÀÊÇÓÐÓõģ¬È»¶øÔÚûÓйØÁª¹ØϵµÄ½ø³Ì¼äͨÐÅʱ£¬Ê¹ÓÃmkfifoº¯Êý¡£Mkfifoº¯Êýʵ¼ÊÉÏÔÚÎļþϵͳ´´½¨Ò»¸öÎļþ¡£Õâ¸öÎļþÖ»ÊÇÆäËûÎļþÔÚͨÐÅʱʹÓõÄÒ»¸ö±êÖ¾¡£ÕâЩÎļþ¾Í½Ð×öFIFO¹ÜµÀ£¨ÏȽøÏȳö¹ÜµÀ£©¡£µ±Ò»¸ö½ø³Ì´´½¨Ò»¸öFIFO¹ÜµÀ£¬Ð´µ½Õâ¸öFIFO¹ÜµÀ²¢²»»áдµ½Õâ¸öÎļþ£¬¶øÊDZ»ÁíÒ»¸ö½ø³Ì¶ÁÈ¡¡£ÕâÖÖÐÐΪÓë¹ÜµÀ·Ç³£ÏàËÆ£¬ËùÒÔFIFOÒ²±»½Ð×öÃüÃû¹ÜµÀ¡£Mkfifoº¯ÊýÓÐÁ½¸ö²ÎÊý¡£µÚÒ»¸ö²ÎÊýÊÇÒ»¸öÒÔnull½áβµÄ×Ö·û´®£¬ÉùÃ÷·¾¶ºÍÎļþÃû¡£µÚ¶þ¸ö²ÎÊýÊǸÃÎļþµÄ´æȡģʽ¡£¸Ã´æȡģʽÊDZê×¼µÄunixÎļþËùÓÐÕ߶ÁдȨÏÞ£¨²Î¿¼/usr/include/sys/stat.hÖеÄS_IRUSR, S_IRGRPµÈ£©¡£
    Ò»µ©mkfifoº¯Êý³É¹¦µ÷Óã¬ÐèÒªÓÃopenº¯Êý´ò¿ª´´½¨µÄfifo¹ÜµÀ½øÐжÁд¡£Èç¹ûµ÷ÓÃʧ°Ü£¬»á·µ»Ø-1¶øÇÒ´íÎó´úÂë»á±»ÏàÓ¦µØÉèÖá£
    ´´½¨fifo¹ÜµÀÓë´´½¨ÎļþÏàÀàËÆ£¬½ø³ÌÒ²±ØÐëÓÐ×ã¹»µÄȨÏÞÀ´´´½¨fifo¹ÜµÀ£¬ÒòΪ¸ÃfifoµÄÓû§ID»á±»ÉèÖÃΪ¸Ã½ø³ÌµÄÓÐЧÓû§ID£¬¶ø×éID»á±»ÉèÖÃΪ¸Ã½ø³ÌµÄÓÐЧ×éID¡£
    ¹ØÓÚFIFOÖØÒªµÄÒ»µãÊÇ£¬ÔÚȱʡÇé¿öÏ£¬ËüÃÇÊÇ×èÈûµÄ¡£Òò´Ë£¬¶ÁÈ¡Ò»¸öfifo¹ÜµÀ»á±»×èÈû£¬Ö±µ½ÁíÒ»¶Ë°ÑÊý¾Ýд½øÀ´£¬·´Ö®ÒàÈ»¡£ÎªÁ˱ÜÃâÕâÖÖÇé¿ö£¬¿ÉÒÔʹÓÃO_NOBLOCK²ÎÊýÀ´´ò¿ª¡£ÔÚÕâÖÖÇé¿öÏ£¬Äã»á»ñµÃÒÔÏÂÐÐΪ£º¶Ô¶ÁµÄµ÷ÓûáÁ¢¼´·µ»Ø£¬·µ»ØֵΪ0£»»òÕ߶ÔдµÄµ÷ÓûᵼÖÂÒ»¸öSIGPIPEÐźš£
    5.6ÏûÏ¢¶ÓÁÐ
    ÁíÒ»ÖÖ½ø³Ì¼äͨÐÅ»úÖÆ£¬ÏûÏ¢¶ÓÁУ¬Îª½ø³Ì¼äͨÐÅÌṩÁíÒ»ÖÖ·½Ê½¡£È»¶øºÍÎÒÌáµ½µÄÆäËûͨÐÅ»úÖƲ»Í¬£¬ÄãÓ¦¸Ã¾¡Á¿±ÜÃâʹÓÃÕâÖÖ·½Ê½¡£Èç¹ûÄãµÄ³ÌÐòʹÓÃÏûÏ¢¶ÓÁУ¬ÊÔ×ÅÓÃfifo»òÕßÉõÖÁUnix Domain SocketÖØÐÂʵÏÖËü¡£ÔÚÎÒÌÖÂÛÔ­Òò֮ǰ£¬ÒÔÏÂÊÇÒ»¸ö¿ìËÙ¸ÅÊö¡£
    ÏûÏ¢¶ÓÁÐÓëFIFOÏàËÆ£¬µ«ËüÃÇʹÓÃÒ»¸ö¼ü¶ø²»ÊÇÒ»¸öÎļþ×÷ΪÒýÓá£Õâ¸ö¼üÊÇÒ»¸öÎÞ·ûºÅÕûÊý¡£Ò»µ©Ò»¸öÏûÏ¢¶ÓÁб»´´½¨£¬·¢Ë͵½Õâ¸öÏûÏ¢¶ÓÁеÄÊý¾Ý»á±»Äں˻º´æ¡£Äں˷ÖÅä¸øÏûÏ¢¶ÓÁеÄÄÚ´æÊÇÓÐÏ޵ġ£Ò»µ©Ò»¸öÏûÏ¢¶ÓÁб»ÌîÂú£¬ÄÇôÔÚÒ»¸ö½ø³Ì´ÓÕâ¸öÏûÏ¢¶ÓÁаÑÊý¾Ý¶Á×ß֮ǰ²»ÄÜÔÙÍùÕâ¸ö¶ÓÁз¢ËÍÊý¾Ý¡£ÔÚÕâÖÖÇé¿öÏ£¬Èç¹ûÁ½¸ö½ø³ÌÒÔ²»Í¬µÄËٶȶÁд£¬¶ÓÁÐÊÇ¿É¿¿µÄ¶øÇÒÔÚ´ó¶àÊýÇé¿öÏÂÊǷǶÂÈûµÄ¡£ÕâÓëFIFO²»Í¬¡£ÔÚFIFOµÄ»úÖÆÖУ¬Ò»¸öÂýµÄ¶Á½ø³ÌÊÂʵÉÏÊÇ»áÍÏÂýÒ»¸ö±È½Ï¿ìµÄд½ø³ÌµÄ£¨³ý·ÇÉèÖÃÁËO_NONBLOCK²ÎÊý£©¡£ÁíÒ»¸öºÃ´¦ÊǼ´Ê¹Ð´½ø³ÌÍ˳öÁË£¬Ð´µ½ÏûÏ¢¶ÓÁÐÖеÄÊý¾ÝÒ²»á±»±£´æÆðÀ´Ö±µ½ÁíÒ»¸ö½ø³Ì¶ÁÈ¡Ëü¡£¶øÔÚFIFOµÄ»úÖÆÏ£¬Èç¹ûд½ø³ÌÍ˳öÁË£¬Õâ¸öFIFO»á±»¹Ø±Õ£¬¶ø¶Á½ø³Ì»áÊÕµ½Ò»¸öÎļþ½áÊøµÄ±êÖ¾¡£
    ÒÔÉÏÕâЩËùÓеÄÏûÏ¢¶ÓÁеĺô¦¿´ÆðÀ´²»´í£¬µ«ÈÃÎÒÃÇÔÙ×Ðϸ¿´¿´¡£¼Ù¶¨Ò»¸ö½ø³Ì´ò¿ªÒ»¸öÏûÏ¢¶ÓÁУ¬ÍùÀïÃæдÁËÒ»´ó¿éÊý¾Ý£¬ÌîÂúÁËÄں˵Ļº³åÇøÈ»ºóÍ˳ö£¬Äں˽«²»µÃ²»±£´æÕâЩÊý¾ÝÖ±µ½ÁíÒ»¸ö½ø³ÌÀ´¶ÁÈ¡Ëü£¬¶øÆäËûÈκÎÏë´´½¨ºÍдÏûÏ¢¶ÓÁеĽø³Ì½«»á±»¾Ü¾ø¡£ÕâÖÖÇé¿ö½«»á³ÖÐøÖ±µ½Ò»¸ö½ø³Ì¶Á×ßÕâЩÊý¾Ý»òÕßϵͳÖØÆô¡£ÔÚÕâÖÖÇé¿öÏ£¬´´½¨Ò»¸ö¼òµ¥µÄÕë¶ÔÏûÏ¢¶ÓÁеľܾø·þÎñÊÇ¿ÉÄܵġ£
    ÁíÒ»¸öÎÊÌâÊǼü²¢²»È·±£ÊÇΨһµÄ¡£»»¾ä»°Ëµ£¬Ò»¸ö½ø³ÌÎÞ·¨È·Á¢Ò»ÖÖ·½Ê½À´È·¶¨ËüÊÇ·ñÊÇʹÓÃÒ»¸öÌض¨ÏûÏ¢¶ÓÁеÄΨһ½ø³Ì¡£¶øÔÚFIFO»úÖÆÖУ¬µ±Ò»¸ö½ø³Ì´´½¨Ò»¸öFIFOʱ£¬ËüÓбȽϺõĻú»áÖªµÀÕâ¸öFIFOÊÇ·ñΨһÒòΪ¿ÉÒÔÉùÃ÷Ò»¸öÕë¶ÔÎļþ·¾¶µÄÔ¤ÏÈÔ¼¶¨£¨±ÈÈ磺/usr/local/myapp/fifo_dir£©¡£ÄãµÄÓ¦ÓóÌÐò¿ÉÒÔÔÚ°²×°Ê±´´½¨Ò»¸öΨһµÄĿ¼£¬ÕâÑù¼¸ºõ¿ÉÒÔÈ·±£Ò»¸öΨһµÄFIFO·¾¶¡£ÓÐЧµÄÏûÏ¢¶ÓÁмüÖµ¿ÉÒÔͨ¹ýµ÷ÓÃÒ»¸öº¯ÊýftokÀ´Éú³ÉÒÔ°ïÖú¼õÉÙÏàͬµÄ¼üÖµ£¬µ«²¢²»ÄÜÈ·±£Î¨Ò»¡£Õâ¸öÎÊÌâµÄ¸±×÷ÓÃÄÑÓÚÈ·¶¨--ÄãµÄ³ÌÐò¿ÉÄܶÁµ½Ëü²¢²»Ïë¶ÁµÄÊý¾Ý£¬»òÕßÄãµÄ³ÌÐòдÈëµÄÊý¾ÝÕý±»ÆäËû²¢²»Ïë¶ÁÈ¡µÄ½ø³Ì¶ÁÈ¡¡£¼ò¶ÌÀ´Ëµ£¬µ±ÄãʹÓÃÏûÏ¢¶ÓÁÐʱ£¬¿ÉÄܵ¼ÖÂÄÑÓÚµ÷ÊÔµÄÆæ¹ÖÐÐΪºÍ´íÎó¡£
    Èç¹ûÄãÈÔÈ»¼á³ÖʹÓÃÏûÏ¢¶ÓÁУ¬²Î¿¼ÒÔϵÄÖ¸ÄÏÒ³£ºftok(3), msgget(3), msgctl(3), msgrcv(3), ºÍmsgsnd(3)¡£
    5.7 ½áÂÛ
    ÕâÒ»Õ½éÉÜÁ˼¸¸ö²Ù×÷´ò¿ªÎļþÃèÊö·ûµÄϵͳµ÷Ó㬰üº¬ÔÚÎÒÃÇforkºÍexec֮ǰ±ØÐë¹Ø±ÕÎļþÃèÊö·ûµÄ³¡¾°¡£ÎÒÃÇÒ²ÌÖÂÛÁËÎļþËø¶¨£¬ÉèÖúÍÒƳýÎļþËø¶¨£¬ÒÔ¼°Ò»Ð©ÌØÊâµÄÎļþÃèÊö·ûÈçFIFOºÍQueueµÈ£¬ÔÚÄÇÖÖÇé¿öϸù±¾²»ÔÚÎļþϵͳÉÏ´æ´¢Êý¾Ý¡£ÕâЩϵͳµ÷ÓÃΪBSDÔö¼ÓÁ˺ܴóµÄ¿É±à³ÌÐÔºÍÁé»îÐÔ¡£µ«µ±Ò»¸ö½ø³ÌÓжà¸ö´ò¿ªµÄÎļþÃèÊö·ûʱ»áÔõôÑù£¿ÏÂÒ»Õ½«»áÌÖÂÛ¸ßЧµØ´¦Àí¶à¸öÎļþÃèÊö·û¡£

  6. linuxguy ÓÚ 2007-06-16 00:38:54·¢±í:

    µÚÈýÕÂ: ½ø³ÌºÍÄں˷þÎñ
    ¸ÃÕ½ڵÄÀý³Ì:
    3.1 ½ø³ÌºÍÄں˷þÎñ
    process:һϵÁÐÓÃÓÚʵÏÖÄ¿±êµÄÐÐΪºÍ²Ù×÷£»ÌرðÖ¸£ºÖÆÔìÖÐÖ¸¶¨µÄÁ¬ÐøµÄ¹¤×÷ºÍ´¦Àí¡£
    Ç°ÃæµÄÕ½ÚÓÐÌáµ½BSDϵͳµÄÆô¶¯¹ý³Ì¡£ÏÖÔÚÈÃÎÒÃÇ¿´¿´Ò»µ©Äں˱»µ¼Èë²¢ÔËÐлᷢÉúʲô¡£Ïñ´ó²¿·ÖÆäËûµÄÏÖ´ú²Ù×÷ϵͳ£¬BSDÊÇÒ»¸ö¶àÓû§¶àÈÎÎñ²Ù×÷ϵͳ£¬ÕâÒâζ×ÅËüÖ§³Ö¶àÓû§ÔËÐв»Í¬µÄ¶à½ø³ÌÀ´Ê¹ÓÃϵͳ×ÊÔ´¡£½ø³ÌµÄ¸ÅÄîÌá³öÁËÒ»¸ö¶Ô²Ù×÷ϵͳ¹ÜÀíµÄËùÓлµÄÓÐÁ¦µÄÌáÈ¡¡£Õâ¸ö¸ÅÄî×÷ΪÔÚ¶àµÀ³ÌÐòÉè¼Æ»·¾³ÖÐÃæÏò¹¤×÷µÄÒ»¸öÌض¨ÊõÓïÊÇÔÚ19ÊÀ¼Í60Äê´úÓÉMultics(Ò»¸öʧ°ÜµÄ·Öʱϵͳ¼Æ»®)µÄÉè¼ÆÕßÌá³öµÄ¡£
    3.2 µ÷¶È
    Ò»¸ö½ø³ÌÊÇÒ»¸öÔËÐгÌÐòµÄµ¥¶ÀʵÀý¡£ÀýÈ磬µ±ÄãÔÚBSDϵͳÖÐÔËÐÐNetscape£¬Ëü½«ÔÚ±»ÔËÐÐʱ´´½¨Ò»¸ö½ø³Ì¡£
    Èç¹ûÓÐ3¸öÓû§µÇ½BSDϵͳ²¢È«²¿Í¬Ê±ÔËÐÐÏàͬµÄNetscape³ÌÐò£¬Ã¿Ò»¸öÓû§½«ÓµÓÐÇø±ðÓÚÆäËûÓû§µÄ×Ô¼ºµÄ
    NetscapeʵÀý¡£BSDϵͳ¿ÉÒÔÖ§³Öͬʱ¶à¸öÕâÑùµÄ½ø³Ì¡£Ã¿Ò»¸ö½ø³Ì½«ºÍÒ»¸ö½ø³Ì±êÖ¾ºÅ(PID)¹ØÁª¡£ÕâЩ½ø³Ì
    ÐèÒª×ÊÔ´²¢ÇÒ¿ÉÄܲ»µÃ²»Ê¹ÓÃһЩÉ豸±ÈÈçÍⲿ´æ´¢¡£
    µ±¶à¸ö³ÌÐòÔÚÒ»¸öϵͳÉÏÔËÐеÄʱºò£¬²Ù×÷ϵͳ²Ù¿Ø³öËûÃǶ¼ÔÚÔËÐеļÙÏó¡£²Ù×÷ϵͳÌض¨µÄµ÷¶ÈËã·¨½«¹ÜÀí
    ³ÌÐòµÄÓÅÏÈȨָÅÉ¡£¸ÃÁìÓòµÄ¼ÆËã¿ÆѧÊǹãÀ«µÄ£¬ºÍÊ®·ÖרҵµÄ¡£¸ü¶àµÄÐÅÏ¢Çë¿´×ÊÔ´Õ½ڡ£
    ²Ù×÷ϵͳʵ¼ÊÉϽ«ÔËÐеĶà½ø³Ì²»¶Ïµ÷Èë»òµ÷³öCPU(s)£¬Í¨¹ýÕâÖÖ·½·¨£¬Ã¿¸ö½ø³ÌµÃµ½Ò»¶ÎÌض¨µÄCPU(s)ÔËÐÐ
    ʱ¼ä¡£Õâ¶Îʱ¼ä±»½Ð×öʱ¼äƬ¡£Ê±¼äƬµÄ³¤¶Ì»ù±¾ÉÏÓÉÄں˵ĵ÷¶ÈËã·¨¾ö¶¨¡£¸ÃËã·¨ÓÐÒ»¸ö¿ÉÒÔµ÷ÕûµÄÖµ½Ð¡®nice¡¯£¬ËüÌṩ¸ø½ø³ÌÐÞ¸ÄÔËÐÐÓÅÏÈȨµÄ³ÌÐòÄÜÁ¦¡£ÕâЩÓÅÏÈȨµÄÖµÈçÏ£º
    Nice values Priority
    -20 - 0 Higher priority for execution
    0 - 20 Lower priority for execution

    ×¢Ò⣺һ¸ö¸ßµÄniceÖµµ¼Öµ͵ÄÔËÐ춱ð£¬Õâ¿ÉÄÜ¿´ÉÏÈ¥Óеã¹ÖÒ졣Ȼ¶ø£¬¿¼Âǵ½ÏÂÃæÕâ¸ö¼ÆË㣺
    (scheduling algo calculation) + (nice value)
    Õâ¸ö¼òµ¥µÄËãÊõ±íÃ÷£ºÌí¼ÓÒ»¸ö¸ºÖµµ½Ò»¸öÕýÖµ½«µÃµ½Ò»¸ö½ÏСµÄÊý£¬¶øµ±ÕâЩÊý±»ÅÅÐòµÄʱºò£¬½ÏСµÄÊý½«ÅÅÔÚ
    ÔËÐжÓÁеÄÇ°Ãæ¡£
    ËùÓнø³ÌĬÈϵÄniceÖµÊÇ0£¬ÔËÐеĽø³Ì¿ÉÒÔÌá¸ß×Ô¼ºµÄniceÖµ£¨¾ÍÊǽµµÍ×Ô¼ºµÄÓÅÏÈȨ£©£¬µ«ÊÇÖ»ÓÐÒÔrootÔËÐеÄ
    ½ø³Ì¿ÉÒÔ½µµÍnice£¨¾ÍÊÇÌá¸ßÓÅÏÈȨ£©¡£BSDϵÁÐÌṩÁËÁ½¸ö»ù±¾µÄ½è¿ÚÓÃÓڸıäºÍ»ñµÃÕâЩֵ¡£ËüÃÇÊÇ£º
    CODE:[Copy to clipboard]int getpriority(int which, int who);
    int setpriority(int which, int who, int prio);
    ºÍ
    CODE:[Copy to clipboard]int nice(int incr);
    niceº¯Êý»á°Ñµ÷ÓÃËüµÄ½ø³ÌµÄniceÖµÉèÖóɴ«¸øËüµÄ²ÎÊýincrµÄÖµ¡£¿ÉÒÔ¿´µ½niceº¯ÊýÊDZȽϲ»ºÃÓõģ¬ÒòΪËü²»ÊÇ
    Ê®·ÖÁé»î£¬Êµ¼ÊÉÏniceº¯Êý²»¹ýÊǶÔÇ°Á½¸öº¯ÊýµÄ·â×°¡£Ê×Ñ¡µÄ·½·¨ÊÇʹÓÃsetpriority()¡£
    Àý³Ìnice_code.cʾ·¶ÈçºÎ»ñÈ¡ºÍÉèÖõ±Ç°½ø³ÌµÄniceÖµ¡£Èç¹ûËü±»rootÒÔ-20µÄÃüÁîÐвÎÊýÔËÐУ¬ÏµÍ³½«¿´ÉÏȥûÓз´Ó¦¡£ÕâÊÇÒòΪÕâ¸ö³ÌÐòÓµÓÐÁË×î¸ßµÄÔËÐм¶±ð£¬½«»á±ÈϵͳÓÅÏÈ¡£ËùÒÔµ±ÉèÖöªÁËСÓÚ0µÄֵʱ£¬ÐèÒª½÷É÷ʹÓú͸ü¶àµÄÄÍÐÄ¡£È¡¾öÓÚCPU£¬ÍêÈ«ÔËÐÐÍê¿ÉÄÜÒªÏûºÄ20·ÖÖÓ¡£½¨ÒéʹÓÃtimeÖ¸ÁîÀ´ÔËÐгÌÐò£¬ÈçÏ£º
    CODE:[Copy to clipboard]bash$ time nice_code 10
    È»ºó£¬µ÷Õû²ÎÊýµÄÖµ¡£ÕâÑù½ø³ÌÏûºÄÁ˶àÉÙÔËÐÐʱ¼ä¾ÍºÜÃ÷ÏÔÁË¡£±ÈÈçµ÷Õû²ÎÊýʹ֮СÓÚ0£º
    CODE:[Copy to clipboard]$ time nice_code -1
    ÏÂÃæ°Ñ²ÎÊýµ÷´ó£º
    CODE:[Copy to clipboard]bash$ time nice_code 20
    ͬʱ£¬³¢ÊÔÓÃÆäËû·ÇrootÓû§ÔËÐгÌÐò£¬²¢Ê¹ÓÃСÓÚ0µÄ²ÎÊý¡£ÏµÍ³½«»á¾Ü¾ø²Ù×÷¡£Ö»ÓÐrootÔËÐеĽø³Ì¿ÉÒÔ½µµÍËûÃǵÄniceÖµ¡££¨ÒòΪUnixϵͳʱ¶àÓû§ÏµÍ³,ÿ¸ö½ø³ÌÓ¦¸Ã»ñµÃºÏÀíµÄCPUʱ¼äƬ,Ö»ÓÐroot¿ÉÒԸıä½ø³ÌµÄÓÅÏȼ¶µ½0ÒÔÏÂ,ÕâÑù¿ÉÒÔ±ÜÃâÓû§Í¨¹ý½µµÍÓÅÏȼ¶À´¶ÀÕ¼CPU×ÊÔ´.£©
    3.3 ϵͳ½ø³Ì
    ±£»¤Ä£Ê½µÄ¸ÅÄîÔÚÇ°Ò»ÕÂÒÑÓнéÉÜ,¼òµ¥µÄ˵,ËüÊÇÏÖ´úCPUϵÁÐÖ§³ÖµÄÒ»¸öÌض¨µÄģʽ,ͨ¹ý¸Ãģʽ²Ù×÷ϵͳ¿ÉÒÔ±£»¤ÄÚ´æ.¾Ý´Ë,ÏÖÔÚÓÐÁ½ÖÖÕâÑùµÄ²Ù×÷ģʽ¡£Ò»ÊÇÄÚºË̬£¬Òâζ׎ø³Ì½«ÔËÐÐÔÚÄں˵ÄÄÚ´æ¿Õ¼ä£¬²¢Òò´Ë£¬ÒÔÓÐÄÚºËÌØȨµÄ±£»¤Ä£Ê½ÔËÐÐÓÚCPUÖС£¶þÊÇÓû§Ì¬£¬¼´ÔËÐв¢ÇÒ²»ÊÇÔË×÷ÔÚÄں˱£»¤Ä£Ê½µÄÒ»ÇС£
    ÕâÇø±ðÊÇÊ®·ÖÖØÒªµÄ£¬ÒòΪËùÓеĸø¶¨½ø³Ì¶¼ÔÚÄÚºË̬ºÍÓû§Ì¬Ê¹ÓÃ×ÊÔ´¡£ÕâЩ×ÊÔ´ÓжàÖÖ¸ñʽ£¬¾ÍÈçÓÃÀ´±êÖ¾½ø³ÌµÄÄں˽ṹ£¬½ø³ÌÖ¸ÅɵÄÄڴ棬´ò¿ªµÄÎļþ£¬ºÍÆäËûÖ´ÐжΡ£
    ÔÚÒ»¸öFreeBSDϵͳÖУ¬ÓÐһЩ¹Ø¼üµÄ½ø³Ì°ïÖúÄÚºËÀ´Ö´ÐÐÈÎÎñ¡£ÕâЩ½ø³ÌÖеÄÒ»²¿·ÝÊÇÍêÈ«ÔËÐÐÓÚÄں˿ռ䣬¶øÓÐЩÔËÐÐÓÚÓû§Ì¬¡£ÕâЩ½ø³ÌÈçÏ£º
    PID Name
    0 swapper
    1 init
    2 pagedaemon
    3 vmdaemon
    4 bufdaemon
    5 syncer
    ÒÔÉÏËùÓеĽø³Ì³ýÁËinit£¬¶¼ÊÇÔËÐÐÓÚÄںˡ£ÕâÒâζ×ÅËüÃDz»´æÔÚÏàÓ¦µÄ¶þ½øÖƳÌÐò¡£ÕâЩ½ø³ÌÓеãÀàËÆÓû§Ì¬½ø³Ì,²¢ÇÒÒòΪËüÃÇÔÚÄÚºËÄÚÔËÐУ¬ËûÃÇÔË×÷ÓÚÄÚºËÌØȨģʽ¡£ÕâÖÖÌåϵ½á¹¹ÊdzöÓÚ¶àÖÖÉè¼Æ¿¼ÂÇ¡£ÀýÈ磬pagedaemon½ø³Ì£¬ÎªÁ˼õÉÙ¸ºÔØ£¬Ö»ÔÚϵͳȱ·¦ÄÚ´æʱ±»»½ÐÑ¡£Òò´ËÈç¹ûϵͳÐí¶à¿ÕÏеÄÄڴ棬Ëü¾ÍûÓб»»½ÐѵıØÒª¡£Èç´Ë£¬±ÈÕâ¸ö½ø³ÌÔËÐÐÓÚÓû§Ì¬µÄÓŵã¾ÍÊdzý·ÇÕæÕýÐèÒª,¿ÉÒÔ±ÜÃâʹÓÃCPU¡£µ«ÊÇ£¬ËüÔÚϵͳÔö¼ÓÁËÒ»¸öÐèÒª±»µ÷¶ÈµÄ½ø³Ì¡£²»¹ýÕâЩµ÷¶È¼ÆËãÏûºÄÈç´ËС¼¸ºõ¿ÉÒÔºöÂÔ²»¼Æ¡£
    init½ø³ÌÊÇËùÓнø³ÌµÄ×ÚÖ÷½ø³Ì¡£³ýÁËÔËÐÐÓÚÄÚºË̬ÌØȨģʽµÄ½ø³Ì£¬Ã¿¸ö½ø³Ì¶¼ÊÇinitµÄºóÒá¡£²¢ÇÒ£¬½©Ê¬»ò¹Â¶ù½ø³Ì¶¼»á±»init½Ó¹Ü¡£ËüÒ²ÔËÐÐһЩ¹ÜÀíÈÎÎñ£¬±ÈÈçµ÷ÓòúÉúϵͳttysµÄgettys£¬ºÍÔËÐÐϵͳµÄÓÐÐò¹Ø»ú¡£
    3.4 ½ø³Ì´´½¨ºÍ½ø³ÌIDϵͳ
    ¾ÍÏñÉÏÃæËù˵£¬µ±Ò»¸ö½ø³Ì±»ÔËÐУ¬ÄÚºËÖ¸ÅɸøËüÒ»¸öΨһPID¡£Õâ¸öPIDÊǸöÕýÕûÊý²¢ÇÒËüµÄÖµÔÚ0µ½PID_MAXÖ®¼ä£¬ÓÉϵͳ¾ö¶¨¡££¨¶ÔÓÚFreeBSD£¬ÔÚ/usr/include/sys/proc.hÖÐÉ趨PID_MAXΪ99999¡££©Äں˽«ÓÃÏÂÒ»¸öÏàÐøµÄ¿ÉÓõÄÖµÀ´Ö¸ÅÉPID¡£Òò´Ë£¬µ±PID_MAXµ½´ïµÄʱºò£¬Öµ½«»áÑ­»·¡£Õâ¸öÑ­»·ÊÇÖØÒªµÄ£¬µ±Ê¹ÓÃPIDÓÚµ±Ç°½ø³Ìͳ¼ÆµÄʱºò¡£
    ÿһ¸öÔÚϵͳÉÏÔËÐеĽø³Ì¶¼ÊÇÓÉÆäËû½ø³Ì²úÉúµÄ¡£ÕâÊÇÓÐһЩϵͳµ÷ÓÃÍê³ÉµÄ£¬½«ÔÚÏÂÒ»ÕÂÂÛÊö¡£µ±Ò»¸ö½ø³Ì´´½¨Ò»¸öеĽø³Ì£¬Ô­½ø³Ì¾Í³ÉΪ¸¸½ø³Ì£¬Ð½ø³Ì¾ÍÊÇ×Ó½ø³Ì¡£ÕâÖÖ¸¸×Ó¹ØϵÌṩÁ˺ܺõÄÀàÍÆ-ÿһ¸ö¸¸½ø³Ì¿ÉÒÔÓкܶà×Ó½ø³Ì¶ø¸¸½ø³ÌÃÇÊÇÓÉÁíÍâÒ»¸ö½ø³ÌÅÉÉúµÄ¡£½ø³Ì¿ÉÒÔͨ¹ýgetpid/getppidº¯ÊýµÃµ½×Ô¼º»òËûÃǸ¸½ø³ÌµÄPID¡£
    ½ø³ÌÒ²¿ÉÒÔÓýø³Ì×éÀ´·Ö×é¡£ÕâЩ½ø³Ì×é¿ÉÒÔͨ¹ýÒ»¸öΨһµÄgrpIDÀ´±êÖ¾¡£½ø³Ì×é×÷Ϊһ¸ö»úÖƱ»ÒýÈëBSDÊÇΪÁËʹshellsÄܹ»¿ØÖƹ¤×÷¡£¿´ÏÂÃæÕâ¸öÀý×Ó£º
    CODE:[Copy to clipboard]bash$ ps auwx | grep root | awk {'print $2' }
    ÕâЩ³ÌÐò£¬ps,grep,awkºÍprint¶¼¹éÊôÓÚÒ»¸öÏàͬµÄ×飬ÕâÔÊÐíËùÓеÄÕâЩָÁî¿ÉÒÔͨ¹ýÒ»¸öµ¥Ò»µÄ¹¤×÷À´¿ØÖÆ¡£
    Ò»¸ö½ø³Ì¿ÉÒÔ»ñµÃËüµÄ×éID/ºÍ¸¸IDͨ¹ýµ÷ÓÃgetpgrp»ògetppid£º
    CODE:[Copy to clipboard] #include
    #include

    pid_t getpid(void);
    pid_t getppid(void);
    pid_t getpgrp(void);
    ËùÓÐÒÔÉÏÀý³öµÄº¯Êý¶¼Ê¹¾ø¶Ô¿ÉÒÔÔËÐгɹ¦µÄ¡£È»¶ø£¬FreeBSD man pages Ç¿ÁÒ½¨Òé,PID²»Ó¦¸ÃÓÃÀ´´´½¨Ò»¸öΨһÎļþ£¬ÒòΪÕâ¸öPIDÖ»ÔÚ´´½¨µÄʱºòΨһ¡£Ò»µ©½ø³ÌÍ˳ö£¬PIDÖµ»á»Øµ½Î´Ê¹ÓÃPID³Ø£¬½«»á±»½Ó×ű»Ê¹Ó㨵±È»ÊÇÔÚϵͳһֱÔËÐÐÇé¿öÏ£©¡£
    Ò»¸ö»ñµÃÕâЩֵµÄÔ´³ÌÐò´úÂëÔÚ proc_ids.cÖÐÁгö¡£
    µ±ÄãÔËÐÐÏÂÃæ³ÌÐò£º
    CODE:[Copy to clipboard]bash$ sleep 10 | ps auwx | awk {'print $2'} | ./proc_ids
    ͬʱÔÚÁíÒ»¸öÖÕ¶ËÔËÐУº
    CODE:[Copy to clipboard]bash$ ps -j
    Ö»ÓÐÕâЩָÁ±»Ò»¸öshellÔËÐв¢ÇÒÿ¸ö¶¼ÓÐÏàͬµÄPPIDºÍPGID¡£
    3.5 ×Ó½ø³Ì
    ½ø³Ì¿ÉÒÔÓÉÆäËû½ø³Ì´´½¨,ÔÚBSDÖÐÓÐ3¸ö·½·¨À´ÊµÏÖÕâ¸öÄ¿µÄ¡£ËûÃÇÊÇ£ºfork,vfork ºÍrfork ¡£ÆäËûµÄµ÷ÓÃÀýÈçsystem²»¹ýÊǶÔÕâ3¸öµÄ·â×°¡£
    fork
    µ±Ò»¸ö½ø³Ìµ÷ÓÃfrok£¬Ò»¸öеĸ´ÖƸ¸½ø³ÌµÄ½ø³Ì½±±»´´½¨£º
    CODE:[Copy to clipboard]#include
    #include
    pid_t fork(void);
    ²»ÏñÆäËûµÄº¯Êýµ÷Óã¬Ôڳɹ¦µÄʱºò£¬fork»á·´À¡Á½´Î-Ò»´ÎÔÚ¸¸½ø³Ì£¬·µ»ØµÄÊÇд´½¨µÄ×Ó½ø³ÌµÄPID£¬¶øµÚ¶þ´ÎÔÚ×Ó½ø³Ì£¬·µ»Ø½«ÊÇ0¡£ÕâÑùÒ»À´£¬Äã¾Í¿ÉÒÔÇø·ÖÁ½¸ö½ø³Ì¡£µ±Ò»¸öеĽø³Ì±»fork´´½¨£¬Ëü¼¸ºõÊǸ¸½ø³ÌµÄ¾«È·¸´ÖÆ¡£ËûÃǵĹ²Í¬µã°üÀ¨Ò»Ï£º£¨²»ÊÇ´´½¨µÄ˳Ðò£©
    Controlling terminal
    Working directory
    Root directory
    Nice value
    Resource limits
    Real, effective and saved user ID
    Real, effective and saved group ID
    Process group ID
    Supplementary group ID
    The set-user-id bits
    The set-group-id bits
    All saved environment variables
    All open file descriptors and current offsets
    Close-on-exec flags
    File mode creation (umask)
    Signals handling settings (SIG_DFL, SIG_IGN, addresses)
    Signals mask
    ÔÚ×Ó½ø³ÌÖв»Í¬µÄÊÇËüµÄPID£¬¶øPPID±»ÉèÖóɸ¸½ø³ÌµÄPID£¬½ø³ÌµÄ×ÊÔ´ÀûÓÃÖµÊÇ0£¬²¢ÇÒ¿½±´Á˸¸½ø³ÌµÄÎļþÃèÊö·û¡£×Ó½ø³Ì¿ÉÒԹرÕÎļþÃèÊö·û¶ø²»¸ÉÈŸ¸½ø³Ì¡£Èç¹û×Ó½ø³ÌÏ£Íû¶ÁдËüÃÇ£¬½«»á±£³Ö¸¸½ø³ÌµÄÆ«ÒÆÁ¿¡£×¢ÒâÒ»¸öDZÔÚµÄÎÊÌ⣬Èç¹û¸¸×Ó½ø³ÌͬʱÊÔͼ¶ÁдÏàͬµÄÎļþÃèÊö·û»áµ¼ÖÂÊä³öÒì³££¬»ò³ÌÐò±ÀÀ£¡£
    µ±Ò»¸öеĽø³Ì±»´´½¨ºó£¬ÔËÐеÄ˳Ðò½«²»¿ÉÖª¡£×îºÃ¿ØÖÆÔËÐÐ˳ÐòµÄ·½·¨ÊÇʹÓÃsemiphores,pipes,»òsignals,ÕâÒÔºóÂÛÊö¡£Í¨¹ýÕâЩ£¬¶Áд¾Í¿ÉÒÔ¿ØÖÆ£¬Ê¹µÃÒ»¸ö½ø³Ì¿ÉÒÔ±ÜÃâÈÅÂÒÆäËû½ø³Ìµ¼ÖÂÒ»Æð±ÀÀ£¡£
    wait
    ¸¸½ø³ÌÓ¦¸ÃʹÓÃÏÂÃæwaitϵͳµ÷ÓõÄÒ»ÖÖÀ´ËѼ¯×Ó½ø³ÌµÄÍ˳ö״̬:
    CODE:[Copy to clipboard] #include
    #include
    pid_t wait(int *status);
    #include
    #include
    pid_t waitpid(pid_t wpid, int *status, int options);
    pid_t wait3(int *status, int options, struct rusage *rusage);
    pid_t wait4(pid_t wpid, int *status, int options, struct rusage *rusage);
    ÔÚµ÷ÓÃwaitµÄʱºò£¬options²ÎÊýÊÇÒ»¸öλֵ£¬»òÕßÊÇÒÔϵÄÒ»¸ö£º
    WNOHANG -ÔÚµ÷ÓõÄʱºò²»×èÈû³ÌÐò£¬Ëû»áµ¼ÖÂwaitÂíÉÏ·´À¡¼´Ê¹Ã»ÓÐ×Ó½ø³Ì±»ÖÕÖ¹¡£
    WUNTRACED -µ±ÄãÏëÒªµÈ´ýÍ£Ö¹²¢ÇÒ²»¿É¸ú×ÙµÄ×Ó½ø³Ì£¨¿ÉÄÜÓÉSIGTTIN, SIGTTOU, SIGTSTP»òSIGSTOPÐźŵ¼Ö£©µÄ״̬ʱÉèÖøÃÖµ¡£
    WLINUXCLONE -Èç¹ûÄãÏëµÈ´ýͨ¹ýlinux_clone²úÉúµÄÄÚºËÏ̡߳£
    µ±Ê¹ÓÃwait4ºÍwaitpidµ÷ÓÃʱ£¬Òª×¢Òâwpid²ÎÊýÊÇÒªµÈ´ýµÄPIDÖµ¡£Èç¹û½«ÆäÉèÖóÉ-1½«»áµ¼Ö¸õ÷ÓõȴýËùÓпÉÄܵÄ×Ó½ø³ÌµÄÖÕÖ¹¡£ÔÚµ÷ÓÃʱʹÓÃrusage½á¹¹£¬Èç¹û½á¹¹²»ÊÇÖ¸ÏòNULL½«·µ»ØÖÕÖ¹½ø³ÌµÄ×ÊԴʹÓÃͳ¼Æ¡£µ±È»Èç¹û½ø³ÌÒѾ­Í£Ö¹£¬ÄÇôÕâЩʹÓÃÐÅÏ¢¾ÍûÓÐʲôÓô¦¡£
    waitº¯Êýµ÷ÓÃÌṩ¸ø¸¸½ø³ÌÒ»¸ö·½·¨È¥»ñÈ¡Ëü×Ó½ø³ÌÍ˳öµÄÐÅÏ¢¡£Ò»µ±µ÷Ó㬵÷ÓÃËüµÄ½ø³Ì½«±»×èÈûÖªµÀÒ»¸ö×Ó½ø³ÌÖÕÖ¹»ò½ÓÊÕµ½Ò»¸öÐźš£Õâ¿ÉÒÔͨ¹ýÉèÖÃWNOHANG ÖµÀ´±ÜÃâ¡£µ±µ÷Óóɹ¦Ê±£¬status²ÎÊý½«°üº¬½áÊø½ø³ÌµÄÐÅÏ¢¡£Èç¹ûµ÷Óýø³Ì¶ÔÍ˳ö״̬ûÓÐÐËȤ£¬¿ÉÒÔ½«statusÉèÖóÉNULL¡£¹ØÓÚʹÓÃWNOHANG µÄ¸ü¶àϸ½Ú½«ÔÚÐźÅÕ½ÚÃèÊö¡£
    Èç¹û¶ÔÍ˳ö״̬ÐÅÏ¢ÓÐÐËȤ£¬ËüÃǵĺ궨ÒåÔÚ/usr/include/sys/wait.hÖС£×îºÃʹÓÃËüÃÇÀ´»ñµÃ¸üºÃµÄ¿çƽ̨¼æÈÝÐÔ¡£ÏÂÃæÁгöËüÃÇ3¸öºÍʹÓÃ˵Ã÷£º
    WIFEXITED(status) -Èç¹û·µ»Øtrue£¨Ò²¾ÍÊÇ·µ»Ø·Ç0Öµ£©ÄÇô½ø³ÌÊÇͨ¹ýµ÷ÓÃexit()»ò_exit()Õý³£ÖÕÖ¹µÄ¡£
    WIFSIGNALED(status) -Èç¹û·µ»ØtrueÄÇô½ø³ÌÊÇÓÉÐźÅÖÕÖ¹µÄ¡£
    WIFSTOPPED(status) -Èç¹û·µ»ØtrueÄÇô½ø³ÌÒѾ­Í£Ö¹²¢ÇÒ¿ÉÒÔÖØпªÊ¼¡£Õâ¸öºêÓ¦¸ÃºÍWUNTRACEDÒ»ÆðʹÓ㬻òµ±×Ó½ø³Ì±»¸ú×ÙµÄʱºò£¨¾ÍÏñʹÓÃptrace£©
    Èç¹ûÓбØÒª£¬ÒÔϵĺê¿ÉÒÔ½øÒ»²½ÌáÈ¡±£´æµÄ״̬ÖÐÐÅÏ¢
    WEXITSTATUS(status) - ÕâÖ»ÄÜÔÚWIFEXITED(status) ºêÆÀ¹ÀΪtrueʱʹÓã¬Ëû»áÆÀ¹Àexit()ºÍ_exit()´«µÝ²ÎÊýµÄµÍ8λ¡£
    WTERMSIG(status) -ÕâÖ»ÄÜÔÚWIFSIGNALED(status)ÆÀ¹ÀΪtrueʱʹÓã¬Ëû»áµÃµ½µ¼Ö½ø³ÌÖÕÖ¹µÄÐźŵÄÖµ¡£
    WCOREDUMP(status) -ÕâÖ»ÄÜÔÚWIFSIGNALED(status)ÆÀ¹ÀΪtrueʱʹÓã¬Èç¹ûÖÕÖ¹µÄ½ø³ÌÔÚÊÕµ½Ðźŵĵ㴴½¨ÁËcore dumpÎļþÄÇô¸Ãºê»á·µ»Øtrue¡£
    WSTOPSIG(status) -ÕâÖ»ÄÜÔÚWIFSTOPPED(status) ÆÀ¹ÀΪtrueʱʹÓ᣸úì»áµÃµ½µ¼Ö½ø³ÌÍ£Ö¹µÄÐźš£
    Èç¹û¸¸½ø³ÌûÓÐÊÕ¼¯×Ó½ø³ÌµÄÍ˳ö״̬£¬»ò¸¸½ø³ÌÔÚ×Ó½ø³Ì½áÊøÇ°¾ÍÒѽáÊø£¬init½«»á½Ó¹Ü×Ó½ø³Ì²¢ÊÕ¼¯ËüÃǵÄÍ˳ö״̬¡£
    vfork ºÍ rfork
    vforkº¯ÊýºÍforkÏàËÆ£¬ÊÇÔÚ2.9BSD±»ÒýÈëµÄ¡£ËüÃÇÁ½ÕßµÄÇø±ðÊÇ£¬vfork»áÍ£Ö¹¸¸½ø³ÌµÄÔËÐв¢ÇÒʹÓø¸½ø³ÌµÄÔËÐÐÏ̡߳£ÕâÊÇΪÁ˵÷ÓÃexecvº¯ÊýÉè¼ÆµÄ£¬ÕâÑù¿ÉÒÔ±ÜÃâµÍЧµÄ¸´ÖƸ¸½ø³ÌµÄµØÖ·¿Õ¼ä¡£Êµ¼ÊÉÏvforkÊDz»±»½¨ÒéʹÓõģ¬ÒòΪËü²»ÊÇ¿çƽ̨µÄ¡£ÀýÈçIrixµÄ5.x¾ÍûÓÐvfork.ÏÂÃæÊÇvforkµÄµ÷ÓÃÀý×Ó:
    CODE:[Copy to clipboard]#include
    #include

    int vfork(void);
    rforkº¯ÊýÒ²ºÍforkÓëvforkÏàËÆ.ËûÊÇÔÚPlan9ÒýÈëµÄ¡£ËüµÄÖ÷ҪĿµÄÊÇÔö¼Ó¸ü³ÉÊìµÄ·½·¨À´¿ØÖƽø³ÌµÄ´´½¨ºÍ´´½¨ÄÚºËÏ̡߳£FreeBSD/OpenBSDÖ§³ÖαÏ̺߳ÍLinux cloneµ÷Ó᣻»¾ä»°Ëµ£¬rforkÔÊÐí±Èfork¸ü¿ì¸üСµÄ½ø³Ì´´½¨¡£Õâ¸öµ÷ÓÿÉÒÔÉèÖÃ×Ó½ø³Ì¿ÉÒÔºÍËüÃǹ²ÏíµÄ×ÊÔ´¡£ÏÂÃæÊÇÒ»¸örforkµ÷ÓÃÀý³Ì£º
    CODE:[Copy to clipboard] #include
    int rfork(int flags);
    rfork¿ÉÒÔÑ¡ÔñµÄ×ÊÔ´ÈçÏ£º
    RFPROC -ÉèÖøÃÖµ±íʾÄãÏë´´½¨Ò»¸öеĽø³Ì£»¶øÆäËûµÄ±êÖ¾½«Ö»Ó°Ï쵱ǰ½ø³Ì¡£¸ÃÖµÊÇĬÈÏÖµ¡£
    RFNOWAIT -ÉèÖøÃÖµ±íʾÄãÏ£Íû´´½¨Ò»¸öºÍ¸¸½ø³Ì·ÖÀëµÄ×Ó½ø³Ì¡£Ò»µ±¸Ã×Ó½ø³Ì±»Í˳ö£¬Ëû²»»áÁôÏÂ״̬µÈ´ý¸¸½ø³ÌÊÕ¼¯¡£
    RFFDG -ÉèÖøÃÖµ±íʾÄãÏ£Íû¸´ÖƸ¸½ø³ÌµÄÎļþÃèÊö·û±í¡£·ñÔò¸¸×Ó½ø³Ì½«»á¹²ÏíÒ»¸öÎļþÃèÊö·û±í¡£
    RFCFDG -¸Ã±êÖ¾ºÍRFDG±êÖ¾»¥³â¡£ÉèÖøÃÖµ±íʾÄãÏ£Íû×Ó½ø³ÌÓµÓÐÒ»¸ö¸É¾»µÄÎļþÃèÊö·û±í¡£
    RFMEM -ÉèÖøÃÖµ±íʾÄãÏëÇ¿ÖÆÄں˹²ÏíÍêÕûµÄµØÖ·¿Õ¼ä¡£ÕâÊÇÖ±½Ó¹²ÏíÓ²¼þÒ³Ãæ±íµÄµäÐÍ×ö·¨¡£Õâ²»ÄÜÖ±½ÓÔÚCÖе÷Óã¬ÒòΪ×Ó½ø³Ì»á·µ»ØºÍ¸¸½ø³ÌÏàͬ¶ÑÕ»¡£Èç¹ûÄãÏëÕâô×ö£¬×îºÃʹÓÃÏÂÃæÁгöµÄrfork_threadº¯Êý£º
    CODE:[Copy to clipboard]#include
    int rfork_thread(int flags, void *stack, int (*func)(void*arg), void *arg);
    Õ⽫´´½¨Ò»¸öеĽø³ÌÔËÐÐÓÚÖ¸¶¨µÄ¶ÑÕ»£¬²¢ÇÒµ÷ÓòÎÊýÖ¸¶¨µÄº¯Êý¡£ºÍfork²»Í¬£¬³É¹¦µÄʱºòÖ»»á·µ»Øд´½¨µÄ½ø³ÌPID¸ø¸¸½ø³Ì£¬ÒòΪ×Ó½ø³Ì½«Ö±½ÓÔËÐÐÖ¸¶¨µÄº¯Êý¡£Èç¹ûµ÷ÓÃʧ°Ü½«·µ»Ø-1£¬²¢ÇÒÉèÖÃerrnoµÄÖµ¡£
    RFSIGSHARE -ÕâÊÇÒ»¸öFreeBSDÌØÓеıêÖ¾²¢ÇÒ×î½üÖÚËùÖÜÖªµØ±»ÓÃÓÚFreeBSD4.3µÄ»º³åÇøÒç³ö¡£¸ÃÖµ½«ÔÊÐí×Ó½ø³ÌºÍ¸¸½ø³Ì¹²ÏíÐźš££¨Í¨¹ý¹²Ïísigacts½á¹¹£©¡£
    RFLINUXTHPN -ÕâÊÇÁíÒ»¸öFreeBSDÌØÓеÄÖµ¡£Õ⽫µ¼ÖÂÄÚºËÔÚ×Ó½ø³ÌÍ˳öʱʹÓÃÐźÅSIGUSR1´úÌæSIGCHILD¡£ÎÒÃÇ»áÔÚÏÂÒ»ÕÂÂÛÊöËü£¬ÏÖÔÚ¿ÉÒÔ°ÑËü¿´×÷rforkÄ£·Âlinux cloneµ÷Óá£
    rforkµÄ·µ»ØÖµºÍforkÏàËÆ¡£×Ó½ø³Ì»ñµÃ0Öµ¶ø¸¸½ø³Ì»ñµÃ×Ó½ø³ÌµÄPID¡£Ò»¸ö΢СµÄÇø±ðÊÇ-rfork»áÔÚÐèҪʱÐÝÃßÖ±µ½±ØÒªµÄϵͳ×ÊÔ´¿ÉÓ᣶øÒ»¸öforkµ÷ÓúÍʹÓÃRFFDG | RFPROCÀ´µ÷ÓÃrforkÏàËÆ¡£µ«ÊÇËû²»Éè¼ÆÏòºó¼æÈÝ¡£Èç¹ûrforkµ÷ÓÃʧ°Ü»á·µ»Ø-1²¢ÇÒÉèÖÃerrno¡££¨¸ü¶àµÄerror ±êºÅÐÅÏ¢¿ÉÒÔ¿´man pageºÍÍ·²¿Îļþ¡££©
    ÏÔÈ»£¬rforkµ÷ÓÃÌṩÁËÒ»¸ö½ÏСÏûºÄµÄfork°æ±¾¡£È±µãÊÇÓÐһЩÖÚËùÖÜÖªµÄ°²È«Â©¶´¡£²¢ÇÒËû¼´Ê¹ÔÚ¿çBSDϵÁÐƽ̨ÉÏÒ²²»ÊǺܼæÈÝ£¬ÔÚ²»Í¬µÄUnix°æ±¾ÖÐÊǶÀÒ»ÎÞ¶þµÄ¡£±ÈÈ統ǰµÄNetBSD-1.5¾Í²»Ö§³Örfork£¬¶øÇÒ²»ÊÇËùÓÐFreeBSD¿ÉÓõıêÖ¾¶¼¿ÉÒÔÓÃÓÚOpenBSD¡£ÕýÒòΪÈç´Ë£¬ÍƼöµÄÏ߳̽ӿÚÊÇʹÓÃpthreads¡£
    3.6 ÔËÐжþ½øÖƳÌÐò
    Ò»¸ö½ø³ÌÈç¹û½ö½öÊǸ¸½ø³ÌµÄ¿½±´ÊÇûÓкܴóÓÃ;µÄ¡£Òò´Ë£¬ÐèҪʹÓÃexecº¯Êý¡£ÕâЩÊÇÉè¼ÆÀ´Ê¹ÓÃеĽø³Ì¾µÏñ´úÌ浱ǰ½ø³ÌµÄ¾µÏñ¡£ºÃ£¬¾Ù¸öÀý×Ó£¬shellÔËÐÐlsÖ¸Áî¡£Ê×ÏÈshell»á°´ÕÕÖ´ÐÐÔËÐÐfork»òvfork½Ó×ÅËü»áµ÷ÓÃexecº¯Êý¡£Ò»µ©exec±»³É¹¦µ÷ÓÃÒ»¸öеĽø³Ì½«ÓÉls´úÌæÔËÐУ¬²¢ÇÒexec×Ô¼ºÃ»Óзµ»Ø¡£Èç¹ûÏñshell»òperlÕâÑùµÄ½Å±¾ÊÇÄ¿±ê³ÌÐò£¬Õâ¸ö¹ý³Ì¾ÍÏñ¶þ½øÖƳÌÐòÔËÐС£ÄÇÀïÓÐÒ»¸ö¸½¼ÓµÄ³ÌÐòµ÷ÓýâÎöÆ÷¡£ÄǾÍÊÇ£¬Í·Á½¸ö×Ö·û½«ÊÇ#£¡ ÀýÈ磬ÏÂÃæչʾÁËÒ»¸ö´ø²ÎÊýµÄ½âÎöÆ÷µ÷Óãº
    CODE:[Copy to clipboard]#! interpreter [arg]
    ÏÂÃæµÄÖ¸ÁʹÓÃ-w²ÎÊýµ÷ÓÃPerl½âÎöÆ÷£º
    CODE:[Copy to clipboard]#!/usr/bin/perl -w
    µ÷ÓÃËüÃǵĻù±¾½á¹¹ÊÇ(Ä¿±ê³ÌÐò)£¬£¨²ÎÊý£©£¬£¨±ØÒªµÄ»·¾³±äÁ¿£©¡£
    CODE:[Copy to clipboard] #include
    #include

    extern char **environ;

    int execl(const char *path, const char *arg0, ... const char *argn, NULL);
    int execle(const char *path, const char *arg0, ... const char *argn, NULL, char *const envp[]);
    int execlp(const char *file, const char *arg0, ... const char *argn , NULL );
    int exect(const char *path, char *const argv[], char *const envp[]);
    int execv(const char *path, char *const argv[]);
    int execvp(const char *file, char *const argv[]);
    ×¢Ò⣻ÔÚʹÓôøÓвÎÊýargµÄexecʱ£¬arg²ÎÊý±ØÐëÊÇÒÔNULL½ØÖ¹µÄ×Ö·û´®£¬¾ÍÈçÔÚarg0£¬arg1£¬arg2 ..argnÖбØÐëÒÔNULL»ò0½áβ¡£Èç¹ûûÓÐÈ·¶¨½áÊø·ûµ÷Óûáʧ°Ü¡£Õâ×Ö·û´®½«³ÉΪĿ±ê³ÌÐòÔËÐеIJÎÊý¡£
    µ÷ÓÃexecʹÓÃ*argv[]²ÎÊýʱÆä±ØÐëÊÇÒ»×é°üº¬½áÊø·ûµÄ×Ö·û´®µÄÊý×飬ËüÃǽ«³ÉΪĿ±ê³ÌÐòÔËÐеIJÎÊý¡£
    ÁíÒ»¸öÇø·Ö¶þ½øÖÆ»ò½Å±¾ÔËÐÐexecµÄÊÇÄ¿±ê³ÌÐòÊÇÈçºÎÖ¸¶¨µÄ¡£Èç¹ûÄ¿±êûÓдø·¾¶£¬º¯ÊýexeclpºÍexecvp»áËÑÑ°ÄãµÄ»·¾³Ä¿Â¼À´²éÕÒÄ¿±ê³ÌÐò¡£¶øÆäËûº¯Êýµ÷Óý«ÒªÇó¾ø¶Ô·¾¶¡£
    ¼òË÷£º
    Êý¾Ý²ÎÊýºÍÐòÁвÎÊý
    array: exect, execv, execvp
    sequence: execl, execle, eseclp
    ·¾¶ËÑË÷ºÍÖ±½ÓÎļþ
    path: execl, execle, exect, execv
    file: execlp, exevp
    Ö¸¶¨»·¾³ºÍ¼Ì³Ð»·¾³
    specify: execle, exect
    inherit: execl, execlp, execv, execvp
    system
    CODE:[Copy to clipboard]
    #include
    int system(const char *string);
    ÁíÍâÒ»¸ö¹Ø¼üµÄÔËÐк¯Êýµ÷ÓÃÊÇsystemµ÷Óá£Õâ¸öº¯ÊýÊ®·ÖÖ±¹Û¡£ÌṩµÄ²ÎÊý½«Ö±½Ó´«Ë͸øshell¡£Èç¹ûÖ¸¶¨µÄ²ÎÊý
    ΪNULL£¬Èç¹ûshell¿ÉÓú¯Êý»á·µ»Ø1·ñÔò·µ»Ø0¡£Ò»µ©µ÷Ó㬽ø³Ç»áµÈ´ýshell½áÊø²¢·µ»ØshellµÄ½áÊø״̬¡£Èç¹û·µ»Ø-1±íʾfork»òwaitpidʧ°Ü£¬127±íʾshellÔËÐÐʧ°Ü¡£
    Ò»µ©µ÷Óã¬Ò»Ð©ÐźÅÀýÈçSIGINTºÍSIGQUIT½«±»ºöÂÔ£¬²¢ÇÒ»á×èÈûSIGCHILD¡£Í¬ÑùÈç¹û×Ó½ø³ÌдÊä³öµ½±ê×¼´íÎóÊä³ö¿ÉÄÜ»áʹµ÷Óýø³Ì±ÀÀ£¡£
    ÏÔÈ»£¬BSD½á¹¹ÌṩÁ˼òµ¥Í¬Ê±·á¸»µÄ½ø³Ì´´½¨½Ó¿Ú¡£ÕâÖÖ³ÉÊìµÄÉè¼Æ¿ÉÒÔºÍÈκÎÒ»¸öÏÖÔÚ²Ù×÷ϵͳæÇÃÀ¡£ÏÂÒ»Õ½«°üÀ¨Ðźźͽø³Ì¹ÜÀí£¬°üº¬×ÊԴʹÓã¬Ï̺߳ͽø³ÌÏÞÖÆ¡£
    µÚËÄÕ ¸ß¼¶½ø³Ì¿ØÖƺÍÐźÅ
    ·­Òë: gvim@chinaunix/bsd
    QUOTE:
    һЩ˵Ã÷£º
    1 ºÜ¸ßÐËÓÐÕâ¸ö»ú»á²ÎÓëµ½ÕâÏî»î¶¯ÖÐÀ´¡£
    2 ÓÉÓÚÓ¢ÓïÓïÑÔµÄÏ°¹ß£¬ÎÄÖÐÓв»ÉÙ¸´ÔÓ³¤¾ä£¬°´ÕÕ×ÖÃæ·­Òë³öÀ´²¢²»ÊʺÏÖйúÈ˵ÄÔĶÁÏ°¹ß¡£ÎÒÔÚ±£ÁôÎÄÕÂÔ­ÒâµÄ»ù´¡ÉÏ×÷ÁËһЩÓïÑÔ×éÖ¯Éϵĵ÷Õû£¬½«´ó²¿·Ö³¤¾ä×Ó×éÖ¯³ÉÖÐÎÄÉó¤µÄ¶Ì¾äÐÍ£¬ËùÒÔÄã»á·¢ÏÖ·­ÒëÆ·Ò»²¿·ÖÄÚÈݲ»ÊÇ°´ÕÕÔ­ÎÄ×ÖÃæ·­ÒëµÄ¡£
    3 ¿¼Âǵ½¸ÃÊéµÄÈëÃÅÒýµ¼×÷Óã¬ÔÚÔ­ÎÄÖÐһЩ±È½ÏÉÙ¼ûµÄ´ÊºóÃæÎÒÌí¼ÓÁËÉÙÐíÒë×¢£¬Ï£Íû¼õÉÙÈëÃÅÐֵܵĸºµ£¡£
    4 ÓÉÓÚ2ÖеÄÓïÑÔµ÷ÕûºÍ3ÖеÄÒë×¢£¬ÊÇÎҵĸöÈËÐÐΪ£¬¿ÉÄÜ»áÓÐÎÒ¶Ô Ô­ÎÄÀí½âÄ£ºý£¬¸ÅÄî²»Ç壬»òÊÇÓïÑÔ×éÖ¯²»Ë³³©µÄµØ·½£¬»¹Çë´ó¼ÒÒ»¶¨¶à¶àÖ¸³öÀ´¡£Ð»Ð»¡£
    ¸öÈËÉùÃ÷£º
    ±¾·­ÒëÆ·ÊÜ¿ØÓÚchinaunix BSD·­ÒëС×é¡£ÈçÐèµ¥¶ÀתÔØ£¬Çë±£Áô¡°·­Òë:gvim@chinaunix/bsd¡±ºÍÒÔÉϼ¸µã˵Ã÷¡£
    4.1 ¸ß¼¶½ø³Ì¿ØÖƺÍÐźÅ
    Ðźţº4 a£ºÒ»ÖÖ¶ÔÏó£¬ÓÃÓÚ´«Êä»ò¸ºÔØÈËÀàÉùÒôÖ®ÍâµÄÐÅÏ¢¡£
    µ½Ä¿Ç°ÎªÖ¹£¬ÎÒÃÇÒѾ­ÌÖÂÛÁ˽ø³ÌµÄ´´½¨ºÍÆäËûϵͳµ÷Óá£ÏÖÔÚÊÇÌÖÂÛÏÂÃæÕâЩÎÊÌâµÄʱºòÁË£ºÄãÒªÔÚ¶à¸ö½ø³Ì¼äͨѶÒÔ»ñµÃ¸üºÃµÄ½ø³Ì¿ØÖÆÁ£¶È£¬»òÕßÒªÆäËû³ÌÐò»ò²Ù×÷ÕßÓÃÐźÅ֪ͨÄãµÄ³ÌÐò¡£ÀýÈ磬Äã¿ÉÄÜÏ£ÍûÄãµÄ³ÌÐòÖØжÁÈ¡ËüµÄÅäÖÃÎļþ¡£»òÕߣ¬ÄãµÄÊý¾Ý¿â³ÌÐòÐèÒªÔÚÍ˳ö֮ǰ½«ÊÂÎñ´ÓÖ÷´æдÈëºó±¸´æ´¢Æ÷£¬È»ºóÔÙÍ˳ö¡£ÕâÁ½¸öÀý×Ó¿ÉÄÜÖ»ÊÇʹÓÃÐźŵĺÜСһ²¿·Ö¡£ËäÈ»ÒѾ­ÓÐÌ×½Ó×Ö£¬ÏÈÈëÏȳö¶ÓÁУ¬¹ÜµÀ£¬ÐźÅÁ¿µÈ¶àÖÖ·½Ê½À´Íê³ÉÀàËƵÄÈÎÎñ£¬µ«ÊÇÎÒÃǽ«°ÑÌÖÂ۵Ľ¹µã·ÅÔÚÐźźÍÆäËü½ø³Ì¿ØÖÆ»úÖÆÉÏ¡£ÔÚÏÖʵÖУ¬Ðźźͽø³Ì¿ØÖÆ»úÖÆ¿ÉÒÔÌṩ´ó²¿·ÖÄãËùÐèÒªµÄÌØÐԺ͹¦ÄÜ¡£
    4.2 ÐźÅ
    ÐźÅÓëÓ²¼þÖжϺÜÏàËÆ¡£µ±É豸ÐèÒªÖжϷþÎñµÄʱºòËü¿ÉÒÔ²úÉúÒ»¸öÓ²¼þÖжÏÀ´Í¨ÖªCPU¡£ÓëÓ²¼þÖжÏÀàËƵģ¬µ±½ø³ÌÐèÒª½«Ò»Ð©Ê¼þ֪ͨ¸øÆäËû½ø³ÌµÄʱºò¿ÉÒÔʹÓÃÐźÅÀ´Íê³É¡£
    ´ó¶àÊýUnixϵͳ¹ÜÀíÔ±»á±È½ÏÊìϤSIGHUPÐźš£µ±Äãͨ¹ýkillÃüÁîÏòºǫ́·þÎñ½ø³Ì·¢³öSIGHUPÐźź󣬴ó¶àÊý½ø³ÌҪôÖØжÁÈëËûÃǵÄÅäÖÃÎļþҪôÖØÐÂÆô¶¯¡£ÕâЩÐźÅÖ®ÖУ¬Ò»Ð©ÓëÓ²¼þÓÐÖ±½Ó¹Øϵ£¬ÈçSIGFPE£¨¸¡µãÒì³££©£¬SIGILL£¨·Ç·¨Ö¸Á£»ÆäËüÔòÊÇÓëÈí¼þÏà¹Ø£¬ÈçSIGSYS£¨Î´ÊµÏÖµÄϵͳµ÷Óñ»µ÷Óã©¡£
    Ò»µ©½ø³Ì½ÓÊÕµ½ÐźÅÖ®ºó£¬¸ÃÐźŵÄÐÐΪÓëÐźű¾ÉíºÍ½ø³Ì¶ÔËüµÄʹÓÃÄ¿µÄÁ½¸öÒòËØÓйء£Ò»Ð©ÐźſÉÒÔ±»×èÈû£¬ºöÂÔ£¬»òÕß²¶»ñ£¬¶øÁíÍâһЩÔò²»¿ÉÒÔ¡£Èç¹û½ø³ÌÐèÒª²¶»ñÒ»¸öÐźŲ¢ÂÄÐÐһЩÏà¹Ø²Ù×÷£¬Äã¿ÉÒÔΪ½ø³ÌÉ趨Õâ¸öÌض¨ÐźŵÄÐźŴ¦Àí¾ä±ú¡£´¦Àí¾ä±ú½ö½öÊÇÒ»¸öº¯Êý£¬ÔÚÕâ¸öÐźű»½ø³Ì½ÓÊÕÖ®ºóµ÷Ó᣻òÕ߸üÈ·ÇеÄ˵£¬´¦Àí¾ä±úÊÇÒ»¸öº¯Êýµ÷Óã¬Äã¿ÉÒÔ¶ÔËü½øÐÐÖ¸ÅÉ£¨specify£©¡£
    µ±ÐźÅûÓÐÖ¸¶¨´¦Àí¾ä±úʱ£¬½«»áÖ´ÐвÙ×÷ϵͳĬÈϵÄÐÐΪ¡£ÕâЩȱʡÐÐΪ¿ÉÒÔÊÇ´ÓÖÕÖ¹½ø³Ìµ½ÍêÈ«ºËÐÄת´¢µÈ²»Í¬µÄ²Ù×÷¡£×¢Ò⣬ÓÐÁ½¸öÐźŲ»Äܱ»²¶»ñ»òºöÂÔ£ºSIGSTOPºÍSIGKILL£¬ÏÂÃæ»á½âÊÍ¡£
    ÔÚBSDϵͳÖж¨ÒåµÄÓÐÐí¶àÐźţ»ÎÒÃÇÌÖÂÛÔÚ/usr/include/sys/signals.h£¨Òë×¢£ºÔÚÎÒµÄFB5.2.1ÖÐÊÇsignal.h£©ÎļþÖж¨ÒåµÄ±ê×¼Ðźš£×¢Ò⣬ NetBSDϵͳÖж¨ÒåµÄÐźÅÊýÁ¿ÉÔ΢¶àÒ»µã£¬²¢ÇÒÎÒÃÇûÓн«ËüÃǵÄÌÖÂÛ·ÅÔÚÕâÀï¡£ËùÒÔÈç¹ûÐèҪʹÓÃij¸öÏÂÃæûÓÐÉæ¼°µ½µÄÐźŵÄʱºò£¬Çë²éÔÄÄãµÄϵͳµÄÍ·Îļþ¡£
    CODE:[Copy to clipboard]#define SIGHUP 1 /* hangup */
    SIGHUPÊÇUnixϵͳ¹ÜÀíÔ±ºÜ³£ÓõÄÒ»¸öÐźš£Ðí¶àºǫ́·þÎñ½ø³ÌÔÚ½ÓÊܵ½¸ÃÐźź󽫻áÖØжÁÈ¡ËüÃǵÄÅäÖÃÎļþ¡£È»¶ø£¬¸ÃÐźŵÄʵ¼Ê¹¦ÄÜÊÇ֪ͨ½ø³ÌËüµÄ¿ØÖÆÖն˱»¶Ï¿ª¡£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGINT 2 /* interrupt */
    ¶ÔÓÚUnixʹÓÃÕßÀ´Ëµ£¬SIGINTÊÇÁíÍâÒ»¸ö³£ÓõÄÐźš£Ðí¶àshellµÄCTRL-C×éºÏʹµÃÕâ¸öÐźű»´ó¼ÒËùÊìÖª¡£¸ÃÐźŵÄÕýʽÃû×ÖÊÇÖжÏÐźš£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGQUIT 3 /* quit */
    SIGQUITÐźű»ÓÃÓÚ½ÓÊÕshellµÄCTRL-/×éºÏ¡£ÁíÍ⣬Ëü»¹ÓÃÓÚ¸æÖª½ø³ÌÍ˳ö¡£ÕâÊÇÒ»¸ö³£ÓÃÐźţ¬ÓÃÀ´Í¨ÖªÓ¦ÓóÌÐò´ÓÈݵģ¨Òë×¢:¼´ÔÚ½áÊøÇ°Ö´ÐÐһЩÍ˳ö¶¯×÷£©¹Ø±Õ¡£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì£¬²¢ÇÒ´´½¨Ò»¸öºËÐÄת´¢¡£
    CODE:[Copy to clipboard]#define SIGILL 4 /* illegal instr. (not reset when caught) */
    Èç¹ûÕýÔÚÖ´ÐеĽø³ÌÖаüº¬·Ç·¨Ö¸Á²Ù×÷ϵͳ½«Ïò¸Ã½ø³Ì·¢ËÍSIGILLÐźš£Èç¹ûÄãµÄ³ÌÐòʹÓÃÁËỊ̈߳¬»òÕßpointer functions£¬ÄÇô¿ÉÄܵĻ°¿ÉÒÔ³¢ÊÔ²¶»ñ¸ÃÐźÅÀ´Ð­Öúµ÷ÊÔ¡££¨×¢Ò⣺ԭÎÄÕâ¾äΪ£º¡°If your program makes use of use of threads, or pointer functions, try to catch this signal if possible for aid in debugging.¡±¡£ÖмäµÄÁ½¸öuse of use of£¬²»ÖªÊÇÔ­ÊéÅÅ°æµÄ覴û¹ÊÇÎÒȷʵûÓÐÃ÷°×ÆäÒâÒ壻ÁíÍ⣬ż¾­³£Ìý˵functions pointer£¬¶ÔÓÚpointer functions£¬googleÁËһϣ¬Ó¦¸ÃÊÇfortranÀïÃæµÄ¶«Î÷£¬²»¹ÜÔõÑù£¬»¹Õæ²»ÖªµÀ£¬È·Çк¬Ò廹ÇëÖªµÀµÄÐֵܸ«Õý¡££©È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì£¬²¢ÇÒ´´½¨Ò»¸öºËÐÄת´¢¡£
    CODE:[Copy to clipboard]#define SIGTRAP 5 /* trace trap (not reset when caught) */
    SIGTRAPÕâ¸öÐźÅÊÇÓÉPOSIX±ê×¼¶¨ÒåµÄ£¬ÓÃÓÚµ÷ÊÔÄ¿µÄ¡£µ±±»µ÷ÊÔ½ø³Ì½ÓÊÕµ½¸ÃÐźÅʱ£¬¾ÍÒâζ×ÅËüµ½´ïÁËijһ¸öµ÷ÊԶϵ㡣һµ©Õâ¸öÐźű»½»¸¶£¬±»µ÷ÊԵĽø³Ì¾Í»áÍ£Ö¹£¬²¢ÇÒËüµÄ¸¸½ø³Ì½«½Óµ½Í¨Öª¡£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì£¬²¢ÇÒ´´½¨Ò»¸öºËÐÄת´¢¡£
    CODE:[Copy to clipboard]#define SIGABRT 6 /* abort() */
    SIGABRTÌṩÁËÒ»ÖÖÔÚÒì³£ÖÕÖ¹(abort)Ò»¸ö½ø³ÌµÄͬʱ´´½¨Ò»¸öºËÐÄת´¢µÄ·½·¨¡£È»¶øÈç¹û¸ÃÐźű»²¶»ñ£¬²¢ÇÒÐźŴ¦Àí¾ä±úûÓзµ»Ø£¬ÄÇô½ø³Ì²»»áÖÕÖ¹¡£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì£¬²¢ÇÒ´´½¨Ò»¸öºËÐÄת´¢¡£
    CODE:[Copy to clipboard]#define SIGFPE 8 /* floating point exception */
    µ±½ø³Ì·¢ÉúÒ»¸ö¸¡µã´íÎóʱ£¬SIGFPEÐźű»·¢Ë͸ø¸Ã½ø³Ì¡£¶ÔÓÚÄÇЩ´¦Àí¸´ÔÓÊýѧÔËËãµÄ³ÌÐò£¬Ò»°ã»á½¨ÒéÄ㲶»ñ¸ÃÐźš£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì£¬²¢ÇÒ´´½¨Ò»¸öºËÐÄת´¢¡£
    CODE:[Copy to clipboard]#define SIGKILL 9 /* kill (cannot be caught or ignored) */
    SIGKILLÊÇÕâЩÐźÅÖÐ×îÄѶԸ¶µÄÒ»¸ö¡£ÕýÈçÄãÔÚËüÅԱߵÄ×¢ÊÍÖп´µ½µÄÄÇÑù£¬Õâ¸öÐźŲ»Äܱ»²¶»ñ»òºöÂÔ¡£Ò»µ©¸ÃÐźű»½»¸¶¸øÒ»¸ö½ø³Ì£¬ÄÇôÕâ¸ö½ø³Ì¾Í»áÖÕÖ¹¡£È»¶ø£¬»áÓÐһЩ¼«ÉÙÊýÇé¿öSIGKILL²»»áÖÕÖ¹½ø³Ì¡£ÕâЩº±¼ûµÄÇéÐÎÔÚ´¦ÀíÒ»¸ö¡°·ÇÖжϲÙ×÷¡±£¨±ÈÈç´ÅÅÌI/O£©µÄʱºò·¢Éú¡£ËäÈ»ÕâÑùµÄÇéÐμ«ÉÙ·¢Éú£¬È»¶øÒ»µ©·¢ÉúµÄ»°£¬»áÔì³É½ø³ÌËÀËø¡£Î¨Ò»½áÊø½ø³ÌµÄ°ì·¨¾ÍÖ»ÓÐÖØÐÂÆô¶¯ÁË¡£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGBUS 10 /* bus error */
    ÈçͬËüµÄÃû×Ö°µÊ¾µÄÄÇÑù£¬CPU¼ì²âµ½Êý¾Ý×ÜÏßÉϵĴíÎóʱ½«²úÉúSIGBUSÐźš£µ±³ÌÐò³¢ÊÔÈ¥·ÃÎÊÒ»¸öûÓÐÕýÈ·¶ÔÆëµÄÄÚ´æµØַʱ¾Í»á²úÉú¸ÃÐźš£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì£¬²¢ÇÒ´´½¨Ò»¸öºËÐÄת´¢¡£
    CODE:[Copy to clipboard]#define SIGSEGV 11 /* segmentation violation */
    CODE:[Copy to clipboard]#define SIGSYS 12 /* non-existent system call invoked */
    SIGSYSÐźŻáÔÚ½ø³ÌÖ´ÐÐÒ»¸ö²»´æÔÚµÄϵͳµ÷ÓÃʱ±»½»¸¶¡£²Ù×÷ϵͳ»á½»¸¶¸ÃÐźţ¬²¢ÇÒ½ø³Ì»á±»ÖÕÖ¹¡£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì£¬²¢ÇÒ´´½¨Ò»¸öºËÐÄת´¢¡£
    CODE:[Copy to clipboard]#define SIGPIPE 13 /* write on a pipe with no one to read it */
    ¹ÜµÀµÄ×÷ÓþÍÏñµç»°Ò»Ñù£¬ÔÊÐí½ø³ÌÖ®¼äµÄͨÐÅ¡£Èç¹û½ø³Ì³¢ÊԶԹܵÀÖ´ÐÐд²Ù×÷£¬È»¶ø¹ÜµÀµÄÁíÒ»±ßȴûÓлØÓ¦Õßʱ£¬²Ù×÷ϵͳ»á½«SIGPIPEÐźŽ»¸¶¸øÕâ¸öÌÖÑáµÄ½ø³Ì£¨ÕâÀï¾ÍÊÇÄǸö´òËãдÈëµÄ½ø³Ì£©¡£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGALRM 14 /* alarm clock */
    ÔÚ½ø³ÌµÄ¼ÆʱÆ÷µ½ÆÚµÄʱºò£¬SIGALRMÐźŻᱻ½»¸¶£¨delivered£©¸ø½ø³Ì¡£ÕâЩ¼ÆʱÆ÷Óɱ¾ÕºóÃ潫»áÌá¼°µÄsetitimerºÍalarmµ÷ÓÃÉèÖá£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGTERM 15 /* software termination signal from kill */
    SIGTERMÐźű»·¢Ë͸ø½ø³Ì£¬Í¨Öª¸Ã½ø³ÌÊÇʱºòÖÕÖ¹ÁË£¬²¢ÇÒÔÚÖÕֹ֮ǰ×öһЩÇåÀí»î¶¯¡£SIGTERMÐźÅÊÇUnixµÄkillÃüÁî·¢Ë͵ÄȱʡÐźţ¬Í¬Ê±Ò²ÊDzÙ×÷ϵͳ¹Ø±ÕʱÏò½ø³Ì·¢Ë͵ÄȱʡÐźš£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGURG 16 /* urgent condition on IO channel */
    ÔÚ½ø³ÌÒÑ´ò¿ªµÄÌ×½Ó×ÖÉÏ·¢ÉúijЩÇé¿öʱ£¬SIGURG½«±»·¢Ë͸ø¸Ã½ø³Ì¡£Èç¹û½ø³Ì²»²¶»ñÕâ¸öÐźŵĻ°£¬ÄÇô½«±»¶ªÆú¡£È±Ê¡ÐÐΪÊǶªÆúÕâ¸öÐźš£
    CODE:[Copy to clipboard]#define SIGSTOP 17 /* sendable stop signal not from tty */
    ±¾ÐźŲ»Äܱ»²¶»ñ»òºöÂÔ¡£Ò»µ©½ø³Ì½ÓÊÕµ½SIGSTOPÐźţ¬Ëü»áÁ¢¼´Í£Ö¹(stop)£¬Ö±µ½½ÓÊÕµ½ÁíÒ»¸öSIGCONTÐźÅΪֹ¡£È±Ê¡ÐÐΪÊÇÍ£Ö¹½ø³Ì£¬Ö±µ½½ÓÊÕµ½Ò»¸öSIGCONTÐźÅΪֹ¡£
    CODE:[Copy to clipboard]#define SIGTSTP 18 /* stop signal from tty */
    SIGSTPÓëSIGSTOPÀàËÆ£¬ËüÃǵÄÇø±ðÔÚÓÚSIGSTPÐźſÉÒÔ±»²¶»ñ»òºöÂÔ¡£µ±shell´Ó¼üÅ̽ÓÊÕµ½CTRL-ZµÄʱºò¾Í»á½»¸¶£¨deliver£©Õâ¸öÐźŸø½ø³Ì¡£È±Ê¡ÐÐΪÊÇÍ£Ö¹½ø³Ì£¬Ö±µ½½ÓÊÕµ½Ò»¸öSIGCONTÐźÅΪֹ¡£
    CODE:[Copy to clipboard]#define SIGCONT 19 /* continue a stopped process */
    SIGCONTÒ²ÊÇÒ»¸öÓÐÒâ˼µÄÐźš£ÈçÇ°ËùÊö£¬µ±½ø³ÌÍ£Ö¹µÄʱºò£¬Õâ¸öÐźÅÓÃÀ´¸æËß½ø³Ì»Ö¸´ÔËÐС£¸ÃÐźŵÄÓÐȤµÄµØ·½ÔÚÓÚ£ºËü²»Äܱ»ºöÂÔ»ò×èÈû£¬µ«¿ÉÒÔ±»²¶»ñ¡£ÕâÑù×öºÜÓÐÒâÒ壺ÒòΪ½ø³Ì´ó¸Å²»Ô¸ÒâºöÂÔ»ò×èÈûSIGCONTÐźţ¬·ñÔò£¬Èç¹û½ø³Ì½ÓÊÕµ½SIGSTOP»òSIGSTPµÄʱºò¸ÃÔõô°ì£¿È±Ê¡ÐÐΪÊǶªÆú¸ÃÐźš£
    CODE:[Copy to clipboard]#define SIGCHLD 20 /* to parent on child stop or exit */
    SIGCHLDÊÇÓÉBerkeley UnixÒýÈëµÄ£¬²¢ÇÒ±ÈSRV 4 UnixÉϵÄʵÏÖÓиüºÃµÄ½Ó¿Ú¡££¨Èç¹ûÐźÅÊÇÒ»¸öûÓÐ×·ËÝÄÜÁ¦µÄ¹ý³Ì(not a retroactive process)£¬ÄÇôBSDµÄSIGCHIDÐźÅʵÏÖ»á±È½ÏºÃ¡£ÔÚsystem V UnixµÄʵÏÖÖУ¬Èç¹û½ø³ÌÒªÇ󲶻ñ¸ÃÐźţ¬²Ù×÷ϵͳ»á¼ì²éÊÇ·ñ´æÔÚÓÐÈκÎδÍê³ÉµÄ×Ó½ø³Ì£¨ÕâЩ×Ó½ø³ÌÊÇÒѾ­Í˳ö(exit)µÄ×Ó½ø³Ì£¬²¢ÇÒÔڵȴýµ÷ÓÃwaitµÄ¸¸½ø³ÌÊÕ¼¯ËüÃǵÄ״̬£©¡£Èç¹û×Ó½ø³ÌÍ˳öµÄʱºò¸½´øÓÐһЩÖÕÖ¹ÐÅÏ¢(terminating information£©£¬ÄÇôÐźŴ¦Àí¾ä±ú¾Í»á±»µ÷Óá£ËùÒÔ£¬½ö½öÒªÇ󲶻ñÕâ¸öÐźŻᵼÖÂÐźŴ¦Àí¾ä±ú±»µ÷ÓÃ(Òë×¢£º¼´ÊÇÉÏÃæ˵µÄ¡°ÐźŵÄ×·ËÝÄÜÁ¦¡±)£¬¶øÕâÊÇÈ´Ò»ÖÖÏ൱»ìÂÒµÄ×´¿ö¡££©
    Ò»µ©Ò»¸ö½ø³ÌµÄ×Ó½ø³Ì״̬·¢Éú¸Ä±ä£¬SIGCHLDÐźžͻᱻ·¢Ë͸ø¸Ã½ø³Ì¡£¾ÍÏñÎÒÔÚÇ°ÃæÕ½ÚÌáµ½µÄ£¬¸¸½ø³ÌËäÈ»¿ÉÒÔfork³ö×Ó½ø³Ì£¬µ«Ã»ÓбØÒªµÈ´ý×Ó½ø³ÌÍ˳ö¡£Ò»°ãÀ´ËµÕâÊDz»Ì«ºÃµÄ£¬ÒòΪÕâÑùµÄ»°£¬Ò»µ©½ø³ÌÍ˳ö¾Í¿ÉÄÜ»á±ä³ÉÒ»¸ö½©Ê¬½ø³Ì¡£¿ÉÊÇÈç¹û¸¸½ø³Ì²¶»ñSIGCHLDÐźŵĻ°£¬Ëü¾Í¿ÉÒÔʹÓÃwaitϵÁе÷ÓÃÖеÄijһ¸öÈ¥ÊÕ¼¯×Ó½ø³Ì״̬£¬»òÕßÅжϷ¢ÉúÁËʲôÊÂÇé¡£µ±·¢ËÍSIGSTOP,SIGSTP»òSIGCONFÐźŸø×Ó½ø³Ìʱ£¬SIGCHLDÐźÅÒ²»á±»·¢Ë͸ø¸¸½ø³Ì¡£È±Ê¡ÐÐΪÊǶªÆú¸ÃÐźš£
    CODE:[Copy to clipboard]#define SIGTTIN 21 /* to readers pgrp upon background tty read */
    µ±Ò»¸öºǫ́½ø³Ì³¢ÊÔ½øÐÐÒ»¸ö¶Á²Ù×÷ʱ£¬SIGTTINÐźű»·¢Ë͸ø¸Ã½ø³Ì¡£½ø³Ì½«»á×èÈûÖ±µ½½ÓÊÕµ½SIGCONTÐźÅΪֹ¡£È±Ê¡ÐÐΪÊÇÍ£Ö¹½ø³Ì£¬Ö±µ½½ÓÊÕµ½SIGCONTÐźš£
    CODE:[Copy to clipboard]#define SIGTTOU 22 /* like TTIN if (tp->t_local<OSTOP) */
    SIGTTOUÐźÅÓëSIGTTINºÜÏàËÆ£¬²»Í¬Ö®´¦ÔÚÓÚSIGTTOUÐźÅÊÇÓÉÓÚºǫ́½ø³Ì³¢ÊÔ¶ÔÒ»¸öÉèÖÃÁËTOSTOPÊôÐÔµÄttyÖ´ÐÐд²Ù×÷ʱ²Å»á²úÉú¡£È»¶ø£¬Èç¹ûttyûÓÐÉèÖÃÕâ¸öÊôÐÔ£¬SIGTTOU¾Í²»»á±»·¢ËÍ¡£È±Ê¡ÐÐΪÊÇÍ£Ö¹½ø³Ì£¬Ö±µ½½ÓÊÕµ½SIGCONTÐźš£
    CODE:[Copy to clipboard]#define SIGIO 23 /* input/output possible signal */
    Èç¹û½ø³ÌÔÚÒ»¸öÎļþÃèÊö·ûÉÏÓÐI/O²Ù×÷µÄ»°£¬SIGIOÐźŽ«±»·¢Ë͸øÕâ¸ö½ø³Ì¡£½ø³Ì¿ÉÒÔͨ¹ýfcntlµ÷ÓÃÀ´ÉèÖá£È±Ê¡ÐÐΪÊǶªÆú¸ÃÐźš£
    CODE:[Copy to clipboard]#define SIGXCPU 24 /* exceeded CPU time limit */
    Èç¹ûÒ»µ©½ø³Ì³¬³öÁËËü¿ÉÒÔʹÓõÄCPUÏÞÖÆ£¨CPU limit£©£¬SIGXCPUÐźžͱ»·¢Ë͸øËü¡£Õâ¸öÏÞÖÆ¿ÉÒÔʹÓÃËæºóÌÖÂÛµÄsetrlimitÉèÖá£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGXFSZ 25 /* exceeded file size limit */
    Èç¹ûÒ»µ©½ø³Ì³¬³öÁËËü¿ÉÒÔʹÓõÄÎļþ´óСÏÞÖÆ£¬SIGXFSZÐźžͱ»·¢Ë͸øËü¡£ÉÔºóÎÒÃÇ»á¼ÌÐøÌÖÂÛÕâ¸öÐźš£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGVTALRM 26 /* virtual time alarm */
    Èç¹ûÒ»µ©½ø³Ì³¬¹ýÁËËüÉ趨µÄÐéÄâ¼ÆʱÆ÷¼ÆÊýʱ£¬SIGVTALRMÐźžͱ»·¢Ë͸øËü¡£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGPROF 27 /* profiling time alarm */
    µ±ÉèÖÃÁ˼ÆʱÆ÷ʱ£¬SIGPROFÊÇÁíÒ»¸ö½«»á·¢Ë͸ø½ø³ÌµÄÐźš£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£
    CODE:[Copy to clipboard]#define SIGWINCH 28 /* window size changes */
    µ±½ø³Ìµ÷ÕûÁËÖն˵ÄÐлòÁÐʱ£¨±ÈÈçÔö´óÄãµÄxtermµÄ³ß´ç£©£¬SIGWINCHÐźű»·¢Ë͸ø¸Ã½ø³Ì¡£È±Ê¡ÐÐΪÊǶªÆú¸ÃÐźš£
    CODE:[Copy to clipboard]#define SIGUSR1 29 /* user defined signal 1 */
    #define SIGUSR2 30 /* user defined signal 2 */
    SIGUSR1ºÍSIGUSR2ÕâÁ½¸öÐźű»Éè¼ÆΪÓû§Ö¸¶¨¡£ËüÃÇ¿ÉÒÔ±»É趨À´Íê³ÉÄãµÄÈκÎÐèÒª¡£»»¾ä»°Ëµ£¬²Ù×÷ϵͳûÓÐÈκÎÐÐΪÓëÕâÁ½¸öÐźŹØÁª¡£È±Ê¡ÐÐΪÊÇÖÕÖ¹½ø³Ì¡£(Òë×¢£º°´Ô­ÎĵÄÒâ˼·­Òë³öÀ´ËƺõÕâÁ½¾ä»°Óеãì¶Ü¡£)
    4.3 ϵͳµ÷ÓÃ
    ÄÇô£¬Äã¸ÃÈçºÎʹÓÃÐźÅÄØ£¿ÓÐʱºòÉõÖÁÄò»×¼ÊÇ·ñÓ¦¸ÃʹÓÃÐźš£ÀýÈ磬µ±Ðźű»½»¸¶µÄʱºò£¬Ò»·½ÃæÄã¿ÉÒÔÔÚÐÐΪ·¢Éú֮ǰ£¬·ÖÎöµ±Ç°Çé¿ö£¬ÕÒ³öÐźŷ¢ÉúµÄÔ­Òò£¬»òÕßÕÒµ½ÕâЩÐźÅÊÇ´ÓÄÄÀï·¢³öÀ´µÄ£»ÁíÒ»·½Ã棬ÆäËûһЩʱºòÄãÒ²¿ÉÒÔÖ»ÊÇÏ£Íû¼òµ¥µÄÍ˳ö³ÌÐò£¬²¢ÇÒÔÚÇå³ýÖ®ºó´´½¨Ò»¸öºËÐÄת´¢Îļþ¡£²Î¼û×îºó²¿·ÖµÄ¼òµ¥´úÂë¿ÉÒÔ»ñµÃÕâЩº¯ÊýµÄ½ÏÏêϸµÄÀý×Ó¡£
    Killº¯Êý
    killº¯Êý¶ÔÓÚÄÇЩ¾­³£ÔÚÃüÁîÐÐʹÓÃkillÃüÁîɱËÀ½ø³ÌµÄÈËÀ´ËµÊÇÔÙÊìϤ²»¹ýµÄ¡£»ù±¾Óï·¨ÊÇ£º
    CODE:[Copy to clipboard]int kill(pid_t pid, int sig);
    Killº¯Êý½«Ö¸¶¨µÄÐźŷ¢Ë͸ø½ø³ÌºÅΪpidµÄ½ø³Ì¡£Ö»Óе±½ø³Ì·ûºÏÏÂÃ漸µãÇé¿öµÄʱºòÐźŲŻᱻ½»¸¶£º
    QUOTE:
    ? ·¢ËÍÓë½ÓÊÕ½ø³ÌÓÐÏàͬµÄÓÐЧÓû§ID£¨UID£©£»
    ? ·¢Ëͽø³ÌÓÐÊʵ±µÄȨÏÞ£¨ÀýÈ磺setuidÃüÁ£»
    ? ·¢Ëͽø³ÌÓг¬¼¶Óû§£¨root£©µÄUID¡£
    ×¢Ò⣺SIGCONTÐźÅÊÇÒ»¸öÌØÀý£¬Ëü¿ÉÒÔÓɵ±Ç°½ø³Ì·¢Ë͸øÈκÎÒ»¸ö¸Ã½ø³ÌµÄÅÉÉú½ø³Ì¡£
    ʹÓò»Í¬µÄµ÷ÓòÎÊýʹµÃkillº¯ÊýµÄÐÐΪ²î±ð·Ç³£´ó¡£ÕâЩÐÐΪÈçÏÂËùÊö£º
    £¨Òë×¢£ºÏÂÃæµÄPIDÓ¦¸ÃÖ¸µÄÊÇÉÏÃækillº¯ÊýÔ­ÐÍÖеÄÄǸöpid£¬ÎÒÔÚÕâÀï×ö³ö˵Ã÷²¢±£ÁôÔ­ÎÄ£©
    QUOTE:
    ? Èç¹ûPID´óÓÚ0£¬²¢ÇÒ·¢Ëͽø³ÌÓÐÊʵ±µÄȨÏÞ£¬ÄÇô²ÎÊýsigÖ¸¶¨µÄÐźŽ«±»½»¸¶¡£
    ? Èç¹ûPIDµÈÓÚ0£¬ÄÇôsigÐźŽ«±»½»¸¶¸øËùÓÐÄÇЩÓë·¢Ëͽø³ÌÓÐÏàͬ×éIDµÄ½ø³Ì¡££¨·¢Ëͽø³ÌͬÑùÐèÒªÂú×ãȨÏÞÐèÇó¡££©
    ? Èç¹ûPIDÊÇ -1£¬ÄÇôÐźŽ«±»·¢Ë͸øËùÓÐÄÇЩÓë·¢Ëͽø³ÌÓÐÏàͬÓÐЧÓû§IDµÄ½ø³Ì£¨²»°üº¬·¢Ëͽø³ÌÔÚÄÚ£©¡£È»¶ø£¬Èç¹û·¢Ëͽø³ÌµÄÓÐЧÓû§IDÓ볬¼¶Óû§£¨root£©µÄÏàͬ£¬ÄÇôÐźű»½»¸¶¸ø³ýÁËϵͳ½ø³Ì£¨ÓÉËüÃǵÄproc½á¹¹ÖеÄp_flagÓòÊÇ·ñÊÇP_SYSTEMÀ´¶¨Ò壩֮ÍâµÄËùÓнø³Ì¡£ÔÚÕâ¸öÌØÊâµÄÀý×ÓÖУ¬Èç¹ûijЩ½ø³Ì²»Äܱ»·¢ËÍ£¨could not be sent£©sigÐźţ¬killº¯Êý²¢²»·µ»ØÒ»¸ö´íÎó¡£
    ? Èç¹ûsigÊÇ0£¬killº¯ÊýÖ»¼ì²é´íÎó£¨ÀýÈ磬ÎÞЧȨÏÞ£¬²»´æÔڵĽø³ÌµÈ£©¡£¸ÃÓ÷¨ÓÐʱºòÓÃÀ´¼ì²éÒ»¸öÖ¸¶¨½ø³ÌÊÇ·ñ´æÔÚ¡£
    ? Èç¹û³É¹¦µÄ»°killº¯Êý·µ»Ø0£¬·ñÔò·µ»Ø-1¡£killµ÷ÓÃʧ°Üʱ»áÔÚerrnoÈ«¾Ö±äÁ¿ÖÐÉèÖÃÏàÓ¦µÄ´íÎóÖµ¡£
    killµÄÁíÒ»¸ö°æ±¾ÊÇraiseº¯Êý£º
    CODE:[Copy to clipboard]int raise(int sig);
    raiseº¯Êý»áÏòµ±Ç°½ø³Ì·¢ËÍsigÐźš£¸Ãº¯ÊýÓô¦²»ÊǺܴó£¬ÒòΪËüÖ»Äܹ»¸øµ±Ç°½ø³Ì·¢ËÍÐźš£raiseº¯Êýµ÷Óóɹ¦Ê±·µ»Ø0£¬·ñÔò·µ»Ø-1¡£µ÷ÓÃʧ°Üʱ»áÔÚerrnoÈ«¾Ö±äÁ¿ÖÐÉèÖÃÏàÓ¦µÄ´íÎóÖµ£¬Ð§¹ûºÍsignalº¯ÊýµÄ·µ»ØÀàËÆ£º£¨Òë×¢£ºÔ­ÎÄÖ»ÓÐÁ½¸öµ¥´Ê¡°as in:¡±£¬ÎÒ²¢²»ÖªµÀ×÷Õß°ÑsignalÁÐÔÚÕâÀïËùÒª±í´ïµÄÒâ˼£¬ËùÒÔÎÒ°´ÕÕÎÒµÄÀí½â+²Â²âÀ´·­ÒëµÄ¡£Èç¹û´ó¼ÒÓÐʲô½¨Ò飬»òÊÇÐèÒª¾ÀÕýµÄ»°£¬ÇëÒ»¶¨¸æËßÎÒ¡££©
    CODE:[Copy to clipboard]void (*signal(int sig, void (*func)(int)))(int);
    4.4 ÐźŴ¦Àí
    ÏÖÔÚÎÒÃÇÖªµÀºÎʱ»á²úÉúÐźţ¬Ò²ÖªµÀÈçºÎ·¢ËÍÐźţ¬ÄÇôÎÒÃÇÔõô´¦ÀíËüÃÇÄØ£¿
    signalº¯Êý
    signalϵͳº¯Êýµ÷ÓÃÌṩÁËÒ»ÖÖ×î¼òµ¥µÄ·¶Àý¡£È»¶ø£¬ÓÉÓÚCÔ­ÐÎÉùÃ÷µÄÔµ¹ÊʹËü¿´ÆðÀ´±Èʵ¼Ê¸´ÔÓ¡£signalº¯Êý½«Ò»¸ö¸ø¶¨µÄº¯ÊýºÍÒ»¸öÌض¨µÄÐźÅÁªÏµ¡£ÕâÀïÊÇFreeBSDÖеĶ¨Òå(ºÍÒ»¸ötypedefÒ»Æð):
    QUOTE:
    typedef void (*sig_t) (int);
    sig_t signal(int sig, sig_t func);
    µÚÒ»¸ö²ÎÊýÊÇÄ¿±êÐźţ¬¿ÉÒÔÊÇÉÏÃæÁоٵÄËùÓÐÐźÅÖеÄÈκÎÒ»¸ö¡£func²ÎÊýÊÇÒ»¸öÖ¸Õ룬ָÏòij¸ö´¦Àí¸ÃÐźŵĺ¯Êý¡£Õâ¸ö´¦ÀíÐźź¯Êý´øÓÐÒ»¸öintÐͲÎÊý£¬²¢Ó¦·µ»Øvoid¡£signalº¯ÊýÖеÄfunc²ÎÊýÒ²¿ÉÒÔÉ趨ΪÏÂÃæµÄһЩֵ£º
    QUOTE:
    SIG_IGN: Èç¹ûfunc²ÎÊý±»ÉèÖÃΪSIG_IGN£¬¸ÃÐźŽ«±»ºöÂÔ¡£
    SIG_DFL: Èç¹ûfunc²ÎÊý±»ÉèÖÃΪSIG_DFL£¬¸ÃÐźŻᰴÕÕÈ·¶¨ÐÐΪ´¦Àí¡£
    sigactionº¯Êý
    sigactionº¯ÊýÊÇÒ»¸ö±Èsignal¸üͨÓõķ½°¸¡£µÚÒ»¸ö²ÎÊýÊÇÄ¿±êÐźš£ÏÂÒ»¸öÃûΪactµÄ²ÎÊý(Ö¸Ïò)sigaction½á¹¹£¬¸Ã½á¹¹°üº¬Ò»Ð©ÓÃÓÚÐźŴ¦ÀíµÄÐÅÏ¢¡£×îºóÒ»¸ö²ÎÊýoactÊÇÒ»¸öÖ¸Õ룬ָÏòÒ»¸ö¿ÉÒÔ´æ´¢ÉÏÒ»´ÎÉèÖÃÐźŴ¦ÀíµÄÐÅÏ¢µÄµØ·½¡£
    QUOTE:
    int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
    sigaction½á¹¹ÓÐÏÂÃæÕâЩ¸ö³ÉÔ±:
    QUOTE:
    void (*sa_handler)(int);
    Õâ¸ö½á¹¹³ÉÔ±ÊÇÒ»¸öÖ¸Ïòº¯ÊýµÄÖ¸Õ룬¸Ãº¯Êý´øÓÐÒ»¸ö¼òµ¥µÄÕûÐβÎÊý£¬²¢·µ»Ø(void)¡£ÕâÓësignalº¯ÊýµÄfunc²ÎÊýÏàͬ£¬Ò²¿ÉÒÔ±»ÉèÖÃΪSIG_IGNºÍSIG_DFL£¬²¢ÇÒÓëµ÷ÓÃsignalµÃµ½µÄЧ¹ûÒ²Ò»Ñù¡£
    QUOTE:
    void (*sa_sigaction)(int, siginfo_t *, void *);
    ¸Ã½á¹¹³ÉÔ±ÊÇÒ»¸öÖ¸Ïòº¯ÊýµÄÖ¸Õ룬·µ»Ø(void)²¢ÐèÒªÈý¸ö²ÎÊý¡£ÕâЩ²ÎÊýÒÀ´ÎΪ£ºÒ»¸öÕûÐβÎÊýÖ¸¶¨Ðźŷ¢ËÍ£»Ò»¸öÖ¸Ïòsiginfo_t½á¹¹µÄÖ¸ÕëÓÃÀ´±£´æ¹ØÓÚÐźŵÄÐÅÏ¢£»×îºóÒ»¸öÒ²ÊÇÒ»¸öÖ¸Õ룬ָÏòÐźŽ»¸¶Ê±µÄÌض¨ÉÏÏÂÎÄ(context)¿Õ¼ä¡£
    QUOTE:
    sigset_t sa_mask;
    ¸Ã½á¹¹³ÉÔ±ÊÇÒ»¸öλÑÚÂë(bitwise mask)£¬ÓÃÀ´Ö¸Ê¾ÐźŽ»¸¶Ê±ÄÄЩÐźŻᱻ×èÈû¡£×èÈûSIGKILLºÍSIGSTOPÐźŵÄ×ö·¨»á±»ºöÂÔ¡£½ÓÏÂÀ´£¬±»×èÈûµÄÐźŽ«±»ÍƳ٣¬Ö±µ½ËüÃDZ»¿ªÆô(unblock)¡£²Î¼ûsigprocmask»ñµÃ¸ü¶à¹ØÓÚÈ«¾ÖÑÚÂë(global masks)µÄÐÅÏ¢¡£
    QUOTE:
    int sa_flags;
    ¸ÃÊý¾Ý³ÉÔ±ÊÇÒ»¸öÓµÓÐÏÂÃæÕâЩ±êÖ¾µÄλÑÚÂ룺
    QUOTE:
    SA_NOCLDSTOP: Èç¹ûSA_NOCLDSTOPλ±»ÖÃλ²¢ÇÒÄ¿±êÐźÅÊÇSIGCHLD£¬³ý·Ç×Ó½ø³ÌÍ˳ö£¬¶øÔÚ×Ó½ø³ÌÍ£Ö¹(stop)ʱ¸¸½ø³Ì½«²»»áÊÕµ½Í¨Öª¡£
    SA_NOCLDWAIT: SA_NOCLDWAIT±êÖ¾»á×èÖ¹×Ó½ø³Ì³ÉΪ½©Ê¬½ø³Ì¡£ÔÚÄ¿±êÐźÅÊÇSIGCHLDµÄʱºòʹÓá£Èç¹û½ø³ÌÉèÖÃÁËÕâ¸ö±êÖ¾£¬½Ó×ŵ÷ÓÃij¸öwaitϵͳµ÷Ó㬽ø³Ì½«±»×èÈûÖ±µ½×Ó½ø³ÌÈ«²¿ÖÕÖ¹£¬×îºó·µ»Ø-1(Òë×¢£º´Ë´¦ÔÚAPUE2edÖеĽâÊÍÊÇ·µ»Ø1)£¬ÉèÖÃerrnoÈ«¾Ö±äÁ¿ÎªECHILD¡£
    SA_ONSTACK: һЩʱºòÐèÒªÔÚÌض¨µÄ¶ÑÕ»ÉϽøÐÐÐźŵĴ¦Àí¡£sigactionϵͳµ÷ÓÃÌṩÁËÕâ¸ö·½Ê½¡£Èç¹û¸Ãλ±»ÖÃ룬ÄÇôÐźŽ«»á±»½»¸¶µ½Ö¸¶¨µÄ¶ÑÕ»ÉÏ¡£
    SA_NODEFER: Èç¹ûSA_NODEFERλ±»ÖÃ룬ÄÇôµ±Ç°ÐźÅÕý±»´¦Àíʱ£¬ÏµÍ³²»»áÆÁ±Î¸ÃÐźÅÒÔºóµÄ½»¸¶¡£
    SA_RESETHAND: Èç¹ûSA_RESETHAND±»ÖÃ룬һµ©Ðźű»½»¸¶£¬ÐźŴ¦Àí¾ä±ú½«±»ÖÃΪSIG_DEF¡£
    SA_SIGINFO: ±»ÖÃλʱ£¬ÓɽṹÌåsigaction µÄ³ÉÔ±sa_sigactionÖ¸ÏòµÄº¯Êý±»Ê¹Óá£×¢Ò⣺ʹÓÃSIG_IGN»òSIG_DFLʱ²»Ó¦¸ÃÉèÖÃÕâ¸ö±êÖ¾¡£³É¹¦µ÷ÓÃsigactionÖ®ºó£¬·µ»Ø0»ò-1£¬²¢ÇÒ½«errorÉèÖóÉÏà¹Ø´íÎóÖµ¡£
    4.5ÐźÅÑÚÂë(×èÈûÓ뿪ÆôÐźÅ)
    ½ø³Ì¿ÉÒÔ×èÈû»òÉèÖÃij¸öÐźš£Ò»µ©¸ÃÐźű»×èÈû£¬¹ØÓÚËüµÄ½»¸¶½«±»ÍƳ٣¬Ö±µ½½ø³ÌÖØпªÆôËü¡£ÔÚÕâÑùµÄÇé¿öÏÂÊǷdz£ÓÐÓõģº½ø³Ì½øÈë´úÂëÖÐij¸ö²¿·Ö£¬²»Äܱ»Öжϵ«ÈÔÏ£Íû¿ÉÒÔ½ÓÊÜ¡¢´¦Àí¿ÉÄܶªÊ§µÄÐźš£¿É¿¿½»¸¶ÐźŵÄÄÜÁ¦Ö±µ½4.2BSDÒýÈëÖ®ºó(²»¾Ã±»SVR3²ÉÓÃ)£¬²Ù×÷ϵͳ²ÅÓµÓиÃÄÜÁ¦¡£
    Ëæ×Å¿É¿¿ÐźŵijöÏÖ£¬ÐźŵÄÉúÃüºÍ½»¸¶(life and delivery)¶¼ÓÐËù¸Ä±ä¡£ÐźſÉÒÔÔÚ֮ǰ²úÉúºÍ½»¸¶¡£ÏÖÔÚ£¬Ò»µ©ÐźÅÊǹÒÆðµÄ(pending)£¬½ø³Ì¿ÉÒÔÔÚ½ÓÊÕËü֮ǰ¾ö¶¨Ôõô´¦Àí¡£½ø³Ì¿ÉÄÜ»áÈ¥´¦ÀíËü£¬Ò²¿ÉÄÜÉèÖÃΪȱʡÐÐΪ£¬»òÕ߶ªÆúÐźÅ
    ×¢Ò⣺Èç¹ûÐí¶àÐźŶ¼¹ÒÆð£¬ÏµÍ³½«»áÊ×ÏȽ»¸¶»á¸Ä±ä½ø³Ì״̬µÄÐźţ¬ÀýÈçSIGBUS¡£
    sigprocmask
    Èκνø³Ì¿ÉÒÔʹÓÃsigprocmaskº¯ÊýÀ´×èÈûÐźš£Óï·¨ÈçÏ£º
    QUOTE:
    int sigprocmask(int how, const sigset_t *set, sigset_t *oset);
    sigprocmaskº¯Êý»áÐ޸Ļò¼ì²é(modify or examine)µ±Ç°ÐźÅÑÚÂë¡£µ±set²ÎÊý²»ÊÇnullµÄʱºò£¬sigprocmaskµÄÐÐΪºÍµÚÒ»¸ö²ÎÊýhowÓйء£º¯ÊýÐÐΪºÍÏà¹ØÒâÒåÁоÙÈçÏ£º
    QUOTE:
    SIG_BLOCK: ÔÚset²ÎÊýÖÐÖ¸¶¨µÄÐźű»×èÈû£¬²¢ÇÒÌí¼Ó½ø×èÈûÐźÅÁÐ±í¡£
    SIG_UNBLOCK: ÔÚset²ÎÊýÖÐÖ¸¶¨µÄÐźŻá´ÓÐźÅÑÚÂëÖÐÒƳý¡£
    SIG_SETMASK: set²ÎÊý½«ÍêÈ«Ìæ´úµ±Ç°ÐźÅÑÚÂë¡£Èç¹ûoset²ÎÊý²»Îªnull£¬Ôò»á±»ÉèÖÃΪǰһ¸öÐźÅÑÚÂë¡£Èç¹ûsetÖµÊÇnull£¬how²ÎÊý±»ºöÂÔ²¢ÇÒÐźÅÑÚÂë±£³Ö²»±ä¡£ËùÒÔ£¬ÎªÁ˼ì²éÐźÅÑÚÂ룬ÎÒÃÇ¿ÉÒÔ½«´«Èëset nullÖµ£¬osetΪ·ÇnullÖµÀ´µ÷ÓÃsigprocmaskº¯Êý¡£Ò»µ©ÑÚÂëµÃµ½Ö®ºó£¬Äã¿ÉÄÜÐèÒª¶ÔËû½øÐмì²é»ò²Ù×÷¡£¿ÉÒÔʹÓÃÏÂÃæµÄ¹ý³Ì(routine)¡£×¢Ò⵱ǰÕâЩ¹ý³ÌÊǺêµÄʵÏÖ¡£
    CODE:[Copy to clipboard]int sigemptyset(sigset_t *set)
    Èç¹ûµ÷ÓÃÕâ¸ö¹ý³Ì£¬set²ÎÊý½«±»³õʼ»¯Ö¸ÏòÒ»¸ö¿ÕÐźż¯¡£
    CODE:[Copy to clipboard]int sigfillset(sigset_t *set)
    Èç¹ûµ÷ÓÃÕâ¸ö¹ý³Ì£¬set²ÎÊý½«±»³õʼ»¯Ö¸ÏòÒ»¸ö°üÀ¨ËùÓÐÐźŵÄÐźż¯¡£
    CODE:[Copy to clipboard]int sigaddset(sigset_t *set, int signo)
    Èç¹ûµ÷ÓÃÕâ¸ö¹ý³Ì£¬signoÖ¸¶¨µÄÐźŽ«±»Ìí¼Ó½øset²ÎÊýÖ¸¶¨µÄÐźż¯¡£
    CODE:[Copy to clipboard]int sigdelset(sigset_t *set, int signo)
    Èç¹ûµ÷ÓÃÕâ¸ö¹ý³Ì£¬signoÖ¸¶¨µÄÐźŽ«´Óset²ÎÊýÖ¸¶¨µÄÐźż¯ÖÐÒƳý¡£
    CODE:[Copy to clipboard]int sigismember(const sigset_t *set, int signo)
    Èç¹ûµ÷ÓÃÕâ¸ö¹ý³Ì£¬Èç¹ûÓÉsignoÖ¸¶¨µÄÐźŴæÔÚÓÚset²ÎÊýÖ¸¶¨µÄÐźż¯ÖÐʱ£¬·µ»Ø1£¬·ñÔò·µ»Ø0¡£
    CODE:[Copy to clipboard]int sigpending(sigset_t *set);
    ½ø³Ì¿ÉÒÔʹÓÃsigpendingº¯ÊýÈ¥²é³öµ±Ç°ÄÇЩÐźű»¹ÒÆð¡£sigpendingº¯Êý»á·µ»ØÒ»¸ö°üº¬ËùÓйÒÆðÐźŵÄÑÚÂë¡£¸ÃÑÚÂë¿ÉÒÔʹÓÃÉÏÃæ½éÉܵĹý³ÌÈ¥¼ì²é¡£sigpending³É¹¦Ê±·µ»Ø0£¬·ñÔò·µ»Ø-1£¬²¢ÇÒÉèÖÃerrnoΪÏàÓ¦´íÎóÖµ¡£
    4.6 ×Ô¶¨ÒåÐÐΪ
    һЩʱºò³ÌÐòÒªÇóÐźŴ¦Àí¾ä±úÔËÐÐÔÚÒ»¸öÌض¨µÄ¶ÑÕ»ÉÏ¡£ÎªÁËʵÏÖÕâ¸öÄ¿µÄ£¬Ò»¸ö±¸Óã¨alternate£©¶ÑÕ»Çø¼ä±ØÐëÓÃsignaltstackº¯ÊýÖ¸³öÀ´¡£Õâ¸öº¯ÊýʹÓõÄÊý¾Ý½á¹¹Îªsignaltstack:
    CODE:[Copy to clipboard]int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
    ËüµÄ½á¹¹³ÉÔ±½âÊÍÈçÏ¡£
    CODE:[Copy to clipboard]char *ss_sp;
    ¸Ã³ÉÔ±Ö¸ÏòÒ»¸ö±»ÓÃ×÷¶ÑÕ»µÄÇøÓò¡£ÏµÍ³ÖÐÓиöMINSIGSTKSZ³£Á¿£¬Ëü¶¨ÒåÁ˽øÐÐÐźŴ¦ÀíʱËùÐèµÄ×îСÄÚ´æ¿Õ¼ä¡£ÏµÍ³Öл¹ÓÐÒ»¸öSIGSTKSZ³£Á¿£¬Ëü¶¨ÒåÁËͨ³£Çé¿öÏ´¦ÀíʱËùÐèÄÚ´æ¿Õ¼ä¡£¸ÃÄÚ´æ¿Õ¼äÐèÒªÔÚµ÷ÓÃsignaltstackº¯ÊýÖ®¼ä·ÖÅä¡£
    CODE:[Copy to clipboard]size_t ss_size;
    Êý¾Ý³ÉÔ±ss_sizeÖ¸³öжÑÕ»µÄ´óС¡£Èç¹ûÕâ¸öÖµÊÇ´íÎóµÄ£¨inaccurate£©£¬µ±ÐźŴ¦Àí¾ä±úÖ´ÐÐʱ£¬ËüµÄÐÐΪ¾Í±äµÃ²»¿ÉÔ¤Öª£¨Äã²»ÄÜÃ÷È·ÖªµÀϵͳÔõÑù´¦ÀíÕâ¸öÐźţ©¡£
    CODE:[Copy to clipboard]int ss_flags;
    ¸ù¾Ýµ÷Óû·¾³£¨calling circumstances£©£¬Êý¾Ý³ÉÔ±ss_flags¿ÉÒÔ¾ßÓÐÉÙÊý¼¸¸ö²»Í¬µÄÖµ¡£Ê×ÏÈ£¬µ±½ø³ÌÏ£ÍûÍ£Óñ¸ÓöÑÕ»µÄʱºò£¬ss_flags»á±»ÉèΪSS_DISABLE¡£ÔÚÕâ¸öÇé¿öÏ£¬ss_spºÍss_size±»ºöÂÔ£¬±¸ÓöÑÕ»±»½ûÖ¹¡£×¢Ò⣬±¸ÓöÑÕ»Ö»ÄÜÔÚµ±Ç°¾ä±úûÓд¦Àíʱ½ûÖ¹¡£
    Èç¹ûʹÓÃÒ»¸önon-nullÖµ×÷ΪossµÄʵ²ÎÈ¥µ÷ÓÃsignaltstack£¬ss_flags½«°üº¬Ö¸Ê¾µ±Ç°×´Ì¬µÄÐÅÏ¢¡£ËüÃÇÊÇ£º
    CODE:[Copy to clipboard]SS_DISABLE: ±¸ÓöÑÕ»±»Í£Óá£
    SS_ONSTACK: ±¸ÓöÑÕ»µ±Ç°ÕýÔÚ±»Ê¹Ó㬲¢ÇÒÏÖÔÚ²»¿ÉÒÔÍ£Óá£
    Èç¹ûµ÷ÓÃsignaltstack µÄossʵ²Î²»ÊÇnull£¬»á·µ»Øµ±Ç°×´Ì¬¡£µ÷Óóɹ¦·µ»Ø0£¬·ñÔò·µ»Ø-1¡£Èç¹ûµ÷ÓÃʧ°Ü£¬errnoÒ²»áÏàÓ¦µÄ±»ÉèÖá£ÒòΪÐźſÉÒÔÔÚÈκÎÒ»µã±»½»¸¶£¬ËùÒÔºÜÄѱ»Ô¤²â¡£³öÓÚÕâ¸öÔ­Òò£¬4.2BSDµÄȱʡÐÐΪÊÇ£ºÖØпªÊ¼±»ÖжϵÄϵͳµ÷Óã¬ÖØÐÂÌṩ»¹Ã»Óб»×ªË͵ÄÊý¾Ý¡£ÔÚ´ó¶àÊýʱºòÕâ¸öÐÐΪÊǺܲ»´íµÄ£¬²¢ÇÒÒ²ÊÇËùÓÐBSDϵͳ²ÉÓõÄȱʡÐÐΪ¡£¿ÉÊÇ£¬Ò²ÓÐһЩº±¼ûµÄÇé¿ö£¬Äã¿ÉÄÜÐèÒª½«Õâ¸öÌØÐԹصô¡£Äã¿ÉÒÔʹÓÃsiginterruptº¯ÊýÍê³ÉÐèÇó¡£Ê¹Óúܼòµ¥:
    CODE:[Copy to clipboard]int siginterrupt(int sig, int flag);
    ½«sig²ÎÊýÉèÖÃΪĿ±êÐźţ¬²¢ÇÒÉèÖÃflagΪÕæ(ÔÚÕâ¸öÇé¿öÏÂÊÇ1)¡£Èç¹ûflag²ÎÊý±»ÉèΪ¼Ù(ÔÚÕâ¸öÇé¿öÏÂÊÇ0)£¬ÄÇôȱʡÐÐΪÊÇÖØÐÂÆô¶¯ÏµÍ³µ÷Óá£
    4.7 µÈ´ýÐźÅ
    sigsuspendº¯Êý¿ÉÒÔÔÝʱ½«µ±Ç°×èÈûÐźż¯¸Ä±äΪÓÉsigmaskÖ¸¶¨µÄÐźż¯¡£¸Ä±äºó£¬sigsuspend»áµÈ´ý£¬Ö±µ½Ò»¸öÐźű»½»¸¶¡£Ò»µ©Ò»¸öÐźű»½»¸¶ºó£¬Ô­ÏȵÄÐźż¯±»»Ö¸´¡£ÓÉÓÚsigsuspendµ÷ÓÃÔÚÐźŽ»¸¶Ö®ºó×ÜÊDZ»ÖÕÖ¹£¬ËüµÄ·µ»ØÖµ×ÜÊÇ-1£¬errno×ÜÊÇEINTR¡£ÏÂÃæÊÇËüµÄÓï·¨£º
    CODE:[Copy to clipboard]int sigsuspend(const sigset_t *sigmask);
    sigwaitº¯ÊýÓÃset²ÎÊýÖ¸¶¨µÄÐźż¯×÷ΪÐźÅÑÚÂë¡£Ëü»á¼ì²é°üº¬ÔÚÕâ¸öÌض¨¼¯ºÏÄÚµÄÊÇ·ñÓÐÈκιÒÆðÐźţ¬Èç¹ûÓеĻ°£¬Ëü½«Çå³ýÕâ¸ö¹ÒÆðµÄÐźţ¬²¢ÔÚsig²ÎÊýÖзµ»ØÕâ¸ö±»Çå³ýÐźŵÄÊýÖµ¡£Èç¹ûûÓÐÐźŹÒÆð£¬sigwait½«Ò»Ö±µÈ´ýÖ±µ½Ö¸¶¨Ðźż¯ºÏÖеÄÈκÎÒ»¸öÐźŲúÉú¡£ÏÂÃæÊÇËüµÄÓï·¨£º
    CODE:[Copy to clipboard]int sigwait(const sigset_t *set, int *sig);
    µ±Ðźű»½»¸¶¸ø½ø³Ì£¨¸Ã½ø³Ì°²×°ÁËÏàÓ¦ÐźŵĴ¦Àí¾ä±ú£©Ê±£¬½ø³Ì½«»áÇл»µ½ÐźŴ¦Àí¾ä±úÖÐÖ´ÐС£ÀýÈ磬¼ÙÉèÄãµÄ³ÌÐò¼àÌýÒ»¸öÓÉÅäÖÃÎļþÉ趨µÄ¶Ë¿Ú¡£ÄãµÄ½ø³Ì°²×°ÁËÒ»¸ö²¶»ñSIGHUPÐźŵĴ¦Àí¾ä±úÀ´ÖØжÁÈ¡ÅäÖÃÎļþ¡£Ò»µ©SIGHUPÐźű»½»¸¶¸øÄãµÄ³ÌÐò£¬½ø³Ì½«»áÖ´ÐÐÐźŴ¦Àí¾ä±úÀ´ÖØжÁÈ¡ÅäÖÃÎļþ¡£ÕâÀï´æÔÚÒ»¸öÎÊÌ⣬ÄãûÓа취֪µÀÔÚ½ø³ÌÖ´Ðйý³ÌÖÐÐźű»½»¸¶µÄÈ·Çеص㡣ÄãËäÈ»¿ÉÒÔʹÓÃһЩÏÂÃæÁгöµÄº¯ÊýÀ´½«·¶Î§ËõС£¬µ«ÊÇÈç¹ûÅöµ½Ïñ´ò¿ªÌ×½Ó×Ö£¬´ò¿ªÁ´½Ó£¬»òÕßÆäËüÄÇЩÊ×ÏÈÐèÒªÇåÀíºó²ÅÄÜÔÚж˿ÚÉϼàÌý ÕâЩÇé¿öµÄʱºòÄØ£¿ÄãÔõÑùÈ·¶¨ÇåÀí»î¶¯ÔÚÄÇÀ↑ʼ£¬Ê²Ã´Ê±ºò¿ªÊ¼£¿Èç¹ûÄãµÄ³ÌÐòÕýÔڵȵ½ÊäÈ룬²¢ÇÒûÓÐÊý¾Ý±»´«½øÀ´µÄʱºò£¬ÏµÍ³µ÷Óý«±»ÖØÆô£¨system call will be restarted£©£¬ËùÒÔ´ÓSIGHUPµÄ·µ»Ø½«»á¼ÌÐøµÈ´ý¡£
    ÕâÊÇʹÓÃsetjmpºÍlongjmpº¯ÊýµÄһЩÇé¿ö£¬ÕâЩº¯ÊýÌṩ·Ç±¾µØ·ÖÖ§(non-local branching)¡£ÎªÁËʹÓÃÕâЩsetjmpº¯Êý£¬ÐèÒªÌṩһ¸öevn²ÎÊý£¬ÈçÏ£º
    CODE:[Copy to clipboard]jmp_buff env;
    int sigsetjmp(sigjmp_buf env, int savemask);
    void siglongjmp(sigjmp_buf env, int val);
    int setjmp(jmp_buf env);
    void longjmp(jmp_buf env, int val);
    int _setjmp(jmp_buf env);
    void _longjmp(jmp_buf env, int val);
    void longjmperror(void);
    Ê×ÏÈ£¬µ÷ÓÃsetjmpµÄ·µ»ØΪ0£¬µ±Ç°µÄ»·¾³½«±»±£´æÔÚenvÖС£½Ó×Å£¬Äã¿ÉÒÔÔÚÐźŴ¦Àí¾ä±úÄÚ²¿µ÷ÓöÔÓ¦µÄlongjmp¡£Ò»µ©µ÷ÓÃÁËlongjmp£¬Ëü½«°ÑÖ´Ðл·¾³»Ö¸´ÎªenvÖб£´æµÄ»·¾³£¬²¢·µ»Øµ½×î³õsetjmp±»µ÷ÓÃʱµÄ»·¾³ÖС£×î³õµÄsetjmpµ÷Ó÷µ»ØÄǸö´«µÝ¸ølongjmpµÄva²ÎÊýµÄÖµ¡£
    ¹ØÓÚsetjmpºÍlongjmpº¯ÊýһЩ˵Ã÷£ºÊ×ÏÈ£¬ÕâÁ½¸öÊDz»ÄÜ»ìÔÓʹÓá£Ò²¾ÍÊÇ˵£¬µ÷ÓÃsetjmpʱ±£´æµÄenv±äÁ¿²»ÄÜ´«µÝ¸ø_longjmpµ÷Óá£ÁíÍ⣬µ÷ÓÃsetjmpµÄº¯Êý·µ»Øºó£¬½ÓÏÂÀ´µ÷ÓÃlongjmp½«»á·¢Éú´íÎó¡£
    The different calls have specific actions that they take. These actions are listed below:
    ²»Í¬µÄµ÷ÓÃÓÐÌض¨µÄÐÐΪ¡£ÕâЩÐÐΪÔÚÏÂÃæÁгöÀ´£º
    CODE:[Copy to clipboard]jmpºÍlongjmp: ËûÃǻᱣ´æ£¨»Ö¸´£©ÐźÅÑÚÂ룬¼Ä´æÆ÷×éºÍ¶ÑÕ»¡£
    CODE:[Copy to clipboard]_setjmpºÍ_longjmp: ËûÃÇÖ»±£´æ(»Ö¸´)¼Ä´æÆ÷×éºÍ¶ÑÕ»¡£
    CODE:[Copy to clipboard]sigsetjmpºÍsiglongjmp: Ö»Òªsavemask²ÎÊý²»ÊÇ0£¬ËûÃǾͱ£´æ(»Ö¸´)¼Ä´æÆ÷×飬¶ÑÕ»ºÍÐźÅÑÚÂë¡£
    ÓÉÓÚһЩԭÒò£¬Èç¹ûenv²ÎÊý±£´æµÄ¶«Î÷±»ÆÆ»µ£¬»òÕßµ÷ÓÃsetjmpµÄº¯Êý·µ»ØÁË£¬longjmpº¯Êý½«»áµ÷ÓÃlongjmperrorº¯Êý¡£Èç¹ûlongjmperrorÒ²·µ»ØÁË£¬³ÌÐò½«±»Òì³£ÖÕÖ¹¡£Äã¿ÉÒÔʹÓÃÓëlongjmperrorÓÐÏàͬԭÐεĺ¯ÊýÀ´×Ô¶¨Òålongjmperrorº¯Êý¡£È±Ê¡µÄlongjmperror»áÔÚ±ê×¼´íÎóÉÏÊä³ö¡±longjmp botch¡±£¬È»ºó·µ»Ø¡£
    4.8 Alarms
    CODE:[Copy to clipboard]unsigned int alarm(unsigned int seconds);
    alarmº¯Êý»ù±¾ÉÏÊÇÒ»¸ö¼òµ¥µÄÄÖÖÓʱÖÓ£¨alarm clock£©£¬Í¬Ê±Ò²ÊÇÒ»¸öºÜÓÐÓõĺ¯Êý¡£ËüÔÊÐí½ø³ÌÔÚ¾­¹ýÖ¸¶¨ÃëÊýÖ®ºóÊÕµ½Ò»¸ö֪ͨ¡£Ò»µ©ÄÖÖÓʱ¼äµ½£¬½ø³Ì½«ÊÕµ½Ò»¸öSIGALRMÐźš£ÈκÎËæºóµÄalarmµ÷Óö¼»á¸²¸ÇÔ­Ïȵĵ÷ÓÃÉ趨¡£alarm²»Ïñsleepº¯Êý£¬Ëü²»»á±»×èÈû¡£
    ËüÓÐһЩ·µ»ØÖµÐèÒªÖµµÃÄã×¢Ò⣺Ê×ÏÈ£¬Èç¹û½ø³ÌûÓÐÉ趨¶¨Ê±Æ÷£¬ÄÇô·µ»ØÖµÊÇ0¡£Æä´Î£¬Èç¹ûÓÐÒ»¸ö¶¨Ê±Æ÷±»É趨µ«»¹Ã»Óг¬Ê±µÄ»°£¬ÄÇô»á·µ»ØÇ°Ò»¸öµ÷Óõ½ÏÖÔÚ»¹ÓеÄÊ£Óàʱ¼ä¡£
    ÏÖÔÚ¿ÉÒÔÉ趨µÄ×î´óʱ¼äÊÇ100,000,000Ãë --- ÒѾ­ÊÇÏ൱³¤µÄʱ¼äÁË¡£
    CODE:[Copy to clipboard]int getitimer(int which, struct itimerval *value);
    getitimerº¯Êý»á¼ìË÷ÓɵÚÒ»¸ö²ÎÊý(which²ÎÊý)ÃèÊöµÄitimerval½á¹¹¡£µÚÒ»¸ö²ÎÊý¿ÉÑ¡µÄÑ¡ÏÔÚÏÂÃæ˵Ã÷£º
    CODE:[Copy to clipboard]int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
    setitmer±ÈÏÈÇ°µÄalarmµ÷ÓÃÌṩÁ˸üÎȶ¨µÄ½Ó¿Ú¡£ÔÚBSDϵͳÉÏ£¬Ã¿¸ö½ø³Ì¿ÉÒÔÌṩÈýÖÖ²»Í¬Ê±¼ä¼ä¸ôµÄ¶¨Ê±Æ÷¡£ËûÃÇÔÚÏÂÃæ½²Êö£º
    CODE:[Copy to clipboard]#define ITIMER_REAL 0
    ʵʱʱÖÓʵʱµÄµÝ¼õ¶ø²»¹Ü½ø³ÌÔÚCPUÉϵÄʵ¼Ê»¨Ïúʱ¼ä(»»¾ä»°Ëµ£¬Ëü×·×Ù×ÔȻʱ¼änatural time)¡£ÕâÔÊÐí½ø³ÌÉèÖÃÒ»¸ö»ùÓÚ×ÔȻʵʱʱ¼ä£¨based on atural real time£©µÄ¶¨Ê±Æ÷¡£µ±ÊµÊ±¶¨Ê±Æ÷³¬Ê±µÄʱºò£¬½ø³Ì»áÊÕµ½SIGALRMÐźš£
    CODE:[Copy to clipboard]#define ITIMER_VIRTUAL 1
    ÐéÄⶨʱÆ÷½öÖ»µÝ¼õ½ø³ÌÔÚCPUÉϵÄÖ´ÐÐʱ¼ä£¬ÔÊÐí½ø³ÌÉ趨һ¸ö»ùÓÚCPUʹÓÃÂʵĶ¨Ê±Æ÷¡£µ±ÐéÄⶨʱÆ÷³¬Ê±µÄʱºò£¬½ø³ÌÊÕµ½SIGVTALRMÐźÅ
    CODE:[Copy to clipboard]#define ITIMER_PROF 2
    Profile¶¨Ê±Æ÷µÝ¼õÔÚCPUÉϵÄÖ´ÐÐʱ¼äºÍ´ú±í½ø³ÌÖ´ÐеÄϵͳµ÷ÓõÄʱ¼ä¡£Õâ¶ÔÓÚÄÇЩҪÇó¾²Ì¬ÆÊÎöµÄ½âÊͳÌÐòÊǺÜÓаïÖúµÄ¡£µ±profile¶¨Ê±Æ÷³¬Ê±µÄʱºò£¬½ø³ÌÊÕµ½SIGPROFÐźš£È»¶ø²¢²»ÏñʵʱºÍÐéÄⶨʱÆ÷ÄÇÑù£¬SIGPROF¿ÉÒÔÔÚϵͳµ÷ÓõÄʱºò±»·¢ËÍ£»½ø³ÌÓ¦¸Ã×¼±¸ºÃÖØÐÂÖ´Ðб»ÖжϵÄϵͳµ÷Óá£
    ±¾Õ½«½¹µã·ÅÔÚÁËÐźſâÉÏ¡£ÕâЩÐźż°ËûÃǵÄʹÓ÷½·¨¶ÔÓÚϵͳ±à³ÌÊǺÜÖØÒªµÄ¡£ÐźÅÔÊÐíϵͳ¹ÜÀíԱ֪ͨӦÓóÌÐòÖØжÁÈ¡ÅäÖÃÎļþ£¬´Ó¶øʹ³ÌÐò¸üÎȶ¨¡£ÆäËüÖØÒªµÄÐźŴ¦Àí¹ÒÆðÔÚ´ò¿ªµÄÎļþÃèÊö·ûÉϵÄI/O²Ù×÷¡£ÏÂÒ»Õ½²ÊöÔõÑùÀûÓÃÕâЩI/OÏà¹ØµÄÐźš£

  7. linuxguy ÓÚ 2007-06-16 00:37:50·¢±í:

    µÚ¶þÕÂ ×Ô¾ÙBSD
    ·­Òë(meilincore@chinaunix)
    2.1 ×Ô¾ÙBSD
    ×Ô¾Ù:ÔÚºÜÉÙ»òÕßûÓÐЭÖúµÄÇé¿öÏÂÖ÷¶¯,ŬÁ¦µÄÌáÉýºÍ·¢Õ¹(°ÑËý×Ô¼º×Ô¾Ùµ½¶¥µã)
    ×Ô¾Ù¼ÆËã»úÊÇÖ¸¼ÓÔزÙ×÷ϵͳµÄ¹ý³Ì,¸Ã¹ý³ÌΪ:³õʼ»¯Ó²¼þ,¶ÁȡһС²¿·Ö´úÂëµ½ÄÚ´æ²¢Ö´ÐÐ.ÕâÒ»µã´úÂë½Ó׿ÓÔØÒ»¸ö´óµÄ²Ù×÷ϵͳ.Ò»µ©²Ù×÷ϵͳ±»¼ÓÔØ,Ëü¾ÍÐèÒª´´½¨×Ô¼ºµÄÕû¸ö»·¾³.Õâ¸ö¹ý³Ì,³Æ×ö×Ô¾Ù¼ÆËã»ú,ÊÇÒ»¸ö¸´ÔÓµÄ,¸ß¶Èƽ̨Ïà¹ØµÄ¹ý³Ì.
    ±¾ÕÂÎÒÃǽ«´Óϸ½ÚÉÏ̽Ë÷FreeBSDÔÚi386ƽ̨ÉϵÄ×Ô¾Ù¹ý³Ì.Ïà¹ØµÄ¸ÅÄîºÍ¹ý³ÌÓëNetBSDºÍOpenBSDÔÚi386ƽ̨ÉϵÄ×Ô¾Ù³ÌÐòÏàÀàËÆ.×¢ÒâһЩ»ã±à´úÂë¶ÔÓÚ´Óʵ¼ÊÉÏÍê³Éi386»ù±¾ÏµÍ³µÄÆô¶¯ÈÎÎñÊDZØÐëµÄ.È»¶ø,ÎÒÃDz»»á´Óϸ½ÚÉϻع˻ã±à´úÂë¶øÊÇÖ÷Òª¼¯ÖÐÔڸ߲ã¸ÅÄîÉÏ,Òò´Ë¾ÍËãÄã²»ÊÇר¼ÒÌÖÂÛÒ²ÊÇÓÐÒâÒåµÄ.
    ×¢Òâ:ËäÈ»±¾ÕÂÖÐÌÖÂÛµÄһЩ¸ÅÄî,ÌرðÊÇ"ʵģʽ"ºÍ"±£»¤Ä£Ê½",²¢²»´æÔÚÓëÏÖ´úÏóPPC»òAlphaÕâЩӲ¼þ¹¹¼Üµ±ÖÐ.µ«ÊÇi386 BSD»ù±¾ÏµÍ³ÊÇĿǰΪֹ×îΪ¹ã·ºµÄϵͳ²¢»á¼ÌÐøÈç´Ë(Ò»¸öÖµµÃ×¢ÒâµÄÀýÍâÊÇMac OS X),¶øÇÒÊÊÓ¦ºÜ¶àÇé¿ö.Èç¹ûÄã¶ÔÆô¶¯ÏµÍ³Ï¸½ÚÓÐÐËȤ,Äã¿ÉÄÜÐèÒª×Ô¶¨ÒåÄÚºË,×Ô¶¨ÒåÎļþϵͳ,É豸Çý¶¯.ͬʱ.i386¹¹¼Ü»¹¹ã·ºÓ¦ÓÃÓëǶÈëʽϵͳ.¸ø¶¨Ò»¸ö°²×°Æ½Ì¨(Given the install base),i386ƽ̨¼°Æä³É¹û½«¼ÌÐøÔÚδÀ´Ò»Ð©ÄêÀïÊÊÓÃ.ÉõÖÁеÄ64λCPUµ½Ä¿Ç°ÎÒÃÇËùÖªµÀµÄΪֹ»¹ÊÇÒ»ÑùµÄÆô¶¯¹ý³Ì.
    2.2 FreeBSDµÄ×Ô¾Ù¹ý³Ì
    FreeBSDʹÓÃÒ»ÖÖÈý¶ÎÆô¶¯¹ý³Ì,µ±Äã´ò¿ª¼ÆËã»úµçÔ´»òÕßÖØÐÂÆô¶¯.Ò»µ©BIOSÍê³Éϵͳ¼ì²â.Ëü½«´ÓDISK0¼ÓÔصÚÒ»¸ö¹ìµÀµ½ÄÚ´æÖÐ.(ÿһ¸ö¹ý³ÌʹÓÃ512×ֽڵijÌÐò.¸ÕºÃÕ¼ÓÃÓ²Å̵ÄÒ»¸ö¿é)µÚÒ»¹ìµÀ¾ÍÊÇÎÒÃÇÊìÖªµÄÖ÷Òýµ¼¼Ç¼(MBR),Ò²¾ÍÊÇboot0,µÚÒ»¸ö±»¼ÆËã»ú¼ÓÔØÖ´ÐеijÌÐò.µÚ¶þ¸ö³ÌÐò,boot1,ÓÖÊǹ̶¨´óСΪ512×Ö½Ú²¢ÖªµÀÈçºÎ¶ÁÈ¡sliceÐÅÏ¢ºÍ¼ÓÔØboot2.Ò»µ©boot2±»¼ÓÔØËü¾ÍÓÐÄÜÁ¦Ö±½ÓÆô¶¯ÏµÍ³»òÕß¼ÓÔؼÓÔØÆ÷(loader program),¼ÓÔØÆ÷¹Ì¶¨´óСΪ512×Ö½Ú.Ï൱¾«Ö²¢±»Éè¼ÆÀ´ÔÊÐí¶Ôϵͳ¾«È·Æô¶¯×÷¸ü¶àµÄ¿ØÖÆ.
    boot0
    ´ÓBIOS¼ÓÔصĵÚÒ»¸ö³ÌÐò,boot0,ÊÇÒ»¸ö¹Ì¶¨´óСΪ512×Ö½ÚµÄС³ÌÐò,λÓÚÖ÷Òýµ¼¼Ç¼(MBR).Äã¿ÉÒÔÔÚ/usr/src/sys/boot/i386/bootÕÒµ½¸Ã³ÌÐòµÄÔ´´úÂë.µ±È»ÏÖ´ú¼ÆËã»úµÄBIOS¿ÉÒÔÉèÖóɴӸ÷ÖÖ²»Í¬µÄÇý¶¯Æ÷°üÀ¨¹âÇý,ÈíÇýºÍIDEÓ²ÅÌÆô¶¯.¶ÔÓÚ±¾ÕÂÀ´Ëµ,ÎÒÃǽ«¼ÙÉè¼ÆËã»úÊÇ´ÓµÚÒ»¸öÓ²ÅÌÆô¶¯µÄ.Ò²¾ÍÊÇ´ÅÅÌÇý¶¯Æ÷0,C:,»òÕß,¶ÔBIOS¶øÑÔ,0X80.
    ´ÓµÚÒ»¸ö´ÅÅ̵ĵÚÒ»¸öÉÈÇø,512×Ö½Ú±»¶Áµ½ÄÚ´æλÖÃ0X7C00.È»ºó,BIOS»á¼ìÑéÄÚ´æλÖÃ0X7DFEµÄÊý×Ö0XAA55(Æô¶¯¿é´úÂëµÄ×îºóÁ½¸ö×Ö½Ú).Õâ¸öλÖÃË÷ÒýÊý×Ö0XAA55¶Ôi386ÊÇÈç´ËÖØÒª¶ø±»¸³ÓèÁËÒ»¸öÊʵ±µÄÃû×Ö----ħÊý.ÕâÒâζ×Å,Ö»ÓÐÔÚÕâ¸öÊý×Ö´æÔÚÓÚÄÚ´æλÖÃ0X7DFEµÄÇé¿öÏÂBIOS²Å»á°Ñ¿ØÖÆȨתÒƸøboot0±»°²ÖõÄÄÚ´æλÖÃ0X7C00.
    ÕâÌá³öÁËÔÚIntel i386ϵͳÉϱàдÆô¶¯´úÂëµÄÒ»¸öÒªµã:¼Ç×ÅÄã´úÂëÖеĵÚÒ»¸öÄÚ´æλÖô¦(0X0)±ØÐëÊÇÒ»ÌõÖ¸Áî.²¢ÇÒ,µ±BIOSתÒÆ¿ØÖÆȨ¸øÄÚ´æλÖÃ0X7C00µÄʱºò¸ÃλÖñØÐë°üº¬Ò»ÌõÖ¸Áî.Õâ¿ÉÒÔÊÇÒ»¸ö¼òµ¥µÄÌøתµ½ÆäËûλÖûòÕßÆô¶¯³ÌÐòÖ÷¹ý³ÌµÄÈë¿Ú.ÁíÍâµ±Æô¶¯´úÂë±»Ö´ÐÐʱÄã¶ÔCPUµ½µ×ÔÚ¸ÉʲôÊÇÍêȫûÓпØÖƵÄ.ÒòΪÕâʱºò¼Ä´æÆ÷µÄ״̬ҲÊÇδ֪µÄ,Äã±ðÖ¸ÍûÒѾ­ÉèÖúÃÁËÊʵ±µÄ¶ÎºÍÕ»¼Ä´æÆ÷.Õâ¸öС¹¤×÷±ØÐëÓÉÆô¶¯´úÂëÀ´Íê³É.ÒòΪĿǰ²Ù×÷ϵͳ»¹Ã»ÓмÓÔØÆðÀ´;ËùÓеÄI/O±ØÐëͨ¹ýBIOS¹ý³ÌÀ´Íê³É(Intel CPU ÎĵµÓÐÒ»¸öÍêÕûÁбí)
    ÔÚboot0±»¼ÓÔز¢»ñµÃ¿ØÖÆȨÒÔºó,Ëü½«ÉèÖÃ×Ô¼ºµÄ¼Ä´æÆ÷ºÍÕ»ÐÅÏ¢.½Ó×Å,boot0Öض¨Î»×Ô¼ºµ½Ò»¸öµÍµÄÄÚ´æλÖò¢Ìøתµ½ËüÖ÷¹ý³ÌµÄеÄÆ«ÒƵØÖ·.Õâʱºòboot0±ØÐ뻹Äܹ»¶¨Î»ºÍÆô¶¯ÆäËû´ÅÅ̺ͷÖÇø,ÔÚËüµÄ×îºó,boot0´úÂëÓÐÒ»¸öСµÄ³£¼ûµÄ¿ÉÆô¶¯ÀàÐÍÁбí,ËüÃǶ¼±ØÐëÔÚÆä×îºóÁ½¸ö×Ö½Ú°üº¬Ä§Êý²Å¿ÉÆô¶¯.
    ×îÖÕ,µ±boot0Íê³ÉËÑË÷¿ÉÆô¶¯´ÅÅ̺ͷÖÇøÒÔºó,½«ÌáʾÓû§×÷Ò»¸öÑ¡Ôñ.Èç¹ûÔÚÒ»¸ö¶ÌµÄʱ¼ä¶ÎÀïÃæûÓÐÈκÎÑ¡Ôñ»òÕßij¸ö¼ü±»°´ÏÂ,boot0½«¼ÓÔØÏÂÒ»¸öÆô¶¯¿éµ½Äڴ沢תÒÆ¿ØÖÆȨ¸øËü.»¹ÊÇÄÇÑù,Õâ¿ÉÒÔÊÇÈκβÙ×÷ϵͳµÄÆô¶¯´úÂë----Äã¿ÉÒÔÉèÖÃËü¼ÓÔØLinuxµÄ×Ô¾Ù´úÂë»òÕßÉõÖÁDOS.¶ÔBSD¶øÑÔ,Ͻ׶εÄÆô¶¯³ÌÐòÊÇboot1.
    boot1
    Óëboot0ÏàËÆ,boot1ÊÇÒ»¸ö·Ç³£¼òµ¥ÇÒ×ܹ²²Å512×ֽڵijÌÐò;ËüÒ²±ØÐëÔÚ×îºóÁ½¸ö×Ö½Ú°üº¬Ä§Êý.ÆäÄ¿µÄÊǶ¨Î»ºÍ¶ÁÈ¡FreeBSD´ÅÅÌ·ÖÇø,½Ó×Ŷ¨Î»ºÍ¼ÓÔØboot2.
    ËäÈ»ÔÚ´ó¶àÊýÇé¿öÏÂ,boot1ÊDZ»boot0¼ÓÔصÄ,µ«ÊÇÕâ¸ö˳Ðò²»±ØÊÇΨһ´æÔÚµÄÑ¡Ïî.ÓÉÓÚFreeBSDµÄÁé»îÐÔÄã¿ÉÒÔʹÓñ»³ÆΪרÓôÅÅ̵ÄÑ¡Ïî(˵µÄ¾ÍÊÇÄǸö¸üΪÉùÃûÀǼ®µÄΣÏÕµÄרÓôÅÅÌ)Ò»¸öרÓôÅÅ̾ÍÊÇÕû¸ö´ÅÅÌ,»òÕßBIOSµÄÿһ¸öÉÈÇø¶¼ÊôÓÚFreeBSD.ͨ³£,Äã»áÔÚPC»ú´ÅÅÌÉÏ·¢ÏÖÒ»¸öfdisk±í,»òÕßÒ»¸öslice±í,ÓÃÀ´ÔÊÐí¶à¸ö²Ù×÷ϵͳ´Óµ¥¸öPC»ú´ÅÅÌÉÏÆô¶¯.Äã¿ÉÒÔÑ¡ÔñʹÓÃÒ»¸öרÓôÅÅ̲¢ÇÒÖ±½Ó´Óboot1Æô¶¯;boot0ÍêȫûÓбØÒª°²×°ÔÚ´ÅÅÌÉÏ.²»¹ÜÄã²ÉÓÃÄÇÖÖʵÏÖ,boot1¶¼ÊǷdz£ÖØÒªµÄÆô¶¯¿é²¢ÐèÒª±»¼ÓÔØ
    Boot1±»¼ÓÔص½ÄÚ´æλÖÃ0X7C00²¢²Ù×÷ÔÚʵģʽ;»·¾³Ã»ÓÐÉèÖÃ,¼Ä´æÆ÷Ò²ÔÚδ֪״̬.boot1±ØÐëÉèÖúÃÕ»,¶¨ÒåËùÓеļĴæÆ÷,²¢Ê¹ÓÃBIOS½Ó¿ÚÀ´×öËùÓеÄI/O.Ò»µ©boot1±»¼ÓÔص½ÄÚ´æ²¢ÇÒ¿ØÖÆȨÒѾ­×ªÒƹýÀ´.Ëü±ØÐëÔÚ×Ô¼ºµÄµÚÒ»¸öÄÚ´æλÖÃ(0X0)°üº¬Ò»ÌõÖ¸Áî.ËùÓÐÕâЩ¶¼³É¹¦ÒÔºó,boot1½«¶Áȡϵͳ´ÅÅÌËÑË÷boot2.
    Ò»µ©boot2±»¼ÓÔØ.³ÌÐò±ØÐëÉèÖúÃboot2µÄ»·¾³;boot2ÊÇÒ»¸öBTX¿Í»§¶Ë(boot Extender Æô¶¯À©Õ¹Æ÷)²¢ÇÒ±ÈÇ°ÃæµÄboot0ºÍboot1ÉÔ΢¸´ÔÓһЩ.boot1ÐèÒª¼ÓÔØboot2µ½ÄÚ´æλÖÃ0X9000²¢ÇÒËüµÄÈë¿ÚÊÇ0X9010.È»ºó,ÉõÖÁÔÚboot1¼ÓÔز¢×ªÒÆ¿ØÖÆȨ¸øboot2ÒÔºó,»¹ÓÐÒ»¸ö±»boot2ʹÓõĹý³Ì´æÔÚÓÚboot1.Èç¹ûÄãÔĶÁboot1µÄÔ´´úÂëÄã»á×¢Òâµ½Ò»¸öº¯Êýºô½Ðxread.Õâ¸öº¯ÊýÓÃÀ´Í¨¹ýBIOS´Ó´ÅÅ̶ÁÈ¡Êý¾Ý.Òò´Ë,boot1ÔÚÄÚ´æÖеÄλÖÃÊǷdz£ÖØÒªµÄ²¢ÇÒboot2±ØÐëÖªµÀËüµÄλÖòÅÄÜÕý³£¹¤×÷.
    boot2
    ĿǰΪֹÎÒÃÇÒѾ­¼ÓÔØÁËÁ½¸öÆô¶¯¿éºÍÒ»¸ö´óµÄ³ÌÐòµ½ÄÚ´æÖÐ.תÒÆÁËÁ½´Î¿ØÖÆȨÇÒÿ´Î¶¼ÖØÐÂÉèÖÃÁËÒ»¸öСµÄ»·¾³(Õ»,¶Î¼Ä´æÆ÷µÈµÈ...),²¢Í¨¹ýBIOSÖ´ÐÐÁËһЩÓÐÏÞµÄI/O.ÎÒÃÇÒÀȻûÓд¥¼°µ½¼ÓÔزÙ×÷ϵͳµÄµã×ÓÉÏÀ´.Èç¹ûÄã¿´¹ýFreeBSDÆô¶¯¹ý³ÌÖеÄÆÁÄ»,µ½Ä¿Ç°ÎªÖ¹Äã¿ÉÄÜ¿´¼ûF1ºÍ¿É°®µÄASCIIÂÝÐýÏß.Äã¿ÉÄÜÓ¡Ïóµ±Öв»»á¾õµÃÕⶼÊÇ,µ«ËûµÄÈ´ÊÇÔ­Ô­±¾±¾,¾«È·Ô­ÉúµÄ»ã±à´úÂëÈÃÕâ¸öÆô¶¯¹ý³Ì¿´ÆðÀ´Èç´ËÓÄÑŶøÇáÇÉ
    ÏÖÔÚµ½×îºóÒ»¸ö×Ô¾Ù¹ý³ÌÁË,boot2.×îºó½×¶ÎºÜ¼òµ¥²¢ÇÒ¿ÉÒÔ·¢ÉúÁ½¼þÊÂÖеÄÒ»¼þ:boot2¼ÓÔؼÓÔØÆ÷(ÎÒÃǽ«ÔÚÏÂÒ»½ÚÌÖÂÛÕâ¸ö)»òÕß,boot2¼ÓÔØÄں˲¢Ö±½ÓÆô¶¯¶øÍêÈ«²»Ê¹ÓüÓÔØÆ÷.Èç¹ûÄãÔø¾­´ò¶Ïboot2µÄ¼ÓÔعý³Ì,Äã¿ÉÄÜÒѾ­¿´¹ýÕâ¸öboot2Êä³öµ½ÆÁÄ»µÄ¶«Î÷:
    CODE:[Copy to clipboard]>>FreeBSD/i386 BOOT
    Defualt: 0:ad(0,a)/boot/loader
    boot:
    Èç¹ûÄã°´Ï»سµ,boot2»á¼òµ¥µÄ¼ÓÔØĬÈϼÓÔØÆ÷,ÏóÁгöµÄÄÇÑù.µ±È»,Èç¹ûÄãÖ»ÊǼüÈë"boot kernel"½Ó×ÅËü¾Í»á¼ÓÔØÄÚºË(/kernel)²¢Æô¶¯.Äã¿ÉÒÔ,µ±È»,¸Ä±äÕâЩĬÈÏÖµ.Èç¹ûÄãÏëÕÒµ½¸ü¶àÐÅÏ¢ÇëÔĶÁboot( 8 )µÄÎĵµ
    ÔçÏÈÎÒÃÇÌáµ½¹ýboot2ÊÇÒ»¸öBTX¿Í»§¶Ë(Boot Extender Æô¶¯À©Õ¹Æ÷).ÕâÁôÏÂʲôÄØ?BTXÌṩÕßÊÇÒ»¸ö»ù´¡µÄÐéÄâ86Ñ°Ö·»·¾³.¸ÃÀ´ÌÖÂÛÒ»ÏÂIntelÓ²¼þÄÚ´æÑ°Ö·µÄÀúÊ·ÁË.
    ĿǰΪֹÎÒÃǶ¼±ÜÃâÌá¼°ÄÚ´æÑ°Ö··½Ê½.Õâ¿ÉÄÜ»á±È½Ï»ìÏýÒòΪIntel CPUÒªÈÌÊÜÀúÊ·ÎÊÌâ,²¢ÇÒÆô¶¯´úÂëÉè¼Æͨ³£ÊÇÁô¸øÄÇЩ·Çд²»¿ÉµÄ¿ª·¢ÈËÔ±.³ý·ÇÄãÒªÒÆÖ²Ò»¸öϵͳµ½ÐµĹ¹¼ÜÒò¶øÄãµÄ´úÂë±ØÐëÍêÈ«ÊÇƽ̨ÒÀÀµµÄ,ͨ³£,Ò»¸ö³ÌÐòÔ±ÓÀÔ¶Ò²²»»á±»ÅÉȥдһ¸öÆô¶¯¼ÓÔØÆ÷.È»¶øÆô¶¯¹ý³Ì¶ÔÓÚÐèÒª±àдÉ豸Çý¶¯´úÂë»òÕßÄÚºËÏà¹Ø±à³ÌµÄ¿ª·¢ÈËÔ±À´ËµÊǷdz£ÖØÒªµÄ.Õâ¾ÍÊÇһЩ¿ª·¢ÈËÔ±»áÔÚÕâÀïÅöµ½BTX¼ÓÔØÆ÷.
    ´óÔ¼´Ó8088µ½80186,Intel´¦ÀíÆ÷Ö»ÓÐÒ»ÖÖ·½Ê½½øÐÐÄÚ´æÑ°Ö·,½Ð×öʵģʽ.ÕâЩÔçÆÚµÄCPUÓо޴óµÄ16λ¼Ä´æÆ÷ºÍ20λÄÚ´æµØÖ·.È»ºóÎÊÌâÀ´ÁË,ÄãÈçºÎÔÚ16λµÄ¼Ä´æÆ÷Öй¹³ÉÒ»¸ö20λµÄµØÖ·ÄØ?´ð°¸¾ÍÊÇ,ÓÃÁ½¸ö16λ¼Ä´æÆ÷,Ò»¸öÌṩ»ùµØÖ·¶øÁíÒ»¸öÌṩÕâ¸ö»ùµØÖ·µÄÆ«ÒÆÁ¿.»ùµØÖ·¼Ä´æÆ÷±»×óÒÆ4λÕâÑùµ±Á½¸öºÏ²¢µÄʱºòÒ»¸öÁîÈ˾ªÒìµÄ20λµØÖ·¾Í±»Ëã³öÀ´ÁË.ÓÃËùÓÐÕâЩÁéÇɵĶμĴæÆ÷ºÍλÒÆ,ÔçÆÚµÄIntel´¦ÀíÆ÷¿ÉÒÔÑ°Ö·×ܹ²1M¿Õ¼ä.½ñÌìÕâÉõÖÁ»¹²»¹»ÈÝÄÉÒ»¸öWordÎĵµ,×÷Ϊһ¸ö°ÁÂýµÄÀý×Ó.
    Ò»µ©80386ϯ¾í¶øÀ´,Ñ°Ö·1M¾Í²»¹»ÁË;Óû§ÐèÒª¸ü¶àµÄÄÚ´æ²¢ÇÒ³ÌÐò¿ªÊ¼Ê¹Óøü¶àµÄÄÚ´æ.Ò»ÖÖеÄѰַģʽ½Ð×ö±£»¤Ä£Ê½±»·¢Ã÷Á˳öÀ´.еı£»¤Ä£Ê½ÔÊÐíÑ°Ö·¸ß´ï4GÄÚ´æ.
    з½Ê½µÄÁíÒ»¸öºÃ´¦¾ÍÊǶÔÓÚ»ã±à³ÌÐòÔ±À´Ëµ¸üÈÝÒ×ʵÏÖ.Ö÷ÒªµÄÇø±ð¾ÍÊÇÄãµÄÀ©Õ¹¼Ä´æÆ÷(ËüÃÇ»¹ÊÇÄÇЩ16Ϊ¼Ä´æÆ÷²»¹ý386ÏÖÔÚÊÇ32λÁË)¿ÉÒÔ°üº¬Ò»¸öÍêÕûµÄ32λµØÖ·.ÉõÖÁÄãÒÔÇ°µÄ¶Î¼Ä´æÆ÷ÏÖÔÚ¶¼±»±£»¤ÁË.³ÌÐò²»ÄÜдÈë»òÕ߶ÁÈ¡ËûÃÇ.ÕâЩ¶Î¼Ä´æÆ÷ÏÖÔÚ±»ÓÃÀ´¶¨Î»ÄãµÄÄÚ´æÖеÄÕæʵµØÖ·,Õâ¸ö¹ý³Ì°üÀ¨È¨ÏÞÑéУλ(¶ÁдµÈµÈ...)²¢ÒýÈëÁËMMU(memory management unitÄÚ´æ¹ÜÀíµ¥Ôª)
    ÏÖÔڻص½BTX¿Í»§¶ËµÄÎÊÌâ.ÎÒÃÇʹÓÃBTX³ÌÐòÓÐʲôºÃ´¦ÄØ?ºÜ¼òµ¥:Áé»îÐÔ.BTXÌṩÁË×ã¹»µÄ·þÎñÒò´ËÒ»¸öСµÄ,ÓµÓÐƯÁÁ½Ó¿ÚµÄ³ÌÐò¿ÉÒÔ±»Ð´³ö²¢ÄÜÔÚ¼ÓÔØÄں˵Ĺý³ÌÖзdz£Áé»î.ÔÚFreeBSDϵͳÖÐÕâ¾ÍÊǼÓÔØÆ÷.´ÓÏÂÒ»½ÚÓн«¿´µ½¼ÓÔØÆ÷ÕæµÄÊǶàôµÄƯÁÁºÍÁé»î.Òò´Ë±¾½ÚÓàϵIJ¿·ÖÎÒÃǽ«º­¸Ç»ù±¾µÄBTX·þÎñ.
    BTX·þÎñ¿ÉÒÔ±»¹éÀൽÁ½¸ö»ù±¾×é.µÚÒ»×éÊÇÓÉÖ±½Óº¯Êýºô½Ð(ÀàËÆÓÚϵͳºô½Ð)ÌṩµÄϵͳ·þÎñ.ÁíÍâÒ»×éÊDz»ÓÉ¿Í»§¶ËÖ±½Óºô½ÐµÄ»·¾³·þÎñ.ÕâЩ·þÎñÀàËÆÓÚÒ»¸ö²Ù×÷ϵͳ,È»¶øBTX³ÌÐòÊÇÏóµ¥ÈÎÎñ»·¾³ÄÇÑù²Ù×÷µÄ.
    Ö±½Óºô½ÐÌṩµÄBTX·þÎñÓÉÁ½¸öϵͳºô½Ð×é³É.exit ºÍ exec.µ±exit±»ºô½ÐµÄʱºòBTX¼ÓÔØÆ÷½áÊø²¢ÇÒϵͳÖØÆô.×îºóµÄÒ»¸öϵͳºô½Ðexec½«×ªÒÆ¿ØÖÆȨ¸øÌṩµÄµØÖ·.ÔÚ¸ö¿ØÖÆȨÒƽ»ÊÇÔÚ³¬¼¶Óû§Ä£Ê½ÏÂÍê³É,²¢ÇÒеijÌÐò¿ÉÒÔÀ뿪±»±£»¤µÄCPUģʽ
    BTX¼ÓÔØÆ÷ÌṩµÄ»·¾³·þÎñÊǷdz£»ù´¡µÄ.BTX¼ÓÔØÆ÷´¦ÀíËùÓеÄÖжÏ.ÕâЩÖжϱ»Ë͵½ºÏÊʵÄBIOS´¦ÀíÆ÷.BTXҲģÄâBIOSÀ©Õ¹ÄÚ´æ´«Êäºô½Ð.²¢ÇÒ×îºó»¹Ä£ÄâһЩ»ã±àÖ¸Áî,ËüÃÇÊÇpushf,popf,cli,sti.iretºÍhlt.
    ×îºó×¢Òâ:ËùÓбàдÀ´ÔËÐÐÔÚBTX»·¾³ÖеijÌÐò¶¼±ØÐë±»±àÒë²¢Á¬½Óµ½btxld.¸ü¶àÐÅÏ¢ÇëÔĶÁBTX¼ÓÔØÆ÷Áª»úÊÖ²á
    2.3¼ÓÔØÆ÷
    2.4¿ªÊ¼Äں˷þÎñ
    ÎÒÃÇÖÕÓÚµ½ÁËÄÚºËÒѾ­±»ÔØÈëÄÚ´æ,CPUµÄ¿ØÖÆȨµÄÒ²±»´«¸øËüµÄ½×¶ÎÁË.Ò»µ©Äں˱»¼ÓÔØ,ËüÐèÒª´Ó³õʼ»¯¿ªÊ¼ÔËÐв¢×¼±¸ÏµÍ³½øÈë¶àÈÎÎñģʽ.¸Ã³õʼ»¯°üÀ¨Èý¸öÖ÷Òª×é³É²¿·Ö.Ç°Á½¸öÊÇ»úÆ÷Ïà¹ØµÄ,ÓÉcºÍ»ã±à»ìºÏ±àд³É.Ç°Á½¸ö½×¶Î×¼±¸ÏµÍ³ºÍ³õʼ»¯CPUÄÚ´æ¹ÜÀíµ¥Ôª(MMU,Èç¹ûËü´æÔÚ)²¢´¦ÀíÓ²¼þ³õʼ»¯.×îºóÒ»¸ö½×¶Î¼ÌÐø½¨Á¢»ù±¾Äں˻·¾³²¢ÈÃϵͳ׼±¸ºÃÔËÐнø³Ì1(/sbin/init).Ç°Á½¸ö½×¶ÎÊǸ߶Èƽ̨ÒÀÀµµÄ.ÒòΪÿһÖÖ¹¹¼Ü¶¼ÓÐÏà¹ØÐèÒª.ÎÒÃǽ«Ìṩһ¸ö¶ÔÕâÁ½¸ö½×¶ÎµÄ¸ß²ã´Îä¯ÀÀ²¢ÔÚ±¾ÊéÉÔºóµ±ÎÒÃǺ­¸Çµ½É豸Çý¶¯µÄʱºò´Óϸ½ÚÉÏÀ´Á˽âÕâЩ¸ÅÄî.
    ½×¶Î 1 & 2 ÄÚºË×°ÅäºÍcÆðʼ¹ý³Ì
    ËäÈ»ÄÚºËÒ»µ©±»¼ÓÔؾͲ»ÔÙ¶Ôϵͳ×öÈκμÙÉè,¼ÓÔØÆ÷»¹ÊǻᴫËÍһЩÐÅÏ¢¸øÄÚºË,±ÈÈçÆô¶¯É豸ºÍÆô¶¯±ê¼Ç.ÁíÍâ,Äں˱ØÐë´´½¨×Ô¼ºµÄ»·¾³²¢ÈÃϵͳ׼±¸ºÃÔËÐнø³Ì0(ÏÂÃæ½âÊÍ).ÕâЩÈÎÎñ°üÀ¨CPU¼ì²â,ÔËÐÐʱ¿ÌÈÎÎñ½¨Á¢,ºÍÄÚ´æÊýÁ¿¼ì²â.
    CPUʶ±ðÊǷdz£ÖØÒªµÄÒ»²½,ÒòΪÿ¸öƽ̨¿ÉÒÔÓжàÖÖ²»Í¬µÄCPU(i386ÊÇÆäÖÐÖ®Ò»),²»ÊÇËùÓеÄCPU¶¼»áÖ§³ÖͬÑùµÄÌØÐÔ.±ÈÈç,ÄÃMMXÖ¸ÁÀ´Ëµ,ËäÈ»Ëü¶ÔÓÚÄÚºËÀ´Ëµ²»ÊÇÒ»¸öÖØÒªÌØÐÔ,µ«ÊǶÔÓÚ¸¡µãµ¥Ôª¾ÍÊÇ,Òò´ËÈç¹ûÕâ¸öÌØÐÔ²»±»¸ÃCPUËùÖ§³ÖµÄ»°¾Í±ØÐëÓÃÈí¼þÀ´Ä£Äâ.Õâ¶ÔÓÚËùÓÐÆäËû²»±»Ö§³ÖµÄÌØÐÔºÍÒÑÖª´íÎó»òÕßCPUµÄ¹ÌÓÐÌØÐÔÀ´Ëµ¶¼ÊdzÉÁ¢µÄ
    µÚ¶þ¸ö½×¶Î»á³õʼ»¯ÏµÍ³Ó²¼þºÍÄÚ´æ.Õâ°üÀ¨Ì½²âÓ²¼þ,³õʼ»¯I/OÉ豸,ΪÄں˽ṹ·ÖÅäÄÚ´æ,½¨Á¢ÏµÍ³ÏûÏ¢»º´æ.¸Ã½×¶ÎÄã»á¿´µ½Æô¶¯ÆÁÄ»ÖÐÉÁ¹ýºÜ¶àÓ²¼þÁбí.°´ÕÕBSD´«Í³,Õâ¸ö½×¶ÎÊÇͨ¹ýºô½Ðcpu_startup()º¯ÊýÀ´³õʼ»¯.
    µÚ3½×¶Î ºÍ ½ø³Ì 0
    Ò»µ©cpu_startup()º¯Êý·µ»Ø,Äں˾ÍÐèÒª½¨Á¢½ø³Ì0.½ø³Ì0ͨ³£³ÆΪ½»»»Æ÷,Èç¹ûÄãÔËÐÐpsÃüÁî»á¿´µ½ËüÔڻ.È»¶ø´Ó¸Ð¹ÙÉÏÀ´Ëµ²¢Ã»ÓÐÕâÑùÒ»¸öÃûΪswapperµÄ¶þ½øÖÆÎļþ¸½ÊôÓڸýø³Ì.Õâ¶ÔÓÚÆäËûËĸöÔÚÏÖ´úFreeBSDϵͳÉÏÕÒµ½½ø³Ì¶¼ÊdzÉÁ¢µÄ:pagedaemon,vmdaemon,bufdaemonºÍsyncer.Ϊ±ÜÃ⸴ÔÓ»¯,ÎÒÃÇֻ˵ÕâЩ½ø³ÌÊÇVM×ÓϵͳµÄÒ»²¿·Ö;ÎÒÃÇÔÚ½ø³ÌÒ»ÕÂÖÐÔÙÌÖÂÛËüÃÇ.×ÅÖØÀí½âËûÃÇÊÇÔÚÆô¶¯¹ý³ÌÖÐÓÉÄں˴´½¨¶ø²»ÊÇÎļþϵͳÖеĶþ½øÖƳÌÐò,²¢ÇÒÊÇÏ൱ƽ̨ÒÀÀµÐÔµÄ.ËûÃÇÊÇÓÃcÓïÑÔд³É²¢ÔÚÆðʼƽ̨»·¾³³õʼ»¯Íê±ÏÒÔºó¿ªÊ¼µÄ
    initºÍϵͳshell½Å±¾
    ÔÚËùÓеĻã±àºÍƽ̨ÒÀÀµÐÔ´úÂëÖ´ÐÐÍê±ÏÒÔºó,ÄÚºËÖÕÓÚÖ´ÐеÚÒ»¸öÕæÕýµÄ³ÌÐò:/sbin/init.¸Ã³ÌÐòÊÇÏ൱¼ò¶ÌµÄ(ÔÚFreeBSDÉÏ,×ܹ²Ô¼1,700ÐÐ).ÏóÎÒÃÇÔÚBSD½ø³ÌÒ»ÕÂËùÌÖÂÛµÄÄÇÑù,Õâ¾ÍÊÇÄǸöËùÓнø³Ì¶¼ÒÅ´«×ԵĽø³Ì.ÕâÑùÉè¼ÆµÄʵÁ¦ÔÚÓÚ,ÒòΪ/sbin/initÖ»ÊÇÎļþϵͳÖеÄÒ»¸ö¶þ½øÖƳÌÐò,Äã¿ÉÒÔ×Ô¼ºÐ´Ò»¸ö×Ô¶¨Òå°æµÄ.¶ÔÓÚ/sbin/initÔÚÆô¶¯Ê±µÄÖ÷ҪĿ±ê¾ÍÊÇÔËÐÐϵͳÆô¶¯½Å±¾²¢ÎªÏµÍ³½øÈë¶àÓû§Ä£Ê½×ö×¼±¸.ÁôÒâÐźÅ:/sbin/initÓ¦µ±ÄÜÓÅÑŵĴ¦ÀíÐźÅ,·ñÔòÄãµÄϵͳ¿ÉÄܽáÊøÔÚ/sbin/init³ÌÐòµÄÒ»¸öÆæ¹ÖµÄ״̬²¢ÇÒÔÚϵͳÆô¶¯Ê±±ÀÀ£µô.ͬʱÔËÐÐÖÐ/sbin/init¿ÉÒÔ½ÓÊÜÐźÅÀ´Ö´ÐÐijЩÈÎÎñ.±ÈÈç,Èç¹ûÄãÏëÈÃϵͳ²»ÎªÄ³¸öÏà¹ØµÄÖÕ¶ËÉú²ú½ø³Ì.ÏóÁÐÔÚ/etc/ttysÀïÃæµÄ.Äã¿ÉÒÔ±ê¼ÇÏëÒªµÄÖÕ¶ËΪ¹Ø±Õ״̬²¢Ö´ÐÐÏÂÃæµÄÃüÁîʹinit¶ÁÈ¡/etc/ttys²¢Ö»ÎªÁгöµÄ±ê¼Ç״̬Ϊ¿ªµÄÖÕ¶ËÉú²ú½ø³Ì.
    CODE:[Copy to clipboard]bash$kill -HUP 1
    ×¢Òâ³ý·ÇÄãºÜСÐÄ,·ñÔò¿ÉÄÜ»áÒÔÒ»¸öÄã²»ÄܵǼµÄϵͳ¸æÖÕ(µ½¹ØÓÚÐźŵÄÒ»ÕÂÀïÃæ²é¿´ÏêϸÐÅÏ¢.)
    init³ÌÐò»áÔÚÆô¶¯¹ý³ÌÖÐÉèÖÃϵͳÒÔ½øÈë¶àÓû§Ä£Ê½.ÕâÊǺÜÁéÇɵÄ.ÒýÈëÁËÏó¿ªÊ¼Ã¿Ò»¸öÊØ»¤½ø³Ì²¢ÉèÖÃÍøÂçÐÅÏ¢ÕâÑù¿ÝÔïµÄÈÎÎñ.ÔÚUNIXϵͳÖÐ.ÓÐһЩÕâÑùµÄ;¾¶,Ö÷ÒªÒýÈëshell½Å±¾.ÔÚijЩ°æ±¾µÄLinuxºÍSystem VϵͳÖÐ,¶ÔÓ¦Óëij¸öÔËÐ춱ðµÄ¿ÉÆô¶¯½Å±¾Î»ÓÚ/etc/rc.d.È»¶øBSDʹÓÃÁ˼òµ¥µÃ¶àµÄ°ì·¨.Õâ¾ÍÊÇÔÚ/etc/ÀïÃæÕÒµ½µÄµÄrc½Å±¾
    BSD rc½Å±¾°üÀ¨ÖµµÃ×¢ÒâµÄÌõÄ¿ÁбíÈçÏÂ:
    CODE:[Copy to clipboard]/etc/rc - Ö÷½Å±¾²¢ÇÒµÚÒ»¸ö±»ºô½Ð,¹Ò½ÓÎļþϵͳ²¢ÔËÐÐËùÓÐÐèÒªµÄrc½Å±¾
    /etc/rc.atm - ÓÃÀ´ÅäÖÃATMÍøÂç.
    /etc/rc.devfs - ÉèÖÃ/dev/ȨÏÞºÍÁ¬½Ó
    /etc/rc.diskless1 - µÚÒ»¸ödiskless¿Í»§¶Ë½Å±¾
    /etc/rc.diskless2 - µÚ¶þ¸ödiskless¿Í»§¶Ë½Å±¾
    /etc/rc.firewall - ÓÃÓÚ·À»ðǽ¹æÔò
    /etc/rc.firewall6 - ÓÃÓÚIPV6·À»ðǽ¹æÔò
    /etc/rc.i386 - intelϵͳÏà¹ØÐèÒª
    /etc/rc.isdn - isdnÍøÂçÉèÖÃ
    /etc/rc.pccard - Ï¥ÉϵçÄÔpc card¿ØÖÆÆ÷
    /etc/rc.sysctl - ÔÚϵͳÆô¶¯Ê±ÉèÖÃsysctlÑ¡Ïî
    /usr/local/etc/rc.d/ - ´æÓÐ×Ô¶¨ÒåÆô¶¯½Å±¾µÄÆÕͨÎļþ¼Ð
    ÕâÀïÓÐÒ»¸öÀý×Ó:
    Èç¹ûÄãÏëÈÃrsync×÷ΪÊØ»¤½ø³ÌÕâ¸ö½Å±¾»áÆð×÷ÓÃ:
    CODE:[Copy to clipboard]#!/bin/sh
    if [ -x /usr/local/bin/rsync ]; then
    /usr/local/bin/rsync --deamon
    fi
    Õ⽫Ê×Ïȼì²ârsyncÊÇ·ñ´æÔÚÈ»ºóÒÔÊØ»¤Ä£Ê½ÔËÐÐËü