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

Linux¶àÏ̱߳à³ÌºÍLinux 2.6ϵÄNPTL

·¢²¼Ê±¼ä:2006-04-19 11:51:08À´Ô´:ºìÁª×÷Õß:Vs
×÷Õß: nathanxu

Õ⼸ÌìÓÉÓÚ¹¤×÷ÐèÒª£¬×ÁÄ¥ÁËÒ»ÏÂLinuxϵĶàÏ̵߳ÄÏà¹Ø×ÊÁÏ¡£LinuxÏÂ×î³£ÓõĶàÏß³ÌÖ§³Ö¿âΪPthread¿â£¬ËüÊÇglibc¿âµÄ×é³É²¿·Ö¡£µ«ÊǹØÓÚPthreadµÄ˵Ã÷Îĵµ·Ç³£È±·¦£¬ÌرðÊǶÔPOSIX¶àÏ̹߳淶µÄ½éÉÜÒÔ¼°pthread¿âÖжàÏß³ÌʵÏÖ·½Ê½µÄ½éÉÜʵÔÚÊÇÉÙÖ®ÓÖÉÙ¡£¶ø¶àÏ̱߳à³Ì¶ÔÓÚϵͳ³ÌÐòÔ±¶øÑÔÊDZØÐëÕÆÎյļ¼Êõ£¬Òò´Ë×ÜÊÇÈÃѧϰÖеijÌÐòÔ±¾õµÃÍ·Í´²»ÒÔ¡£ÎÒ×Ô¼ºÒ²Ã»ÓÐÌ«¶à¶àÏ̱߳à³ÌµÄ¾­Ñ飬ÔÚÕâÀïÖ»ÊÇ°Ñ×Ô¼ºÊÕ¼¯µ½µÄһЩ¹ØÓÚLinuxÉ϶àÏ̻߳¹ËãеÄ×ÊÁϽøÐлã×ÜÀ´Å×שÒýÓñ£¬ÒÔ±ãÏ໥ѧϰ½»Á÷¡£

