使用libevent多线程验证Linux上的服务器惊群现象

2024-03-18 15:48

本文主要是介绍使用libevent多线程验证Linux上的服务器惊群现象,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

什么是惊群现象?

惊群(thundering herd)是指,只有一个子进程能获得连接,但所有N个子进程却都被唤醒了,这种情况将使性能受损。
举一个很简单的例子,当你往一群鸽子中间扔一块食物,虽然最终只有一个鸽子抢到食物,但所有鸽子都会被惊动来争夺,没有抢到食物的鸽子只好回去继续睡觉, 等待下一块食物到来。这样,每扔一块食物,都会惊动所有的鸽子,即为惊群。对于操作系统来说,多个进程/线程在等待同一资源时,也会产生类似的效果,其结 果就是每当资源可用,所有的进程/线程都来竞争资源,造成的后果:
1)系统对用户进程/线程频繁的做无效的调度、上下文切换,系统系能大打折扣。
2)为了确保只有一个线程得到资源,用户必须对资源操作进行加锁保护,进一步加大了系统开销。

最常见的例子就是对于socket描述符的accept操作,当多个用户进程/线程监听在同一个端口上时,由于实际只可能accept一次,因此就会产生惊群现象.这个问题是一个古老的问题,新的操作系统内核已经解决了这一问题。

在多线程情况下,每个线程都监听同一个fd,当有数据来的时候,是否会有惊群现象呢?验证如下

服务器端代码

//g++ -g libevent_server.cpp -o libevent_server -levent -lpthread
//说明:服务器监听在本地19870端口, 等待udp client连接,有惊群现象: 当有数据到来时, 每个线程都被唤醒, 但是只有一个线程可以读到数据
//#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);
}//设置libevent事件回调
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处监听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; //创建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个线程都监听同一个socket描述符, 检查是否产生惊群现象?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

这篇关于使用libevent多线程验证Linux上的服务器惊群现象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/822846

相关文章

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地