使用libevent多线程验证Linux上的服务器惊群现象
什么是驚群現(xiàn)象?
驚群(thundering herd)是指,只有一個子進程能獲得連接,但所有N個子進程卻都被喚醒了,這種情況將使性能受損。
舉一個很簡單的例子,當你往一群鴿子中間扔一塊食物,雖然最終只有一個鴿子搶到食物,但所有鴿子都會被驚動來爭奪,沒有搶到食物的鴿子只好回去繼續(xù)睡覺, 等待下一塊食物到來。這樣,每扔一塊食物,都會驚動所有的鴿子,即為驚群。對于操作系統(tǒng)來說,多個進程/線程在等待同一資源時,也會產(chǎn)生類似的效果,其結(jié) 果就是每當資源可用,所有的進程/線程都來競爭資源,造成的后果:
1)系統(tǒng)對用戶進程/線程頻繁的做無效的調(diào)度、上下文切換,系統(tǒng)系能大打折扣。
2)為了確保只有一個線程得到資源,用戶必須對資源操作進行加鎖保護,進一步加大了系統(tǒng)開銷。
最常見的例子就是對于socket描述符的accept操作,當多個用戶進程/線程監(jiān)聽在同一個端口上時,由于實際只可能accept一次,因此就會產(chǎn)生驚群現(xiàn)象.這個問題是一個古老的問題,新的操作系統(tǒng)內(nèi)核已經(jīng)解決了這一問題。
在多線程情況下,每個線程都監(jiān)聽同一個fd,當有數(shù)據(jù)來的時候,是否會有驚群現(xiàn)象呢?驗證如下
服務(wù)器端代碼
//g++ -g libevent_server.cpp -o libevent_server -levent -lpthread
//說明:服務(wù)器監(jiān)聽在本地19870端口, 等待udp client連接,有驚群現(xiàn)象: 當有數(shù)據(jù)到來時, 每個線程都被喚醒, 但是只有一個線程可以讀到數(shù)據(jù)
//#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <event.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>using namespace std;int init_count = 0;
pthread_mutex_t init_lock;
pthread_cond_t init_cond;typedef struct {pthread_t thread_id; /* unique ID of this thread */struct event_base *base; /* libevent handle this thread uses */struct event notify_event; /* listen event for notify pipe */
} mythread;void *worker_libevent(void *arg)
{mythread *p = (mythread *)arg;pthread_mutex_lock(&init_lock);init_count++;pthread_cond_signal(&init_cond);pthread_mutex_unlock(&init_lock);event_base_loop(p->base, 0);
}int create_worker(void*(*func)(void *), void *arg)
{mythread *p = (mythread *)arg;pthread_t tid;pthread_attr_t attr;pthread_attr_init(&attr);pthread_create(&tid, &attr, func, arg);p->thread_id = tid;pthread_attr_destroy(&attr);return 0;
}void process(int fd, short which, void *arg)
{mythread *p = (mythread *)arg;printf("I am in the thread: [%lu]\n", p->thread_id);char buffer[100];memset(buffer, 0, 100);int ilen = read(fd, buffer, 100);printf("read num is: %d\n", ilen);printf("the buffer: %s\n", buffer);
}//設(shè)置libevent事件回調(diào)
int setup_thread(mythread *p, int fd)
{p->base = event_init();event_set(&p->notify_event, fd, EV_READ|EV_PERSIST, process, p);event_base_set(p->base, &p->notify_event);event_add(&p->notify_event, 0);return 0;
}int main()
{struct sockaddr_in in;int fd;fd = socket(AF_INET, SOCK_DGRAM, 0);//在127.0.0.1:19870處監(jiān)聽struct in_addr s;bzero(&in, sizeof(in));in.sin_family = AF_INET;inet_pton(AF_INET, "127.0.0.1", (void *)&s);in.sin_addr.s_addr = s.s_addr;in.sin_port = htons(19870);bind(fd, (struct sockaddr*)&in, sizeof(in));int threadnum = 10; //創(chuàng)建10個線程int i;pthread_mutex_init(&init_lock, NULL);pthread_cond_init(&init_cond, NULL);mythread *g_thread;g_thread = (mythread *)malloc(sizeof(mythread)*10);for(i=0; i<threadnum; i++){ //10個線程都監(jiān)聽同一個socket描述符, 檢查是否產(chǎn)生驚群現(xiàn)象?setup_thread(&g_thread[i], fd);}for(i=0; i<threadnum; i++){create_worker(worker_libevent, &g_thread[i]);}//master線程等待worker線程池初始化完全pthread_mutex_lock(&init_lock);while(init_count < threadnum){pthread_cond_wait(&init_cond, &init_lock);}pthread_mutex_unlock(&init_lock);printf("IN THE MAIN LOOP\n");while(1){sleep(1);}//沒有回收線程的代碼free(g_thread);return 0;
}
客戶端代碼
//g++ -g libevent_client.cpp -o libevent_client
//#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>using namespace std;int main()
{struct sockaddr_in in;int fd;fd = socket(AF_INET, SOCK_DGRAM, 0);struct in_addr s;bzero(&in, sizeof(in));in.sin_family = AF_INET;inet_pton(AF_INET, "127.0.0.1", (void *)&s);in.sin_addr.s_addr = s.s_addr;in.sin_port = htons(19870);string str = "I am Michael";sendto(fd, str.c_str(), str.size(), 0, (struct sockaddr *)&in, sizeof(struct sockaddr_in));return 0;
}
測試效果圖
參考文獻
[1].http://blog.chinaunix.net/uid-26575352-id-3075103.html
[2].http://blog.csdn.net/nanjunxiao/article/details/9140769
[3].http://blog.163.com/leyni@126/blog/static/16223010220122611523786/
[4].http://simohayha.iteye.com/blog/658012
總結(jié)
以上是生活随笔為你收集整理的使用libevent多线程验证Linux上的服务器惊群现象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用sed快速批量替换文件夹内的文件中的
- 下一篇: js控制图片的缩放代码示例