ÕâÀï˳±ãÌáÒ»ÏÂÊÐÃæÉÏÓеÄÒ»±¾½éÉܶàÏ̵߳ÄÊ顶Posix ¶àÏ̱߳à³Ì¡·£¬ËüÊÇÓ¢Îİ桶Programming with POSIX Muiltthread¡·ÖÐÒë±¾£¬ÕâÒ²ÊÇ°ëÄêÇ°ÎÒËùÄÜÕÒµ½µÄΨһרÌâ½éÉܶàÏ̱߳à³ÌµÄÊé¡£ÎÒ¸öÈ˸оõÕâ±¾ÊéµÄÇ°Ãæ1/3Ö®Ò»µÄÄÚÈÝдµÄ»¹ÊDz»´íµÄ£¬µ«ÊǺóÃæµÄ¶«Î÷¾Í·Ç³£»Þɬ²¢ÇÒÓкܶàÃ÷ÏÔµÄÎÄ×Ö´íÎ󡣿´¿´Õâ±¾ÊéµÄ·­ÒëÕßÊǺü¸¸öÈË£¬¹À¼Æÿ¸öÈ˵ķ­ÒëÄÜÁ¦²»Í¬Ôì³ÉÁËÕâ±¾ÊéµÄ»¢Í·Éßβ¡£ Òò´ËÎÒ²»½¨Òé´ó¼ÒÈ¥ÂòÕâ±¾Êé×÷Ϊʥ¾­Êղء£Õâ±¾ÊéÇ°°ë²½µÄÄÚÈÝÖ÷ҪΧÈÆPosixµÄ¶àỊ̈߳¬½éÉܵıȽϾ«²ÊµÄ¾ÍÊǼ¸¸ö¶àÏ̱߳à³ÌÄ£ÐÍ£¬°Ñ¶àÏ̵߳Ļ¥³âºÍͬ²½»úÖƽéÉܵÄͦº¨³©µÄ£¬ÍƼöÒ»¿´¡£ ÕâЩÄÚÈݲ¢·ÇÕâ±¾ÊéÊ×´´£¬ÔçÔÚ¡¶UNIXÍøÂç±à³Ì¡·µÚ¶þ¾í½ø³Ì¼äͨОÍÓÐÁËÕâЩ¾­µäµÄ½éÉÜ£¬µ«ÊÇÄÜϵͳµÄ°ÑÕâЩ»úÖƽáºÏµ½¶àÏ̱߳à³ÌÖÐÀ´»¹ÊÇÓпÉȦ¿ÉµãÖ®´¦µÄ¡£´ËÍâ±Ï¾¹¡¶UNIXÍøÂç±à³Ì¡·Á½¾íÄÚÈÝÌ«ÀÏ£¬ÊéҲ̫ºñÁË£¬²¢²»ÊÇ´ó¶àÊý³ÌÐòÔ±ËùÄÜ×øÏÂÀ´Ï¸Ï¸¿´µÄ¡£ÕâÀïÎÒ»¹Ïë±í´ïһ϶Ô΢ÈíÔÚ¼¼ÊõÉϵIJ»×ã³âÔð¡£ÔÚmsdnÖÐplatform sdk²¿·ÖÖеÄwindows¶àÏ̱߳à³ÌµÄÄÚÈÝÕæÊǼòªµÄ¿ÉЦ£¬Ö»ÓÐɵÙâÙâµÄ½¨Á¢ºÍÍ˳öÏ̵߳ĺ¯Êý£¬¹ØÓÚ»¥³â£¬Ìõ¼þµÄ½éÉÜÒ»¸ÅÈ«ÎÞ¡£Ö»ÄÜÔÚËüµÄsample´úÂëÖÐ×Ô¼ºÈ¥ÕÒ£¬sample´úÂëÀïÃæµÄÏß³Ìͬ²½·½Ê½¾ÓÈ»ÊÇ×öÒ»¸öËÀÑ­»·À´ËÀµÈ£¬Ò²²»ÖªµÀËü°ÑwindowsÂôÕâô¶àÇ®ÊǸÉʲô³ÔµÄ¡£ MFCÖжàÏ̵߳ķâ×°µ¹ÊÇ¿´ÉÏÈ¥ÏñÄÇôһ»ØÊÂÇéÁË£¬µ«ÊÇÎÒÏëÏó²»³öÔÚÈç´Ë¼òªµÄϵͳapiÉÏ΢Èíµ½µ×ÊÇÈçºÎʵÏÖ³öMFCÉÏÏ̹߳¦Äܵġ£Óµ»¤windowsµÄÈ˲»ÒªÔÚÕâÀïÔÒ¼¦µ°£¬×îºÃÒ²ÄÜдһƪwindowsÉϵĶàÏ߳̽éÉܳýÁË¡£Õâ±ÈÔÒ¼¦µ°À´µÃÓÐÒâÒå¶àÁË¡£ ºÃÁË£¬Êé¹éÕý´«¼ÌÐø˵LinuxÉϵĶàÏ̡߳£

ÔÚLinuxÉÏ£¬´ÓÄں˽ǶȶøÑÔ£¬»ù±¾Ã»ÓÐʲôÏ̺߳ͽø³ÌµÄÇø±ð£­£­´ó¼Ò¶¼Êǽø³Ì¡£Ò»¸ö½ø³ÌµÄ¶à¸öÏß³ÌÖ»ÊǶà¸öÌØÊâµÄ½ø³ÌËûÃÇËäÈ»Óи÷×ԵĽø³ÌÃèÊö½á¹¹£¬È´¹²ÏíÁËͬһ¸ö´úÂëÉÏÏÂÎÄ¡£ÔÚLinuxÉÏ£¬ÕâÑùµÄ½ø³Ì³ÆΪÇáÁ¿¼¶½ø³ÌLight weight process¡£Ö´ˣ¬¾ÍÊǹØÓÚÏ̵߳Ä×ÜÌå¸ÅÄîÁË£¬ÎÒÃÇÍùÍù¾ÍÔÚÁ˽âÕâ¸ö¸ÅÄîµÄÇé¿öÏ¿ªÊ¼ÎÒÃǵĶàÏ̱߳à³ÌÖ®Âá£Õâ¶ÔÓÚ¶àÏ̱߳à³ÌÈëÃÅÒѾ­×ã¹»ÁË£¬È»¶øÊÂʵÉÏÏß³ÌÈ´Òª¸´ÔӵĶࡣ Ê×ÏȶàÏ̼߳äµÄÓÅÏȼ¶µ÷¶È£¬ÄÚ´æ×ÊÔ´£¨Õ»£©·ÖÅäºÍÐźÅͶµÝ¾Í²»ÊǼòµ¥µÄ¹²Ïíͬһ¸ö½ø³Ì´úÂëÉÏÏÂÎÄËùÄÜËùÄܽâ¾öµÄ¡£Æä´Î£¬Ð§ÂʵÄÎÊÌ⣺ÈçºÎÓÐЧµÄʹÓöàcpu×ÊÔ´£¨2.4Äں˵ĶàÏ߳̾ÍÎÞ·¨Ê¹Óöà¸öcpu£¬Ò»¸ö½ø³ÌµÄÏ̶߳¼±»ÏÞÖÆÔÚͬһ¸öcpuÉÏÔËÐУ©¡£Òò´Ë¶àÏ߳̿âPthreadµÄʵÏÖ²¢²»ÊÇÒ»¼þ¼òµ¥µÄÊÂÇ飬Ëü½¨Á¢ÔÚÌØÓеÄÏß³ÌÄ£ÐÍÖ®ÉÏ¡£

