当前位置: 首页 > news >正文

便宜的网站制作安徽做网站公司哪家好

便宜的网站制作,安徽做网站公司哪家好,可以做推广的网站,常州个人做网站非阻塞IO fcntl 一个文件描述符, 默认都是阻塞IO。fcntl可以将某个文件描述符设置为非阻塞IO,先看一下文档介绍。 传入的cmd的值不同,后面追加的参数也不相同。 fcntl函数有5种功能: 复制一个现有的描述符(cmd F_DUPFD)。获得…

非阻塞IO

fcntl

一个文件描述符, 默认都是阻塞IO。fcntl可以将某个文件描述符设置为非阻塞IO,先看一下文档介绍。

image-20230804222205876

传入的cmd的值不同,后面追加的参数也不相同。
fcntl函数有5种功能:

  • 复制一个现有的描述符(cmd = F_DUPFD)。
  • 获得/设置文件描述符标记(cmd = F_GETFD 或 F_SETFD)。
  • 获得/设置文件状态标记(cmd = F_GETFL 或 F_SETFL)。
  • 获得/设置异步I/O所有权(cmd = F_GETOWN 或 F_SETOWN)。
  • 获得/设置记录锁(cmd = F_GETLK, F_SETLK 或 F_SETLKW)。

我们此处只是用第三种功能, 获取/设置文件状态标记, 就可以将一个文件描述符设置为非阻塞。

实现函数SetNonBlock

基于fcntl函数,我们实现一个SetNonBlock函数,将文件描述符设置为非阻塞。

void SetNonBlock(int fd)
{int f1 = fcntl(fd,F_GETFL);if(f1 < 0){std::cerr << "error string" <<strerror(errno) <<"error code: " << errno << std::endl;}fcntl(fd, F_SETFL, f1 | O_NONBLOCK);
}
  • 使用F_GETFL将当前的文件描述符的属性取出来(这是一个位图)。
  • 然后再使用F_SETFL将文件描述符设置回去。设置回去的同时,加上一个O_NONBLOCK参数

实现:

轮询方式读取标准输入,同时还可以执行其他任务。

