国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

epoll多路復(fù)用_并發(fā)服務(wù)器

這篇具有很好參考價(jià)值的文章主要介紹了epoll多路復(fù)用_并發(fā)服務(wù)器。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

應(yīng)用程序:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdlib.h>

#define ERR_MSG(msg)                   \
    do                                 \
    {                                  \
        printf("line:%d\n", __LINE__); \
        perror(msg);                   \
    } while (0)

#define PORT 6666            //端口號(hào)
#define IP "192.168.250.100" // IP地址

int main(int argc, const char *argv[])
{
    //創(chuàng)建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("socket create success sfd=%d __%d__\n", sfd, __LINE__);

    //設(shè)置端口快速被復(fù)用
    int reuse = 1;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }

    //填充服務(wù)器自身的地址信息結(jié)構(gòu)體
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);

    //存儲(chǔ)客戶端信息結(jié)構(gòu)體
    int s_res = 0;
    struct sockaddr_in cin;
    socklen_t len = sizeof(cin);
    struct sockaddr_in savecin[1024];

    //將IP地址和端口號(hào)綁定到指定的套接字文件描述符上
    if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("bind success __%d__\n", __LINE__);

    //將套接字設(shè)置為被動(dòng)監(jiān)聽狀態(tài)
    if (listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }
    printf("listen success __%d__\n", __LINE__);

    char buf[128] = "";
    ssize_t res = 0;
    // epoll
    int fd, newfd, epfd;

    struct epoll_event event;
    struct epoll_event events[10]; //存放就緒事件描述符的數(shù)組

    //創(chuàng)建epoll句柄
    epfd = epoll_create(1);
    if (epfd < 0)
    {
        printf("epoll_create filed\n");
        exit(-1);
    }

    event.events = EPOLLIN;
    event.data.fd = sfd;
    if (epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &event) < 0)
    {
        printf("epoll_ctl add filed\n");
    }

    //監(jiān)聽事件是否發(fā)生
    while (1)
    {
        //等待事件發(fā)生
        int ret = epoll_wait(epfd, events, 10, -1);
        if (ret < 0)
        {
            printf("epoll_wait filed\n");
            exit(-1);
        }
        int i;

        //循環(huán)遍歷數(shù)組,做事件的處理
        for (i = 0; i < ret; i++)
        {
            //監(jiān)聽到新連接
            if (events[i].data.fd == sfd)
            {
                newfd = accept(sfd, (struct sockaddr *)&cin, &len);
                if (newfd < 0)
                {
                    perror("accept");
                    return -1;
                }
                printf("[%s:%d] 客戶端連接成功 newfd=%d __%d__\n",
                       inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);

                savecin[newfd] = cin;

                //新客戶端套接字加入epoll
                event.events = EPOLLIN;
                event.data.fd = newfd;
                if (epoll_ctl(epfd, EPOLL_CTL_ADD, newfd, &event) < 0)
                {
                    printf("epoll_ctl add filed\n");
                }
            }
            else if (events[i].data.fd == 0) //鍵盤輸入事件
            {
                printf("觸發(fā)鍵盤輸入事件 __%d__\n", __LINE__);
                int sndfd = -1;
                res = scanf("%d %s", &sndfd, buf);
                while (getchar() != 10)
                    ;
                if (res != 2)
                {
                    printf("請輸入正確數(shù)據(jù)格式:fd string\n");
                    continue;
                }
            }
            else
            {
                //客戶端交互事件
                printf("觸發(fā)客戶端交互事件__%d__\n", __LINE__);
                bzero(buf, sizeof(buf));
                //接收數(shù)據(jù)
                res = recv(events[i].data.fd, buf, sizeof(buf), 0);
                if (res < 0)
                {
                    ERR_MSG("recv");
                    return -1;
                }
                else if (0 == res)
                {
                    printf("[%s:%d] 客戶端下線 newfd=%d __%d__\n",
                           inet_ntoa(savecin[i].sin_addr), ntohs(savecin[i].sin_port), i, __LINE__);

                    close(i); //未接收到數(shù)據(jù),關(guān)閉文件描述符
                    continue; //進(jìn)入下一次循環(huán)
                }

                //發(fā)送數(shù)據(jù)
                strcat(buf, "*_*");
                if (send(events[i].data.fd, buf, sizeof(buf), 0) < 0)
                {
                    ERR_MSG("send");
                    return -1;
                }
                printf("發(fā)送成功\n");
            }
        }
    }
    if (close(sfd) < 0)
    {
        ERR_MSG("close");
        return -1;
    }

    return 0;
}