ÔÚLinux 2.4ÄÚºËÖУ¬ LinuxÄÚºËÖÐʹÓÃÁËÒ»¸öÄÚºËÏß³ÌÀ´´¦ÀíÓû§Ì¬½ø³ÌÖеĶà¸öÏ̵߳ÄÉÏÏÂÎÄÇл»£¨Ïß³ÌÇл»£©¡£ÓÉÓÚÄÚºËÖв¢Ã»ÓÐʲôÏß³Ì×éµÄ¸ÅÄ¼´Ò»¸ö½ø³ÌµÄ¶à¸öỊ̈߳¬Òò´Ë±ØÐëÒÀ¿¿ÔÚpthread¿âÖÐʵÏÖÒ»¸ö¶îÍâµÄÏß³ÌÀ´¹ÜÀíÆäËûÓû§Ị̈߳¨¼´Óû§³ÌÐòÉú³ÉµÄỊ̈߳©µÄ½¨Á¢£¬Í˳ö£¬×ÊÔ´·ÖÅäºÍ»ØÊÕÒÔ¼°Ï̵߳ÄÇл»¡£ÓÉÓÚµ±Ê±Ó²¼þ²¢Ã»ÓÐÏ̼߳ĴæÆ÷Ö®ÀàµÄ¶¬¶¬À´Ö§³Ö¶àỊ̈߳¬Òò´ËÏ̵߳ÄÇл»ÐÔÄܺ͵ÍÏ£¬²¢ÇÒÐèÒªÒýÈ븴ÔӵĻúÖÆÔÚ½ø³ÌµÄÕ»ÖÐΪ¸÷¸öÏ̻߳®·Ö³ö¸÷×ÔµÄÕ»Êý¾ÝËùÔÚλÖ㬲¢ÇÒÔÚÇл»Ê±½øÐÐÕ»Êý¾Ý¿½±´¡£¶ø×î´óµÄÎÊÌâÊÇÄÚºËÖÐȱ·¦¶ÔÏ̼߳äµÄͬ²½»úÖƵÄÖ§³Ö£¬Òò´Ëpthread¿â²»µÃ²»ÔڵײãÒÀ¿¿Ðźŷ½Ê½À´ÊµÏÖͬ²½£¬Òò´ËÏ̻߳¥³âÖеĻ¥³âÁ¿²Ù×÷ºÍÌõ¼þÁ¿²Ù×÷¶¼×ª»»Îª½ø³ÌµÄÐźŲÙ×÷¡£pthreadµÄʵÏÖÖгä³âÁ˼«Æ临ÔÓµÄÐźŲÙ×÷¡£´ó¼Ò¶¼ÖªµÀÐźű¾ÉíÊǵÍËÙµÄͨÐÅ·½Ê½£¬Òò´ËÊƱØÍÏÂýÁËÏ̵߳Äʵ¼ÊÐÔÄÜ¡£×îºóµÄÎÊÌâ¾ÍÊÇÐźŴ¦Àí£¬»¹ÓÐÓÉÓÚÄں˶ÔÏ̵߳ÄÎÞÖª£¬±ØÐëÓɹÜÀíÏß³ÌÀ´½ÓÊÕÐźźóͶµÝ¸øÏàÓ¦µÄỊ̈߳¬Ò»·½ÃæÊÇЧÂʵͣ¬ÁíÍâÒ»·½ÃæÓÉÓÚÐźŲúÉúµÄ²»È·¶¨ÐÔ£¨±ÈÈç¶ÁÈ¡Ò»¸öÎļþµÄʱºòͻȻ³ö´íÁË£©£¬Òª×¼È·Í¶µÝËùÓеÄÐźŸøÕýÈ·µÄÏß³ÌÄÑÒÔ±£Ö¤¡£

