sem可以为网站建设做什么,营销型建设网站实训总结,vue网页模板免费,企业vi系统文章目录 1、IO2、同、异步IO#xff08;5种IO类型#xff09;3、其它高级IO4、非阻塞IO 其它IO类型的实现在这篇之后的三篇 1、IO
input#xff0c;output。调用read或recv接口时#xff0c;如果对方长时间不向我方接收缓冲区拷贝数据#xff0c;我们的进程就只能阻塞5种IO类型3、其它高级IO4、非阻塞IO 其它IO类型的实现在这篇之后的三篇 1、IO
inputoutput。调用read或recv接口时如果对方长时间不向我方接收缓冲区拷贝数据我们的进程就只能阻塞这是读取条件不满足。阻塞的时间成本最后会体现在用户上。因此可以说IO 等 数据拷贝。高效IO则是单位事件内等的比重越低IO效率越高。
可以看出IO是有条件的满足条件就叫IO时间就绪。
IO有五种模型
2、同、异步IO5种IO类型
调用读取用的接口后对于有自己的接收缓冲区有5种IO方式
阻塞IO如果缓冲区内没有好数据系统调用就一直阻塞等待不做别的事情直到准备好数据才开始读取并返回。所有的套接字默认都是阻塞IO。
非阻塞IO单次检验缓冲区内没有数据recv函数就返回EWOULDBLOCK每隔一段时间再调用recv接口来查看缓冲区如果有就读取并返回数据没有就还返回上面的。这也就是非阻塞轮询。
阻塞与非阻塞的区别在于等的方式不同不过都要自己去拷贝数据。非阻塞在没得到结果前就不阻塞当前进程直接返回过一会再来查看阻塞就是一直阻塞等待直到读取数据再返回。
上面两个等的方式不同但拷贝方式相同。
信号驱动IO缓冲区内有数据时就发送SIGIO信号通知上层调用接口来读取。信号是告知上层什么时候可以读取的接口是在信号处理了之后才调用的。
recv函数调用后会先检测有没有数据也就是条件检测没有返回有才读取再返回。像这样的接口只使用一个文件描述符。
多路复用/多路转接IO执行一个进程传多个文件描述符调用多个接口来等待数据进程只负责等待数据等到有了数据也就是IO条件就绪就通知上层调用recv接口来读取并返回。
多路复用IO是效率最高的因为并不只是一个进程在等而是很多个进程在等那么等待成功的可能性就更高。单个进程等待的概率和多个进程等待的概率相比。
上面4个都是同步IO。
异步IO不参与等待过程只是发起一个进程给一个缓冲区来接收数据。至于结果怎么样由操作系统来决定。多路复用是进程在等待异步IO是系统在等待。
两种IO方式的区别是有没有参与等以及读取的过程。实际上对应到系统应当是一个进程通过特定的文件描述符等待IO事件就绪通过系统调用来读取数据拷贝到上层。
recv函数就是在做条件检测条件通过再读取。
3、其它高级IO
记录锁、系统V流机制。存储映射IOmmapreadv和writev函数等
4、非阻塞IO
int main()
{char buffer[64];while(true){printf( );fflush(stdout);ssize_t n read(0, buffer, sizeof(buffer) - 1);if(n 0){buffer[n - 1] 0;std::cout echo# buffer std::endl;}}
}这就是一个等待的过程如果我们不输入就一直卡在read那里。这是阻塞如果要变成非阻塞用fcntl接口。 #include fcntl.h int fcntl(int fd, int cmd, …/* arg */); cmd值有5种 复制一个现有的描述符cmdF_DUPFD 获得/设置文件描述符标记(cmdF_GETFD或F_SETFD) 获得/设置文件状态标记(cmdF_GETFL或F_SETFL)//GET获得文件状态标记位SET设置这个文件新的状态 获得/设置异步I/O所有权(cmdF_GETOWN或F_SETOWN) 获得/设置记录锁(cmdF_GETLKF_SETLK或F_SETLKW) void SetNonBlock(int fd) {int fl fcntl(fd, F_GETFL);//使用F_GETFL将当前的文件描述符的属性取出来(这是一个位图)if (fl 0) {perror(fcntl);return; }fcntl(fd, F_SETFL, fl | O_NONBLOCK);//然后再使用F_SETFL将文件描述符设置回去. 设置回去的同时, 加上一个O_NONBLOCK参数
}非阻塞和阻塞都是文件的读取方式在文件结构体中就是一个变量设为1或0这里的O_NONBLOCK是宏只有一个比特位通过fcntl将这个数设置进文件结构体。
#include iostream
#include fcntl.h
#include unistd.h
#include cstdio
#include cstringvoid SetNonBlock(int fd)
{int fl fcntl(fd, F_GETFL);//使用F_GETFL将当前的文件描述符的属性取出来(这是一个位图)if (fl 0){std::cerr error string: strerror(errno) error code: errno std::endl;return ;}fcntl(fd, F_SETFL, fl | O_NONBLOCK);//然后再使用F_SETFL将文件描述符设置回去. 设置回去的同时, 加上一个O_NONBLOCK参数
}int main()
{char buffer[64];//SetNonBlock(0);//0表示非阻塞while(true){printf( );fflush(stdout);ssize_t n read(0, buffer, sizeof(buffer) - 1);if(n 0){buffer[n - 1] 0;std::cout echo# buffer std::endl;}else if(n 0){std::cout end file std::endl;break;} else{std::cout read error?? std::endl;break;} }
}不设置成0就是阻塞。read那里不是0而是4当前没有打开文件描述符为4的文件就会read error。加上错误原因 else{std::cerr read error? error string: strerror(errno) error code: errno std::endl;break;} 现在再用上SetNonBlock设置为0那么即使不输入也会出错因为它不阻塞直接去判断没输入也没读取完所以就打印错误语句 那么这里就在每一次循环后sleep一会。只有在输入时才会打印正常语句不输入就一直打印错误语句。非阻塞IO在底层没有数据时就以出错形式返回不过不算正式地出错。
区分一下 else{if(errno EAGAIN || errno EWOULDBLOCK)//也就是错误码为11的两个表示非阻塞式的出错返回{//底层数据没有准备好下次继续检测sleep(1);std::cout data not ready std::endl;continue;}else if(errno EINTR){//这次IO被信号中断需要重新读取continue;}std::cerr read error? error string: strerror(errno) error code: errno std::endl;break;}非阻塞IO在没有得到数据之前可以做别的工作模拟一下
#include iostream
#include unistd.h
#include fcntl.h
#include cstdio
#include cstring
#include vector
#include functionalvoid PrintLog()
{std::cout 这个是一个日志例程 std::endl;
}void OpenMysql()
{std::cout 这个是一个操作数据库的例程 std::endl;
}void CheckNet()
{std::cout 这个是一个检测网络状态的例程 std::endl;
}using func_t std::functionvoid (void);
std::vectorfunc_t funcs;void LoadTask()
{funcs.push_back(PrintLog);funcs.push_back(OpenMysql);funcs.push_back(CheckNet);
}void HandlerAllTask()
{for(const auto func: funcs) func();
}void SetNonBlock(int fd)
{int fl fcntl(fd, F_GETFL);//使用F_GETFL将当前的文件描述符的属性取出来(这是一个位图)if (fl 0){std::cerr error string: strerror(errno) error code: errno std::endl;return ;}fcntl(fd, F_SETFL, fl | O_NONBLOCK);//然后再使用F_SETFL将文件描述符设置回去. 设置回去的同时, 加上一个O_NONBLOCK参数
}int main()
{char buffer[64];SetNonBlock(0);//0表示非阻塞LoadTask();while(true){printf( );fflush(stdout);ssize_t n read(0, buffer, sizeof(buffer) - 1);if(n 0){buffer[n - 1] 0;std::cout echo# buffer std::endl;}else if(n 0){std::cout end file std::endl;break;}else{if(errno EAGAIN || errno EWOULDBLOCK)//也就是错误码为11的两个表示非阻塞式的出错返回{//底层数据没有准备好下次继续检测HandlerAllTask();sleep(1);std::cout data not ready std::endl;continue;}else if(errno EINTR){//这次IO被信号中断需要重新读取continue;}std::cerr read error? error string: strerror(errno) error code: errno std::endl;break;}sleep(3); }
}本篇gitee
结束。 文章转载自: http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.kjmws.cn.gov.cn.kjmws.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.nzhzt.cn.gov.cn.nzhzt.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.zypnt.cn.gov.cn.zypnt.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.wwxg.cn.gov.cn.wwxg.cn http://www.morning.dtlqc.cn.gov.cn.dtlqc.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.nwfxp.cn.gov.cn.nwfxp.cn http://www.morning.knngw.cn.gov.cn.knngw.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.prlgn.cn.gov.cn.prlgn.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.sooong.com.gov.cn.sooong.com http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.nmfxs.cn.gov.cn.nmfxs.cn http://www.morning.nlnmy.cn.gov.cn.nlnmy.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.wklmj.cn.gov.cn.wklmj.cn http://www.morning.gqjwz.cn.gov.cn.gqjwz.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.jmlgk.cn.gov.cn.jmlgk.cn http://www.morning.zrgsg.cn.gov.cn.zrgsg.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.qhmgq.cn.gov.cn.qhmgq.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.nqmhf.cn.gov.cn.nqmhf.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.txtzr.cn.gov.cn.txtzr.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.bylzr.cn.gov.cn.bylzr.cn http://www.morning.ktlfb.cn.gov.cn.ktlfb.cn http://www.morning.rlqwz.cn.gov.cn.rlqwz.cn