驅(qū)動(dòng)程序:文章來源地址http://www.zghlxwxcb.cn/news/detail-617343.html

#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/io.h>
#include<linux/device.h>
#include<linux/uaccess.h>
#include<linux/poll.h>
struct class *cls;
struct device *dev;
unsigned int major;//定義一個(gè)變量保存主設(shè)備號(hào)
char kbuf[128]={0};
//定義等待隊(duì)列頭
wait_queue_head_t wq_head;
int condition=0;//數(shù)據(jù)是否準(zhǔn)備好的標(biāo)志變量
//封裝操作方法
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 *lof)
{
     printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
      if(size>sizeof(kbuf))//用戶的需求內(nèi)核滿足不了
     {
        size=sizeof(kbuf);
     }
      long ret;
     ret=copy_to_user(ubuf,kbuf,size);
     if(ret)
     {
        printk("copy_to_user filed\n");
        return -EIO;
     }
     condition=0;//表示下一次數(shù)據(jù)沒有準(zhǔn)備好
    return 0;
}
ssize_t mycdev_write(struct file *file, const char  *ubuf, size_t size, loff_t *lof)
{
     printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
     if(size>sizeof(kbuf))//用戶的需求內(nèi)核滿足不了
     {
        size=sizeof(kbuf);
     }
      long ret;
     ret=copy_from_user(kbuf,ubuf,size);//表示模擬硬件數(shù)據(jù)就緒
     if(ret)
     {
        printk("copy_from_user filed\n");
        return -EIO;
     }
     condition=1;
     wake_up_interruptible(&wq_head);//喚醒休眠的進(jìn)程
     
    return 0;
}
//封裝poll方法
__poll_t mycdev_poll(struct file *file, struct poll_table_struct *wait)
{
    __poll_t mask=0;
    //1.向上提交等待隊(duì)列頭
    poll_wait(file,&wq_head,wait);
    //2.根據(jù)事件是否發(fā)生給一個(gè)合適的返回值
    if(condition)
    {
        mask=POLLIN;
    }
    return mask;
}
int mycdev_close(struct inode *inode, struct file *file)
{
     printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
//定義一個(gè)操作方法結(jié)構(gòu)體變量并且初始化
struct file_operations fops={
    .open=mycdev_open,
    .release=mycdev_close,
    .read=mycdev_read,
    .poll=mycdev_poll,
    .write=mycdev_write,
};
static int __init mycdev_init(void)
{
    //初始化等待隊(duì)列頭
    init_waitqueue_head(&wq_head);
    //注冊字符設(shè)備驅(qū)動(dòng)
    major=register_chrdev(0,"mychrdev",&fops);
    if(major<0)
    {
        printk("注冊字符設(shè)備驅(qū)動(dòng)失敗\n");
        return major;
    }
    printk("注冊字符設(shè)備驅(qū)動(dòng)成功major=%d\n",major);
    // 向上提交目錄
    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目錄失敗\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目錄信息成功\n");
    // 向上提交設(shè)備節(jié)點(diǎn)信息
   
        dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev");
        if (IS_ERR(dev))
        {
            printk("向上提交設(shè)備節(jié)點(diǎn)信息失敗\n");
            return -PTR_ERR(dev);
        }
    printk("向上提交設(shè)備節(jié)點(diǎn)成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
    // 銷毀節(jié)點(diǎn)信息
    
        device_destroy(cls, MKDEV(major, 0));
  
    // 銷毀目錄信息
    class_destroy(cls);
    //注銷字符設(shè)備驅(qū)動(dòng)
    unregister_chrdev(major,"mychrdev");

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


到了這里,關(guān)于epoll多路復(fù)用_并發(fā)服務(wù)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 驅(qū)動(dòng)開發(fā),IO多路復(fù)用實(shí)現(xiàn)過程,epoll方式

    驅(qū)動(dòng)開發(fā),IO多路復(fù)用實(shí)現(xiàn)過程,epoll方式

    被稱為當(dāng)前時(shí)代最好用的io多路復(fù)用方式; 核心操作:一棵樹(紅黑樹)、一張表(內(nèi)核鏈表)以及三個(gè)接口; ?思想:(fd代表文件描述符) ????????epoll要把檢測的事件fd掛載到內(nèi)核空間紅黑樹上,遍歷紅黑樹,調(diào)用每個(gè)fd對應(yīng)的操作方法,找到發(fā)生事件的fd,如果沒有發(fā)

    2024年02月07日
    瀏覽(30)
  • 【Linux網(wǎng)絡(luò)編程】TCP并發(fā)服務(wù)器的實(shí)現(xiàn)(IO多路復(fù)用select)

    【Linux網(wǎng)絡(luò)編程】TCP并發(fā)服務(wù)器的實(shí)現(xiàn)(IO多路復(fù)用select)

    服務(wù)器模型主要分為兩種, 循環(huán)服務(wù)器 和 并發(fā)服務(wù)器 。 循環(huán)服務(wù)器 : 在同一時(shí)間只能處理一個(gè)客戶端的請求。 并發(fā)服務(wù)器 : 在同一時(shí)間內(nèi)能同時(shí)處理多個(gè)客戶端的請求。 TCP的服務(wù)器默認(rèn)的就是一個(gè)循環(huán)服務(wù)器,原因是有兩個(gè)阻塞 accept函數(shù) 和recv函數(shù) 之間會(huì)相互影響。

    2024年02月03日
    瀏覽(98)
  • 【高并發(fā)網(wǎng)絡(luò)通信架構(gòu)】引入IO多路復(fù)用(select,poll,epoll)實(shí)現(xiàn)高并發(fā)tcp服務(wù)端

    【高并發(fā)網(wǎng)絡(luò)通信架構(gòu)】引入IO多路復(fù)用(select,poll,epoll)實(shí)現(xiàn)高并發(fā)tcp服務(wù)端

    目錄 一,往期文章 二,基本概念 IO多路復(fù)用 select 模型 poll 模型 epoll 模型 select,poll,epoll 三者對比 三,函數(shù)清單 1.select 方法 2.fd_set 結(jié)構(gòu)體 3.poll 方法 4.struct pollfd 結(jié)構(gòu)體 5.epoll_create 方法 6.epoll_ctl 方法 7.epoll_wait 方法 8.struct epoll_event 結(jié)構(gòu)體 四,代碼實(shí)現(xiàn) select 操作流程 s

    2024年02月12日
    瀏覽(31)
  • 多路IO—POll函數(shù),epoll服務(wù)器開發(fā)流程

    多路IO—POll函數(shù),epoll服務(wù)器開發(fā)流程

    \\\"在計(jì)算機(jī)網(wǎng)絡(luò)編程中,多路IO技術(shù)是非常常見的一種技術(shù)。其中,Poll函數(shù)和Epoll函數(shù)是最為常用的兩種多路IO技術(shù)。這兩種技術(shù)可以幫助服務(wù)器端處理多個(gè)客戶端的并發(fā)請求,提高了服務(wù)器的性能。本文將介紹Poll和Epoll函數(shù)的使用方法,并探討了在服務(wù)器開發(fā)中使用這兩種技

    2024年02月06日
    瀏覽(17)
  • 【高并發(fā)網(wǎng)絡(luò)通信架構(gòu)】3.引入IO多路復(fù)用(select,poll,epoll)實(shí)現(xiàn)高并發(fā)tcp服務(wù)端

    【高并發(fā)網(wǎng)絡(luò)通信架構(gòu)】3.引入IO多路復(fù)用(select,poll,epoll)實(shí)現(xiàn)高并發(fā)tcp服務(wù)端

    目錄 一,往期文章 二,基本概念 IO多路復(fù)用 select 模型 poll 模型 epoll 模型 select,poll,epoll 三者對比 三,函數(shù)清單 1.select 方法 2.fd_set 結(jié)構(gòu)體 3.poll 方法 4.struct pollfd 結(jié)構(gòu)體 5.epoll_create 方法 6.epoll_ctl 方法 7.epoll_wait 方法 8.struct epoll_event 結(jié)構(gòu)體 四,代碼實(shí)現(xiàn) select 操作流程 s

    2024年02月14日
    瀏覽(25)
  • I/O多路轉(zhuǎn)接——epoll服務(wù)器代碼編寫

    I/O多路轉(zhuǎn)接——epoll服務(wù)器代碼編寫

    目錄 一、poll? 二、epoll 1.epoll 2.epoll的函數(shù)接口 ①epoll_create ②epoll_ctl ③epoll_wait 3.操作原理 三、epoll服務(wù)器編寫 1.日志打印 2.TCP服務(wù)器 3.Epoll ①雛形 ②InitEpollServer 與 RunServer ③HandlerEvent 四、Epoll的工作模式 1.LT模式與ET模式 2.基于LT模式的epoll服務(wù)器 ①整體框架 ②處理BUG ③優(yōu)

    2024年02月02日
    瀏覽(22)
  • epoll實(shí)現(xiàn)并發(fā)服務(wù)器

    epoll 是Linux操作系統(tǒng)提供的一種事件通知機(jī)制,用于高效處理大量文件描述符上的事件。它是一種基于內(nèi)核的I/O事件通知接口,可以用于實(shí)現(xiàn)高性能的并發(fā)服務(wù)器和異步I/O操作。 與傳統(tǒng)的事件通知機(jī)制(如 select 和 poll )相比, epoll 具有更高的性能和擴(kuò)展性。它采用了一種基

    2024年02月09日
    瀏覽(28)
  • 多路轉(zhuǎn)接高性能IO服務(wù)器|select|poll|epoll|模型詳細(xì)實(shí)現(xiàn)

    多路轉(zhuǎn)接高性能IO服務(wù)器|select|poll|epoll|模型詳細(xì)實(shí)現(xiàn)

    那么這里博主先安利一下一些干貨滿滿的專欄啦! Linux專欄 https://blog.csdn.net/yu_cblog/category_11786077.html?spm=1001.2014.3001.5482 操作系統(tǒng)專欄 https://blog.csdn.net/yu_cblog/category_12165502.html?spm=1001.2014.3001.5482 手撕數(shù)據(jù)結(jié)構(gòu) https://blog.csdn.net/yu_cblog/category_11490888.html?spm=1001.2014.3001.5482 去倉庫獲

    2024年02月15日
    瀏覽(24)
  • epoll并發(fā)服務(wù)器的實(shí)現(xiàn)

    epoll并發(fā)服務(wù)器的實(shí)現(xiàn)

    1.實(shí)現(xiàn)并發(fā)通信的三種方式 ? 實(shí)現(xiàn)并發(fā)通信主要有三種方式: 多進(jìn)程服務(wù)器 、 多路復(fù)用服務(wù)器 (I/O復(fù)用)、 多線程服務(wù)器 多進(jìn)程服務(wù)器 ? 多進(jìn)程服務(wù)器指的是利用不同進(jìn)程處理來自不同客戶端發(fā)來的連接請求,進(jìn)程之間以輪轉(zhuǎn)的方式運(yùn)行,由于各個(gè)進(jìn)程之間輪轉(zhuǎn)運(yùn)行的時(shí)

    2024年02月03日
    瀏覽(27)
  • Linux網(wǎng)絡(luò)編程:多路I/O轉(zhuǎn)接服務(wù)器(select poll epoll)

    Linux網(wǎng)絡(luò)編程:多路I/O轉(zhuǎn)接服務(wù)器(select poll epoll)

    文章目錄: 一:select 1.基礎(chǔ)API? select函數(shù) 思路分析 select優(yōu)缺點(diǎn) 2.server.c 3.client.c 二:poll 1.基礎(chǔ)API? poll函數(shù)? poll優(yōu)缺點(diǎn) read函數(shù)返回值 突破1024 文件描述符限制 2.server.c 3.client.c 三:epoll 1.基礎(chǔ)API epoll_create創(chuàng)建? ?epoll_ctl操作? epoll_wait阻塞 epoll實(shí)現(xiàn)多路IO轉(zhuǎn)接思路 epoll優(yōu)缺點(diǎn)

    2024年02月11日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包