ÉîÈëÃ÷È·LinuxÖеÄÀú³ÌÓÅÏȼ¶
ÉîÈëÃ÷È·LinuxÖеÄÀú³ÌÓÅÏȼ¶£¬ÐèÒªÏêϸ´úÂëʾÀý
ÔÚLinuxϵͳÖУ¬Àú³ÌµÄÓÅÏȼ¶ÊǺÜÊÇÖ÷ÒªµÄÒ»¸ö¿´·¨¡£Í¨¹ýºÏÀíÉèÖÃÀú³ÌµÄÓÅÏȼ¶£¬¿ÉÒÔÓÐÓõؿØÖÆϵͳ×ÊÔ´µÄ·ÖÅÉ£¬Ìá¸ßϵͳµÄÐÔÄÜÌåÏÖ¡£±¾ÎĽ«ÉîÈë̽ÌÖLinuxÖеÄÀú³ÌÓÅÏȼ¶¿´·¨£¬²¢Í¨¹ýÏêϸµÄ´úÂëʾÀý¾ÙÐÐÑÝʾºÍʵ¼ù¡£
Ò»¡¢LinuxÖеÄÀú³ÌÓÅÏȼ¶¿´·¨
ÔÚLinuxϵͳÖУ¬Àú³ÌµÄÓÅÏȼ¶¹æÄ£ÊÇ-20µ½19£¬ÆäÖÐ-20´ú±í×î¸ßÓÅÏȼ¶£¬19´ú±í×îµÍÓÅÏȼ¶¡£Ä¬ÈÏÇéÐÎÏ£¬ËùÓÐÀú³ÌµÄÓÅÏȼ¶¾ùΪ0¡£Àú³ÌµÄÓÅÏȼ¶¾öÒéÁËÆäÔÚϵͳÖлñµÃCPUʱ¼äƬµÄ¾Þϸ£¬ÓÅÏȼ¶Ô½¸ßµÄÀú³ÌÏà¶ÔÀ´Ëµ»á»ñµÃ¸ü¶àµÄCPUʱ¼ä¡£
LinuxÄÚºËͨ¹ýµ÷ÀíËã·¨À´¾öÒéÄĸöÀú³Ì»ñµÃCPUʱ¼ä£¬²¢ÇÒ¿ÉÒÔƾ֤ϵͳ¸ºÔغÍÐÔÄÜÐèÇó¶¯Ì¬µØµ÷½âÀú³ÌµÄÓÅÏȼ¶¡£Àú³ÌµÄÓÅÏȼ¶¿ÉÒÔͨ¹ýniceÖµÀ´ÉèÖã¬niceÖµÔ½µÍ´ú±íÓÅÏȼ¶Ô½¸ß£¬·´Ö®ÒàÈ»¡£
¶þ¡¢Àú³ÌÓÅÏȼ¶µÄÏêϸÉèÖúͻñÈ¡
ÔÚLinuxϵͳÖУ¬¿ÉÒÔʹÓÃniceÏÂÁîÀ´ÉèÖÃÀú³ÌµÄÓÅÏȼ¶¡£ÀýÈ磬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁÀú³ÌµÄniceÖµÉèÖÃΪ-10£º
$ nice -n -10 ./my_program
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬Àú³Ìmy_programµÄÓÅÏȼ¶½«»áÌá¸ß£¬»ñµÃ¸ü¶àµÄCPUʱ¼ä¡£³ýÁËͨ¹ýniceÏÂÁîÍ⣬»¹¿ÉÒÔʹÓÃϵͳŲÓÃsched_setschedulerÀ´ÉèÖÃÀú³ÌµÄµ÷ÀíÕ½ÂÔºÍÓÅÏȼ¶¡£
ÔÚCÓïÑÔÖУ¬¿ÉÒÔʹÓÃsched_setschedulerϵͳŲÓÃÀ´ÉèÖÃÀú³ÌµÄµ÷Àí²ÎÊý£¬Ïêϸ´úÂëÈçÏÂËùʾ£º
#include <stdio.h> #include <sched.h> int main() { struct sched_param param; param.sched_priority = 10; // ÉèÖÃÀú³ÌµÄÓÅÏȼ¶Îª10 if(sched_setscheduler(0, SCHED_FIFO, ¶m) < 0) { perror("sched_setscheduler"); return -1; } while(1) { // Àú³ÌÖ´ÐеĴúÂë } return 0; }
µÇ¼ºó¸´ÖÆ
¸Ã³ÌÐòͨ¹ýŲÓÃsched_setschedulerº¯Êý½«Àú³ÌµÄµ÷ÀíÕ½ÂÔÉèÖÃΪSCHED_FIFO£¨ÏȽøÏȳö£©²¢½«ÓÅÏȼ¶ÉèÖÃΪ10¡£ÔÚÕâ¸öÀý×ÓÖУ¬³ÌÐò½«»áÒ»Ö±Ö´ÐÐÒ»¸öÎÞÏÞÑ»·£¬Ö±µ½±»ÊÖ¶¯ÖÕÖ¹¡£
ͨ¹ýÒÔÉÏ´úÂëʾÀý£¬ÎÒÃÇ¿ÉÒÔÏàʶÔõÑùÔÚCÓïÑÔÖÐÉèÖÃLinuxÀú³ÌµÄÓÅÏȼ¶£¬²¢ÇÒ½øÒ»²½Ã÷È·Àú³ÌÓÅÏȼ¶¶ÔϵͳÐÔÄܵÄÓ°Ïì¡£ºÏÀíÉèÖÃÀú³ÌµÄÓÅÏȼ¶¹ØÓÚÌá¸ßϵͳµÄÐÔÄܺÍÏìÓ¦ËÙÂÊÊǺÜÊÇÖ÷ÒªµÄ¡£
Èý¡¢×ܽá
ÔÚLinuxϵͳÖУ¬Àú³ÌµÄÓÅÏȼ¶ÊÇÒ»¸ö¿ÉÒÔͨ¹ýniceÖµÉèÖõÄÖ÷Òª¿´·¨£¬Ëü¾öÒéÁËÀú³ÌÔÚϵͳÖлñµÃCPUʱ¼äµÄ¼¸¶à¡£Í¨¹ýÉèÖúÏÊʵÄÓÅÏȼ¶£¬¿ÉÒÔÓÐÓõؿØÖÆϵͳ×ÊÔ´µÄ·ÖÅÉ£¬Ìá¸ßϵͳµÄÐÔÄÜÌåÏÖ¡£
ͨ¹ý±¾ÎĵÄÏÈÈݺʹúÂëʾÀý£¬Ï£Íû¶ÁÕßÄܹ»ÉîÈëÃ÷È·LinuxÖеÄÀú³ÌÓÅÏȼ¶£¬²¢ÇÒÄܹ»Æ¾Ö¤ÏÖʵÐèÇóÎÞаµØÉèÖÃÀú³ÌµÄÓÅÏȼ¶£¬ÒÔµÖ´ï×î¼ÑµÄϵͳÐÔÄܺÍÏìÓ¦ËÙÂÊ¡£
²Î¿¼×ÊÁÏ£º
Linux man pages
Linux Advanced Programming by Mark Mitchell, Jeffrey Oldham, and Alex Samuel
ÒÔÉϾÍÊÇÉîÈëÃ÷È·LinuxÖеÄÀú³ÌÓÅÏȼ¶µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