LinuxÏÂÏ̵߳Ĵ´½¨
×÷Õß:Hoyt Email:hoytluo@21cn.comÔÎĵØÖ· http://wiki.chinaunix.net/index.php/Zhuan_c7
Ç°ÑÔ: LinuxÏÂÏ̵߳Ĵ´½¨ºÍ»ù±¾µÄʹÓÃ
Ï̵߳Äͬ²½ºÍ»¥³â
Ï̵߳ÄÌõ¼þ±äÁ¿
ÏȽéÉÜʲôÊÇÏß³Ì.ÎÒÃDZàдµÄ³ÌÐò´ó¶àÊý¿ÉÒÔ¿´³ÉÊǵ¥Ï̵߳Ä.¾ÍÊdzÌÐòÊÇ°´ÕÕÒ»¶¨µÄ ˳ÐòÀ´Ö´ÐÐ.Èç¹ûÎÒÃÇʹÓÃÏ̵߳Ļ°,³ÌÐò¾Í»áÔÚÎÒÃÇ´´½¨Ï߳ɵĵط½·Ö²æ,±ä³ÉÁ½¸ö"³Ì Ðò"ÔÚÖ´ÐÐ.´ÖÂԵĿ´À´ºÃÏóºÍ×Ó½ø³Ì²î²»¶àµÄ,Æäʵ²»È».×Ó½ø³ÌÊÇͨ¹ý¿½±´¸¸½ø³ÌµÄµØ Ö·¿Õ¼äÀ´Ö´ÐеÄ.¶øÏß³ÌÊÇͨ¹ý¹²Ïí³ÌÐò´úÂëÀ´Ö´ÐеÄ,½²µÄͨË×Ò»µã¾ÍÊÇÏ̵߳ÄÏàͬµÄ ´úÂë»á±»Ö´Ðм¸´Î.ʹÓÃÏ̵߳ĺô¦ÊÇ¿ÉÒÔ½ÚÊ¡×ÊÔ´,ÓÉÓÚÏß³ÌÊÇͨ¹ý¹²Ïí´úÂëµÄ,ËùÒÔû Óнø³Ìµ÷¶ÈÄÇô¸´ÔÓ.
1.Ï̵߳Ĵ´½¨ºÍʹÓÃ
Ï̵߳Ĵ´½¨ÊÇÓÃÏÂÃæµÄ¼¸¸öº¯ÊýÀ´ÊµÏÖµÄ.
#include <pthread.h>
int pthread_create(pthread_t *thread,pthread_attr_t *attr,
void *(*start_routine)(void *),void *arg);
void pthread_exit(void *retval);
int pthread_join(pthread *thread,void **thread_return);
pthread_create´´½¨Ò»¸öÏß³Ì,threadÊÇÓÃÀ´±íÃ÷´´½¨Ï̵߳ÄID,attrÖ¸³öÏ̴߳´½¨Ê±ºò µÄÊôÐÔ,ÎÒÃÇÓÃNULLÀ´±íÃ÷ʹÓÃȱʡÊôÐÔ.start_routineº¯ÊýÖ¸ÕëÊÇÏ̴߳´½¨³É¹¦ºó¿ªÊ¼ Ö´Ðеĺ¯Êý,argÊÇÕâ¸öº¯ÊýµÄΨһһ¸ö²ÎÊý.±íÃ÷´«µÝ¸østart_routineµÄ²ÎÊý. pthrea d_exitº¯ÊýºÍexitº¯ÊýÀàËÆÓÃÀ´Í˳öÏß³Ì.Õâ¸öº¯Êý½áÊøÏß³Ì,Êͷź¯ÊýµÄ×ÊÔ´,²¢ÔÚ×îºó ×èÈû,Ö±µ½ÆäËûÏß³ÌʹÓÃpthread_joinº¯ÊýµÈ´ýËü.È»ºó½«*retvalµÄÖµ´«µÝ¸ø**thread_ return.ÓÉÓÚÕâ¸öº¯ÊýÊÍ·ÅËùÒԵĺ¯Êý×ÊÔ´,ËùÒÔretval²»Äܹ»Ö¸Ïòº¯ÊýµÄ¾Ö²¿±äÁ¿. pt hread_joinºÍwaitµ÷ÓÃÒ»ÑùÓÃÀ´µÈ´ýÖ¸¶¨µÄÏß³Ì.×¢ÒâÓÉÓÚÏ̲߳»Í¬ÓÚ½ø³Ì.ËùÒÔÏß³ÌµÄ Í˳öÒ»¶¨ÒªÓÃpthread_exit¶ø²»ÊÇʹÓÃexit.·ñÔòÕû¸ö³ÌÐò»áÍ˳öµÄ. ÏÂÃæÎÒÃÇʹÓÃÒ»¸ö ʵÀýÀ´½âÊÍÒ»ÏÂʹÓ÷½·¨.ÔÚʵ¼ùÖÐ,ÎÒÃǾ³£Òª±¸·ÝһЩÎļþ.ÏÂÃæÕâ¸ö³ÌÐò¿ÉÒÔʵÏÖµ± ǰĿ¼ÏµÄËùÓÐÎļþ±¸·Ý.±¸·ÝºóµÄºó׺ÃûΪbak
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h&
Ïà¹ØÎĵµ£º
LinuxÏÂC++¿ª·¢¹¤¾ß½éÉÜ
¡¡¡¡¸ÅÊö
¡¡¡¡¾ÍC£«£«¿ª·¢¹¤¾ß¶øÑÔ£¬ÓëWindowsÏÂ΢Èí£¨VC£¬ VS2005µÈ£©Ò»Í³ÌìÏÂÏà±È£¬Linux/UnixÏÂC£«£«¿ª·¢£¬¿ÉνÎ廨°ËÃÅ£¬¸÷ʽ¸÷Ñù¡£Emacs, vi, eclipse, anjuta£¬kdevelopµÈ²ã³ö²»Çî¡£
¡¡¡¡WindowsÏ£¬¿ª·¢¹¤¾ß¶àÒÔ¼¯³É¿ª·¢»·¾³IDEµÄÐÎʽչÏÖ¸ø×îÖÕÓû§¡£ÀýÈ磬VS2005¼¯³ ......
1. Sockets ½éÉÜ
2. ÓòºÍµØÖ··ÖÀà
3. µØַת»»º¯Êý
4. SocketÀàÐÍÒÔ¼°¸÷ÖÖÐÒé
5. ½«µØÖ·ºÍSocketÏà°ó¶¨
6. ÃæÏòÎÞÁ¬½ÓµÄÐÒé
7. ÃæÏòÁ¬½ÓµÄÐÒé
8. ·þÎñÆ÷¶ËµÄÃæÏòÁ¬½ÓÐÒé
9. Ö÷»úºÍÍøÂçÃû²éѯ
---------------------------------------------
10. ÔÚSocketÉÏʹÓñê×¼I/O
11. ²¢·¢¿Í»§ºÍ·þÎñÆ÷
12. Sock ......
select()µÄ»úÖÆÖÐÌṩһfd_setµÄÊý¾Ý½á¹¹£¬Êµ¼ÊÉÏÊÇÒ»longÀàÐ͵ÄÊý×飬 ÿһ¸öÊý×éÔªËض¼ÄÜÓëÒ»´ò¿ªµÄÎļþ¾ä±ú£¨²»¹ÜÊÇSocket¾ä±ú,»¹ÊÇÆäËû Îļþ»òÃüÃû¹ÜµÀ»òÉ豸¾ä±ú£©½¨Á¢ÁªÏµ£¬½¨Á¢ÁªÏµµÄ¹¤×÷ÓɳÌÐòÔ±Íê³É£¬ µ±µ÷ÓÃselect()ʱ£¬ÓÉÄں˸ù¾ÝIO״̬ÐÞ¸Äfd_setµÄÄÚÈÝ£¬ÓÉ´ËÀ´Í¨ÖªÖ´ ÐÐÁËselect()µÄ½ø³ÌÄ ......
±¾½ÚºÍ´ó¼ÒÒ»ÆðѧϰһÏÂÔÚlinuxÏÂʹÓÃapache+svn+sslÅäÖð²È«°æ±¾¿ØÖÆƽ̨ÎÊÌ⣬¿´Íê±¾ÎÄÄã¿Ï¶¨Óв»ÉÙÊÕ»ñ£¬Ï£Íû±¾ÎÄÄ̻ܽáÄã¸ü¶à¶«Î÷¡£Ê×ÏȽéÉÜÒ»ÏÂlinuxÏÂapache+svn+sslÅäÖõĻ·¾³¡£
»·¾³£º
centos5.4_x64
apache2.2.14
subversion-1.4.2(µ£ÐÄ°ü¹ØÁªÐÔÎÊÌ⣬¾ÍûÓп¼ÂÇ×îа汾£©
°²×°¹ý³Ì£º
1£¬apache°²×°
#./co ......
ÔÚLinuxϱàдDaemon
ÔÚLinux£¨ÒÔRedhat Linux Enterprise Edition 5.3ΪÀý£©Ï£¬ÓÐʱÐèÒª±àдService¡£ServiceÒ²ÊdzÌÐò£¬Ò»°ãËæϵͳÆô¶¯Óû§²»¸ÉÔ¤¾Í²»Í˳öµÄ³ÌÐò£¬¿ÉÒÔ³ÆΪService¡£LinuxϵÄServiceÒ»°ã³ÆΪDaemon¡£
ÒÔÉÏÊǹãÒåµÄServiceµÄ¶¨Òå¡£LinuxϵķþÎñÒ»°ã·ÅÔÚ/etc/init.dÎļþ¼ÐÏ¡£ä¯ÀÀÒ»ÏÂÕ ......