尊龙凯时人生就是搏

½â¶ÁNginxµÄÇëÇó´¦ÀíģʽºÍÏ̳߳ص÷Àí»úÖƵĵײãʵÏÖÔ­Àí

½â¶ÁnginxµÄÇëÇó´¦ÀíģʽºÍÏ̳߳ص÷Àí»úÖƵĵײãʵÏÖÔ­Àí

Nginx×÷Ϊһ¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷ £¬ÆäÆæÒìµÄÇëÇó´¦ÀíģʽºÍÓÅÒìµÄÏ̳߳ص÷Àí»úÖÆ £¬Ê¹ÆäÄܹ»´¦Àí´ó×ڵIJ¢·¢ÇëÇó £¬°ü¹ÜϵͳµÄ¸ßÐÔÄܺÍÎȹÌÐÔ ¡£±¾ÎĽ«ÉîÈëÆÊÎöNginxÇëÇó´¦ÀíģʽºÍÏ̳߳ص÷Àí»úÖƵĵײãʵÏÖÔ­Àí £¬²¢¾ÙÐдúÂëʾÀýչʾ ¡£

Ò»¡¢NginxÇëÇó´¦Àíģʽ

NginxµÄÇëÇó´¦Àíģʽ½ÓÄɵÄÊǶà·IO¸´ÓÃÄ£×Ó £¬Ö÷Òª°üÀ¨ÒÔϼ¸¸ö×é¼þ£ºmasterÀú³Ì¡¢workerÀú³Ì¡¢ÊÂÎñÄ£¿éºÍÅþÁ¬³Ø ¡£

MasterÀú³Ì£ºÈÏÕæÖÎÀíworkerÀú³Ì £¬Í¨¹ýfork()º¯Êý½¨Éè¶à¸öworkerÀú³Ì £¬²¢¼àÌý¶Ë¿Ú £¬ÎüÊÕÀ´×Ô¿Í»§¶ËµÄÅþÁ¬ÇëÇó ¡£

WorkerÀú³Ì£ºÏÖʵ´¦ÀíÇëÇóµÄÀú³Ì £¬Ã¿¸öworkerÀú³Ì¶¼ÊÇÒ»¸ö×ÔÁ¦µÄÀú³Ì £¬Í¨¹ý¸´ÖÆmasterÀú³ÌµÄ×ÊÔ´¶ø½¨Éè £¬ÈÏÕæÎüÊÕ²¢´¦Àí¿Í»§¶ËµÄÇëÇó ¡£

ÊÂÎñÄ£¿é£ºÍ¨¹ý¶à·IO¸´ÓûúÖÆ£¨Èçepoll¡¢kqueueµÈ£©ÊµÏÖ¸ßЧµÄÊÂÎñÇýÄîÍ·ÖÆ £¬ÓÃÓÚ¼à¿ØÎļþÐÎò·ûÉϱ¬·¢µÄÊÂÎñ²¢Í¨ÖªworkerÀú³Ì´¦Àí ¡£

ÅþÁ¬³Ø£ºÎ¬»¤Ò»¸öÔ¤ÏÈ·ÖÅɵÄÅþÁ¬³Ø £¬Ìá¸ßÇëÇóµÄ´¦ÀíЧÂʺÍÄÚ´æµÄÖÎÀíЧÂÊ ¡£

NginxµÄÇëÇó´¦ÀíģʽÈçÏ£º

int main(int argc, char *const *argv) {
    // ½¨ÉèÒ»¸ömasterÀú³Ì
    master_process_cycle();

    // ½¨Éè¶à¸öworkerÀú³Ì
    for (i = 0; i < worker_process_num; i++) {
        worker_process_cycle();
    }

    return 0;
}

µÇ¼ºó¸´ÖÆ

ͨ¹ýÒÔÉÏ´úÂëʾÀý¿ÉÒÔ¿´³ö £¬NginxµÄÇëÇó´¦ÀíģʽÖÐ £¬masterÀú³ÌÈÏÕæÖÎÀíworkerÀú³ÌµÄ½¨ÉèºÍ¼à¿Ø £¬¶øworkerÀú³ÌÔòÈÏÕæ´¦ÀíÏêϸµÄ¿Í»§¶ËÇëÇó ¡£

¶þ¡¢Ï̳߳ص÷Àí»úÖƵĵײãʵÏÖÔ­Àí

Nginxͨ¹ýÏ̳߳ص÷Àí»úÖÆÀ´Ìá¸ß²¢·¢ÇëÇóµÄ´¦ÀíЧÂÊ £¬Æäµ×²ãʵÏÖÔ­Àí°üÀ¨Ï̳߳صĽ¨ÉèºÍʹÃüµÄµ÷Àí ¡£

Ï̳߳صĽ¨É裺ÔÚworkerÀú³Ì³õʼ»¯Ê± £¬½¨ÉèÒ»¸ö°üÀ¨¶à¸öÏ̵߳ÄÏ̳߳Ø £¬ÓÃÓÚ´¦Àí¿Í»§¶ËµÄÇëÇó ¡£

