°´Õս̿ÆÊéÉϵĶ¨Ò壬½ø³ÌÊÇ×ÊÔ´¹ÜÀíµÄ×îСµ¥Î»£¬Ïß³ÌÊdzÌÐòÖ´ÐеÄ×îСµ¥Î»¡£ÔÚ²Ù×÷ϵͳÉè¼ÆÉÏ£¬´Ó½ø³ÌÑÝ»¯³öỊ̈߳¬×îÖ÷ÒªµÄÄ¿µÄ¾ÍÊǸüºÃµÄÖ§³ÖSMPÒÔ¼°¼õС£¨½ø³Ì/Ị̈߳©ÉÏÏÂÎÄÇл»¿ªÏú¡£
ÎÞÂÛ°´ÕÕÔõÑùµÄ·Ö·¨£¬Ò»¸ö½ø³ÌÖÁÉÙÐèÒªÒ»¸öÏß³Ì×÷ΪËüµÄÖ¸ÁîÖ´ÐÐÌ壬½ø³Ì¹ÜÀí×Å×ÊÔ´£¨±ÈÈçcpu¡¢ÄÚ´æ¡¢ÎļþµÈµÈ£©£¬¶ø½«Ï̷߳ÖÅ䵽ij¸öcpuÉÏÖ´ÐС£Ò»¸ö½ø³Ìµ±È»¿ÉÒÔÓµÓжà¸öỊ̈߳¬´Ëʱ£¬Èç¹û½ø³ÌÔËÐÐÔÚSMP»úÆ÷ÉÏ£¬Ëü¾Í¿ÉÒÔͬʱʹÓöà¸öcpuÀ´Ö´Ðи÷¸öỊ̈߳¬´ïµ½×î´ó³Ì¶ÈµÄ²¢ÐУ¬ÒÔÌá¸ßЧÂÊ£»Í¬Ê±£¬¼´Ê¹ÊÇÔÚµ¥cpuµÄ»úÆ÷ÉÏ£¬²ÉÓöàÏß³ÌÄ£ÐÍÀ´Éè¼Æ³ÌÐò£¬ÕýÈçµ±Äê²ÉÓöà½ø³ÌÄ£ÐÍ´úÌæµ¥½ø³ÌÄ£ÐÍÒ»Ñù£¬Ê¹Éè¼Æ¸ü¼ò½à¡¢¹¦ÄܸüÍ걸£¬³ÌÐòµÄÖ´ÐÐЧÂÊÒ²¸ü¸ß£¬ÀýÈç²ÉÓöà¸öÏß³ÌÏìÓ¦¶à¸öÊäÈ룬¶ø´Ëʱ¶àÏß³ÌÄ£ÐÍËùʵÏֵŦÄÜʵ¼ÊÉÏÒ²¿ÉÒÔÓöà½ø³ÌÄ£ÐÍÀ´ÊµÏÖ£¬¶øÓëºóÕßÏà±È£¬Ï̵߳ÄÉÏÏÂÎÄÇл»¿ªÏú¾Í±È½ø³ÌҪС¶àÁË£¬´ÓÓïÒåÉÏÀ´Ëµ£¬Í¬Ê±ÏìÓ¦¶à¸öÊäÈëÕâÑùµÄ¹¦ÄÜ£¬Êµ¼ÊÉϾÍÊǹ²ÏíÁ˳ýcpuÒÔÍâµÄËùÓÐ×ÊÔ´µÄ¡£
Õë¶ÔÏß³ÌÄ£Ð͵ÄÁ½´óÒâÒ壬·Ö±ð¿ª·¢³öÁ˺ËÐļ¶Ï̺߳ÍÓû§¼¶Ïß³ÌÁ½ÖÖÏß³ÌÄ£ÐÍ£¬·ÖÀàµÄ±ê×¼Ö÷ÒªÊÇÏ̵߳ĵ÷¶ÈÕßÔÚºËÄÚ»¹ÊÇÔÚºËÍ⡣ǰÕ߸üÀûÓÚ²¢·¢Ê¹ÓöദÀíÆ÷µÄ×ÊÔ´£¬¶øºóÕßÔò¸ü¶à¿¼ÂǵÄÊÇÉÏÏÂÎÄÇл»¿ªÏú¡£ÔÚÄ¿Ç°µÄÉÌÓÃϵͳÖУ¬Í¨³£¶¼½«Á½Õß½áºÏÆðÀ´Ê¹Ó㬼ÈÌṩºËÐÄÏß³ÌÒÔÂú×ãsmpϵͳµÄÐèÒª£¬Ò²Ö§³ÖÓÃÏ߳̿âµÄ·½Ê½ÔÚÓû§Ì¬ÊµÏÖÁíÒ»Ì×Ï̻߳úÖÆ£¬´Ëʱһ¸öºËÐÄÏß³Ìͬʱ³ÉΪ¶à¸öÓû§Ì¬Ï̵߳ĵ÷¶ÈÕß¡£ÕýÈçºÜ¶à¼¼ÊõÒ»Ñù£¬"»ìºÏ"ͨ³£¶¼ÄÜ´øÀ´¸ü¸ßµÄЧÂÊ£¬µ«Í¬Ê±Ò²´øÀ´¸ü´óµÄʵÏÖÄѶȣ¬³öÓÚ"¼òµ¥"µÄÉè¼Æ˼·£¬Linux´ÓÒ»¿ªÊ¼¾ÍûÓÐʵÏÖ»ìºÏÄ£Ð͵ļƻ®£¬µ«ËüÔÚʵÏÖÉϲÉÓÃÁËÁíÒ»ÖÖ˼·µÄ"»ìºÏ"¡£
ÔÚÏ̻߳úÖƵľßÌåʵÏÖÉÏ£¬¿ÉÒÔÔÚ²Ù×÷ϵͳÄÚºËÉÏʵÏÖỊ̈߳¬Ò²¿ÉÒÔÔÚºËÍâʵÏÖ£¬ºóÕßÏÔȻҪÇóºËÄÚÖÁÉÙʵÏÖÁ˽ø³Ì£¬¶øÇ°ÕßÔòÒ»°ãÒªÇóÔÚºËÄÚͬʱҲ֧³Ö½ø³Ì¡£ºËÐļ¶Ïß³ÌÄ£ÐÍÏÔȻҪÇóÇ°ÕßµÄÖ§³Ö£¬¶øÓû§¼¶Ïß³ÌÄ£ÐÍÔò²»Ò»¶¨»ùÓÚºóÕßʵÏÖ¡£ÕâÖÖ²îÒ죬ÕýÈçÇ°ËùÊö£¬ÊÇÁ½ÖÖ·ÖÀ෽ʽµÄ±ê×¼²»Í¬´øÀ´µÄ¡£
µ±ºËÄÚ¼ÈÖ§³Ö½ø³ÌÒ²Ö§³ÖÏß³Ìʱ£¬¾Í¿ÉÒÔʵÏÖÏß³Ì-½ø³ÌµÄ"¶à¶Ô¶à"Ä£ÐÍ£¬¼´Ò»¸ö½ø³ÌµÄij¸öÏß³ÌÓɺËÄÚµ÷¶È£¬¶øͬʱËüÒ²¿ÉÒÔ×÷ΪÓû§¼¶Ï̳߳صĵ÷¶ÈÕߣ¬Ñ¡ÔñºÏÊʵÄÓû§¼¶Ïß³ÌÔÚÆä¿Õ¼äÖÐÔËÐС£Õâ¾ÍÊÇÇ°ÃæÌáµ½µÄ"»ìºÏ"Ïß³ÌÄ£ÐÍ£¬¼È¿ÉÂú×ã¶à´¦Àí»úϵͳµÄÐèÒª£¬Ò²¿ÉÒÔ×î´óÏ޶ȵļõСµ÷¶È¿ªÏú¡£¾ø´ó¶àÊýÉÌÒµ²Ù×÷ϵͳ£¨ÈçDigital Unix¡¢Solaris¡¢Irix£©¶¼²ÉÓõÄÕâÖÖÄܹ»ÍêȫʵÏÖPOSIX1003.1c±ê×¼µÄÏß³ÌÄ£ÐÍ¡£ÔÚºËÍâʵÏÖµÄÏß³ÌÓÖ¿ÉÒÔ·ÖΪ"Ò»¶ÔÒ»"¡¢"¶à¶ÔÒ»"Á½ÖÖÄ£ÐÍ£¬Ç°ÕßÓÃÒ»¸öºËÐĽø³Ì£¨Ò²ÐíÊÇÇáÁ¿½ø³Ì£©¶ÔÓ¦Ò»¸öỊ̈߳¬½«Ï̵߳÷¶ÈµÈͬÓÚ½ø³Ìµ÷¶È£¬½»¸øºËÐÄÍê³É£¬¶øºóÕßÔòÍêÈ«ÔÚºËÍâʵÏÖ¶àỊ̈߳¬µ÷¶ÈÒ²ÔÚÓû§Ì¬Íê³É¡£ºóÕß¾ÍÊÇÇ°ÃæÌáµ½µÄµ¥´¿µÄÓû§¼¶Ïß³ÌÄ£Ð͵ÄʵÏÖ·½Ê½£¬ÏÔÈ»£¬ÕâÖÖºËÍâµÄÏ̵߳÷¶ÈÆ÷ʵ¼ÊÉÏÖ»ÐèÒªÍê³ÉÏß³ÌÔËÐÐÕ»µÄÇл»£¬µ÷¶È¿ªÏú·Ç³£Ð¡£¬µ«Í¬Ê±ÒòΪºËÐÄÐźţ¨ÎÞÂÛÊÇͬ²½µÄ»¹ÊÇÒì²½µÄ£©¶¼ÊÇÒÔ½ø³ÌΪµ¥Î»µÄ£¬Òò¶øÎÞ·¨¶¨Î»µ½Ị̈߳¬ËùÒÔÕâÖÖʵÏÖ·½Ê½²»ÄÜÓÃÓڶദÀíÆ÷ϵͳ£¬¶øÕâ¸öÐèÇóÕý±äµÃÔ½À´Ô½´ó£¬Òò´Ë£¬ÔÚÏÖʵÖУ¬´¿Óû§¼¶Ï̵߳ÄʵÏÖ£¬³ýËã·¨Ñо¿Ä¿µÄÒÔÍ⣬¼¸ºõÒѾÏûʧÁË¡£
LinuxÄÚºËÖ»ÌṩÁËÇáÁ¿½ø³ÌµÄÖ§³Ö£¬ÏÞÖÆÁ˸ü¸ßЧµÄÏß³ÌÄ£Ð͵ÄʵÏÖ£¬µ«Linux×ÅÖØÓÅ»¯Á˽ø³ÌµÄµ÷¶È¿ªÏú£¬Ò»¶¨³Ì¶ÈÉÏÒ²ÃÖ²¹ÁËÕâһȱÏÝ¡£Ä¿Ç°×îÁ÷ÐеÄÏ̻߳úÖÆLinuxThreadsËù²ÉÓõľÍÊÇÏß³Ì-½ø³Ì"Ò»¶ÔÒ»"Ä£ÐÍ£¬µ÷¶È½»¸øºËÐÄ£¬¶øÔÚÓû§¼¶ÊµÏÖÒ»¸ö°üÀ¨ÐźŴ¦ÀíÔÚÄÚµÄÏ̹߳ÜÀí»úÖÆ¡£Linux-LinuxThreadsµÄÔËÐлúÖÆÕýÊDZ¾ÎĵÄÃèÊöÖص㡣
--------------------------------------------------------------------------------
»ØÒ³Ê×
¶þ.Linux 2.4ÄÚºËÖеÄÇáÁ¿½ø³ÌʵÏÖ
×î³õµÄ½ø³Ì¶¨Ò嶼°üº¬³ÌÐò¡¢×ÊÔ´¼°ÆäÖ´ÐÐÈý²¿·Ö£¬ÆäÖгÌÐòͨ³£Ö¸´úÂ룬×ÊÔ´ÔÚ²Ù×÷ϵͳ²ãÃæÉÏͨ³£°üÀ¨ÄÚ´æ×ÊÔ´¡¢IO×ÊÔ´¡¢ÐźŴ¦ÀíµÈ²¿·Ö£¬¶ø³ÌÐòµÄÖ´ÐÐͨ³£Àí½âΪִÐÐÉÏÏÂÎÄ£¬°üÀ¨¶ÔcpuµÄÕ¼Ó㬺óÀ´·¢Õ¹ÎªÏ̡߳£ÔÚÏ̸߳ÅÄî³öÏÖÒÔÇ°£¬ÎªÁ˼õС½ø³ÌÇл»µÄ¿ªÏú£¬²Ù×÷ϵͳÉè¼ÆÕßÖð½¥ÐÞÕý½ø³ÌµÄ¸ÅÄÖð½¥ÔÊÐí½«½ø³ÌËùÕ¼ÓеÄ×ÊÔ´´ÓÆäÖ÷Ìå°þÀë³öÀ´£¬ÔÊÐíijЩ½ø³Ì¹²ÏíÒ»²¿·Ö×ÊÔ´£¬ÀýÈçÎļþ¡¢Ðźţ¬Êý¾ÝÄڴ棬ÉõÖÁ´úÂ룬Õâ¾Í·¢Õ¹³öÇáÁ¿½ø³ÌµÄ¸ÅÄî¡£LinuxÄÚºËÔÚ2.0.x°æ±¾¾ÍÒѾʵÏÖÁËÇáÁ¿½ø³Ì£¬Ó¦ÓóÌÐò¿ÉÒÔͨ¹ýÒ»¸öͳһµÄclone()ϵͳµ÷Óýӿڣ¬Óò»Í¬µÄ²ÎÊýÖ¸¶¨´´½¨ÇáÁ¿½ø³Ì»¹ÊÇÆÕͨ½ø³Ì¡£ÔÚÄÚºËÖУ¬clone()µ÷Óþ¹ý²ÎÊý´«µÝºÍ½âÊͺó»áµ÷ÓÃdo_fork()£¬Õâ¸öºËÄÚº¯ÊýͬʱҲÊÇfork()¡¢vfork()ϵͳµ÷ÓõÄ×îÖÕʵÏÖ£º
int do_fork(unsigned long clone_flags, unsigned long stack_start,
struct pt_regs *regs, unsigned long stack_size)
ÆäÖеÄclone_flagsÈ¡×ÔÒÔϺêµÄ"»ò"Öµ£º
#define CSIGNAL 0x000000ff
/* signal mask to be sent at exit */
#define CLONE_VM 0x00000100
/* set if VM shared between processes */
#define CLONE_FS 0x00000200
/* set if fs info shared between processes */
#define CLONE_FILES 0x00000400
/* set if open files shared between processes */
#define CLONE_SIGHAND 0x00000800
/* set if signal handlers and blocked signals shared */
#define CLONE_PID 0x00001000
/* set if pid shared */
#define CLONE_PTRACE 0x00002000
/* set if we want to let tracing continue on the child too */
#define CLONE_VFORK 0x00004000
/* set if the parent wants the child to wake it up on mm_release */
#define CLONE_PARENT 0x00008000
/* set if we want to have the same parent as the cloner */
#define CLONE_THREAD 0x00010000
/* Same thread group? */
#define CLONE_NEWNS 0x00020000 /* New namespace group? */
#define CLONE_SIGNAL (CLONE_SIGHAND | CLONE_THREAD)
ÔÚdo_fork()ÖУ¬²»Í¬µÄclone_flags½«µ¼Ö²»Í¬µÄÐÐΪ£¬¶ÔÓÚLinuxThreads£¬ËüʹÓã¨CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND£©²ÎÊýÀ´µ÷ÓÃclone()´´½¨"Ïß³Ì"£¬±íʾ¹²ÏíÄÚ´æ¡¢¹²ÏíÎļþϵͳ·ÃÎʼÆÊý¡¢¹²ÏíÎļþÃèÊö·û±í£¬ÒÔ¼°¹²ÏíÐźŴ¦Àí·½Ê½¡£±¾½Ú¾ÍÕë¶ÔÕ⼸¸ö²ÎÊý£¬¿´¿´LinuxÄÚºËÊÇÈçºÎʵÏÖÕâЩ×ÊÔ´µÄ¹²ÏíµÄ¡£
1.CLONE_VM
do_fork()ÐèÒªµ÷ÓÃcopy_mm()À´ÉèÖÃtask_structÖеÄmmºÍactive_mmÏÕâÁ½¸ömm_structÊý¾ÝÓë½ø³ÌËù¹ØÁªµÄÄÚ´æ¿Õ¼äÏà¶ÔÓ¦¡£Èç¹ûdo_fork()ʱָ¶¨ÁËCLONE_VM¿ª¹Ø£¬copy_mm()½«°ÑеÄtask_structÖеÄmmºÍactive_mmÉèÖóÉÓëcurrentµÄÏàͬ£¬Í¬Ê±Ìá¸ß¸Ãmm_structµÄʹÓÃÕßÊýÄ¿£¨mm_struct::mm_users£©¡£Ò²¾ÍÊÇ˵£¬ÇáÁ¿¼¶½ø³ÌÓ븸½ø³Ì¹²ÏíÄÚ´æµØÖ·¿Õ¼ä£¬ÓÉÏÂͼʾÒâ¿ÉÒÔ¿´³ömm_structÔÚ½ø³ÌÖеĵØλ£º
2.CLONE_FS
task_structÖÐÀûÓÃfs£¨struct fs_struct *£©¼Ç¼Á˽ø³ÌËùÔÚÎļþϵͳµÄ¸ùĿ¼ºÍµ±Ç°Ä¿Â¼ÐÅÏ¢£¬do_fork()ʱµ÷ÓÃcopy_fs()¸´ÖÆÁËÕâ¸ö½á¹¹£»¶ø¶ÔÓÚÇáÁ¿¼¶½ø³ÌÔò½öÔö¼Ófs->count¼ÆÊý£¬Ó븸½ø³Ì¹²ÏíÏàͬµÄfs_struct¡£Ò²¾ÍÊÇ˵£¬ÇáÁ¿¼¶½ø³ÌûÓжÀÁ¢µÄÎļþϵͳÏà¹ØµÄÐÅÏ¢£¬½ø³ÌÖÐÈκÎÒ»¸öÏ̸߳ı䵱ǰĿ¼¡¢¸ùĿ¼µÈÐÅÏ¢¶¼½«Ö±½ÓÓ°Ïìµ½ÆäËûÏ̡߳£
3.CLONE_FILES
Ò»¸ö½ø³Ì¿ÉÄÜ´ò¿ªÁËһЩÎļþ£¬ÔÚ½ø³Ì½á¹¹task_structÖÐÀûÓÃfiles£¨struct files_struct *£©À´±£´æ½ø³Ì´ò¿ªµÄÎļþ½á¹¹£¨struct file£©ÐÅÏ¢£¬do_fork()Öе÷ÓÃÁËcopy_files()À´´¦ÀíÕâ¸ö½ø³ÌÊôÐÔ£»ÇáÁ¿¼¶½ø³ÌÓ븸½ø³ÌÊǹ²Ïí¸Ã½á¹¹µÄ£¬copy_files()ʱ½öÔö¼Ófiles->count¼ÆÊý¡£ÕâÒ»¹²ÏíʹµÃÈκÎÏ̶߳¼ÄÜ·ÃÎʽø³ÌËùά»¤µÄ´ò¿ªÎļþ£¬¶ÔËüÃǵIJÙ×÷»áÖ±½Ó·´Ó³µ½½ø³ÌÖеÄÆäËûÏ̡߳£
4.CLONE_SIGHAND
ÿһ¸öLinux½ø³Ì¶¼¿ÉÒÔ×ÔÐж¨Òå¶ÔÐźŵĴ¦Àí·½Ê½£¬ÔÚtask_structÖеÄsig£¨struct signal_struct£©ÖÐʹÓÃÒ»¸östruct k_sigaction½á¹¹µÄÊý×éÀ´±£´æÕâ¸öÅäÖÃÐÅÏ¢£¬do_fork()ÖеÄcopy_sighand()¸ºÔð¸´ÖƸÃÐÅÏ¢£»ÇáÁ¿¼¶½ø³Ì²»½øÐи´ÖÆ£¬¶ø½ö½öÔö¼Ósignal_struct::count¼ÆÊý£¬Ó븸½ø³Ì¹²Ïí¸Ã½á¹¹¡£Ò²¾ÍÊÇ˵£¬×Ó½ø³ÌÓ븸½ø³ÌµÄÐźŴ¦Àí·½Ê½ÍêÈ«Ïàͬ£¬¶øÇÒ¿ÉÒÔÏ໥¸ü¸Ä¡£
do_fork()ÖÐËù×öµÄ¹¤×÷ºÜ¶à£¬Ôڴ˲»ÏêϸÃèÊö¡£¶ÔÓÚSMPϵͳ£¬ËùÓеĽø³Ìfork³öÀ´ºó£¬¶¼±»·ÖÅäµ½Ó븸½ø³ÌÏàͬµÄcpuÉÏ£¬Ò»Ö±µ½¸Ã½ø³Ì±»µ÷¶Èʱ²Å»á½øÐÐcpuÑ¡Ôñ¡£
¾¡¹ÜLinuxÖ§³ÖÇáÁ¿¼¶½ø³Ì£¬µ«²¢²»ÄÜ˵Ëü¾ÍÖ§³ÖºËÐļ¶Ị̈߳¬ÒòΪLinuxµÄ"Ïß³Ì"ºÍ"½ø³Ì"ʵ¼ÊÉÏ´¦ÓÚÒ»¸öµ÷¶È²ã´Î£¬¹²ÏíÒ»¸ö½ø³Ì±êʶ·û¿Õ¼ä£¬ÕâÖÖÏÞÖÆʹµÃ²»¿ÉÄÜÔÚLinuxÉÏʵÏÖÍêÈ«ÒâÒåÉϵÄPOSIXÏ̻߳úÖÆ£¬Òò´ËÖÚ¶àµÄLinuxÏ߳̿âʵÏÖ³¢ÊÔ¶¼Ö»Äܾ¡¿ÉÄÜʵÏÖPOSIXµÄ¾ø´ó²¿·ÖÓïÒ壬²¢ÔÚ¹¦ÄÜÉϾ¡¿ÉÄܱƽü¡£
--------------------------------------------------------------------------------
»ØÒ³Ê×
Èý.LinuxThreadµÄÏ̻߳úÖÆ
LinuxThreadsÊÇÄ¿Ç°Linuxƽ̨ÉÏʹÓÃ×îΪ¹ã·ºµÄÏ߳̿⣬ÓÉXavier Leroy (Xavier.Leroy@inria.fr)¸ºÔ𿪷¢Íê³É£¬²¢ÒÑ°ó¶¨ÔÚGLIBCÖз¢ÐС£ËüËùʵÏֵľÍÊÇ»ùÓÚºËÐÄÇáÁ¿¼¶½ø³ÌµÄ"Ò»¶ÔÒ»"Ïß³ÌÄ£ÐÍ£¬Ò»¸öÏß³ÌʵÌå¶ÔÓ¦Ò»¸öºËÐÄÇáÁ¿¼¶½ø³Ì£¬¶øÏß³ÌÖ®¼äµÄ¹ÜÀíÔÚºËÍ⺯Êý¿âÖÐʵÏÖ¡£
1.Ïß³ÌÃèÊöÊý¾Ý½á¹¹¼°ÊµÏÖÏÞÖÆ
LinuxThreads¶¨ÒåÁËÒ»¸östruct _pthread_descr_structÊý¾Ý½á¹¹À´ÃèÊöỊ̈߳¬²¢Ê¹ÓÃÈ«¾ÖÊý×é±äÁ¿__pthread_handlesÀ´ÃèÊöºÍÒýÓýø³ÌËùϽÏ̡߳£ÔÚ__pthread_handlesÖеÄÇ°Á½ÏLinuxThreads¶¨ÒåÁËÁ½¸öÈ«¾ÖµÄϵͳỊ̈߳º__pthread_initial_threadºÍ__pthread_manager_thread£¬²¢ÓÃ__pthread_main_thread±íÕ÷__pthread_manager_threadµÄ¸¸Ị̈߳¨³õʼΪ__pthread_initial_thread£©¡£
struct _pthread_descr_structÊÇÒ»¸öË«»·Á´±í½á¹¹£¬__pthread_manager_threadËùÔÚµÄÁ´±í½ö°üÀ¨ËüÒ»¸öÔªËØ£¬Êµ¼ÊÉÏ£¬__pthread_manager_threadÊÇÒ»¸öÌØÊâỊ̈߳¬LinuxThreads½öʹÓÃÁËÆäÖеÄerrno¡¢p_pid¡¢p_priorityµÈÈý¸öÓò¡£¶ø__pthread_main_threadËùÔÚµÄÁ´Ôò½«½ø³ÌÖÐËùÓÐÓû§Ï̴߳®ÔÚÁËÒ»Æð¡£¾¹ýһϵÁÐpthread_create()Ö®ºóÐγɵÄ__pthread_handlesÊý×齫ÈçÏÂͼËùʾ£º
д´½¨µÄÏ߳̽«Ê×ÏÈÔÚ__pthread_handlesÊý×éÖÐÕ¼¾ÝÒ»ÏȻºóͨ¹ýÊý¾Ý½á¹¹ÖеÄÁ´Ö¸ÕëÁ¬ÈëÒÔ__pthread_main_threadΪÊ×Ö¸ÕëµÄÁ´±íÖС£Õâ¸öÁ´±íµÄʹÓÃÔÚ½éÉÜÏ̵߳Ĵ´½¨ºÍÊͷŵÄʱºò½«Ìáµ½¡£
LinuxThreads×ñÑPOSIX1003.1c±ê×¼£¬ÆäÖжÔÏ߳̿âµÄʵÏÖ½øÐÐÁËһЩ·¶Î§ÏÞÖÆ£¬±ÈÈç½ø³Ì×î´óÏß³ÌÊý£¬Ïß³Ì˽ÓÐÊý¾ÝÇø´óСµÈµÈ¡£ÔÚLinuxThreadsµÄʵÏÖÖУ¬»ù±¾×ñÑÕâЩÏÞÖÆ£¬µ«Ò²½øÐÐÁËÒ»¶¨µÄ¸Ä¶¯£¬¸Ä¶¯µÄÇ÷ÊÆÊÇ·ÅËÉ»òÕß˵À©´óÕâЩÏÞÖÆ£¬Ê¹±à³Ì¸ü¼Ó·½±ã¡£ÕâЩÏÞ¶¨ºêÖ÷Òª¼¯ÖÐÔÚsysdeps/unix/sysv/linux/bits/local_lim.h£¨²»Í¬Æ½Ì¨Ê¹ÓõÄÎļþλÖò»Í¬£©ÖУ¬°üÀ¨Èçϼ¸¸ö£º
ÿ½ø³ÌµÄ˽ÓÐÊý¾ÝkeyÊý£¬POSIX¶¨Òå_POSIX_THREAD_KEYS_MAXΪ128£¬LinuxThreadsʹÓÃPTHREAD_KEYS_MAX£¬1024£»Ë½ÓÐÊý¾ÝÊÍ·ÅʱÔÊÐíÖ´ÐеIJÙ×÷Êý£¬LinuxThreadsÓëPOSIXÒ»Ö£¬¶¨ÒåPTHREAD_DESTRUCTOR_ITERATIONSΪ4£»Ã¿½ø³ÌµÄÏß³ÌÊý£¬POSIX¶¨ÒåΪ64£¬LinuxThreadsÔö´óµ½1024£¨PTHREAD_THREADS_MAX£©£»Ïß³ÌÔËÐÐÕ»×îС¿Õ¼ä´óС£¬POSIXδָ¶¨£¬LinuxThreadsʹÓÃPTHREAD_STACK_MIN£¬16384£¨×Ö½Ú£©¡£
2.¹ÜÀíÏß³Ì
"Ò»¶ÔÒ»"Ä£Ð͵ĺô¦Ö®Ò»ÊÇÏ̵߳ĵ÷¶ÈÓɺËÐÄÍê³ÉÁË£¬¶øÆäËûÖîÈçÏß³ÌÈ¡Ïû¡¢Ï̼߳äµÄͬ²½µÈ¹¤×÷£¬¶¼ÊÇÔÚºËÍâÏ߳̿âÖÐÍê³ÉµÄ¡£ÔÚLinuxThreadsÖУ¬×¨ÃÅΪÿһ¸ö½ø³Ì¹¹ÔìÁËÒ»¸ö¹ÜÀíỊ̈߳¬¸ºÔð´¦ÀíÏß³ÌÏà¹ØµÄ¹ÜÀí¹¤×÷¡£µ±½ø³ÌµÚÒ»´Îµ÷ÓÃpthread_create()´´½¨Ò»¸öÏ̵߳Äʱºò¾Í»á´´½¨£¨__clone()£©²¢Æô¶¯¹ÜÀíÏ̡߳£
ÔÚÒ»¸ö½ø³Ì¿Õ¼äÄÚ£¬¹ÜÀíÏß³ÌÓëÆäËûÏß³ÌÖ®¼äͨ¹ýÒ»¶Ô"¹ÜÀí¹ÜµÀ£¨manager_pipe[2]£©"À´Í¨Ñ¶£¬¸Ã¹ÜµÀÔÚ´´½¨¹ÜÀíÏß³Ì֮ǰ´´½¨£¬Ôڳɹ¦Æô¶¯Á˹ÜÀíÏß³ÌÖ®ºó£¬¹ÜÀí¹ÜµÀµÄ¶Á¶ËºÍд¶Ë·Ö±ð¸³¸øÁ½¸öÈ«¾Ö±äÁ¿__pthread_manager_readerºÍ__pthread_manager_request£¬Ö®ºó£¬Ã¿¸öÓû§Ï̶߳¼Í¨¹ý__pthread_manager_requestÏò¹ÜÀíÏ̷߳¢ÇëÇ󣬵«¹ÜÀíÏ̱߳¾Éí²¢Ã»ÓÐÖ±½ÓʹÓÃ__pthread_manager_reader£¬¹ÜµÀµÄ¶Á¶Ë£¨manager_pipe[0]£©ÊÇ×÷Ϊ__clone()µÄ²ÎÊýÖ®Ò»´«¸ø¹ÜÀíÏ̵߳ģ¬¹ÜÀíÏ̵߳Ť×÷Ö÷Òª¾ÍÊǼàÌý¹ÜµÀ¶Á¶Ë£¬²¢¶Ô´ÓÖÐÈ¡³öµÄÇëÇó×÷³ö·´Ó¦¡£
´´½¨¹ÜÀíÏ̵߳ÄÁ÷³ÌÈçÏÂËùʾ£º
£¨È«¾Ö±äÁ¿pthread_manager_request³õֵΪ-1£©
³õʼ»¯½áÊøºó£¬ÔÚ__pthread_manager_threadÖмǼÁËÇáÁ¿¼¶½ø³ÌºÅÒÔ¼°ºËÍâ·ÖÅäºÍ¹ÜÀíµÄÏß³Ìid£¬2*PTHREAD_THREADS_MAX+1Õâ¸öÊýÖµ²»»áÓëÈκγ£¹æÓû§Ïß³Ìid³åÍ»¡£¹ÜÀíÏß³Ì×÷Ϊpthread_create()µÄµ÷ÓÃÕßÏ̵߳Ä×ÓÏß³ÌÔËÐУ¬¶øpthread_create()Ëù´´½¨µÄÄǸöÓû§Ïß³ÌÔòÊÇÓɹÜÀíÏß³ÌÀ´µ÷ÓÃclone()´´½¨£¬Òò´Ëʵ¼ÊÉÏÊǹÜÀíÏ̵߳Ä×ÓÏ̡߳££¨´Ë´¦×ÓÏ̵߳ĸÅÄîÓ¦¸Ãµ±×÷×Ó½ø³ÌÀ´Àí½â¡££©
__pthread_manager()¾ÍÊǹÜÀíÏ̵߳ÄÖ÷Ñ»·ËùÔÚ£¬ÔÚ½øÐÐһϵÁгõʼ»¯¹¤×÷ºó£¬½øÈëwhile(1)Ñ»·¡£ÔÚÑ»·ÖУ¬Ïß³ÌÒÔ2ÃëΪtimeout²éѯ£¨__poll()£©¹ÜÀí¹ÜµÀµÄ¶Á¶Ë¡£ÔÚ´¦ÀíÇëÇóÇ°£¬¼ì²éÆ丸Ị̈߳¨Ò²¾ÍÊÇ´´½¨managerµÄÖ÷Ị̈߳©ÊÇ·ñÒÑÍ˳ö£¬Èç¹ûÒÑÍ˳ö¾ÍÍ˳öÕû¸ö½ø³Ì¡£Èç¹ûÓÐÍ˳öµÄ×ÓÏß³ÌÐèÒªÇåÀí£¬Ôòµ÷ÓÃpthread_reap_children()ÇåÀí¡£
È»ºó²ÅÊǶÁÈ¡¹ÜµÀÖеÄÇëÇ󣬸ù¾ÝÇëÇóÀàÐÍÖ´ÐÐÏàÓ¦²Ù×÷£¨switch-case£©¡£¾ßÌåµÄÇëÇó´¦Àí£¬Ô´ÂëÖбȽÏÇå³þ£¬ÕâÀï¾Í²»×¸ÊöÁË¡£
3.Ïß³ÌÕ»
ÔÚLinuxThreadsÖУ¬¹ÜÀíÏ̵߳ÄÕ»ºÍÓû§Ï̵߳ÄÕ»ÊÇ·ÖÀëµÄ£¬¹ÜÀíÏß³ÌÔÚ½ø³Ì¶ÑÖÐͨ¹ýmalloc()·ÖÅäÒ»¸öTHREAD_MANAGER_STACK_SIZE×Ö½ÚµÄÇøÓò×÷Ϊ×Ô¼ºµÄÔËÐÐÕ»¡£
Óû§Ï̵߳ÄÕ»·ÖÅä°ì·¨Ëæ×ÅÌåϵ½á¹¹µÄ²»Í¬¶ø²»Í¬£¬Ö÷Òª¸ù¾ÝÁ½¸öºê¶¨ÒåÀ´Çø·Ö£¬Ò»¸öÊÇNEED_SEPARATE_REGISTER_STACK£¬Õâ¸öÊôÐÔ½öÔÚIA64ƽ̨ÉÏʹÓã»ÁíÒ»¸öÊÇFLOATING_STACKºê£¬ÔÚi386µÈÉÙÊýƽ̨ÉÏʹÓ㬴ËʱÓû§Ïß³ÌÕ»ÓÉϵͳ¾ö¶¨¾ßÌåλÖò¢Ìṩ±£»¤¡£Óë´Ëͬʱ£¬Óû§»¹¿ÉÒÔͨ¹ýÏß³ÌÊôÐԽṹÀ´Ö¸¶¨Ê¹ÓÃÓû§×Ô¶¨ÒåµÄÕ»¡£Òòƪ·ùËùÏÞ£¬ÕâÀïÖ»ÄÜ·ÖÎöi386ƽ̨ËùʹÓõÄÁ½ÖÖÕ»×éÖ¯·½Ê½£ºFLOATING_STACK·½Ê½ºÍÓû§×Ô¶¨Ò巽ʽ¡£
ÔÚFLOATING_STACK·½Ê½Ï£¬LinuxThreadsÀûÓÃmmap()´ÓÄں˿ռäÖзÖÅä8MB¿Õ¼ä£¨i386ϵͳȱʡµÄ×î´óÕ»¿Õ¼ä´óС£¬Èç¹ûÓÐÔËÐÐÏÞÖÆ£¨rlimit£©£¬Ôò°´ÕÕÔËÐÐÏÞÖÆÉèÖã©£¬Ê¹ÓÃmprotect()ÉèÖÃÆäÖеÚһҳΪ·Ç·ÃÎÊÇø¡£¸Ã8M¿Õ¼äµÄ¹¦ÄÜ·ÖÅäÈçÏÂͼ£º
µÍµØÖ·±»±£»¤µÄÒ³ÃæÓÃÀ´¼à²âÕ»Òç³ö¡£
¶ÔÓÚÓû§Ö¸¶¨µÄÕ»£¬ÔÚ°´ÕÕÖ¸Õë¶Ô½çºó£¬ÉèÖÃÏß³ÌÕ»¶¥£¬²¢¼ÆËã³öÕ»µ×£¬²»×ö±£»¤£¬ÕýÈ·ÐÔÓÉÓû§×Ô¼º±£Ö¤¡£
²»ÂÛÄÄÖÖ×éÖ¯·½Ê½£¬Ïß³ÌÃèÊö½á¹¹×ÜÊÇλÓÚÕ»¶¥½ôÁÚ¶ÑÕ»µÄλÖá£
4.Ïß³ÌidºÍ½ø³Ìid
ÿ¸öLinuxThreadsÏ̶߳¼Í¬Ê±¾ßÓÐÏß³ÌidºÍ½ø³Ìid£¬ÆäÖнø³Ìid¾ÍÊÇÄÚºËËùά»¤µÄ½ø³ÌºÅ£¬¶øÏß³ÌidÔòÓÉLinuxThreads·ÖÅäºÍά»¤¡£
__pthread_initial_threadµÄÏß³ÌidΪPTHREAD_THREADS_MAX£¬__pthread_manager_threadµÄÊÇ2*PTHREAD_THREADS_MAX+1£¬µÚÒ»¸öÓû§Ï̵߳ÄÏß³ÌidΪPTHREAD_THREADS_MAX+2£¬´ËºóµÚn¸öÓû§Ï̵߳ÄÏß³Ìid×ñÑÒÔϹ«Ê½£º
tid=n*PTHREAD_THREADS_MAX+n+1
ÕâÖÖ·ÖÅ䷽ʽ±£Ö¤Á˽ø³ÌÖÐËùÓеÄỊ̈߳¨°üÀ¨ÒѾÍ˳ö£©¶¼²»»áÓÐÏàͬµÄÏß³Ìid£¬¶øÏß³ÌidµÄÀàÐÍpthread_t¶¨ÒåΪÎÞ·ûºÅ³¤ÕûÐÍ£¨unsigned long int£©£¬Ò²±£Ö¤ÁËÓÐÀíÓɵÄÔËÐÐʱ¼äÄÚÏß³Ìid²»»áÖظ´¡£
´ÓÏß³Ìid²éÕÒÏß³ÌÊý¾Ý½á¹¹ÊÇÔÚpthread_handle()º¯ÊýÖÐÍê³ÉµÄ£¬Êµ¼ÊÉÏÖ»Êǽ«Ï̺߳Ű´PTHREAD_THREADS_MAXÈ¡Ä££¬µÃµ½µÄ¾ÍÊǸÃÏß³ÌÔÚ__pthread_handlesÖеÄË÷Òý¡£
5.Ï̵߳Ĵ´½¨
ÔÚpthread_create()Ïò¹ÜÀíÏ̷߳¢ËÍREQ_CREATEÇëÇóÖ®ºó£¬¹ÜÀíÏ̼߳´µ÷ÓÃpthread_handle_create()´´½¨ÐÂÏ̡߳£·ÖÅäÕ»¡¢ÉèÖÃthreadÊôÐÔºó£¬ÒÔpthread_start_thread()Ϊº¯ÊýÈë¿Úµ÷ÓÃ__clone()´´½¨²¢Æô¶¯ÐÂÏ̡߳£pthread_start_thread()¶ÁÈ¡×ÔÉíµÄ½ø³ÌidºÅ´æÈëÏß³ÌÃèÊö½á¹¹ÖУ¬²¢¸ù¾ÝÆäÖмǼµÄµ÷¶È·½·¨ÅäÖõ÷¶È¡£Ò»ÇÐ×¼±¸¾ÍÐ÷ºó£¬ÔÙµ÷ÓÃÕæÕýµÄÏß³ÌÖ´Ðк¯Êý£¬²¢Ôڴ˺¯Êý·µ»Øºóµ÷ÓÃpthread_exit()ÇåÀíÏÖ³¡¡£
6.LinuxThreadsµÄ²»×ã
ÓÉÓÚLinuxÄں˵ÄÏÞÖÆÒÔ¼°ÊµÏÖÄѶȵȵÈÔÒò£¬LinuxThreads²¢²»ÊÇÍêÈ«POSIX¼æÈݵģ¬ÔÚËüµÄ·¢ÐÐREADMEÖÐÓÐ˵Ã÷¡£
1)½ø³ÌidÎÊÌâ
Õâ¸ö²»×ãÊÇ×î¹Ø¼üµÄ²»×㣬ÒýÆðµÄÔÒòÇ£Éæµ½LinuxThreadsµÄ"Ò»¶ÔÒ»"Ä£ÐÍ¡£
LinuxÄں˲¢²»Ö§³ÖÕæÕýÒâÒåÉϵÄỊ̈߳¬LinuxThreadsÊÇÓÃÓëÆÕͨ½ø³Ì¾ßÓÐͬÑùÄں˵÷¶ÈÊÓͼµÄÇáÁ¿¼¶½ø³ÌÀ´ÊµÏÖÏß³ÌÖ§³ÖµÄ¡£ÕâЩÇáÁ¿¼¶½ø³ÌÓµÓжÀÁ¢µÄ½ø³Ìid£¬ÔÚ½ø³Ìµ÷¶È¡¢ÐźŴ¦Àí¡¢IOµÈ·½ÃæÏíÓÐÓëÆÕͨ½ø³ÌÒ»ÑùµÄÄÜÁ¦¡£ÔÚÔ´ÂëÔĶÁÕß¿´À´£¬¾ÍÊÇLinuxÄں˵Äclone()ûÓÐʵÏÖ¶ÔCLONE_PID²ÎÊýµÄÖ§³Ö¡£
ÔÚÄÚºËdo_fork()ÖжÔCLONE_PIDµÄ´¦ÀíÊÇÕâÑùµÄ£º
if (clone_flags & CLONE_PID) {
if (current->pid)
goto fork_out;
}
Õâ¶Î´úÂë±íÃ÷£¬Ä¿Ç°µÄLinuxÄں˽öÔÚpidΪ0µÄʱºòÈÏ¿ÉCLONE_PID²ÎÊý£¬Êµ¼ÊÉÏ£¬½öÔÚSMP³õʼ»¯£¬ÊÖ¹¤´´½¨½ø³ÌµÄʱºò²Å»áʹÓÃCLONE_PID²ÎÊý¡£
°´ÕÕPOSIX¶¨Ò壬ͬһ½ø³ÌµÄËùÓÐÏß³ÌÓ¦¸Ã¹²ÏíÒ»¸ö½ø³ÌidºÍ¸¸½ø³Ìid£¬ÕâÔÚÄ¿Ç°µÄ"Ò»¶ÔÒ»"Ä£ÐÍÏÂÊÇÎÞ·¨ÊµÏֵġ£
2)ÐźŴ¦ÀíÎÊÌâ
ÓÉÓÚÒì²½ÐźÅÊÇÄÚºËÒÔ½ø³ÌΪµ¥Î»·Ö·¢µÄ£¬¶øLinuxThreadsµÄÿ¸öÏ̶߳ÔÄÚºËÀ´Ëµ¶¼ÊÇÒ»¸ö½ø³Ì£¬ÇÒûÓÐʵÏÖ"Ïß³Ì×é"£¬Òò´Ë£¬Ä³Ð©ÓïÒå²»·ûºÏPOSIX±ê×¼£¬±ÈÈçûÓÐʵÏÖÏò½ø³ÌÖÐËùÓÐÏ̷߳¢ËÍÐźţ¬README¶Ô´Ë×÷ÁË˵Ã÷¡£
Èç¹ûºËÐIJ»ÌṩʵʱÐźţ¬LinuxThreads½«Ê¹ÓÃSIGUSR1ºÍSIGUSR2×÷ΪÄÚ²¿Ê¹ÓõÄrestartºÍcancelÐźţ¬ÕâÑùÓ¦ÓóÌÐò¾Í²»ÄÜʹÓÃÕâÁ½¸öÔ±¾ÎªÓû§±£ÁôµÄÐźÅÁË¡£ÔÚLinux kernel 2.1.60ÒÔºóµÄ°æ±¾¶¼Ö§³ÖÀ©Õ¹µÄʵʱÐźţ¨´Ó_SIGRTMINµ½_SIGRTMAX£©£¬Òò´Ë²»´æÔÚÕâ¸öÎÊÌâ¡£
ijЩÐźŵÄȱʡ¶¯×÷ÄÑÒÔÔÚÏÖÐÐÌåϵÉÏʵÏÖ£¬±ÈÈçSIGSTOPºÍSIGCONT£¬LinuxThreadsÖ»Äܽ«Ò»¸öÏ̹߳ÒÆ𣬶øÎÞ·¨¹ÒÆðÕû¸ö½ø³Ì¡£
3)Ïß³Ì×ÜÊýÎÊÌâ
LinuxThreads½«Ã¿¸ö½ø³ÌµÄÏß³Ì×î´óÊýÄ¿¶¨ÒåΪ1024£¬µ«Êµ¼ÊÉÏÕâ¸öÊýÖµ»¹Êܵ½Õû¸öϵͳµÄ×ܽø³ÌÊýÏÞÖÆ£¬ÕâÓÖÊÇÓÉÓÚÏß³ÌÆäʵÊǺËÐĽø³Ì¡£
ÔÚkernel 2.4.xÖУ¬²ÉÓÃÒ»Ì×ȫеÄ×ܽø³ÌÊý¼ÆËã·½·¨£¬Ê¹µÃ×ܽø³ÌÊý»ù±¾ÉϽöÊÜÏÞÓÚÎïÀíÄÚ´æµÄ´óС£¬¼ÆË㹫ʽÔÚkernel/fork.cµÄfork_init()º¯ÊýÖУº
max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8
ÔÚi386ÉÏ£¬THREAD_SIZE=2*PAGE_SIZE£¬PAGE_SIZE=2^12£¨4KB£©£¬mempages=ÎïÀíÄÚ´æ´óС/PAGE_SIZE£¬¶ÔÓÚ256MµÄÄÚ´æµÄ»úÆ÷£¬mempages=256*2^20/2^12=256*2^8£¬´Ëʱ×î´óÏß³ÌÊýΪ4096¡£
µ«ÎªÁ˱£Ö¤Ã¿¸öÓû§£¨³ýÁËroot£©µÄ½ø³Ì×ÜÊý²»ÖÁÓÚÕ¼ÓÃÒ»°ëÒÔÉÏÎïÀíÄڴ棬fork_init()ÖмÌÐøÖ¸¶¨£º
init_task.rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
init_task.rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
ÕâЩ½ø³ÌÊýÄ¿µÄ¼ì²é¶¼ÔÚdo_fork()ÖнøÐУ¬Òò´Ë£¬¶ÔÓÚLinuxThreadsÀ´Ëµ£¬Ïß³Ì×ÜÊýͬʱÊÜÕâÈý¸öÒòËصÄÏÞÖÆ¡£
4)¹ÜÀíÏß³ÌÎÊÌâ
¹ÜÀíÏß³ÌÈÝÒ׳ÉΪƿ¾±£¬ÕâÊÇÕâÖֽṹµÄͨ²¡£»Í¬Ê±£¬¹ÜÀíÏß³ÌÓÖ¸ºÔðÓû§Ï̵߳ÄÇåÀí¹¤×÷£¬Òò´Ë£¬¾¡¹Ü¹ÜÀíÏß³ÌÒѾÆÁ±ÎÁ˴󲿷ֵÄÐźţ¬µ«Ò»µ©¹ÜÀíÏß³ÌËÀÍö£¬Óû§Ï߳̾Ͳ»µÃ²»ÊÖ¹¤ÇåÀíÁË£¬¶øÇÒÓû§Ï̲߳¢²»ÖªµÀ¹ÜÀíÏ̵߳Ä״̬£¬Ö®ºóµÄÏ̴߳´½¨µÈÇëÇó½«ÎÞÈË´¦Àí¡£
5)ͬ²½ÎÊÌâ
LinuxThreadsÖеÄÏß³Ìͬ²½ºÜ´ó³Ì¶ÈÉÏÊǽ¨Á¢ÔÚÐźŻù´¡Éϵģ¬ÕâÖÖͨ¹ýÄں˸´ÔÓµÄÐźŴ¦Àí»úÖƵÄͬ²½·½Ê½£¬Ð§ÂÊÒ»Ö±ÊǸöÎÊÌâ¡£
6£©ÆäËûPOSIX¼æÈÝÐÔÎÊÌâ
LinuxÖкܶàϵͳµ÷Ó㬰´ÕÕÓïÒ嶼ÊÇÓë½ø³ÌÏà¹ØµÄ£¬±ÈÈçnice¡¢setuid¡¢setrlimitµÈ£¬ÔÚÄ¿Ç°µÄLinuxThreadsÖУ¬ÕâЩµ÷Óö¼½ö½öÓ°Ïìµ÷ÓÃÕßÏ̡߳£
7£©ÊµÊ±ÐÔÎÊÌâ
Ï̵߳ÄÒýÈëÓÐÒ»¶¨µÄʵʱÐÔ¿¼ÂÇ£¬µ«LinuxThreadsÔÝʱ²»Ö§³Ö£¬±ÈÈçµ÷¶ÈÑ¡ÏĿǰ»¹Ã»ÓÐʵÏÖ¡£²»½öLinuxThreadsÈç´Ë£¬±ê×¼µÄLinuxÔÚʵʱÐÔÉÏ¿¼ÂǶ¼ºÜÉÙ¡£
--------------------------------------------------------------------------------
»ØÒ³Ê×
ËÄ.ÆäËûµÄÏß³ÌʵÏÖ»úÖÆ
LinuxThreadsµÄÎÊÌ⣬ÌرðÊǼæÈÝÐÔÉϵÄÎÊÌ⣬ÑÏÖØ×è°ÁËLinuxÉϵĿçƽ̨ӦÓã¨ÈçApache£©²ÉÓöàÏß³ÌÉè¼Æ£¬´Ó¶øʹµÃLinuxÉϵÄÏß³ÌÓ¦ÓÃÒ»Ö±±£³ÖÔڱȽϵ͵Äˮƽ¡£ÔÚLinuxÉçÇøÖУ¬ÒѾÓкܶàÈËÔÚΪ¸Ä½øÏß³ÌÐÔÄܶøŬÁ¦£¬ÆäÖмȰüÀ¨Óû§¼¶Ï߳̿⣬Ҳ°üÀ¨ºËÐļ¶ºÍÓû§¼¶ÅäºÏ¸Ä½øµÄÏ߳̿⡣Ŀǰ×îΪÈË¿´ºÃµÄÓÐÁ½¸öÏîÄ¿£¬Ò»¸öÊÇRedHat¹«Ë¾Ç£Í·Ñз¢µÄNPTL£¨Native Posix Thread Library£©£¬ÁíÒ»¸öÔòÊÇIBMͶ×Ê¿ª·¢µÄNGPT£¨Next Generation Posix Threading£©£¬¶þÕ߶¼ÊÇΧÈÆÍêÈ«¼æÈÝPOSIX 1003.1c£¬Í¬Ê±ÔÚºËÄںͺËÍâ×ö¹¤×÷ÒÔ¶øʵÏÖ¶à¶Ô¶àÏß³ÌÄ£ÐÍ¡£ÕâÁ½ÖÖÄ£ÐͶ¼ÔÚÒ»¶¨³Ì¶ÈÉÏÃÖ²¹ÁËLinuxThreadsµÄȱµã£¬ÇÒ¶¼ÊÇÖØÆð¯ÔîÈ«ÐÂÉè¼ÆµÄ¡£
1.NPTL
NPTLµÄÉè¼ÆÄ¿±ê¹éÄɿɹéÄÉΪÒÔϼ¸µã£º
POSIX¼æÈÝÐÔ
SMP½á¹¹µÄÀûÓÃ
µÍÆô¶¯¿ªÏú
µÍÁ´½Ó¿ªÏú£¨¼´²»Ê¹ÓÃÏ̵߳ijÌÐò²»Ó¦µ±ÊÜÏ߳̿âµÄÓ°Ï죩
ÓëLinuxThreadsÓ¦ÓõĶþ½øÖƼæÈÝÐÔ
ÈíÓ²¼þµÄ¿ÉÀ©Õ¹ÄÜÁ¦
¶àÌåϵ½á¹¹Ö§³Ö
NUMAÖ§³Ö
ÓëC++¼¯³É
ÔÚ¼¼ÊõʵÏÖÉÏ£¬NPTLÈÔÈ»²ÉÓÃ1:1µÄÏß³ÌÄ£ÐÍ£¬²¢ÅäºÏglibcºÍ×îеÄLinux Kernel2.5.x¿ª·¢°æÔÚÐźŴ¦Àí¡¢Ïß³Ìͬ²½¡¢´æ´¢¹ÜÀíµÈ¶à·½Ãæ½øÐÐÁËÓÅ»¯¡£ºÍLinuxThreads²»Í¬£¬NPTLûÓÐʹÓùÜÀíỊ̈߳¬ºËÐÄÏ̵߳ĹÜÀíÖ±½Ó·ÅÔÚºËÄÚ½øÐУ¬ÕâÒ²´øÁËÐÔÄܵÄÓÅ»¯¡£
Ö÷ÒªÊÇÒòΪºËÐĵÄÎÊÌ⣬NPTLÈÔÈ»²»ÊÇ100%POSIX¼æÈݵģ¬µ«¾ÍÐÔÄܶøÑÔÏà¶ÔLinuxThreadsÒѾÓкܴó³Ì¶ÈÉϵĸĽøÁË¡£
2.NGPT
IBMµÄ¿ª·ÅÔ´ÂëÏîÄ¿NGPTÔÚ2003Äê1ÔÂ10ÈÕÍƳöÁËÎȶ¨µÄ2.2.0°æ£¬µ«Ïà¹ØµÄÎĵµ¹¤×÷»¹²îºÜ¶à¡£¾ÍÄ¿Ç°ËùÖª£¬NGPTÊÇ»ùÓÚGNU Pth£¨GNU Portable Threads£©ÏîÄ¿¶øʵÏÖµÄM:NÄ£ÐÍ£¬¶øGNU PthÊÇÒ»¸ö¾µäµÄÓû§¼¶Ï߳̿âʵÏÖ¡£
°´ÕÕ2003Äê3ÔÂNGPT¹Ù·½ÍøÕ¾ÉϵÄ֪ͨ£¬NGPT¿¼Âǵ½NPTLÈÕÒæ¹ã·ºµØΪÈËËù½ÓÊÜ£¬Îª±ÜÃⲻͬµÄÏ߳̿â°æ±¾ÒýÆðµÄ»ìÂÒ£¬½ñºó½«²»ÔÙ½øÐнøÒ»²½¿ª·¢£¬¶ø½ñ½øÐÐÖ§³ÖÐÔµÄά»¤¹¤×÷¡£Ò²¾ÍÊÇ˵£¬NGPTÒѾ·ÅÆúÓëNPTL¾ºÕùÏÂÒ»´úLinux POSIXÏ߳̿â±ê×¼¡£
3.ÆäËû¸ßЧÏ̻߳úÖÆ
´Ë´¦²»Äܲ»Ìáµ½Scheduler Activations¡£Õâ¸ö1991ÄêÔÚACMÉÏ·¢±íµÄ¶àÏß³ÌÄں˽ṹӰÏìÁ˺ܶà¶àÏß³ÌÄں˵ÄÉè¼Æ£¬ÆäÖаüÀ¨Mach3.0¡¢NetBSDºÍÉÌÒµ°æ±¾Digital Unix£¨ÏÖÔÚ½ÐCompaq True64 Unix£©¡£ËüµÄʵÖÊÊÇÔÚʹÓÃÓû§¼¶Ï̵߳÷¶ÈµÄͬʱ£¬¾¡¿ÉÄܵؼõÉÙÓû§¼¶¶ÔºËÐĵÄϵͳµ÷ÓÃÇëÇ󣬶øºóÕßÍùÍùÊÇÔËÐпªÏúµÄÖØÒªÀ´Ô´¡£²ÉÓÃÕâÖֽṹµÄÏ̻߳úÖÆ£¬Êµ¼ÊÉÏÊǽáºÏÁËÓû§¼¶Ï̵߳ÄÁé»î¸ßЧºÍºËÐļ¶Ï̵߳ÄʵÓÃÐÔ£¬Òò´Ë£¬°üÀ¨Linux¡¢FreeBSDÔÚÄڵĶà¸ö¿ª·ÅÔ´Âë²Ù×÷ϵͳÉè¼ÆÉçÇø¶¼ÔÚ½øÐÐÏà¹ØÑо¿£¬Á¦Í¼ÔÚ±¾ÏµÍ³ÖÐʵÏÖScheduler Activations¡£
²Î¿¼×ÊÁÏ
[Linus Torvalds£¬2002] LinuxÄÚºËÔ´Âëv2.4.20
[GNU£¬2002] GlibcÔ´Âëv2.2.2£¨ÄÚº¬LinuxThreads v0.9£©
[Thomas E. Terrill£¬1997] An Introduction to Threads Using The LinuxThreads Interface
[Ulrich Drepper£¬Ingo Molnar£¬2003] The Native POSIX Thread Library for Linux
http://www.ibm.com/developerworks/oss/pthreads/£¬NGPT¹Ù·½ÍøÕ¾
[Ralf S. Engelschall£¬2000] Portable Multithreading
[Thomas E. Anderson, Brian N. Bershad, Edward D. Lazowska, Henry M. Levy£¬1992] Scheduler Activations: Effective Kernel Support for the User-Level Management of Parallelism
[pcjockey@21cn.com] LinuxÏ̳߳õ̽
¹ØÓÚ×÷Õß
ÑîɳÖÞ£¬Ä¿Ç°ÔÚ¹ú·À¿Æ¼¼´óѧ¼ÆËã»úѧԺ¹¥¶ÁÈí¼þ·½Ïò²©Ê¿Ñ§Î»¡£
lxxwyy ÓÚ 2011-06-11 22:28:39·¢±í:
xuexi
hml1006 ÓÚ 2011-04-29 17:36:38·¢±í:
²»´í
txgc_wm ÓÚ 2011-04-15 18:54:02·¢±í:
5# risechen
²»ÊÇÎҵģ¬ÊDZðÈËдµÄ£¬ÎÒÖ»ÊÇÕª³£¬ºóÃæÓÐ×÷Õߵı¸×¢£¡
risechen ÓÚ 2011-04-14 23:00:37·¢±í:
°Ý¶ÁÄãµÄÎÄÕ£¬¾õµÃºÜ²»´í
hml1006 ÓÚ 2011-04-08 09:33:12·¢±í:
²»´íŶ
hml1006 ÓÚ 2011-04-08 09:32:56·¢±í:
²»´íŶ
sutovs ÓÚ 2011-04-08 08:40:10·¢±í:
·¹ý