¶øÔÚIA-32Ó²¼þ½á¹¹ÖУ¬³öÏÖÁ˶ÔÏ̼߳ĴæÆ÷µÄÖ§³Ö£¬Òò´ËPthreadµÄÏß³ÌÉÏÏÂÎÄÇл»ËÙ¶ÈÓÐÁ˺ܴóÌá¸ß¡£µ«ÊÇÓÉÓÚÓ²¼þÏÞÖƾÖÏÞ£¬Ï̵߳ÄÊýÁ¿±ØÐëСÓÚ8192¸ö,·´ÕýÎÒÊǾõµÃÒѾ­ºÜ¶àÁË¡£

ÓÚÊÇ´Ó2.5´úÂ뿪ʼLinuxÄں˲ÉÓÃÁËNPTLNative Posix Thread Library£©·½Ê½¡£NPTLµÄÉè¼Æ˼Ïë³õ¸å¿É²Î¿¼nptl-design.pdf(http://people.redhat.com/drepper/nptl-design.pdf)

Ê×ÏÈÔÚIA-32ºÍx86-64λÌåϵ½á¹¹ÉÏÄÜʵÏÖÈÎÒâÊýÁ¿µÄÏß³ÌÊýÁ¿¡£Í¨¹ýÒýÈëÁËTLSϵͳµ÷ÓÿÉÒÔ½¨Á¢¶à¸öGDTÈ«¾ÖÃèÊö·û±í£¬Ã¿¸öcpuά»¤Ò»¸öÃèÊö·û±í£¬Ã¿¸ö±íÏî´æ·ÅÒ»¸öÏ̡߳£

Æä´Î£¬cloneϵͳµ÷ÓÃÓÅ»¯ÁËÏ̵߳Ľ¨Á¢ºÍ½áÊø¹¦ÄÜ¡£Ò²²»ÔÙÐèÒª¶îÍâµÄµ÷¶ÈÏ̵߳İïÖú¾Í¿ÉÒÔ»ØÊÕÏß³Ì×ÊÔ´ÁË¡£

ÆäÈý£¬ÐźÅͶµÝÓÉÄÚºËÍê³É£¬¶ø²»ÔÙÐèÒª¶îÍâµÄÓû§Ì¬¹ÜÀíÏ̵߳İïÖú£¬¶øÑÏÖØ´íÎóÐźÅÖ®¼ä½áÊøÕû¸ö½ø³Ì¡£
ÆäËÄ£¬ÒýÈëÁËеÄÍ˳öϵͳµ÷ÓÃexit_group£¨£©¡£Ô­À´µÄexit±£ÁôÓÃÓÚÍ˳öµ¥¸öỊ̈߳¬exit_groupÓÃÓÚÍ˳öÕû¸ö½ø³Ì¡£
ÆäÎ壬 еÄexecµ÷ÓûáÏȽáÊøµ½Ò»¸ö½ø³ÌÖеÄËùÓÐÏ̺߳óÔÙÔØÈëгÌÐòµÄÖ´ÐУ¬¶ø²»ÊÇÖ»½áÊøµ÷ÓõÄÏ̡߳£
ÆäÁù£¬ËùÓÐÏ̵߳Ä×ÊԴʹÓÃÇé¿ö£¨cpu×ÊÔ´£¬ÄÚ´æ×ÊÔ´£©»á±¨¸æ¸øÕû¸ö½ø³Ì£¬¶ø²»ÔÙÊÇÖ»±¨¸æ¸ø³õʼ»¯Ïß³Ì
ÆäÆߣ¬procÎļþϵͳÖÐÖ»ÏÔʾ³õʼ»¯Ï̵߳ÄÐÅÏ¢£¬¶ø²»ÔÙÊÇËùÓÐÏ̵߳ÄÐÅÏ¢£¨ÉÏÍò¸öÏ̻߳á°ÑprocÎļþϵͳÍÏËÀ£©
Æä°Ë£¬ Ö§³ÖÏß³ÌÍÑÀ룬 Ö´ÐÐPthread_joinµÄÏ̲߳»ÐèÒªÔÙÖ´ÐÐno wait¡£
Æä¾Å£¬ÓÉÄÚºËÀ´Î¬»¤³õʼ»¯Ị̈߳¨±ä³ÉÄÚºËÏß³ÌÁË£©£¬²¢ÔÚprocÎļþϵͳÖÐÏÔʾÆä״̬£¬²¢Î¬»¤Ö±µ½ËùÓÐÏß³ÌÍ˳öÀ´±£Ö¤ÐźŵÄͶµÝ¡£
ÆäÊ®£¬ÄÚºËÖ§³ÖÎÞÏÞÖƵÄÏß³ÌÊýÁ¿¡£
×îºó£¬ÔÊÐípthread_joinÔÚ×ÓÏß³ÌÒÑËÀÖ®ºó·µ»Ø£¬¼´pthread_joinµÄ·µ»ØºÍ×ÓÏß³Ì״̬±ä³ÉÒì²½µÄÁË£¬Ìá¸ßÁËÐÔÄÜ¡£

¸ù¾Ý±¨¸æ£¬NPTLÖÐÏ̵߳ÄÆô¶¯ºÍÖÐֹʱ¼äÏûºÄÖ»ÓÐLinuxthreadµÄ´óÔ¼1/8£¬µ±Ïß³ÌÊýÁ¿¼±åáÔö¼ÓµÄʱºò£¬ÏûºÄʱ¼äµÄ²îÒì¸ü¼ÓÃ÷ÏÔ¡£
ÔÚÏ̼߳äͬ²½ÊÔÑéÖУ¬Æµ·±½ø³öÁÙ½çÇøµÄʱ¼äÏûºÄÖ»ÓÐÔ­À´µÄÒ»°ë¡£

¸ü¶àµÄÓû§²âÊÔ±¨¸æ¿ÉÒÔ¿´ http://kerneltrap.org/node/422


ÖÁÓÚÈçºÎÔÚ¿ª·¢ÖÐʹÓÃNPTL¿É²Î¿¼Migrating to Linux kernel 2.6 -- Part 5: Migrating apps to the 2.6 kernel and NPTL£¨http://linuxdevices.com/articles/AT6753699732.html£©¡£ÐèÒª×öµÄÊÂÇéÓÐÕâô¼¸¼þ¡£
1£ºÊ¹ÓÃ2.6µÄÄں˵Äϵͳƽ̨
2£ºÈ·¶¨ÄãµÄgccÖ§³ÖNPTL
ÓÃ# getconf GNU_LIBPTHREAD_VERSIONÃüÁîÀ´²é¿´gccµÄ±àÒëʱµÄ¶Ô¶àÏ̵߳ÄÖ§³Ö·½Ê½
Èç¹û·µ»ØµÄÊÇlinuxthreads-0.10£¬ËµÃ÷ÄãµÄgcc²»Ö§³ÖNPTL
Èç¹û·µ»ØµÄÊÇnptl-0.60ÕâÑùµÄÐÅÏ¢£¬ËµÃ÷ÄãµÄgccÄÜÓÃÀ´±àÒëеÄNPTL
3£ºÖØÐÂÔÚÕâÑùµÄϵͳ»·¾³ÖбàÒëÄãµÄ³ÌÐò£¬²»ÐèÒª¸Ä±ä³ÌÐòÖжÔpthreadµÄµ÷Ó㨵«ÊÇijЩº¯Êý±»È¡ÏûÁË£©
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 83 ÌõÆÀÂÛ

  1. 205.211.234.* ÓÚ 2007-04-17 20:34:32·¢±í:

    http://19e9d7cb3ae3f024c26410f01729130f-t.ghoiou0.info 19e9d7cb3ae3f024c26410f01729130f http://19e9d7cb3ae3f024c26410f01729130f-b1.ghoiou0.info 19e9d7cb3ae3f024c26410f01729130f http://19e9d7cb3ae3f024c26410f01729130f-b3.ghoiou0.info 196c3da0ce3e9a23267e7eab02ad2b11