ʹÃüµÄµ÷Àí£ºµ±Óпͻ§¶ËÇëÇóµÖ´ïʱ £¬ÊÂÎñÄ£¿é»á½«Ê¹ÃüÌí¼Óµ½Ï̳߳صÄʹÃüÐÐÁÐÖÐ £¬ÈôÊÇÏ̳߳ØÖÐÓпÕÏеÄÏß³Ì £¬ÔòÖ±½Ó½«Ê¹Ãü·Ö·¢¸ø¿ÕÏÐÏ߳̾ÙÐд¦Àí £»ÈôÊÇÏ̳߳ØÖÐûÓпÕÏÐÏß³Ì £¬ÔòʹÃü»á±»·ÅÈëÆÚ´ýÐÐÁÐÖÐ £¬´ýÓÐÏ߳̿ÕÏÐʱÔÙ¾ÙÐе÷Àí ¡£

Ï̳߳ص÷Àí»úÖƵĵײãʵÏÖÔ­ÀíÈçÏ£º

typedef struct {
    pthread_mutex_t         mutex;           // »¥³âËø£¬ÓÃÓÚ¶ÔʹÃüÐÐÁеIJÙ×÷¾ÙÐмÓËø±£»¤
    pthread_cond_t          cond;            // Ìõ¼þ±äÁ¿£¬ÓÃÓÚÔÚÓÐÐÂʹÃüµÖ´ïʱ½ÐÐÑÆÚ´ýµÄÏß³Ì
    ngx_thread_task_queue_t  task_queue;      // ʹÃüÐÐÁÐ
    ngx_thread_task_queue_t  waiting_queue;   // ÆÚ´ýÐÐÁÐ
    ngx_thread_pool_conf_t  *conf;            // Ï̳߳صÄÉèÖÃÐÅÏ¢
} ngx_thread_pool_t;

int ngx_thread_pool_init(ngx_thread_pool_t *tp) {
    // ³õʼ»¯»¥³âËøºÍÌõ¼þ±äÁ¿
    pthread_mutex_init(&tp->mutex, NULL);
    pthread_cond_init(&tp->cond, NULL);

    // ³õʼ»¯Ê¹ÃüÐÐÁкÍÆÚ´ýÐÐÁÐ
    ngx_thread_task_queue_init(&tp->task_queue);
    ngx_thread_task_queue_init(&tp->waiting_queue);

    // ½¨ÉèÏ̳߳ØÖеÄÏß³Ì
    for (i = 0; i < tp->conf->threads; i++) {
        pthread_create(&tid, NULL, ngx_thread_pool_worker, tp);
    }

    return 0;
}

µÇ¼ºó¸´ÖÆ

ͨ¹ýÒÔÉÏ´úÂëʾÀý¿ÉÒÔ¿´³ö £¬NginxµÄÏ̳߳ص÷Àí»úÖÆͨ¹ý»¥³âËøºÍÌõ¼þ±äÁ¿À´ÊµÏÖ¶ÔʹÃüÐÐÁеIJÙ×÷¾ÙÐмÓËø± £»¤ºÍÏ̵߳Äͬ²½ £¬°ü¹ÜÁ˶à¸öÏß³ÌÄܹ»Çå¾²µØ´¦ÀíʹÃü £¬²¢Ìá¸ßÁËÇëÇóµÄ´¦ÀíЧÂÊ ¡£

×ܽ᣺

±¾ÎÄÉîÈë½â¶ÁÁËNginxµÄÇëÇó´¦ÀíģʽºÍÏ̳߳ص÷Àí»úÖƵĵײãʵÏÖÔ­Àí £¬Õ¹Ê¾ÁËÏà¹Ø´úÂëʾÀý ¡£Nginx×÷Ϊһ¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷ £¬ÆäÆæÒìµÄÇëÇó´¦ÀíģʽºÍÓÅÒìµÄÏ̳߳ص÷Àí»úÖÆ £¬Ê¹ÆäÄܹ»´¦Àí´ó×ڵIJ¢·¢ÇëÇó £¬°ü¹ÜϵͳµÄ¸ßÐÔÄܺÍÎȹÌÐÔ ¡£ÉîÈëÃ÷È·NginxµÄÇëÇó´¦ÀíģʽºÍÏ̳߳ص÷Àí»úÖƹØÓÚ¾ÙÐÐÐÔÄܵ÷ÓźÍϵͳÉè¼Æ¾ßÓÐÖ÷ÒªµÄÖ¸µ¼ÒâÒå ¡£

ÒÔÉϾÍÊǽâ¶ÁNginxµÄÇëÇó´¦ÀíģʽºÍÏ̳߳ص÷Àí»úÖƵĵײãʵÏÖÔ­ÀíµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