epoll实现并发服务器

#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include<linux/device.h>
#include<linux/poll.h>


int major;
struct class *cls;
struct device *dev;
struct fasync_struct *fapp;
char kbuf[128]={0};
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t  mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *off)
{
    int ret; 
    //将进程2写到内核的数据传递给进程1
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;//拷贝失败返回错误码
    }
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
 
    return 0;
}
ssize_t  mycdev_write(struct file *file, const char  *ubuf, size_t size, loff_t *off)
{
    int ret;
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;//拷贝失败返回错误码
    }
   //给进程发送信号
   kill_fasync(&fapp, SIGIO, POLL_IN);

    return 0;
}


//定义fasync操作方法
int mycdev_fasync(int fd, struct file *file, int on)
{
    //完成发送信号之前的准备工作
    return fasync_helper(fd, file, on, &fapp);
}

int  mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
//定义操作方法结构体变量并初始化
struct file_operations fops=
{
    .open=mycdev_open,
    .read=mycdev_read,
    .write=mycdev_write,
     .fasync=mycdev_fasync,
    .release=mycdev_close,
};

//入口函数
static int __init mycdev_init(void)
{
      int i;
    //进行字符设备驱动的注册
    major=register_chrdev(0,"mycdev",&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n",major);
    //向上提交目录
    cls=class_create(THIS_MODULE,"mycdev");
    if(IS_ERR(cls))//指针指向预留空间,函数调用失败
    {
        printk("向上提交目录失败\n");
        return PTR_ERR(cls);
    }
     printk("向上提交目录成功\n");
     //向上提交设备节点
    
     for(i=0;i<3;i++)
     {
        dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
         if(IS_ERR(dev))//指针指向预留空间,函数调用失败
        {
             printk("向上提交目录失败\n");
            return PTR_ERR(dev);
        }
     }
     printk("设备节点向上提交成功\n");
  
    return 0;
}
//出口函数
static void __exit mycdev_exit(void)
{
      int i;
    
  
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //删除目录信息
    class_destroy(cls);
    //字符设备驱动的注销
    unregister_chrdev(major,"mycdev");

}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

以下是使用epoll和线程池实现并发服务器的C++11代码示例: ```cpp #include <iostream> #include <thread> #include <vector> #include <queue> #include <mutex> #include <condition_variable> #include <sys/epoll.h> #include <unistd.h> #define MAX_EVENTS 100 #define THREAD_POOL_SIZE 10 std::mutex mtx; std::condition_variable cv; std::queue<int> taskQueue; void workerThread() { while (true) { std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, [] { return !taskQueue.empty(); }); int fd = taskQueue.front(); taskQueue.pop(); // 处理任务,这里可以根据具体需求进行处理 lock.unlock(); // 继续监听其他事件 } } int main() { // 创建epoll句柄 int epoll_fd = epoll_create(1); if (epoll_fd == -1) { std::cerr << "Failed to create epoll" << std::endl; return 1; } // 创建线程池 std::vector<std::thread> threadPool; for (int i = 0; i < THREAD_POOL_SIZE; ++i) { threadPool.emplace_back(workerThread); } // 添加监听事件到epoll句柄 struct epoll_event event; event.events = EPOLLIN; event.data.fd = /* 监听的文件描述符 */; if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, /* 监听的文件描述符 */, &event) == -1) { std::cerr << "Failed to add event to epoll" << std::endl; return 1; } // 开始监听事件 struct epoll_event events[MAX_EVENTS]; while (true) { int num_events = epoll_wait(epoll_fd, events, MAX_EVENTS, -1); if (num_events == -1) { std::cerr << "Failed to wait for events" << std::endl; return 1; } for (int i = 0; i < num_events; ++i) { if (events[i].events & EPOLLIN) { // 处理读事件,将任务添加到任务队列 std::lock_guard<std::mutex> lock(mtx); taskQueue.push(events[i].data.fd); cv.notify_one(); } } } // 清理资源 close(epoll_fd); for (auto& thread : threadPool) { thread.join(); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值