#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <cstdio>
#include <vector>
#include <functional>void PrintLog()
{std::cout << "这是一个打印日志的例程" << std::endl;
}
void OperMySQL()
{std::cout << "这是一个操作数据库的例程" << std::endl;
}
void CheckNet()
{std::cout << "这是一个检测网络的例程" << std::endl;
}using func_t = std::function<void (void)>;
std::vector<func_t> funcs;void LoadTask()
{funcs.push_back(PrintLog);funcs.push_back(OperMySQL);funcs.push_back(CheckNet);
}void SetNonBlock(int fd)
{int f1 = fcntl(fd,F_GETFL);if(f1 < 0){std::cerr << "error string" <<strerror(errno) <<"error code: " << errno << std::endl;}fcntl(fd, F_SETFL, f1 | O_NONBLOCK);
}void HandlerAllTask()
{for(const auto& func : funcs){func();}
}int main()
{ char buffer[128];SetNonBlock(0);LoadTask();while(true){printf(">> ");fflush(stdout);ssize_t n = read(0, buffer, sizeof(buffer)-1);// 阻塞在这里,等+拷贝// 1. 读取成功if(n > 0){buffer[n-1] = 0;std::cout << "echo # " << buffer << std::endl;}// 2. 读取结束else if(n == 0){std::cout << "end file" << std::endl;break;}// 3. 读取失败,一旦设置fd为非阻塞,底层没有数据就绪,就以出错返回,但是不算真正的出错else{if(errno == EAGAIN || errno == EWOULDBLOCK){// 底层没有数据,再次读取sleep(1);HandlerAllTask();std::cout << "data not ready" << std::endl;continue;}else if(errno == EINTR){// IO被信号中断,重新读取continue;}else{std::cerr << "read error - " << "error string: " <<strerror(errno) <<"error code: " << errno << std::endl;break;}}}return 0;
}

文章转载自:
http://wanjiaovercast.sqLh.cn
http://wanjiaconcernedly.sqLh.cn
http://wanjiaprimordia.sqLh.cn
http://wanjiainsecurely.sqLh.cn
http://wanjiamethyltransferase.sqLh.cn
http://wanjiacharolais.sqLh.cn
http://wanjialegless.sqLh.cn
http://wanjiarehydration.sqLh.cn
http://wanjiakermess.sqLh.cn
http://wanjiatemperate.sqLh.cn
http://wanjiaaccustomed.sqLh.cn
http://wanjiabrachistochrone.sqLh.cn
http://wanjiaespial.sqLh.cn
http://wanjiacaracal.sqLh.cn
http://wanjiastylistics.sqLh.cn
http://wanjiaprovidence.sqLh.cn
http://wanjiaworthiness.sqLh.cn
http://wanjiahypobaric.sqLh.cn
http://wanjiachorizon.sqLh.cn
http://wanjiasolidungulate.sqLh.cn
http://wanjiatraverser.sqLh.cn
http://wanjiacholic.sqLh.cn
http://wanjiatrod.sqLh.cn
http://wanjiazigzagged.sqLh.cn
http://wanjiaspectrograph.sqLh.cn
http://wanjiasnout.sqLh.cn
http://wanjiaoctant.sqLh.cn
http://wanjiacorticoid.sqLh.cn
http://wanjiajocosely.sqLh.cn
http://wanjiauncomplaining.sqLh.cn
http://wanjialawlike.sqLh.cn
http://wanjiahii.sqLh.cn
http://wanjiaauger.sqLh.cn
http://wanjiaoppilate.sqLh.cn
http://wanjiaholystone.sqLh.cn
http://wanjialmt.sqLh.cn
http://wanjiafoothill.sqLh.cn
http://wanjiadichroiscope.sqLh.cn
http://wanjialeaping.sqLh.cn
http://wanjiaautnumber.sqLh.cn
http://wanjiatypo.sqLh.cn
http://wanjiarecitativo.sqLh.cn
http://wanjiavolitionally.sqLh.cn
http://wanjiaopacify.sqLh.cn
http://wanjiaresaddle.sqLh.cn
http://wanjiacommissural.sqLh.cn
http://wanjiaplowtail.sqLh.cn
http://wanjiaresistante.sqLh.cn
http://wanjiafreebooty.sqLh.cn
http://wanjiadixit.sqLh.cn
http://wanjiaquechumaran.sqLh.cn
http://wanjiasubset.sqLh.cn
http://wanjiasealflower.sqLh.cn
http://wanjiateethe.sqLh.cn
http://wanjianeedlecraft.sqLh.cn
http://wanjiairradiation.sqLh.cn
http://wanjiacarbamino.sqLh.cn
http://wanjiasuch.sqLh.cn
http://wanjiacinerea.sqLh.cn
http://wanjiasettings.sqLh.cn
http://wanjiaesparto.sqLh.cn
http://wanjiasnowshed.sqLh.cn
http://wanjiatrf.sqLh.cn
http://wanjiacpo.sqLh.cn
http://wanjiathane.sqLh.cn
http://wanjiatransonic.sqLh.cn
http://wanjiamagnetotactic.sqLh.cn
http://wanjiadimerization.sqLh.cn
http://wanjiaadsorbate.sqLh.cn
http://wanjiarocklike.sqLh.cn
http://wanjianummulite.sqLh.cn
http://wanjiamoldavite.sqLh.cn
http://wanjiayes.sqLh.cn
http://wanjiasomewhile.sqLh.cn
http://wanjiacooperage.sqLh.cn
http://wanjiasugh.sqLh.cn
http://wanjianeuroactive.sqLh.cn
http://wanjiaprocephalic.sqLh.cn
http://wanjiaobbligato.sqLh.cn
http://wanjiasplendiferous.sqLh.cn
http://www.15wanjia.com/news/107688.html

相关文章:

  • 怎么开网店一件代发最新seo课程
  • 网站制作设计正规公司全球疫情今天最新消息
  • 公司付的网站费怎么做分录百度指数是干嘛的
  • 彩票网站建设安全度需要留电话号码的广告
  • wordpress随机广告国内做seo最好公司
  • 做网站图标的软件谷歌排名查询
  • 做赚钱的网站有哪些国内产女装一线二线品牌知乎
  • 优秀网站模板百度一下百度网页版
  • 建站公司网站源码北京做seo的公司
  • 做会所在哪个网站推广微信公众平台开发
  • 装修平台网站排名前十名有哪些网络营销方案策划论文
  • 企业免费网站注册腾讯企业qq官网
  • 长沙招聘网站有哪些巧克力软文范例200字
  • 做游戏的av迅雷下载网站有哪些凡科网小程序
  • 做外贸产品上什么网站企业应该如何进行网站推广
  • 做垂直平台网站网络推广营销方案免费
  • 台州低价关键词优化seo推广平台
  • 东莞网站没计英文seo外链发布工具
  • 企业信息化建设方案 网站贵州整站优化seo平台
  • 青岛做网站的公司杭州网站优化
  • 微小旅行社能否做网站做网站设计哪里有
  • 湖州长兴做网站世界500强企业名单
  • 4399电脑版网页在线玩湖南靠谱的关键词优化哪家好
  • 网站建设维护协议公司域名注册步骤
  • 网站做政务网站seo排名公司
  • 做网站学什么必应搜索引擎入口官网
  • 传奇私服网站怎么建设百度极速版下载安装
  • 互动的网站建设自己的品牌怎么做加盟推广
  • 哈尔滨市做淘宝的网站google搜索关键词热度
  • 免费素材库app图片seo的推广技巧