µÚ31Õ£ºÄں˵÷¶È»úÖÆ£º
¸ÅÊö£ºÕâÒ»ÕÂÒª½éÉÜËÄÖÖÄں˵÷¶ÈµÄ»úÖÆ£¬Ô×Ó²Ù×÷£¬ÐźÅÁ¿£¬»¥³âËø£¬×ÔÐýËø£¬ÕâËÄÖÖ·½Ê½¶¼ÊǽøÐжԽøÐжÀռʽ·ÃÎÊ»òÕßÓÐÏÞÖÆ·ÃÎʵÄÇé¿öËù×¼±¸µÄ¡£±ÈÈ磺»ð³µÆ±²»ÄÜͬʱÂô³öÈ¥Á½ÕÅ¡£ÔÙ±ÈÈ磬½ÌÊÒÀïµÄ×ùλÊÇÓÐÏ޵ģ¬±ØÐë×ßÒ»¸öÈË£¬²ÅÄÜнøÀ´Ò»¸öÈË£¬ËÄÖÖ·½Ê½¸÷Óи÷µÄÌص㣬ijһЩÇé¿öÏÂÊÇ¿ÉÒÔͨÓõģ¬±ÈÈçÔÚ²»ÐèÒªÐÝÃߵij¡ºÏ£¬Ä³Ð©Çé¿öÊÇÒª¸ù¾Ý×ÔÉíµÄÌصã½øÐÐÓ¦Ó㬱ÈÈçÓÐÐÝÃߵij¡ºÏ£¬Ô×Ó²Ù×÷²»ÄܶÔÕû¸öÇý¶¯½øÐжÀռʽµÄ·ÃÎÊ£¬¶øÖ»ÄܶÔÇý¶¯µ±ÖеIJ¿·Ö×ÊÔ´½øÐжÀռʽµÄ·ÃÎÊ¡£Ô×Ó²Ù×÷²»ÊÇËùÓеÄCPU¶¼Ö§³ÖµÄ£¬ÐèÒªCPUÓÐÔ×ÓµÄÓ²¼þ¹¦ÄܲſÉÒÔ£¬ÎÒÃǵÄEXYNOS4412CPU¿ÉÒÔÖ§³Ö¡£
31-1-1£ºÔ×Ó²Ù×÷£º
¹ËÃû˼Ò壬¾ÍÊÇ»úÆ÷Âë²ãÃæÉϵÄ×îС²Ù×÷µ¥Î»£¬ÊÇÔÚ»ã±à²ãÃæÉÏʵÏֵġ£¿ÉÒÔͨ¹ýÕâÖÖ·½·¨À´¶Ô×ÊÔ´½øÐжÀռʽµÄÅжϡ£
31-1-2£ºÔ×Ó²Ù×÷Ïà¹ØµÄÍ·Îļþ£¬½á¹¹Ì壬º¯Êý£¬Í·Îļþ£º
1) Í·Îļþ£º#include <linux/atomic.h>
2) ½á¹¹Ì壺atomic_t ¶¨Òå½á¹¹ÌåÀàÐÍ£¬ÄÚ²¿Ö»ÓÐÒ»¸ö³ÉÔ± counter £ºÔ×ÓÖµ
3) ºê(º¯Êý)Ïà¹Ø£º
a. ATOMIC_INIT(i)
×÷Ó㺶ÔÔ×ÓÁ¿¸³³õÖµ£¬³õʼ»¯Ò»¸öÔ×ÓÁ¿
²ÎÊý£ºÔ×ÓÖµµÄ³õÖµ
·µ»ØÖµ£º·µ»Ø¸³ÖµºÅµÄÔ×ÓÖµ
b. atomic_dec_and_test(v)
×÷Ó㺶ÔÔ×ÓÖµ½øÐмõ1²Ù×÷Èç¹û½á¹ûΪ0£¬±íʾÕ棬Ôò·µ»Ø1£¬Èç¹û½á¹û²»ÎªÁ㣬±íʾ¼Ù£¬Ôò·µ»Ø0
²ÎÊý£ºÔ×ӽṹÌåÖ¸Õë
·µ»ØÖµ£ºÇë¿´×÷ÓÃ
c. atomic_inc_and_test(v)
×÷Ó㺶ÔÔ×ÓÖµ½øÐмÓ1²Ù×÷Èç¹û½á¹ûΪ0£¬±íʾÕ棬Ôò·µ»Ø1£¬Èç¹û½á¹û²»ÎªÁ㣬±íʾ¼Ù£¬Ôò·µ»Ø0
²ÎÊý£ºÔ×ӽṹÌåÖ¸Õë
·µ»ØÖµ£ºÇë¿´×÷ÓÃ
d. atomic_inc(v)»òÕßÊÇatomic_dec(v)
×÷Ó㺶ÔÔ×ÓÁ¿½øÐмÓ1»òÕßÊǼõ1²Ù×÷¶ø²»ÅжÏ
²ÎÊý£ºÔ×ӽṹÌåÖ¸Õë
e. atomic_read(v)
×÷Ó㺶ÁÈ¡Ô×ÓÁ¿µÄÖµ
²ÎÊý£ºÔ×ӽṹÌåÖ¸Õë
·µ»ØÖµ£ºµ±Ç°²éѯµ½µÄÔ×ÓÖµ
f. atomic_set(struct atomic_t *v,count)
×÷Ó㺽«Ô×ÓÖµÉèÖÃΪָ¶¨µÄÖµ
²ÎÊý1£ºÔ×ӽṹÌåÖ¸Õë
²ÎÊý2£ºÏëÒªÉ趨µÄÔ×ÓÖµ
31-1-3£º¾ßÌå´úÂ룺
²Î¿¼ÊÓƵºÍ´úÂë
31-2-1£º»¥³âÐźÅÁ¿£º
»¥³âÐźÅÁ¿ºÍÔ×Ó²Ù×÷ÀàËÆ£¬ÊǶԹ²Ïí×ÊÔ´½øÐжÀռʽ·ÃÎʵŦÄÜ£¬µ±Ò»¸öAPP»ñµÃÇý¶¯Ïà¹Ø×ÊÔ´ºó£¬±ðµÄAPPÊÔͼ·ÃÎÊÕâЩ¹²Ïí×ÊԴʱ£¬¾Í»á½øÈëÐÝÃß״̬£¬²»ÔÙ¼ÌÐøÍùÏÂÖ´ÐУ¬Ö±µ½µÚÒ»¸öAPPÊͷŵôÕâЩ×ÊÔ´²¢±»ÁíÍâµÄAPP»ñµÃºó£¬²ÅÄܹ»¼ÌÐøÍùÏÂÖ´ÐС£
31-2-2£º»¥³âÐźÅÁ¿Ê¹Óõ½µÄÍ·Îļþ£¬½á¹¹Ì壬º¯Êý£¬Í·Îļþ£º
1) Í·Îļþ£º#include <linux/mutex.h>
2) ½á¹¹Ì壺struct mutex{
¡£¡£¡£¡£¡£¡£ //³ÉÔ±²»ÐèҪĿǰÎÒÃÇÈ¥ÕÆÎÕ£¬ÏÈÒªÇóѧ»áÓ÷¨
}
3) º¯ÊýÏà¹Ø
a. mutex_init(mutex)
×÷Ó㺳õʼ»¯Ò»°Ñ»¥³âËø
²ÎÊý£º»¥³âËø½á¹¹ÌåÖ¸Õë
b. mutex_lock(lock)
×÷ÓãºÉêÇ뻥³âËø£¬µ±ÉêÇëµ½»¥³âËøÖ®ºó£¬ÕâÒ»ÐдúÂëÖ®ºóµ½ÊÍ·Å»¥³âËø֮ǰµÄ×ÊÔ´¾ÍÊôÓÚ¶ÀÕ¼·ÃÎÊ×ÊÔ´ÁË¡£
²ÎÊý£º»¥³âËø½á¹¹ÌåÖ¸Õë
c. mutex_unlock(x)
×÷ÓãºÊÍ·Å»¥³âËø£¬Êͷź󣬱ðµÄ½ø³Ì¾Í¿ÉÒÔÈ¥ÇÀÕ¼»òÕßÓÉϵͳ·ÖÅ以³âËøÁË
²ÎÊý£º»¥³âËø½á¹¹ÍËÖ¸Õë
d. static inline voidmutex_destroy(struct mutex *lock)
×÷ÓãºÏú»Ù»¥³âËø
²ÎÊý£º»¥³âËø½á¹¹ÌåÖ¸Õë
e. int __schedmutex_trylock(struct mutex *lock)
×÷Ó㺷Ç×èÈûµÄÉêÇëÒ»¸öÐźÅÁ¿£¬²»¹ÜÊÇ·ñÉêÇëµ½£¬¶¼¼ÌÐøÖ´ÐнÓÏÂÀ´µÄ´úÂ룬µ«ÊÇÒ»µ©ÉêÇëµ½ÁË£¬±ðµÄapp²ÉÓÃ×èÈû·½Ê½À´ÉêÇ뻥³âËø(mutex_lock)ʱͬÑù»á½øÈëÐÝÃß
²ÎÊý£º»¥³âËø½á¹¹ÌåÖ¸Õë
·µ»ØÖµ£ºÉêÇëµ½ÁË·µ»Ø1£¬Ã»ÓÐÉêÇëµ½·µ»Ø0
Ìرð×¢Ò⣺ÒòΪ»á²úÉúÐÝÃߣ¬²»ÄÜÔÚÖжϵ±ÖÐʹÓ㬵«ÊÇ¿ÉÒÔÔÚÖжϵײ¿µÄ¹¤×÷¶ÓÁÐÖÐʹÓ㬲»ÄÜÔÚ¶¨Ê±Æ÷ºÍÈÎÎñµ±ÖÐʹÓ᣻¥³âËøºÍ×ÔÐýËøµÄ×÷Óü¸ºõÍêÈ«Ò»Ö£¬Çø±ðÔÚÓÚÉêÇë²»µ½ËøµÄ½ø³Ì»á½øÈëÐÝÃß²¢ÊÍ·ÅCPU£¬¶ø×ÔÐýËøÉêÇë²»µ½ËøµÄ½ø³Ì»áËÀµÈ²»»áÊÍ·ÅCPU
31-2-3£º¾ßÌå´úÂ룺
Çë²Î¿¼ÊÓƵºÍ´úÂë
31-3-1£ºÐźÅÁ¿£º
ÐźÅÁ¿ÊǶԹ²Ïí×ÊԴʵÏÖÓÐÏÞ·ÃÎʵÄÒ»ÖÖ´ëÊ©£¬»¥³âËøÖ»ÔÊÐíÒ»¸ö½ø³Ì¶Ô×ÊÔ´½øÐжÀռʽ·ÃÎÊ£¬¶øÐźÅÁ¿¿ÉÒÔn¸ö½ø³Ì¶Ô¹²Ïí×ÊÔ´½øÐзÃÎÊ£¬n¿ÉÒÔÓÉ´úÂ뿪·¢ÈËÔ±¿ØÖÆ¡£ÐźÅÁ¿ÔÚ±»½ø³Ì»ñµÃºó»á×ÔÐмõÉÙ£¬Ò²¿ÉÒÔÓÉ¿ª·¢ÈËԱȥÔö¼Ó£¬»¥³âËøÆðʼ¾ÍÊÇÖ»ÓÐÒ»¸öÐźÅÖµµÄÐźÅÁ¿£¬½Ð×ö¶þÖµÐźÅÁ¿£¬Ö»ÓÐ0£¬ºÍ1Á½¸öÖµ£¬½ø³ÌÔÚÉêÇëÐźÅÁ¿µÄʱºò£¬Èç¹ûÐźÅÁ¿ÒѾûÓÐÁË£¬ÄÇôÊÔͼ·ÃÎÊÐźÅÁ¿µÄ½ø³Ì¾Í»á½øÈëÐÝÃß״̬£¬µÈ´ýÐźÅÁ¿±»ÊͷŲ¢»ñµÃÖ®ºó²ÅÄÜ»½ÐѲ¢¼ÌÐø¹¤×÷¡£Òò´Ë£¬ÐźÅÁ¿Ò²²»ÄÜÓÃÔÚÖжϵ±ÖÐʹÓÃ
Ìرð×¢Ò⣺ÐźÅÁ¿ÊÇÕë¶Ô¶à¸ö½ø³Ì¶àͳһ×ÊÔ´µÄ¹²Ïíʽ·ÃÎʶø´´½¨µÄ»úÖÆ£¬¿ÉÒÔn¸ö½ø³Ì·ÃÎÊͬһ¸ö×ÊÔ´£¬n¿ÉÒÔÓÉ¿ª·¢ÈËÔ±ÉèÖã¬ÉêÇë²»µ½ÐźÅÁ¿µÄ½ø³Ì»á½øÈëÐÝÃߵȴý״̬£¬ÒòΪÐźÅÁ¿»á²úÉúÐÝÃߣ¬ËùÒÔ²»ÄÜÔÚÖжϵ±ÖÐʹÓ㬿ÉÒÔÔÚ¹¤×÷¶ÓÁÐÖÐʹÓ㬲»ÄÜÔÚ¶¨Ê±Æ÷ºÍÈÎÎñµ±ÖÐʹÓÃ
31-3-2£ºÐźÅÁ¿ËùʹÓõ½µÄÍ·Îļþ£¬½á¹¹Ì壬º¯Êý£º
1) Í·Îļþ£º#include <linux/semaphore.h>
2) ½á¹¹Ì壺
struct semaphore
{
¡£¡£¡£¡£¡£¡£//³ÉÔ±ÔÝʱ²»ÐèҪȥÉ
}
3) static inline voidsema_init(struct semaphore *sem, int val)
×÷Ó㺳õʼ»¯Ò»¸öÐźÅÁ¿¡¢
²ÎÊý1£ºÐźÅÁ¿Ö¸Õë
²ÎÊý2£ºÐźÅÁ¿µÄ³õʼֵ
4) void down(struct semaphore*sem)
×÷ÓãºÉêÇëÐźÅÁ¿(ÉêÇëµ½ÁËÖ®ºó£¬»áÏûºÄÒ»¸öÐźÅÖµ)
²ÎÊý£ºÐźÅÁ¿½á¹¹ÌåÖ¸Õë
5) up(x)
×÷ÓãºÊÍ·ÅÐźÅÁ¿(ÈÃÐźÅÁ¿µÄÖµÔö¼Ó1)
²ÎÊý£ºÐźÅÁ¿½á¹¹ÌåÖ¸Õë
6) ²»ÓÃרÃÅÈ¥ÊÍ·ÅËü£¬µÈµ½½ø³Ì½áÊøÖ®ºó×Ô¶¯ÐźÅÁ¿×Ô¶¯É¾³ý
7) int down_trylock(structsemaphore *sem)
×÷Ó㺷Ç×èÈû·½Ê½ÉêÇëÐźÅÁ¿
²ÎÊý£ºÐźÅÁ¿½á¹¹ÌåÖ¸Õë
ÉêÇëµ½ÐźÅÁ¿£º·µ»Ø1
ûÓÐÉêÇëµ½£º·µ»Ø0
×¢Ò⣺Õâ¸öº¯Êý²»¹ÜÊÇ·ñÉêÇëµ½ÁËÐźÅÁ¿×ÔÉí¶¼²»»á²úÉú×èÈû£¬µ«ÊÇÈç¹ûÉêÇëµ½ÁË×îºóÒ»¸ö£¬±ðµÄ½ø³Ìµ÷ÓÃdown()º¯ÊýÔÙ´ÎÉêÇëÐźÅÁ¿µÄʱºòÒ²»á²úÉú×èÈû£¬Ó¦¸Ãͨ¹ý·µ»ØÖµÀ´ÅжϱàдÉêÇëµ½ºÍûÓÐÉêÇëµ½ÐźÅÁ¿µÄÇé¿ö
31-3-3£º¾ßÌå´úÂë
²Î¿¼ÊÓƵºÍ´úÂë
31-4-1£º×ÔÐýËø£º
×ÔÐýËøºÍ»¥³âËøµÄ¹¦ÄÜÊǷdz£½Ó½üµÄ£¬Çø±ðÔÚÓÚµ÷Óû¥³âËøµÄ½ø³ÌÈç¹ûÉêÇë²»µ½Ëø£¬»á½øÈëÐÝÃß״̬µÈ´ýËø±»ÊÍ·Å£¬¶øµ÷ÓÃ×ÔÐýËøµÄ½ø³ÌÈç¹ûÉêÇë²»µ½ËøÔò»á½øÈëËÀµÈ״̬£¬²»»áÐÝÃߣ¬ÀàËÆÓÚwhile(1)µÄËÀÑ»·£¬µÈ´ýËøÊÍ·Å¡£×ÔÐýËø±£»¤ÆðÀ´µÄ×ÊÔ´Öоø¶Ô²»ÄÜÐÝÃߣ¬·ñÔòÔÚ¼«¶ËÇé¿öÏ¿ÉÄÜ»á³öÏÖûÓÐCPUÈ¥»½ÐÑÐÝÃß½ø³ÌµÄÇé¿ö¡£Êµ¼ÊÉÏ£¬×ÔÐýËø¶Ô´úÂëµÄ±£»¤Ö»ÄÜÊǼ«¶ÌµÄʱ¼ä£¬ÒªÇó±»±£»¤µÄ´úÂëÔ½¶ÌÔ½ºÃ£¬Èç¹ûʱ¼äÉÔ³¤£¬¾Í»á³öÏÖÄں˱¨´í¡£×ÔÐýËø¿ÉÒÔÔÚÖжϵ±ÖÐʹÓã¬ÒòΪ²»»á²úÉúÐÝÃß
ÐèÒªÌرð×¢ÒâµÄ£º×ÔÐýËø±£»¤µÄ×ÊÔ´Öв»ÄÜÓÐÐÝÃߺ¯Êý£¬Èç¹û·ÇÒªÓÃudelay()ÕâÑùµÄ·ÇÐÝÃßÑÓʱº¯Êý£¬»¹²»ÈçÓû¥³âËø£¬×ÔÐýËø¿ÉÒÔÔÚÖжϵ±ÖÐʹÓã¬ÉêÇëÁËÖ®ºóÓ¦¸Ã¾¡¿ìÊÍ·Å£¬±£»¤µÄ´úÂ뾡Á¿¼ò¶Ì¡£ÒòΪ×ÔÐýËø±£»¤×ÊԴʱ¼ä¶ÌµÄÌص㣬²»ÄܶÔÕû¸öÇý¶¯×ÊÔ´½øÐб£»¤
31-4-2£º×ÔÐýËøËùʹÓõ½µÄÍ·Îļþ£¬½á¹¹Ì壬º¯Êý
1) Í·Îļþ #include <linux/spinlock.h>
2) ½á¹¹Ì壺ͨ¹ý spinlock_t ÀàÐÍÀ´¶¨Òå½á¹¹Ìå
3) spin_lock_init(_lock)
×÷Ó㺳õʼ»¯×ÔÐýËø
²ÎÊý£º×ÔÐýËø½á¹¹ÌåÖ¸Õë
4) static inline voidspin_lock(spinlock_t *lock)
×÷ÓãºÉêÇë×ÔÐýËø
²ÎÊý£º×ÔÐýËø½á¹¹ÌåÖ¸Õë
5) static inline voidspin_unlock(spinlock_t *lock)
×÷ÓãºÊÍ·Å×ÔÐýËø
²ÎÊý£º×ÔÐýËø½á¹¹ÌåÖ¸Õë
31-4-3£º¾ßÌå´úÂë
µÚ32Õ£ºµÈ´ý¶ÓÁÐÓëPOLLº¯Êý
32-1£ºµÈ´ý¶ÓÁÐÊÇʲô£¿ÓÐʲôÒâÒ壿
ÎÄ×ÖÃèÊö£º½ø³Ì¿ÉÒÔ±»ÔÚij´¦ÐÝÃßÖ®ºó¹ÒÔص½Ò»¸öÌáÇ°¶¨ÒåºÃµÄµÈ´ý¶ÓÁÐÁ´±íÖУ¬¹ÒÉÏÁ´±íÖ®ºó£¬½ø³Ì¾Í´¦ÓÚÐÝÃß״̬£¬Á´±íÖпÉÒÔ¹ÒÈë¶à¸ö½ø³Ì£¬µÈ´ýÐèÒª»½Ðѽø³ÌµÄʱºò£¬Í¨¹ýרÃŵĺ¯ÊýÈ¥»½Ðѵȴý¶ÓÁÐÁ´±íÍ·£¬¾Í¿ÉÒÔÈò»Í¬Ê±½øÈëÐÝÃß״̬µÄ¶à¸ö½ø³Ìͬʱ»½ÐѲ¢Í¬Ê±¿ªÊ¼¼ÌÐø¹¤×÷¡£
32-2£ºÓÃÆÕͨµÄ·½·¨À´ÍêÉÆread()º¯ÊýºÍÆäȱÏÝ
1) ÔÚfcntl.hÍ·Îļþµ±ÖÐÓÐÒ»¸ö O_NONBLOCKºê£¬Õâ¸öºêµÄ×÷ÓÃÊDz»×èÈûµÄ´ò¿ªÎļþ
2) Struct file *fd³ÉÔ±µÄfd->f_flags³ÉÔ±´ú±íµÄÊÇÓ¦Óò㴫µÝ½øÀ´µÄ²ÎÊý£¬Èç¹ûÓëÉÏO_NONBLOCK½á¹ûΪÕýÖµ£¬ËµÃ÷·Ç×èÈû·½Ê½´ò¿ªÎļþ£¬Èç¹û½á¹ûΪ0£¬ÔòΪ×èÈû·½Ê½´ò¿ªÎļþ
3) ³ÌÐòÉè¼Æ£ºÍ¨¹ý°´¼üÖжÏÀ´ÉèÖÃÊý¾ÝÊÇ·ñ±ä»¯£¬Èç¹ûÓа´¼ü°´Ï£¬±íʾÊý¾Ý·¢ÉúÁ˱仯£¬¿ÉÒÔ¶ÁÈ¡£¬Èç¹ûûÓа´¼ü°´Ï£¬´ú±íÊý¾ÝûÓз¢Éú±ä»¯£¬Ó¦¸Ã×èÈûס²»ÄܶÁÈ¡¡£
4) ´úÂëÇë²Î¿¼ÊÓƵ
5) ȱÏÝ£º×èÈû״̬ÏÂËÀµÈ£¬Õ¼ÓÃCPU
32-3£ºµÈ´ý¶ÓÁÐÍ·Îļþ
#include <linux/wait.h> #include<linux/sched.h>
32-4£ºµÈ´ý¶ÓÁнṹÌ壬º¯Êý£¬µÈµÈ
1) ½á¹¹Ì嶨Ò壺ͨ¹ýÀàÐÍwait_queue_head_t¶¨Òå½á¹¹Ì壬½á¹¹ÌåµÄÄÚÈÝÏȲ»ÓÃÈ¥É£¬ÏÈÕÆÎÕʹÓ÷½·¨
2) init_waitqueue_head(q)
×÷Ó㺳õʼ»¯Ò»¸öµÈ´ý¶ÓÁÐÍ·(½á¹¹Ìå)
²ÎÊý£ºµÈ´ý¶ÓÁнṹÌåÖ¸Õë
3) wait_event(wq, condition)
×÷ÓãºÐγÉÒ»¸öµÈ´ýʼþ£¬±¾ÖÊÉÏÊǽ«½ø³Ì¹Òµ½µÈ´ý¶ÓÁÐÁ´±íÖÐÈ¥£¬Õâ¸öºê¿ÉÒÔÓÃÔÚ½ø³ÌµÄÈÎÒâ´¦(ÖжϳýÍâ)£¬µ±³ÌÐòÖ´ÐеÄÕâÒ»ÐдúÂë¾Í»á±»¹Òµ½µÈ´ý¶ÓÁÐÖУ¬Í£Ö¹¼ÌÐøÍùÏÂÖ´ÐУ¬²¢ÇÒÊÍ·ÅCPU£¬½øÈëÐÝÃß״̬
²ÎÊý1£ºµÈ´ý¶ÓÁнṹÌ壬עÒ⣺²»ÊÇÖ¸Õ룬ÊǽṹÌå±äÁ¿
²ÎÊý2£º»½ÐÑÌõ¼þ£¬»½ÐÑÌõ¼þÔÚδ»½ÐѵÄʱºòÓ¦¸ÃÊÇ0£¬»½ÐÑ״̬֮ºóÓ¦¸ÃÖÃ1£¬Ö»ÓÐÌæ»»½«»½ÐÑÌõ¼þÖÃ1Ö®ºó£¬²ÅÄÜÈ¥»½Ðѵȴý¶ÓÁУ¬·ñÔò»½ÐÑÎÞЧ£¬conditionÊÇÒ»¸ö±äÁ¿£¬×¢Ò⣬Ӧ¸ÃÊÇÒ»¸öÈ«¾Ö±äÁ¿£¬¶øÇÒ´Ë´¦±ØÐëÊDZäÁ¿²»ÄÜÊdz£Á¿
4) wake_up(x)
×÷Ó㺻½ÐÑÒ»Õû¸öµÈ´ý¶ÓÁÐ
²ÎÊý£ºµÈ´ý¶ÓÁÐÍ·½á¹¹ÌåÖ¸Õë
×¢Ò⣺»½ÐÑ֮ǰӦ¸ÃÒª½²»½ÐÑÌõ¼þÖÃ1£¬·ñÔò»½ÐÑÎÞЧ
32-5£ºÊ¹Óõȴý¶ÓÁÐÀ´ÊµÏÖread()º¯ÊýµÄ×èÈû
²Î¿¼ÊÓƵºÍ´úÂë
32-6£ºpollº¯Êý½éÉÜ
ÂÖѯº¯Êý£ºpollº¯Êý¿ÉÒÔ¶Ô¶à¸öÃèÊö·û½øÐÐÂÖѯ£¬ÂÖѯµÄÄ¿±ê²»ÊǶÁÈ¡Êý¾Ý£¬¶øÊǶÁÈ¡ÃèÊö·ûÊÇ·ñÊý¾Ý¿É¶ÁµÄ±êʶ(¾ÍÊÇÅжÏÊÇ·ñ·¢ÉúÁ˱仯»òÕßÊý¾Ý¸üÐÂ),Èç¹ûÅжϱêʶ¿É¶Á£¬ÔÙ¾«È·È¥¶ÁÖ¸¶¨µÄÃèÊö·û£¬Èç¹ûÅжϲ»¿É¶Á£¬¶øÁ¢ÂíÂÖѯÏÂÒ»¸ö£¬ÕâÑù¿ÉÒÔ¼«´óµÄ½ÚÔ¼CPU×ÊÔ´¡£
¶ÔÓÚLINUXÇý¶¯¿ª·¢ÕßÀ´Ëµ£¬µ×²ãµÄPOLLº¯ÊýÐèÒª×Ô¼ºÈ¥ÊµÏÖ¹¦ÄÜ
ÎÒÃÇÒª×öµÄÊÂÇé
a. ÔÚÇý¶¯²ãʵÏÖPOLL¹¦ÄÜ£¬Ê²Ã´Ê±ºò±íʾ¿É¶Á£¬Ê²Ã´Ê±ºò±íʾ¿Éд
b. ÔÚÓ¦Óòãѧ»áʹÓÃPOLLº¯Êý£¬ÈçºÎÈ¥²éѯÃèÊö·û״̬ÊÇ·ñÓиıä
32-7£ºpollº¯ÊýÍ·ÎļþºÍº¯ÊýÔÐÍ
1) Ó¦Óò㣺#include <poll.h>
º¯ÊýÔÐÍ£ºint poll(structpollfd *fds, nfds_t nfds, int timeout)
×÷ÓãºÃèÊö·û״̬ÂÖѯ¹¦ÄÜ
²ÎÊý1£ºstructpollfd ÊÇÒ»¸öרÃÅÓÃÀ´±£´æ¶à¸öÃèÊö·ûµÄ½á¹¹Ìå
²ÎÊý2£ºÃèÊö·ûµÄÊýÁ¿
²ÎÊý3£º³¬Ê±ÉèÖÃ: -1£º×èÈû 0£º²»×èÈû ´óÓÚ0£º³¬Ê±ºÁÃëÊý
·µ»ØÖµ£º-1£º´ú±íº¯ÊýÖ´Ðгö´í£¬½á¹û²»¿ÉÓà 0£º³¬Ê±£¬Ã»ÓÐÃèÊö·û·¢Éú±ä»¯+1£º
´ú±íÓÐÃèÊö·û·¢ÉúÁ˱仯£¬È»ºó¾ÍҪȥÅжÏÊÇÄÄÒ»¸öÃèÊö·û·¢ÉúÁ˱仯
2) Çý¶¯²ã£º#include <linux/poll.h>
unsigned int (*poll) (struct file *, structpoll_table_struct *) //ÕâÊǺ¯ÊýÖ¸ÕëÔÐÍ
×÷ÓãºÅäºÏÓ¦Óòãpollº¯ÊýʵÏÖÂÖѯ¹¦ÄÜ£¬ÐèÒª¿ª·¢ÈËԱȥʵÏÖ
²ÎÊý1£ºÎļþ½á¹¹ÌåÖ¸Õë
²ÎÊý2£ºµÈ´ýÁÐ±í£¬µÈ´ýÁ´±íÊÇϵͳµ±Öеĵȴý¶ÓÁÐΪµ¥Î»×é³ÉµÄÁбí
·µ»ØÖµ£º·µ»ØµÄÊDzéѯ½á¹û£¬ÊÇ¿ª·¢ÈËÔ±Öص㿼ÂǵĶÔÏ󣬴˺¯ÊýÔÐ͵ķµ»ØÖµ¾ÍÊÇÓ¦ÓòãÓÃÀ´ÅжÏÃèÊö·ûÊÇ·ñ·¢Éú±ä»¯µÄÒÀ¾Ý
3) static inline voidpoll_wait(struct file * filp, wait_queue_head_t * wait_address,
poll_table *p)
×÷ÓãºÊµÏÖPOLL¹¦ÄܵĹؼüº¯Êý£¬´Ëº¯ÊýÒª½áºÏÒ»¸öµÈ´ý¶ÓÁÐÀ´Ê¹Óã¬Èç¹ûÓ¦ÓòãÉèÖÃΪ×èÈû£¬ÄÇôÕâ¸öº¯ÊýÓë¾Í»áÒ»Ö±¿¨×¡(½«½ø³Ì³¹µ×¹ÒÉϵȴý¶ÓÁÐ),Èç¹ûÓ¦ÓòãÉèÖÃΪ³¬Ê±,ÄÇô´Îº¯Êý»áÔÚ³¬Ê±Ê±¼äÄÚ½«½ø³Ì¹ÒÉϵȴý¶ÓÁУ¬³¬Ê±Ê±¼ä½áÊø½«½ø³Ì´ÓµÈ´ý¶ÓÁÐÉÏÈ¡Ï£¬Èç¹ûÓ¦ÓòãÉèÖÃΪ²»×èÈû£¬ÄÇô´Ëº¯Êý²»»á²úÉú×èÈû¡£
²ÎÊý1£ºÎļþ½á¹¹Ìå
²ÎÊý2£ºµÈ´ý¶ÓÁнṹÌåÖ¸Õë
²ÎÊý3£ºµÈ´ýÁÐ±í£¬Ö±½ÓÈ¡ÖµÇý¶¯²ãpollº¯ÊýµÄ²ÎÊý
4) Ïà¹ØµÄºê£ºÎļþÊÇ·ñ¿É¶Á¿ÉдµÄ±êʶ²»ÊÇËæÒⶨµÄ£¬ÄÚºËÌṩÁËרÃŵĺêÀ´±íʾÎļþÊÇ·ñ·¢ÉúÁ˿ɶÁ»òÕß¿ÉдµÄ±ä»¯£º
#define POLLIN 0x0001 //·¢ÉúÁ˿ɶÁʼþ
#define POLLOUT 0x0004 //·¢ÉúÁË¿Éдʼþ
#definePOLLRDNORM 0x0040 //Óë¿É¶ÁʼþÒ»Æð±íʾÕý³£¿É¶Áʼþ
#define POLLWRNORM 0x0100 //Óë¿ÉдʼþÒ»Æð±íʾÕý³£¿Éдʼþ
5) struct pollfd {
int fd; /* file descriptor ÎļþÃèÊö·û*/
short events; /* requested eventsÇëÇó·¢ÉúµÄʼþ£¬±ÈÈçÎÒÃÇÒª´ÓÎļþÖжÁÈ¡
Êý¾Ý£¬ÄÇôÕâ¸ö¾ÍÓ¦¸ÃÌîPOLLIN£¬±íʾÎÒÃÇÆÚ´ýµÄÊÇ¿ÉÒÔ´ÓÎļþÖжÁÊý¾ÝµÄ״̬ */
short revents; /* returnedeventsÕâ¸ö³ÉÔ±ÌîÈë0£¬µÈµ½POLLº¯ÊýÖ´ÐÐÍê±ÏÖ®ºó
£¬Õâ¸ö³ÉÔ±»á±»³ÌÐò¸³Öµ£¬Í¨¹ýÅжÏÕâ¸ö³ÉÔ±ÊÇ·ñÊÇÎÒÃÇÆÚÍûµÄ״̬£¬¾ÍÖªµÀÎļþÊÇ·ñ¿É¶Á»òÕß¿ÉдÁË */