Habrãå«ãå€ãã®èšäºã§ããããã¯ãŒã¯ãµãŒãã¹ïŒããŒã¢ã³ïŒã®ã¢ãŒããã¯ãã£ãæ§ç¯ããããŸããŸãªæ¹æ³ãèšåããã説æãããŠããŸãã ãã ããæ°äžã®åææ¥ç¶ãã®ã¬ããããã©ãã£ãã¯ã§åäœããããŒã¢ã³ãäœæããã³æé©åããçµéšã®ããèè
ã¯ã»ãšãã©ããŸããã
ã»ãšãã©ã®èè
ã¯ããã¥ã¡ã³ãã«ç
©ããããããšãããªãã®ã§ãéåžžããã®ãããªèšäºã§ã¯ããã¹ãŠã®æ
å ±ã¯ç¹å®ã®åãšåã®åèªã«åºã¥ããŠããŸãã ãããã®ãããã¯Webãããã€ããWikipediaãHabrahabrããã³ãã®ä»ã®è©å€ã®ãããªãœãŒã¹ããããããŸãã çµæã¯ã
ããªãã¯ããããåè«ã ãããDahlããŸãã¯Node.jsãªã®ããã®ãããªå察掟
ã§ã ïŒèè
ã®å¥èªç¹ã¯ä¿æãããŸãïŒïŒããã¯åºæ¬çã«æ¬è³ªçã«çå®ã§ãããäžæ£ç¢ºãã§ãã£ã±ãã§ãããã€ãã®äºå®äžã®èª€ããå«ã¿ãããã€ãã®ãããŸããªãªããžã§ã¯ããæåããŸãççž®ã
ãçŸåšãæå¹ãªããŒãªã³ã°ã®å®è£
ã¯* nix-systemsã§ã®ã¿å©çšå¯èœããªã©ã®ãã¬ãŒãºãè±å¯ãªèšäºããã€ãã¹ããããšã¯å°é£ã§ããïŒpollïŒïŒã¯* nixãé€ãã©ããã«ããããã«ïŒã ãã®æçš¿ã¯ã圌ã®èšäºã§å°æ¬ãããŠãã
ã€ãã¯ãªã³ã®èª€ãã説æãã解説ãšããŠå§ãŸããŸããã å·çã®éçšã§ãæåããäž»é¡ã説æããæ¹ãç°¡åã§ããããšãããããŸãããããã¯å®éã«ã¯å¥ã®æçš¿ãšããŠè¡ããŸãã
ç§ã®ãšãã»ã€ã§ã¯ãã«ããŒã®å
èš³ãããã€ãã®æªç¥ã®ããªãã¯ã¯ãããŸãããç°ãªããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§å®éã«ãããã©ã®ããã«æ©èœãããããã§ãã¯ãã人ã«ãããç°ãªãã¢ãããŒãã®é·æãšçæãåã«èª¬æããŠããŸãã
åçºããã人ã®ããã«ãç«ãžããããã
ãããã¯ãŒã¯ããŒã¢ã³ã®TK
ãŸãããããã¯ãŒã¯ãµãŒãã¹ãæ£ç¢ºã«äœããã¹ããããããŠäžè¬çã«ã¯äœãåé¡ãªã®ããç解ããå¿
èŠããããŸãã
ãã¹ãŠã®ããŒã¢ã³ã¯ããããã¯ãŒã¯æ¥ç¶ãåãå
¥ããŠåŠçããå¿
èŠããããŸãã TCP / IPãããã³ã«ã¹ã¿ãã¯ã¯UNIXããçºå±ãããã®OSã¯ããã¹ãŠã¯ãã¡ã€ã«ããšããæ矩ãå
¬èšããŠããããããããã¯ãŒã¯æ¥ç¶ã¯ããã¡ã€ã«ãæäœããããã®æšæºOSæ©èœã«ãã£ãŠéãããéããããèªã¿æžããããç¹å¥ãªã¿ã€ãã®ãã¡ã€ã«ã§ãã ã¢ãŒãã«åè©ãcanãã«æ³šæããŠãã ããããçè«çã«ããšããèšèãšçµã¿åãããŠãçŸå®ãéåžžã«æ£ç¢ºã«è¡šããŸãã
ãããã£ãŠããŸããããŒã¢ã³ã¯ã·ã¹ãã é¢æ°socketïŒïŒãåŒã³åºãã次ã«bindïŒïŒãlistenïŒïŒãåŒã³åºãããã®çµæãç¹æ®ãªã¿ã€ãã®ãlistening socketãã®ãã¡ã€ã«ãåãåããŸãã ãããã®æ©èœã®ãã©ã¡ãŒã¿ãšããŒã¢ã³ã®ãããªãã¢ã¯ã·ã§ã³ã¯ã䜿çšããããã©ã³ã¹ããŒããããã³ã«ïŒTCPãUDPãICMPãRPDãªã©ïŒã«å€§ããäŸåããŸãããã»ãšãã©ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ã¯ãæåã®2ã€ã®ã¿ããã€ã³ãã§ããŸãã ãã®èšäºã§ã¯ãæã人æ°ã®ããTCPãããã³ã«ãäŸãšããŠèŠãŠãããŸãã
ãªã¹ãã³ã°ãœã±ããã¯ãã¡ã€ã«ã§ãããçºçããå¯èœæ§ãããã®ã¯ããçä¿¡æ¥ç¶èŠæ±ããªã©ã®å®æçã«çºçããã€ãã³ãã§ãã ããŒã¢ã³ã¯acceptïŒïŒé¢æ°ã䜿çšããŠãã®æ¥ç¶ãåãå
¥ããããšãã§ããŸãããã®é¢æ°ã¯æ°ãããã¡ã€ã«ãäœæããŸãããä»åã¯ãTCP / IPãããã¯ãŒã¯ãœã±ãããéããã¿ã€ãã§ãã ãããããããŒã¢ã³ã¯ãã®æ¥ç¶ããèŠæ±ãèªã¿åãããããåŠçããŠçµæãéãè¿ãå¿
èŠããããŸãã
åæã«ããããã¯ãŒã¯ãœã±ããã¯æ¢ã«ã»ãŒéåžžã®ãã¡ã€ã«ã§ããæãæšæºçãªæ¹æ³ã§ã¯äœæãããŠããŸããããå°ãªããšãããããããŒã¿ã®èªã¿åããšæžã蟌ã¿ãè©Šã¿ãããšãã§ããŸãã ãã ãããã¡ã€ã«ã·ã¹ãã ã«ããéåžžã®ãã¡ã€ã«ãšã¯å€§ããç°ãªããŸãã
*ãã¹ãŠã®ã€ãã³ãã¯ãå®éã«ã¯éåæã«çºçããæéã¯äžæã§ãã ææªã®å Žåã®æäœã«ã¯æ°ååãããå ŽåããããŸãã äžè¬çã«ã¯ã
*ãã¡ã€ã«ãšã¯ç°ãªããæ¥ç¶ã¯ãæãäºæãã¬ç¬éã«ãèªåã§ãéããããšãã§ããŸãã
* OSã¯åžžã«éããæ¥ç¶ãå ±åããããã§ã¯ãããŸãããããããããœã±ããã¯30åãã³ã°ããããšããããŸãã
*ã¯ã©ã€ã¢ã³ããšãµãŒããŒã®æ¥ç¶ã¯ç°ãªãæéã«éããããŸãã ã¯ã©ã€ã¢ã³ããæ°ããæ¥ç¶ãäœæããŠããŒã¿ããéä¿¡ãããããšãããšãããŒã¿ã®è€è£œãå¯èœã«ãªããã¯ã©ã€ã¢ã³ãã誀ã£ãŠæžã蟌ãŸããå Žåã倱ãããå¯èœæ§ããããŸãã ãµãŒããŒã1ã€ã®ã¯ã©ã€ã¢ã³ãããè€æ°ã®æ¥ç¶ãéããŠããå¯èœæ§ããããŸãã
*ããŒã¿ã¯ãã€ãã®ã¹ããªãŒã ãšèŠãªãããæåéã1ãã€ãã®éšåã§åä¿¡ã§ããŸãã ãããã£ãŠãããšãã°UTF-8æååãšèŠãªãããšã¯ã§ããŸããã
*ãããã¯ãŒã¯äžã®ããŒã¢ã³ã«ãã£ãŠæäŸããããã®ä»¥å€ã®ãããã¡ã¯ãããŸããã ãããã£ãŠã1ãã€ãã§ããœã±ããã«æžã蟌ããšãããŒã¢ã³ãæ°ååãããã¯ãããå¯èœæ§ããããŸãïŒäžèšãåç
§ïŒã ããã«ããµãŒããŒäžã®ã¡ã¢ãªã¯ãéãŽã ãã§ãããããŒã¢ã³ã¯çµæã®çæé床ãå¶éã§ããå¿
èŠããããŸãã
*ãšã©ãŒã¯ã©ãã§ãçºçããå¯èœæ§ããããããŒã¢ã³ã¯ãããããã¹ãŠæ£ããåŠçããå¿
èŠããããŸãã
éããŠãããã¹ãŠã®æ¥ç¶ãé¡ãã§ã«ãŒããäœæãããšãæåã®ããã³ã°ãæ¥ç¶ã¯ä»ã®ãã¹ãŠã®æ¥ç¶ããããã¯ããŸãã ã¯ããæ°ååã§ãã ãããŠãããŒã¢ã³ã®ããŸããŸãªã¢ãžã¥ãŒã«ã®çžäºäœçšãæŽçããããã®ããŸããŸãªãªãã·ã§ã³ããããŸãã åçãåç
§ããŠãã ããïŒ
å
責äºé
ïŒå³ã¯ãçŸå®ã«å¯Ÿå¿ããªãæ¬äŒŒèšèªã³ãŒãã瀺ããŠããŸãã å€ãã®éèŠãªã·ã¹ãã ã³ãŒã«ãšãã¹ãŠã®ãšã©ãŒåŠçã³ãŒãã¯ãæ確ã«ããããã«çç¥ãããŠããŸãã
2.ãã«ãããã»ã¹ã¢ãŒããã¯ãã£
æ¥ç¶ãçžäºã«åœ±é¿ããªãããã«ããæãç°¡åãªæ¹æ³ã¯ãæ¥ç¶ããšã«åå¥ã®ããã»ã¹ïŒã€ãŸããããã°ã©ã ã®åå¥ã®ã³ããŒïŒãéå§ããããšã§ãã ãã®æ¹æ³ã®æ¬ ç¹ã¯æããã§ã-å¥ã®ããã»ã¹ãéå§ãããšãéåžžã«ãªãœãŒã¹ãæ¶è²»ããæäœã«ãªããŸãã ããããã»ãšãã©ã®èšäºã§ã¯ããã®ã¡ãœãããåãApacheã§äœ¿çšãããŠããçç±ã説æããŠããŸããã
ãããŠããã¹ãŠã®OSã§ã®ããã»ã¹ã¯ãã·ã¹ãã ãªãœãŒã¹ïŒã¡ã¢ãªããªãŒãã³ãã¡ã€ã«ãã¢ã¯ã»ã¹æš©ãã¯ã©ãŒã¿ãªã©ïŒã®ã¢ã«ãŠã³ãã£ã³ã°ã®åäœã§ãããšããããšã§ãã ã·ã§ã«ãFTPãªã©ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã çšã®ãªã¢ãŒãã¢ã¯ã»ã¹ããŒã¢ã³ãäœæããå Žåããã¡ã€ã«ã®ã¢ã¯ã»ã¹èš±å¯ãæ£ããèæ
®ããããã«ããã°ã€ã³ããŠããåãŠãŒã¶ãŒã«ä»£ãã£ãŠåå¥ã®ããã»ã¹ãéå§ããã ãã§ãã åæ§ã«ãç°ãªããŠãŒã¶ãŒã®æ°çŸã®ãµã€ãã1ã€ã®ãç©çãããŒãã§åæã«å
±æãã¹ãã£ã³ã°ãµãŒããŒäžã§ã¹ãã³ããŠããŸãããŸããäžéšã®ãã¹ãã£ã³ã°ãŠãŒã¶ãŒã®ãµã€ããä»ã®ãŠãŒã¶ãŒã®ããŒã¿ã«ã¢ã¯ã»ã¹ã§ããªãããã«ãããã»ã¹ã«Apacheãå¿
èŠã§ãã ããã»ã¹ã䜿çšããŠããå®éã«ã¯Apacheã®ããã©ãŒãã³ã¹ã«ã¯åœ±é¿ããŸããã
ã°ã©ãäž-Linuxã«ãŒãã«ã®ããŒãžã§ã³ã«å¿ããã1ç§ãããã®éçãã¡ã€ã«ãžã®åŠçæžã¿ãªã¯ãšã¹ãã®æ°ã å€ãã»ã©è¯ãã
ãã¹ããã³ãïŒCore i7 970ã3Gb DDR3ãNvidia GTX 460ã64GB OCZ Vertex SSDã
åºå
žïŒPhoronixã
ããªãã®æªéãæ¯ç§17000ãã¡ã€ã«ãäžããããšãæã¿ãŸãã
ããŒã¢ã³ã®ãŠãŒã¶ãŒããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ç»é²ãããŠããªããŠãããµãŒãã¹ã®ã»ãã¥ãªãã£èŠä»¶ãé«ããªã£ãŠããå Žåã§ãããŠãŒã¶ãŒããšã«åå¥ã®ããã»ã¹ãå²ãåœãŠãããšã¯éåžžã«åççãªã¢ãŒããã¯ãã£ãœãªã¥ãŒã·ã§ã³ã§ãã ããã«ãããæªæã®ãããŠãŒã¶ãŒãä»ã®ãŠãŒã¶ãŒã®ããŒã¿ã®èªã¿åããããŒã¢ã³ããã»ã¹ã®ç Žå£ãèš±å¯ãããã°ãããŒã¢ã³ã«èŠã€ãããšããŠããä»ã®ãŠãŒã¶ãŒã®ããŒã¿ãžã®ã¢ã¯ã»ã¹ãååŸãŸãã¯ãããã¯ããããšã¯ã§ããŸããã
æåŸã«ãåããã»ã¹ã«ã¯ç¬èªã®ã¢ãã¬ã¹ç©ºéããããç°ãªãããã»ã¹ãçžäºã®ã¡ã¢ãªäœ¿çšã劚ããããšã¯ãããŸããã ãã®å©ç¹ãäžéšèª¬æãããŠããçç±
3.ãã«ãã¹ã¬ããã¢ãŒããã¯ãã£
ã¹ããªãŒã ã¯ãå
±æã¡ã¢ãªãã·ã¹ãã ãªãœãŒã¹ãã¢ã¯ã»ã¹æš©ãæã¡ãã¹ã¿ãã¯ãç°ãªãæãç°¡åãªãããã»ã¹ãã§ãã ã€ãŸããã¹ã¬ããã«ã¯å
±éã®åçå€æ°ãã°ããŒãã«å€æ°ãéçå€æ°ããããŸãããããŒã«ã«å€æ°ã¯ç°ãªããŸãã ãã«ãããã»ããµããã³/ãŸãã¯ãã«ãã³ã¢ã·ã¹ãã ã§ã¯ãåãããã»ã¹ã®ç°ãªãã¹ã¬ãããç©ççã«åæã«å®è¡ã§ããŸãã
ãã«ãã¹ã¬ããã¢ãŒããã¯ãã£ã¯ãããã©ãŒãã³ã¹ãšã¡ã¢ãªæ¶è²»ã®ããã«ã»ãã¥ãªãã£ãšå®å®æ§ãç ç²ã«ãªããã«ãããã»ã¹ã¢ãŒããã¯ãã£ã«äŒŒãŠããŸãã
ããã©ãŒãã³ã¹åŸã®ãã«ãã¹ã¬ããã¢ãŒããã¯ãã£ã®äž»ãªå©ç¹ã¯ããªãŒãã³æ¥ç¶åŠçã¢ã«ãŽãªãºã ã®ã·ãŒã±ã³ã¹ãšåæã§ãã ã€ãŸããã¢ã«ãŽãªãºã ã¯ãæåã®éšåã®å³ã«ç€ºãããŠãããšããã«è¡šç€ºããã³å®è¡ãããŸãã æåã«ãå¿
èŠãªæéã ãããŒã¿ããœã±ããããèªã¿åããã次ã«åŠçãããŸãããã®å Žåãããã®åŠçã«å¿
èŠãªæéã ãçµæãã¯ã©ã€ã¢ã³ãã«éä¿¡ãããŸãã åæã«ãçµæã®éä¿¡ãéããããšãã¹ããªãŒã ã¯writeïŒïŒé¢æ°ã«ãã£ãŠèªåçã«ãããã¯ãããŸãã åŠçã¢ã«ãŽãªãºã ã¯åçŽã§ãå°ãªããšãæé«ã¬ãã«ã§ã¯ç°¡åã§ãã ããã¯éåžžã«å€§ããªãã©ã¹ã§ãã
åææ¥ç¶æ°ãæ¯èŒçå°ãªãå Žåããã«ãã¹ã¬ããã¢ãŒããã¯ãã£ãæé©ã§ãã ããããå®éã«å€æ°ã®æ¥ç¶ïŒ1äžãªã©ïŒãããå Žåãã¹ã¬ããéã®åãæ¿ãã«ã¯æéãããããããŸãã ããããããã§ããã«ãã¹ã¬ããã¢ãŒããã¯ãã£ã®äž»ãªæ¬ ç¹ã§ã¯ãããŸããã
ãããŠäž»ãªããšã¯ãã¹ã¬ãããç¬ç«ããŠãããããäºãããããã¯ã§ããïŒãããŠãããã¯ããïŒããšã§ãã ãããã©ã®ããã«çºçããããç解ããã«ã¯ãäŸãèããŠãã ããã
åŒã®å€ãèšç®ããå¿
èŠããããšããŸããã
a = b + c;
ãããã§ãaãbãããã³cã¯ã°ããŒãã«å€æ°ã§ãã
éåžžã®ã·ã³ã°ã«ã¹ã¬ããã®ç¶æ³ã§ã¯ãã³ã³ãã€ã©ã¯æ¬¡ã®ãã·ã³ã³ãŒãã®ãããªãã®ãçæããŸãã
a = b; // MOV A, B
a += c; // ADD A, C
ãã«ãã¹ã¬ããããŒãžã§ã³ã§ã¯ããã®ã³ãŒãã¯äœ¿çšã§ããŸããã å¥ã®ã¹ã¬ããã¯ãæåã®åœä»€ãš2çªç®ã®åœä»€ã®éã§bã®å€ãå€æŽã§ããŸãããã®çµæã誀ã£ãaã®å€ãååŸããŸãã aãåžžã«b + cã«çãããšèŠãªãããå ŽåãããããŒãã£ã³ã°ããšã©ãŒãåçŸããã®ã¯éåžžã«å°é£ã§ãã
ãããã£ãŠããã«ãã¹ã¬ããããŒãžã§ã³ã§ã¯ãã³ãŒãã¯æ¬¡ã®ããã«äœ¿çšãããŸãã
lock a;
lock b;
lock c;
a = b;
a += c;
unlock c;
unlock b;
unlock a;
ããã§ãlockããã³unlockã¯ãå€æ°ãžã®ã¢ã¯ã»ã¹ãããã¯ããã³ããã¯è§£é€ãã
ã¢ãããã¯æäœã§ãã ãããã¯ãå€æ°ãå¥ã®ã¹ã¬ããã«ãã£ãŠæ¢ã«ããã¯ãããŠããå ŽåãlockïŒïŒæäœããã®å€æ°ã®è§£æŸãåŸ
æ©ããããã«é
眮ãããŸãã
ãããã£ãŠã2ã€ã®ã¹ã¬ãããåæã«æäœa = b + cããã³b = c + aã®å®è¡ãéå§ãããšããããã¯äºãã«æ°žé ã«ãããã¯ãããŸãã ãã®ç¶æ³ã¯ã¯ãªã³ããšåŒã°ããã¯ãªã³ãã®æ€çŽ¢ãšè§£æ±ºã¯ã䞊åããã°ã©ãã³ã°ã®å¥ã®ãçãäž»é¡ãã§ãã ããããã¯ãªã³ãããªããŠããã¹ã¬ããã¯ãããã¯ãè¿
éã«è§£é€ããªããšãéåžžã«é·ãæéäºãã«åæ¢ããå¯èœæ§ããããŸãã
ããã«ãã¢ãããã¯æäœã¯ãRAMãã¹ãšRAMèªäœã®æä»å¶åŸ¡ã«ãã£ãŠç©ççã«å®è£
ãããŸãã ãã£ãã·ã¥ã§ã¯ãªãã¡ã¢ãªãçŽæ¥æäœããããšèªäœãéåžžã«é
ãããã®å Žåãä»ã®ãã¹ãŠã®ãµãŒããŒããã»ããµã®ãã¹ãŠã®ã³ã¢ã®å¯Ÿå¿ãããã£ãã·ã¥ã©ã€ã³ãç¡å¹åïŒãªã»ããïŒãããŸãã ã€ãŸããæè¯ã®å Žåã§ããããã¯ããªãå Žåãåã¢ãããã¯æäœã¯ååã«é·ãæéå®è¡ãããä»ã®ã¹ã¬ããã®ããã©ãŒãã³ã¹ãäœäžããŸãã
ããããæªéã®ã€ãªããã¯ã»ãšãã©ç¬ç«ããŠãããããã©ãããå
±éå€æ°ãååŸã§ããã®ã§ããããïŒ
ããããã©ãããïŒ
*æ°ããååç©ã®äžè¬çãªãã¥ãŒã
*ããŒã¿ããŒã¹ãŸãã¯åæ§ã®ãªãœãŒã¹ã«ã¢ã¯ã»ã¹ããããã®äžè¬çãªãã¥ãŒã
*ã¡ã¢ãªå²ãåœãŠã®ãªã¯ãšã¹ãã®äžè¬çãªãã¥ãŒïŒyesãmallocïŒïŒããã³newïŒïŒã¯ããããã³ã°ãåŒãèµ·ããå¯èœæ§ããããŸãïŒ;
*äžè¬ãã°ïŒãã°ãã¡ã€ã«ïŒããã³çµ±èšèšç®ã®äžè¬ãªããžã§ã¯ãã
ãããã¯æãæçœãªãã®ã§ãã
å Žåã«ãã£ãŠã¯ãå
±æå€æ°ãçãæ¹æ³ããããŸãã ããšãã°ãã¹ããªãŒã ã®1ã€ã«ããã£ã¹ãããã£ãŒãã®æ©èœãä»äžãããšãæ°ããæ¥ç¶ã®ãã¥ãŒã®ãããã¯ãæåŠã§ããŸããããã«ãããå·§åŠãªæ¹æ³ã§ã¿ã¹ã¯ãé
ãããŸãã ç¹å¥ãªãéããããã³ã°ãããŒã¿æ§é ãé©çšã§ããå ŽåããããŸãã ããããäžè¬çã«ããã«ãã¹ã¬ããã¢ãŒããã¯ãã£ã®ãããããã¯ã®åé¡ã¯è§£æ±ºãããŠããŸããã
4.ãã³ããããã³ã°ã¢ãŒããã¯ãã£
çæ³çã«ã¯ãã¢ããªã±ãŒã·ã§ã³å
ã®ã¹ã¬ããã®æ°ã¯ãããã»ããµã³ã¢ã®æ°ãšã»ãŒåæ°ã§ããå¿
èŠããããŸãã ãããå®çŸãã1ã€ã®ã¡ã«ããºã ã¯ãéããããã³ã°I / Oã§ãã
ãã³ããããã³ã°I / Oã¯ãã»ãšãã©ã®ææ°ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ã€ã³ã¹ããŒã«ã§ããåãªããã¡ã€ã«ã¢ã¯ã»ã¹ã¢ãŒãã§ãã éåžžã®ãããããã³ã°ãã¢ãŒãã§èªã¿åãé¢æ°ãããã°ã©ããŒã泚æããæ°ã ããã¡ã€ã«ãããã€ããèªã¿åãããã®èªã¿åããé²è¡äžã«ãããåŒãèµ·ãããã¹ã¬ããããç¡å¹ãã«ãããšãéããããã³ã°ã¢ãŒãã§ã¯åãèªã¿åãé¢æ°ã¯ãã¡ã€ã«ããèªã¿åããŸãããããã£ãã·ã¥ããã®ãã£ãã·ã¥å
ã®ãã€ãæ°ãšåãæ°ã«ãªãããã®åŸããã©ãã£ãã¯ãã¹ããªãŒãã³ã°ããããããã¯ãããããããšãªããããã«æ»ããŸãã ãã£ãã·ã¥ã空ã®å Žåãéããããã³ã°èªã¿åãïŒïŒã¯0ãã€ããèªã¿åããã·ã¹ãã ãšã©ãŒã³ãŒããEWOULDBLOCKã«èšå®ããããã«æ»ããŸãã ããã§ããããã¯éåžžã®åæé¢æ°ãžã®éåžžã®åæåŒã³åºãã§ãã
ç¹ã«è±èªçãŠã£ãããã£ã¢ã§ã¯ãéããããã³ã°åæå
¥åºåããéåæããšåŒã°ããæ··ä¹±ããæããã«ãLinux OSã®ããŸã奜å¥å¿ã®ãªãè¬çœªè
ã«ãã£ãŠåŒãèµ·ããããŠããŸãã ãã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ã¯ãã«ãŒãã«2.6.22-2.6.29ãŸã§é·ãéãéåæI / Oé¢æ°ããŸã£ãããããŸããã§ããïŒãããŠãçŸåšã§ãå¿
èŠãªãã¹ãŠã®ã»ããããªããç¹ã«éåæfnctlã¯ãããŸããïŒããã®OSã§ã®ã¿ã圌ãã¯èª€ã£ãŠãã³ããããã³ã°åæé¢æ°ããéåæããšåŒãã§ããŸãããããã¯å€ãã®å€ãLinuxããã¥ã¢ã«ã§è¿œè·¡ã§ããŸãã
éåæI / Oã«ã€ããŠã¯æ¬¡ã®ããŒãã§è©³ãã説æããŸãããããã§ã¯éããããã³ã°èªã¿åãããã³æžã蟌ã¿é¢æ°ã®äœ¿çšã«çŠç¹ãåœãŠãŸãã
å®éã®æ¡ä»¶ã§ã¯ããã³ããããã³ã°readïŒïŒã³ãŒã«ã®95ïŒ
ããããã0ãã€ããèªã¿åããŸãã OSã«ãŒãã«ã®ãããã®ãã¢ã€ãã«ãåŒã³åºããåé¿ããããã«ããã§ã«èªã¿åãããã³/ãŸãã¯æžã蟌ã¿å¯èœãªæ¥ç¶ã®ãªã¹ãããéžæãããããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«èŠæ±ã§ããselectïŒïŒé¢æ°ããããŸãã äžéšã®* nixãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ã¯ãpollïŒïŒãšåŒã°ãããã®é¢æ°ã®ããªã¢ã³ãããããŸãã
pollïŒïŒã®è©³çŽ°ïŒãã®æ©èœã¯ãPOSIXæšæºã®æ¬¡ã®ããŒãžã§ã³ã®èŠä»¶ãšããŠç»å ŽããŸããã Linuxã®å ŽåãpollïŒïŒã¯æåã«æšæºCèšèªã©ã€ãã©ãªïŒlibc> = 5.4.28ïŒã®é¢æ°ãšããŠãéåžžã®selectïŒïŒã®ã©ãããŒãšããŠå®è£
ããããã°ããããŠããã«ãŒãã«ã«ã移åããããŸããã ããšãã°ãWindowsã§ã¯ãéåžžã®pollïŒïŒé¢æ°ã¯ãŸã ãããŸããããVistaããã¯ã ã¢ããªã±ãŒã·ã§ã³ã®ç§»è¡ãç°¡çŽ åããããã®ç¹å®ã®ç·©åçããããCã®selectïŒïŒã®ã©ãããŒãšããŠãå®è£
ãããŠããŸãã
ããããã¹ãŠã®é©æ°ããããããã®ã瀺ãã°ã©ããå
±æããããåŸãŸããã ã°ã©ãäž-ã«ãŒãã«ã®ããŒãžã§ã³ã«å¿ããŠãã«ãŒãã€ã³ã¿ãŒãã§ã€ã¹ãä»ããŠ10 GBã®ããŒã¿ããã³ãã³ã°ããæéã å°ãªãã»ã©è¯ãã ãœãŒã¹ã¯åãã§ããã¹ããã³ãã¯åãã§ãã
ãããã«ãããselectïŒïŒã«ã¯äžå®ã®å¶éããããŸããïŒç¹ã«ã1ã€ã®ãªã¯ãšã¹ãã®ãã¡ã€ã«æ°ïŒããã®é¢æ°ãšãã³ããããã³ã°I / Oã¢ãŒãã䜿çšãããšããã¹ãŠã®äœæ¥ã30è¡ã®ã³ãŒãã§ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«è»¢éããåçŽã«åŠçããããšãã§ããŸãããªãã®ããŒã¿ã ã»ãšãã©ã®å Žåãéããããã³ã°I / Oãæ
åœããã¹ã¬ããã¯ã1ã€ã®ã³ã¢ã®åŠçèœåã®æ°ããŒã»ã³ãããæ¶è²»ããŸããã ãã¹ãŠã®èšç®ãå®è¡ãããšããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®ã«ãŒãã«ã®å
éšã¹ã¬ããã¯10å以äžãæ¶è²»ããããŸãã
ã¹ã¬ããæ°ã®åæžã«æ»ããŸãããã
ãã®ãããããŒã¢ã³ã«ã¯ãæ¥ç¶ãã¯ã©ã¹ã®ãªããžã§ã¯ããå€æ°ãããåæ¥ç¶ãªããžã§ã¯ãã«æ£ããé åºã§é©çšããå¿
èŠãããäžé£ã®æäœããããŸãã
ãã«ãã¹ã¬ããã¢ãŒããã¯ãã£ã§ã¯ãæ¥ç¶ãªããžã§ã¯ãããšã«åå¥ã®ã¹ã¬ãããäœæãããæäœã¯ç®çã®é åºã§ãããã¯ã¢ãŒãã§èªç¶ã«å®è¡ãããŸãã
ãã³ããããã³ã°I / Oã¢ãŒããã¯ãã£ã§ã¯ãæäœããšã«ã¹ããªãŒã ãäœæãããç°ãªããªããžã§ã¯ãã«é 次é©çšãããŸãã ããã¯ãMMXãSSEãªã©ã®SIMDåœä»€ã«å°ã䌌ãŠããŸãã1ã€ã®åœä»€ãè€æ°ã®ãªããžã§ã¯ãã«åæã«é©çšãããŸãã å¿
èŠãªäžé£ã®æäœã«èããããïŒã€ãŸããæåã«çµæãèšç®ããŠããéä¿¡ããïŒãã¹ã¬ããéã®ãžã§ããã¥ãŒãããã»ã¹ã®å
±æã¡ã¢ãªã«äœæãããŸãã éåžžããã¥ãŒã¯ãªã³ã°ãããã¡ã«åºã¥ããŠäœæãããŸãããã®å Žåããéããããã³ã°ãæ¹åŒã§å®è£
ã§ããŸãã
å®éã®ãããã¯ãŒã¯ãµãŒãã¹ã§ã¯ãèŠæ±ã®èªã¿åããšçµæã®éä¿¡ã®éã«ãã¢ããªã±ãŒã·ã§ã³ãµãŒããŒãDBMSããŸãã¯ãã®ä»ã®ãéããæäœã®åŒã³åºããå«ããããªãè€éãªåå²åŠçã¢ã«ãŽãªãºã ãšããã¹ãŠã®åå²ãã«ãŒãããã¹ãŠã®ã¹ãããã§ã®ãšã©ãŒåŠçãªã©ããããŸãã åæã«å®è¡ãããŠããã¹ã¬ããã®ä»¥åã®æªç¥æ°ã«åŸã£ãŠããããã¹ãŠå解ããããã«ããã»ããµã³ã¢ã®è² è·ãã»ãŒåãã«ãªãããã«-ããã¯ãã·ã¹ãã ããã°ã©ãã³ã°ã®ãã¹ãŠã®åŽé¢ããã¹ã¿ãŒããå¿
èŠãããæé«ã¬ãã«ã®éçºè
ã¹ãã«ã§ãã ã»ãšãã©ã®å Žåãããã¯ã¯ããã«åçŽã«ããŸããreadïŒïŒãšwriteïŒïŒã®éã®ãã¹ãŠãå¥ã®ã¹ããªãŒã ã«å²ã¿ãéå§N =ãã®ã¹ããªãŒã ã®ã³ããŒã®ã³ã¢ã®æ°ã§ãã ãããŠããªãœãŒã¹ã奪ãåã£ãããDBMSã殺ãããããã¯ãªã³ãã¡ã³ããŒã®ããã«æŸèæãçºæãããŸããã 䞊åã¹ã¬ããã
5.éåæI / O
éåæé¢æ°ãšåæé¢æ°ã®éããç解ããŠããªãå Žåãç°¡åã«ããããã«ãéåæé¢æ°ã¯ãããšãã°é£æ¥ããã«ãŒãã«ã§åŒã³åºããããã°ã©ã ãšäžŠè¡ããŠåæã«å®è¡ããããšæ³å®ã§ããŸãã äžæ¹ã§ã¯ãåŒã³åºãåŽããã°ã©ã ã¯èšç®ã®çµäºãåŸ
ã€å¿
èŠããªããäœã圹ã«ç«ã€ããšãã§ããŸãã äžæ¹ãéåæé¢æ°ã®çµæã®æºåãã§ããããäœããã®æ¹æ³ã§ããã«ã€ããŠé¡§å®¢ããã°ã©ã ã«éç¥ããå¿
èŠããããŸãã ãã®ã¡ãã»ãŒãžãçºçããæ¹æ³ã¯ãããŸããŸãªOSã§éåžžã«ç°ãªãæ¹æ³ã§å®è£
ãããŸãã
æŽå²çã«ãWindows 2000ã¯éåæI / OããµããŒãããæåã®OSã®1ã€ã§ããã
å
žåçãªãŠãŒã¹ã±ãŒã¹ã¯æ¬¡ã®ãšããã§ããããšãã°ãæ°åç§ä»¥å
ã«å€§ããªãã¡ã€ã«ãããŒãããã·ã³ã°ã«ã¹ã¬ããã¢ããªã±ãŒã·ã§ã³ïŒãã«ãã³ã¢ããã»ããµã¯ãããŸããã§ããïŒã readïŒïŒã®åæåŒã³åºãã§èŠãããã€ã³ã¿ãŒãã§ãŒã¹ãšãã¯ããã¯ããããªãŒãºãã代ããã«ãéåæããŒãžã§ã³ã§ã¯ã¡ã€ã³ããã°ã©ã ãããŒãããã³ã°ããããããŒãããã»ã¹ãšããã£ã³ã»ã«ããã¿ã³ã衚瀺ããçŸããããã°ã¬ã¹ããŒãäœæããããšãã§ããŸãã
ãããã°ã¬ã¹ããŒããå®è£
ããããã«ãç¹å¥ãªOVERLAPPEDæ§é ãéåæWindows I / Oé¢æ°ã«æž¡ãããããã§OSã¯çŸåšã®è»¢éãã€ãæ°ãèšé²ããŸãã ããã°ã©ããŒã¯ããã®æ§é äœã®å
容ãèªåã«ãšã£ãŠéœåã®è¯ããšãã«ãã€ã§ãèªãããšãã§ããŸã-ã¡ã€ã³ã®ã¡ãã»ãŒãžåŠçãµã€ã¯ã«ãã¿ã€ããŒãªã©ã§ã åãæ§é ã§ãæäœã®æåŸã«ããã®æçµçµæãèšé²ãããŸãïŒè»¢éããããã€ãã®åèšæ°ããšã©ãŒã³ãŒãïŒããå ŽåïŒãªã©ïŒã
ãã®æ§é ã«å ããŠãç¬èªã®ã³ãŒã«ããã¯é¢æ°ããæäœã®çµäºæã«ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ãã£ãŠåŒã³åºãããOVERLAPPEDãžã®ãã€ã³ã¿ãŒãåãéåæI / Oé¢æ°ã«è»¢éã§ããŸãã
ã©ãã«ããŠãããã°ã©ã ãäžæããããšã«ããã³ãŒã«ããã¯é¢æ°ã®çã®æ£çŽãªéåæèµ·åã¯ãåãã«ãŒãã«äžã§ããã°ã©ã å®è¡ã®2çªç®ã®ã¹ã¬ãããéå§ããããšãšåºå¥ã§ããŸããã ãããã£ãŠãã³ãŒã«ããã¯é¢æ°ãéåžžã«æ
éã«èšè¿°ããããã¢ã¯ã»ã¹ããã¯ã«é¢ãããã¹ãŠã®ããã«ãã¹ã¬ãããã«ãŒã«ãå
±æããŒã¿ã«é©çšããå¿
èŠããããŸããããã¯ãã·ã³ã°ã«ã¹ã¬ããã¢ããªã±ãŒã·ã§ã³ã§ã¯éåžžã«å¥åŠã§ãã ã·ã³ã°ã«ã¹ã¬ããã¢ããªã±ãŒã·ã§ã³ã§ã®æœåšçãªãšã©ãŒãåé¿ããããã«ãWindowsã¯çã®ã³ãŒã«ããã¯ããã¥ãŒã«å
¥ããŸããããã°ã©ããŒã¯ããããã®ã³ãŒã«ããã¯ãå®è¡ããããã«äžæã§ããããã°ã©ã å
ã®å Žæãæ瀺çã«æå®ããå¿
èŠããããŸãïŒWaitFor *ãªããžã§ã¯ããã¡ããªãŒé¢æ°ïŒã
äžèšã®éåæI / Oã¹ããŒã ã¯ãWindowsNTã«ãŒãã«ã®ããã€ãã£ããã§ããã€ãŸããä»ã®ãã¹ãŠã®æäœã¯äœããã®æ¹æ³ã§å®è£
ãããŸãã æ°å-IOCPïŒå
¥å/åºåå®äºããŒãïŒã ãã®ã¹ããŒã ã¯ãéããçè«çã«æ倧ã®æ§èœãéæã§ãããšèããããŠããŸãã Windowsã§ã®æ¬æ Œçãªäœæ¥çšã«èšèšãããããŒã¢ã³ã¯ãIOCPã«åºã¥ããŠéçºããå¿
èŠããããŸãã 詳现ã«ã€ããŠã¯ã
MSDNã®IOCPã®æŠèŠãåç
§ããŠãã ããã
Linuxã§ã¯ãéåžžã®OVERLAPPEDæ§é ã®ä»£ããã«ãaiocbãšã®åŒ±ãé¡äŒŒæ§ããããŸããããã«ãããæäœã®å®äºã®äºå®ã®ã¿ãå€æã§ããçŸåšã®é²è¡ç¶æ³ã¯å€æã§ããŸããã ã«ãŒãã«ã¯ããŠãŒã¶ãŒå®çŸ©ã®ã³ãŒã«ããã¯ã®ä»£ããã«ãUNIXã·ã°ãã«ïŒã¯ãã殺ãã·ã°ãã«ïŒã䜿çšããŸãã ã·ã°ãã«ã¯å®å
šã«éåæã§å°çãããã¹ãŠã®çµæãçããŸããããªãšã³ãã©ã³ãé¢æ°ã®äœæã«æ
£ããŠããªãå Žåã¯ãç¹æ®ãªã¿ã€ãã®ãã¡ã€ã«ïŒsignalfdïŒãäœæããéããããã³ã°ãå«ãéåžžã®åæI / Oé¢æ°ã䜿çšããŠçä¿¡ä¿¡å·ã«é¢ããæ
å ±ãèªã¿åãããšãã§ããŸãã 詳现ã«ã€ããŠã¯ã
man aio.hãåç
§ããŠãã ããã
éåæI / Oã䜿çšããŠããããŒã¢ã³ã®ã¢ãŒããã¯ãã£ã«å¶éã¯èª²ããããŸãããçè«çã«ã¯ä»»æã§ãã ãã ããååãšããŠãããã€ãã®ã¯ãŒã¯ãããŒãïŒããã»ããµã³ã¢ã®æ°ã«å¿ããŠïŒäœ¿çšããããã®éã«ãµãŒãã¹æ¥ç¶ãåçã«åæ£ãããŸãã æ¥ç¶ããšã«ãæéç¶æ
ãã·ã³ïŒFSMïŒãæ§ç¯ããã³ããã°ã©ã ãããã€ãã³ãïŒã³ãŒã«ããã¯é¢æ°ããã³/ãŸãã¯ãšã©ãŒã®åŒã³åºãïŒã®åºçŸã«ããããã®ãã·ã³ã1ã€ã®ç¶æ
ããå¥ã®ç¶æ
ã«ç§»è¡ããŸãã
ãŸãšã
ã芧ã®ãšãããããããã®æ¹æ³ã«ã¯é·æãçæãçšéããããŸãã ã»ãã¥ãªãã£ãå¿
èŠãªå Žå-ããã»ã¹ã䜿çšããé«è² è·äžã§é床ãéèŠãªå Žå-ãã³ããããã³ã°I / Oã§ãéçºã®é床ãšã³ãŒãã®ãããããããéèŠãªå Žåã¯ããã«ãã¹ã¬ããã¢ãŒããã¯ãã£ãé©ããŠããŸãã éåæI / Oã¯ãWindowsã®äž»ãªæ¹æ³ã§ãã ãããã«ãããèªåã§I / Oãæäœããã³ãŒããæžããªãã§ãã ããã ãã®ãããã¯ãŒã¯ã«ã¯ããã¹ãŠã®ã¢ãŒããã¯ãã£ãšãªãã¬ãŒãã£ã³ã°ã·ã¹ãã çšã®ç¡æã®æ¢è£œã®ã©ã€ãã©ãªããããäœå幎ãã®éãã»ãšãã©èŒããæŸã£ãŠããŸãã ã»ãšãã©-ããªãã®å Žåãããªãã¯ãŸã ããªãã®æ¡ä»¶ã«ãããããã¡ã€ã«ãã調æŽããå¿
èŠãããããã§ãã ã€ã³ã¿ãŒãããã¯è€éãªãã®ã§ãããäžèœãªè§£æ±ºçã¯ãããŸããã
ããŒã¢ã³ã¯1ã€ã®I / Oã§ååã§ã¯ãªããèŠæ±åŠçäžã«ããè€éãªãã®ã£ã°ããçºçããå¯èœæ§ããããŸãã ãããã誰ããèå³ãæã£ãŠããå Žåãããã¯å¥ã®èšäºã®ãããã¯ã§ãã
åç
§è³æ
1.
C10Kã®åé¡ ããã³ã
o_O_Tyncã«æè¬
2.
libevã©ã€ãã©ãªã®ãã«ã ã倧éã®å
¥åºåïŒengïŒã®ããŸããŸãªã¡ã«ããºã ã®èª¬æãä»ããããããéšåã§ã
ãtipsaterenkoã«æè¬ããŸã
ã
3.
ãããã質åecho ru.unix.prog
4. UNIXã©ã€ãã©ãª
libaioã®æŠèŠ
5.
2.6.12ãã2.6.37ãŸã§ã®ã³ã¢ããã©ãŒãã³ã¹ãã¹ã ã