快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

go语言和iocp比较 go语言和java比较

[杂烩]Windows IOCP与Linux的epoll机制对比

看到一些资料,说Windows的IOCP后发制人,比Linux的epoll优越很多。那么优势到底在哪里?如何选择Linux或Windows作为服务器呢?

创新互联建站服务项目包括江孜网站建设、江孜网站制作、江孜网页制作以及江孜网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,江孜网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到江孜省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

也看到有资料说,同样在Linux上,使用reactor机制的网络库性能比使用Proactor机制的Asio性能好接近1/5,这个例子可能与题目无关,不过我在文中可能会做一些相关的分析。

系统I/O模型 可分为三类:

IOCP基于非阻塞异步模型,而epoll基于非阻塞同步模型。

参考 Hyunjik Bae, A game programmer since 1995 说的:

参考[4]比较了Windows IOCP和Linux epoll的性能,结论是如果使用Linux,应该使用支持RSS(multi-queue)的NIC,这样可以达到与IOCP类似的性能。

Boost.Asio为了兼容Windows和Linux,在Linux上用epoll和select去模拟proactor模式,影响了它的效率和实现复杂度。其效率不及使用原生I/O机制的其它实现为Reactor模式的网络库。

引用来自参考[3]的话:

[1] Why doesn't Linux have a system like IOCP or Rio does? , 2014.

[2] 两种高性能I/O设计模式(Reactor/Proactor)的比较 - 文章 - 伯乐在线

[3] Practical difference between epoll and Windows IO Completion Ports (IOCP)

[4] Windows IOCP vs Linux EPOLL Performance Comparison

php新手学习路线是怎样的

第一阶段:基础阶段(基础PHP程序员)

重点:把LNMP搞熟练(核心是安装配置基本操作)  目标:能够完成基本的LNMP系统安装,简单配置维护;能够做基本的简单系统的PHP开发;能够在PHP中型系统中支持某个PHP功能模块的开发。

时间:完成本阶段的时间因人而异,有的成长快半年一年就过了,成长慢的两三年也有。

Linux

基本命令、操作、启动、基本服务配置(包括rpm安装文件,各种服务配置等);会写简单的shell脚本和awk/sed 脚本命令等。

Nginx

做到能够安装配置nginx+php,知道基本的nginx核心配置选项,知道 server/fastcgi_pass/access_log 等基础配置,目标是能够让nginx+php_fpm顺利工作。

MySQL

会自己搭建mysql,知道基本的mysql配置选项;知道innodb和myisam的区别,知道针对InnoDB和MyISAM两个引擎的不同配置选项;知道基本的两个引擎的差异和选择上面的区别;能够纯手工编译搭建一个MySQL数据库并且配置好编码等正常稳定运行;核心主旨是能够搭建一个可运行的MySQL数据库。

PHP

基本语法数组、字符串、数据库、XML、Socket、GD/ImageMgk图片处理等等;熟悉各种跟MySQL操作链接的api(mysql/mysqli/PDO),知道各种编码问题的解决;知道常规熟练使用的PHP框架(ThinkPHP、Zendframework、Yii、Yaf等);了解基本MVC的运行机制和为什么这么做,稍微知道不同的PHP框架之间的区别;能够快速学习一个MVC框架。能够知道开发工程中的文件目录组织,有基本的良好的代码结构和风格,能够完成小系统的开发和中型系统中某个模块的开发工作。

前端

如果条件时间允许,可以适当学习下 HTML/CSS/JS 等相关知识,知道什么web标准,div+css的web/wap页面模式,知道HTML5和HTML4的区别;了解一些基本的前端只是和JS框架(jQuery之类的);了解一些基本的JavaScript编程知识;(本项不是必须项,如果有时间,稍微了解一下是可以的,不过不建议作为重点,除非个人有强烈兴趣)。

系统设计

能够完成小型系统的基本设计,包括简单的数据库设计,能够完成基本的:浏览器 - Nginx+PHP - 数据库 架构的设计开发工作;能够支撑每天几十万到数百万流量网站的开发维护工作;

第二阶段:提高阶段 (中级PHP程序员)

重点:提高针对LNMP的技能,能够更全面的对LNMP有熟练的应用。  目标:能够随时随地搭建好LNMP环境,快速完成常规配置;能够追查解决大部分遇到的开发和线上环境的问题;能够独立承担中型系统的构架和开发工作;能够在大型系统中承担某个中型模块的开发工作。

1. Linux

在第一阶段的基础上面,能够流畅的使用Shell脚本来完成很多自动化的工作;awk/sed/perl 也操作的不错,能够完成很多文本处理和数据统计等工作;基本能够安装大部分非特殊的Linux程序(包括各种库、包、第三方依赖等等,比如MongoDB/Redis/Sphinx/Luncene/SVN之类的);了解基本的Linux服务,知道如何查看Linux的性能指标数据,知道基本的Linux下面的问题跟踪等。

2. Nginx

在第一阶段的基础上面,了解复杂一些的Nginx配置;包括 多核配置、events、proxy_pass,sendfile/tcp_*配置,知道超时等相关配置和性能影响;知道nginx除了web server,还能够承担代理服务器、反向静态服务器等配置;知道基本的nginx配置调优;知道如何配置权限、编译一个nginx扩展到nginx;知道基本的nginx运行原理(master/worker机制,epoll),知道为什么nginx性能比apache性能好等知识。

3. MySQL/MongoDB

在第一阶段的基础上面,在MySQL开发方面,掌握很多小技巧,包括常规SQL优化(group by/order by/rand优化等);除了能够搭建MySQL,还能够冷热备份MySQL数据,还知道影响innodb/myisam性能的配置选项(比如key_buffer/query_cache/sort_buffer/innodb_buffer_pool_size/innodb_flush_log_at_trx_commit等),也知道这些选项配置成为多少值合适;另外也了解一些特殊的配置选项,比如  知道如何搭建mysql主从同步的环境,知道各个binlog_format的区别;知道MySQL的性能追查,包括slow_log/explain等,还能够知道基本的索引建立处理等知识;原理方面了解基本的MySQL的架构(Server+存储引擎),知道基本的InnoDB/MyISAM索引存储结构和不同(聚簇索引,B树);知道基本的InnoDB事务处理机制;了解大部分MySQL异常情况的处理方案(或者知道哪儿找到处理方案)。条件允许的情况,建议了解一下NoSQL的代表MongoDB数据库,顺便对比跟MySQL的差别,同事能够在合适的应用场景安全谨慎的使用MongoDB,知道基本的PHP与MongoDB的结合开发。

4. Redis/Memcached

在大部分中型系统里面一定会涉及到缓存处理,所以一定要了解基本的缓存;知道Memcached和Redis的异同和应用场景,能够独立安装 Redis/Memcached,了解Memcahed的一些基本特性和限制,比如最大的value值,知道PHP跟他们的使用结合;Redis了解基本工作原理和使用,了解常规的数据类型,知道什么场景应用什么类型,了解Redis的事务等等。原理部分,能够大概了解Memcached的内存结构(slab机制),redis就了解常用数据类型底层实现存储结构(SDS/链表/SkipList/HashTable)等等,顺便了解一下Redis的事务、RDB、AOF等机制更好。

5. PHP

除了第一阶段的能力,安装配置方面能够随意安装PHP和各种第三方扩展的编译安装配置;了解php-fpm的大部分配置选项和含义(如max_requests/max_children/request_terminate_timeout之类的影响性能的配置),知道mod_php/fastcgi的区别;在PHP方面已经能够熟练各种基础技术,还包括各种深入些的PHP,包括对PHP面向对象的深入理解/SPL/语法层面的特殊特性比如反射之类的;在框架方面已经阅读过最少一个以上常规PHP MVC框架的代码了,知道基本PHP框架内部实现机制和设计思想;在PHP开发中已经能够熟练使用常规的设计模式来应用开发(抽象工厂/单例/观察者/命令链/策略/适配器 等模式);建议开发自己的PHP MVC框架来充分让开发自由化,让自己深入理解MVC模式,也让自己能够在业务项目开发里快速升级;熟悉PHP的各种代码优化方法,熟悉大部分PHP安全方面问题的解决处理;熟悉基本的PHP执行的机制原理(Zend引擎/扩展基本工作机制)。

6. C/C++

开始涉猎一定的C/C++语言,能够写基本的C/C++代码,对基本的C/C++语法熟悉(指针、数组操作、字符串、常规标准API)和数据结构(链表、树、哈希、队列)有一定的熟悉下;对Linux下面的C语言开发有基本的了解概念,会简单的makefile文件编写,能够使用简单的GCC/GDB的程序编译简单调试工作;对基本的网络编程有大概了解。(本项是为了向更高层次打下基础)。

7. 前端

在第一阶段的基础上面,熟悉基本的HTTP协议(协议代码200/300/400/500,基本的HTTP交互头);条件允许,可以在深入写出稍微优雅的HTML+CSS+JavaScript,或者能够大致简单使用某些前端框架(jQuery/YUI/ExtJS/RequireJS/BootStrap之类);如果条件允许,可以深入学习JavaScript编程,比如闭包机制、DOM处理;再深入些可以读读jQuery源码做深入学习。(本项不做重点学习,除非对前端有兴趣)。

8. 系统设计

能够设计大部分中型系统的网站架构、数据库、基本PHP框架选型;性能测试排查处理等;能够完成类似:浏览器 - CDN(Squid) - Nginx+PHP - 缓存 - 数据库 结构网站的基本设计开发维护;能够支撑每天数百万到千万流量基本网站的开发维护工作;

第三阶段:高级阶段 (高级PHP程序员)

重点:除了基本的LNMP程序,还能够在某个方向或领域有深入学习。(纵深维度发展)  目标:除了能够完成基本的PHP业务开发,还能够解决大部分深入复杂的技术问题,并且可以独立设计完成中大型的系统设计和开发工作;自己能够独立hold深入某个技术方向,在这块比较专业。(比如在MySQL、Nginx、PHP、Redis等等任一方向深入研究)

1. Linux

除了第二阶段的能力,在Linux下面除了常规的操作和性能监控跟踪,还能够使用很多高级复杂的命令完成工作(watch/tcpdump/starce/ldd/ar等);在shell脚本方面,已经能够编写比较复杂的shell脚本(超过500行)来协助完成很多包括备份、自动化处理、监控等工作的shell;对awk/sed/perl 等应用已经如火纯青,能够随意操作控制处理文本统计分析各种复杂格式的数据;对Linux内部机制有一些了解,对内核模块加载,启动错误处理等等有个基本的处理;同时对一些其他相关的东西也了解,比如NFS、磁盘管理等等;

2. Nginx

在第二阶段的基础上面,已经能够把Nginx操作的很熟练,能够对Nginx进行更深入的运维工作,比如监控、性能优化,复杂问题处理等等;看个人兴趣,更多方面可以考虑侧重在关于Nginx工作原理部分的深入学习,主要表现在阅读源码开始,比如具体的master/worker工作机制,Nginx内部的事件处理,内存管理等等;同时可以学习Nginx扩展的开发,可以定制一些自己私有的扩展;同时可以对Nginx+Lua有一定程度的了解,看看是否可以结合应用出更好模式;这个阶段的要求是对Nginx原理的深入理解,可以考虑成为Nginx方向的深入专业者。

3. MySQL/MongoDB

在第二阶段的基础上面,在MySQL应用方面,除了之前的基本SQL优化,还能够在完成一些复杂操作,比如大批量数据的导入导出,线上大批量数据的更改表结构或者增删索引字段等等高危操作;除了安装配置,已经能够处理更多复杂的MySQL的问题,比如各种问题的追查,主从同步延迟问题的解决、跨机房同步数据方案、MySQL高可用架构等都有涉及了解;对MySQL应用层面,对MySQL的核心关键技术比较熟悉,比如事务机制(隔离级别、锁等)、对触发器、分区等技术有一定了解和应用;对MySQL性能方面,有包括磁盘优化(SAS迁移到SSD)、服务器优化(内存、服务器本身配置)、除了二阶段的其他核心性能优化选项(innodb_log_buffer_size/back_log/table_open_cache/thread_cache_size/innodb_lock_wait_timeout等)、连接池软件选择应用,对show *(show status/show profile)类的操作语句有深入了解,能够完成大部分的性能问题追查;MySQL备份技术的深入熟悉,包括灾备还原、对Binlog的深入理解,冷热备份,多IDC备份等;在MySQL原理方面,有更多了解,比如对MySQL的工作机制开始阅读部分源码,比如对主从同步(复制)技术的源码学习,或者对某个存储引擎(MyISAM/Innodb/TokuDB)等等的源码学习理解,如果条件允许,可以参考CSV引擎开发自己简单的存储引擎来保存一些数据,增强对MySQL的理解;在这个过程,如果自己有兴趣,也可以考虑往DBA方向发展。MongoDB层面,可以考虑比如说在写少读多的情况开始在线上应用MongoDB,或者是做一些线上的数据分析处理的操作,具体场景可以按照工作来,不过核心是要更好的深入理解RMDBS和NoSQL的不同场景下面的应用,如果条件或者兴趣允许,可以开始深入学习一下MongoDB的工作机制。

4. Redis/Memcached

在第二阶段的基础上面,能够更深入的应用和学习。因为Memcached不是特别复杂,建议可以把源码进行阅读,特别是内存管理部分,方便深入理解;Redis部分,可以多做一些复杂的数据结构的应用(zset来做排行榜排序操作/事务处理用来保证原子性在秒杀类场景应用之类的使用操作);多涉及aof等同步机制的学习应用,设计一个高可用的Redis应用架构和集群;建议可以深入的学习一下Redis的源码,把在第二阶段积累的知识都可以应用上,特别可以阅读一下包括核心事件管理、内存管理、内部核心数据结构等充分学习了解一下。如果兴趣允许,可以成为一个Redis方面非常专业的使用者。

5. PHP

作为基础核心技能,我们在第二阶段的基础上面,需要有更深入的学习和应用。从基本代码应用上面来说,能够解决在PHP开发中遇到95%的问题,了解大部分PHP的技巧;对大部分的PHP框架能够迅速在一天内上手使用,并且了解各个主流PHP框架的优缺点,能够迅速方便项目开发中做技术选型;在配置方面,除了常规第二阶段会的知识,会了解一些比较偏门的配置选项(php auto_prepend_file/auto_append_file),包括扩展中的一些复杂高级配置和原理(比如memcached扩展配置中的memcache.hash_strategy、apc扩展配置中的apc.mmap_file_mask/apc.slam_defense/apc.file_update_protection之类的);对php的工作机制比较了解,包括php-fpm工作机制(比如php-fpm在不同配置机器下面开启进程数量计算以及原理),对zend引擎有基本熟悉(vm/gc/stream处理),阅读过基本的PHP内核源码(或者阅读过相关文章),对PHP内部机制的大部分核心数据结构(基础类型/Array/Object)实现有了解,对于核心基础结构(zval/hashtable/gc)有深入学习了解;能够进行基本的PHP扩展开发,了解一些扩展开发的中高级知识(minit/rinit等),熟悉php跟apache/nginx不同的通信交互方式细节(mod_php/fastcgi);除了开发PHP扩展,可以考虑学习开发Zend扩展,从更底层去了解PHP。

6. C/C++

在第二阶段基础上面,能够在C/C++语言方面有更深入的学习了解,能够完成中小型C/C++系统的开发工作;除了基本第二阶段的基础C/C++语法和数据结构,也能够学习一些特殊数据结构(b-tree/rb-tree/skiplist/lsm-tree/trie-tree等)方便在特殊工作中需求;在系统编程方面,熟悉多进程、多线程编程;多进程情况下面了解大部分多进程之间的通信方式,能够灵活选择通信方式(共享内存/信号量/管道等);多线程编程能够良好的解决锁冲突问题,并且能够进行多线程程序的开发调试工作;同时对网络编程比较熟悉,了解多进程模型/多线程模型/异步网络IO模型的差别和选型,熟悉不同异步网络IO模型的原理和差异(select/poll/epoll/iocp等),并且熟悉常见的异步框架(ACE/ICE/libev/libevent/libuv/Boost.ASIO等)和使用,如果闲暇也可以看看一些国产自己开发的库(比如muduo);同时能够设计好的高并发程序架构(leader-follow/master-worker等);了解大部分C/C++后端Server开发中的问题(内存管理、日志打印、高并发、前后端通信协议、服务监控),知道各个后端服务RPC通信问题(struct/http/thirft/protobuf等);能够更熟络的使用GCC和GDB来开发编译调试程序,在线上程序core掉后能够迅速追查跟踪解决问题;通用模块开发方面,可以积累或者开发一些通用的工具或库(比如异步网络框架、日志库、内存池、线程池等),不过开发后是否应用要谨慎,省的埋坑去追bug。

7. 前端

深入了解HTTP协议(包括各个细致协议特殊协议代码和背后原因,比如302静态文件缓存了,502是nginx后面php挂了之类的);除了之前的前端方面的各种框架应用整合能力,前端方面的学习如果有兴趣可以更深入,表现形式是,可以自己开发一些类似jQuery的前端框架,或者开发一个富文本编辑器之类的比较琐碎考验JavaScript功力。

8. 其他领域语言学习

在基础的PHP/C/C++语言方面有基本积累,建议在当前阶段可以尝试学习不同的编程语言,看个人兴趣爱好,脚本类语言可以学学 Python/Ruby 之类的,函数式编程语言可以试试 Lisp/Haskell/Scala/Erlang 之类的,静态语言可以试试 Java/Golang,数据统计分析可以了解了解R语言,如果想换个视角做后端业务,可以试试 Node.js还有前面提到的跟Nginx结合的Nginx_Lua等。学习不同的语言主要是提升自己的视野和解决问题手段的差异,比如会了解除了进程/线程,还有轻量级协程;比如在跨机器通信场景下面,Erlang的解决方案简单的惊人;比如在不想选择C/C++的情况下,还有类似高效的Erlang/Golang可用等等;主要是提升视野。

9. 其他专业方向学习

在本阶段里面,会除了基本的LNMP技能之外,会考虑一些其他领域知识的学习,这些都是可以的,看个人兴趣和长期的目标方向。目前情况能够选择的领域比较多,比如、云计算(分布式存储、分布式计算、虚拟机等),机器学习(数据挖掘、模式识别等,应用到统计、个性化推荐),自然语言处理(中文分词等),搜索引擎技术、图形图像、语音识别等等。除了这些高大上的,也有很多偏工程方面可以学习的地方,比如高性能系统、移动开发(Android/IOS)、计算机安全、嵌入式系统、硬件等方向。

10. 系统设计

系统设计在第二阶段的基础之上,能够应用掌握的经验技能,设计出比较复杂的中大型系统,能够解决大部分线上的各种复杂系统的问题,完成类似 浏览器 - CDN - 负载均衡 -接入层 - Nginx+PHP - 业务缓存 - 数据库 - 各路复杂后端RPC交互(存储后端、逻辑后端、反作弊后端、外部服务) - 更多后端 酱紫的复杂业务;能够支撑每天数千万到数亿流量网站的正常开发维护工作。

.NET平台下几种SOCKET模型的简要性能供参考

1.Socket + Threads/ThreadPool大概性能:小于1500个连接 实现:Accept一个Socket,就交给一个线程去管理,比较笨,但也比较有效,因为是同步方式,控制起来很方便。高级点的,就是交给一个线程池去管理,线程池由系统自动托管,省去了开销线程的时间。一般小型项目,用这个完全足够,开发也简单。但要注意,如果若干Socket长时间占用线程池中的线程,同时其它连接数又比较多,很容易出现提示说你没有足够的线程供使用。呵呵,让Socket少做点事,少占用时间,换一个快点的CPU是不错的方式。另外,如果有一些比较好的第三方线程池组件,也可以选择使用,比如SmartThreadPool。2.Socket + Select大概性能:大于1500个连接后性能下降 实现:Select是很常用的一种模型。是在阻塞功能中轮询一个或多个Socket,将要处理的Socket放到一个IList中,当Select轮询结束后,然后我们再自己处理这个IList中的Socket。具体的用法可以看一下MSDN。Select的效率并不能说是高的,因为当队列中待处理的Socket比较多的时候,处理最后几个Socket相当于要遍历所有前面的Socket,非常不划算的.3.Socket + Asynchronous大概性能:约7500个客户端连接 实现:BeginXXXX,EndXXXX,再熟悉不过了吧。异步Socket归根到底,还是用的线程池技术,用线程池来处理异步IO。这就又引出个问题,.NET的线程池又是用的什么实现方式,以前看过有人说,.NET的线程池是用的完成端口来实现的,我不知道这样的说法是不是正确,从查到的资料中也没有办法确认(希望这点有朋友可以告诉我)。异步Socket对于程序的处理流程来说比同步复杂了许多,异步回调函数的控制不如同步方式那样直观。但有一点我想应该是要注意的,就是回调函数应该轻装上阵,不应该处理过多的事务,对传递数据的处理,应该交给其它线程进行处理。 4.IOCP(完成端口)大概性能:约20000~50000个客户端连接 实现:现在.NET下有一些伪IOCP,大家可以去搜索一下,还没有见过开放出来的用这些伪IOCP来实现的SOCKET例子。我说的20000~50000个客户端连接,是指在C++下开发的情况,这样的情况下,需要用到的基本技术还包括内存池、查询算法等。伪IOCP能实现多少最大连接,没有资料可以查,如果有朋友知道,可以讨论一下。另外上 面提到的许多数据,是从一些资料上摘抄下来的,我没有自己试过,仅仅是拿出来和大家讨论一下。1. Introduction - Native Win32 IOCPI/O Completion Ports (IOCP) supported on Microsoft Windows platforms has two facets. It first allows I/O handles like file handles, socket handles, etc., to be associated with a completion port. Any async I/O completion event related to the I/O handle associated with the IOCP will get queued onto this completion port. This allows threads to wait on the IOCP for any completion events. The second facet is that we can create a I/O completion port that is not associated with any I/O handle. In this case, the IOCP is purely used as a mechanism for efficiently providing a thread-safe waitable queue technique. This technique is interesting and efficient. Using this technique, a pool of a few threads can achieve good scalability and performance for an application. Here is a small example. For instance, if you are implementing a HTTP server application, then you need to do the following mundane tasks apart from the protocol implementation:Create a client connection listen socket. Once we get the client connection, use the client socket to communicate with the client to and fro. You can implement it by creating one dedicated thread per client connection that can continuously communicate with the client to and fro. But this technique quickly becomes a tremendous overhead on the system, and will reduce the performance of the system as the number of simultaneous active client connections increase. This is because, threads are costly resources, and thread switching is the major performance bottle neck especially when there are more number of threads.The best way to solve this is to use an IOCP with a pool of threads that can work with multiple client connections simultaneously. This can be achieved using some simple steps...Create a client connection listen socket. Once we get the client connection, post an IOCP read message on the socket to an IOCP. One of the threads waiting for completion events on this IOCP will receive the first read message for the client. It immediately posts another read onto the same IOCP and continues processing the read message it got. Once processing the read message is completed, it again waits on the IOCP for another event. This technique will allow a small pool of threads to efficiently handle communication with hundreds of client connections simultaneously. Moreover, this is a proven technique for developing scalable server side applications on Windows platforms.The above is a simplified description of using IOCP in multithreaded systems. There are some good in-depth articles on this topic in CodeProject and the Internet. Do a bit of Googling on words like IO Completion Ports, IOCP, etc., and you will be able to find good articles.2. Introduction - Managed IOCPManaged IOCP is a small .NET class library that provides the second facet of Native Win32 IOCP. This class library can be used both by C# and VB.NET applications. I chose the name Managed IOCP to keep the readers more close to the techniques they are used to with native Win32 IOCP. As the name highlights, Managed IOCP is implemented using pure .NET managed classes and pure .NET synchronization primitives. At its core, it provides a thread-safe object queuing and waitable object receive mechanism. Apart from that, it provides a lot more features. Here is what it does:Multiple Managed IOCP instances per process. Registration of multiple threads per Managed IOCP instance. Dispatching System.Object types to a threadsafe queue maintained by each Managed IOCP instance. Waitable multi-thread safe retrieval of objects from the Managed IOCP instance queue by all the threads registered for that particular Managed IOCP instance. Ability to restrict the number of concurrent active threads processing the queued objects related to a particular Managed IOCP instance. Policy based replaceable/customizable approach for choosing a registered thread to process the next available queued object. Ability to pause the Managed IOCP processing. Internally, pauses processing of queued objects by registered threads. Also, by default, disallows enqueuing new objects (can be changed). Run the Managed IOCP instance. Internally re-starts the processing of queued objects by registered threads. Also allows enqueuing new objects (if it is disallowed previously). Modify the max. allowed concurrent threads at runtime. Provides easy accessibility to Managed IOCP instance runtime properties like... Number of active concurrent threads. Number of objects left in queue. Number of allowed concurrent threads. Running status. Safe and controlled closing of a Managed IOCP instance. 2.1. Managed IOCP in Job/Task Oriented Business ProcessesManaged IOCP can be used in other scenarios apart from the sample that I mentioned in the introduction to native Win32 IOCP. It can be used in process oriented server side business applications. For instance, if you have a business process ( _not_ a Win32 process) with a sequence of tasks that will be executed by several clients, you will have to execute several instances of the business process, one for each client in parallel. As mentioned in my introduction to native Win32 IOCP, you can achieve this by spawning one dedicated thread per business process instance. But the system will quickly run out of resources, and the system/application performance will come down as more instances are created. Using Managed IOCP, you can achieve the same sequential execution of multiple business process instances, but with fewer threads. This can be done by dispatching each task in a business process instance as an object to Managed IOCP. It will be picked up by one of the waiting threads and will be executed. After completing the execution, the thread will dispatch the next task in the business process instance to the same Managed IOCP, which will be picked up by another waiting thread. This is a continuous cycle. The advantage is that you will be able to achieve the sequential execution goal of a business process, as only one waiting thread can receive a dispatched object, and at the same time keep the system resource utilization to required levels. Also, the system and business process execution performance will increase as there are few threads executing multiple parallel business processes.3. Using Managed IOCP in .NET applicationsMultithreaded systems are complex in the context that most problems will show up in real time production scenarios. To limit the possibility of such surprises while using Managed IOCP, I created a test application using which several aspects of the Managed IOCP library can be tested. Nevertheless, I look forward for any suggestions/corrections/inputs to improve this library and its demo application.Before getting into the demo application, below is the sequence of steps that an application would typically perform while using the Managed IOCP library:Create an instance of the ManagedIOCP class: 2. using Sonic.Net;ManagedIOCP mIOCP = new ManagedIOCP();The ManagedIOCP constructor takes one argument, concurrentThreads. This is an integer that specifies how many maximum concurrent active threads are allowed to process objects queued onto this instance of ManagedIOCP. I used a no argument constructor, which defaults to a maximum of one concurrent active thread.From a thread that needs to wait on objects queued onto the ManagedIOCP instance, call the Register() method on the ManagedIOCP instance. This will return an instance of the IOCPHandle class. This is like native Win32 IOCP handle, using which the registered thread can wait on the arrival of objects onto the ManagedIOCP instance. This thread can use the Wait() method on the IOCPHandle object. The Wait() will indefinitely wait until it grabs an object queued onto the ManagedIOCP instance to which the calling thread is registered. It either comes out with an object, or an exception in case the ManagedIOCP instance is stopped (we will cover this later). 4. IOCPHandle hIOCP = mIOCP.Register();5. while(true)6. {7. try8. {9. object obj = hIOCP.Wait();10. // Process the object11. 12. }13. catch(ManagedIOCPException e)14. {15. break;16. }17. catch(Exception e)18. {19. break;20. }}Any thread (one that is registered with the ManagedIOCP instance and any non-registered thread) that has access to the ManagedIOCP instance can dispatch (Enqueue) objects to it. These objects are picked up by waiting threads that are registered with the ManagedIOCP instance onto which objects are being dispatched. 22. string str = "Test string";mIOCP.Dispatch(str);When a thread decides not to wait for objects any more, it should un-register with the ManagedIOCP instance. mIOCP.UnRegister();Once the application is done with an instance of ManagedIOCP, it should call the Close() method on it. This will release any threads waiting on this instance of ManagedIOCP, clears internal resources, and resets the internal data members, thus providing a controlled and safe closure of a ManagedIOCP instance. mIOCP.Close();There are certain useful statistics that are exposed as properties in the ManagedIOCP class. You can use them for fine tuning the application during runtime.// Current number of threads that are // concurrently processing the objects queued // onto this instance of Managed IOCP // (This is readonly property) int activeThreads = mIOCP.ActiveThreads;// Max number of concurrent threads // allowed to process objects queued onto this // instance of Managed IOCP (This is a read/write property) int concurThreads = mIOCP.ConcurrentThreads;// Current count of objects queued onto this Managed IOCP instance. // NOTE: This value may change very quickly // as multiple concurrent threads might // be processing objects from this instance of Managed IOCP queue. // So _do not_ depend on this value // for logical operations. Use this only for // monitoring purpose (Status reporting, etc.) // and during cleanup processes // (like not exiting main thread untill the queued object becomes 0, // i.e. no more objects to be processed, etc) // (This is readonly property) int qCount = mIOCP.QueuedObjectCount;// Number of threads that are registered with this instance of Managed IOCP // (This is readonly property) int regThreadCount = mIOCP.RegisteredThreads;3.1. Advanced usageFollowing are the advanced features of Managed IOCP that need to be used carefully.Managed IOCP execution can be paused at runtime. When a Managed IOCP instance is paused, all the threads registered with this instance of Managed IOCP will stop processing the queued objects. Also, if the 'EnqueueOnPause' property of the ManagedIOCP instance is false (by default, it is false), then no thread will be able to dispatch new objects onto the Managed IOCP instance queue. Calling Dispatch on the ManagedIOCP instance will throw an exception in the Pause state. If the 'EnqueueOnPause' property is set to true, then threads can dispatch objects onto the queue, but you need to be careful while setting this property to true, as this will increase the number of pending objects in the queue, thus occupying more memory. Also, when the Managed IOCP instance is re-started, all the registered threads will suddenly start processing a huge number of objects thus creating greater hikes in the system resource utilization.mIOCP.Pause();Once paused, the ManagedIOCP instance can be re-started using the Run method.mIOCP.Run();The running status of the Managed IOCP instance can be obtained using the IsRunning property:bool bIsRunning = mIOCP.IsRunning;You can retrieve the System.Threading.Thread object of the thread associated with the IOCPHandle instance, from its property named 'OwningThread'.3.2. Demo ApplicationI provided two demo applications with similar logic. The first is implemented using Managed IOCP, the other using native Win32 IOCP. These two demo applications perform the following steps:Create a global static ManagedIOCP instance or native Win32 IOCP. Create five threads. Each thread will dispatch one integer value at a time to the ManagedIOCP instance or native Win32 IOCP until the specified number of objects are completed. Start (creates a new set of five threads) and stop (closes the running threads) the object processing. The Sonic.Net (ManagedIOCP) demo application additionally demonstrates the following features of Managed IOCP that are unavailable in the Win32 IOCP:Pause and continue object processing during runtime. Change concurrent threads at runtime. Statistics like, Active Threads, Maximum Concurrent threads, Queued Objects Count and Running Status of Managed IOCP. Below is the image showing both the demo applications after their first cycle of object processing:Demo application resultsAs you can see in the above figure, Managed IOCP gives the same speed (slightly even better) as native Win32 IOCP. The goal of these two demo applications is _not_ to compare the speed or features of Win32 IOCP with that of Managed IOCP, but rather to highlight that Managed IOCP provides all the advantages of native Win32 IOCP (with additional features) but in a purely managed environment.I tested these two demo applications on a single processor CPU and a dual processor CPU. The results are almost similar, in the sense the Managed IOCP is performing as good as (sometimes performing better than) native Win32 IOCP.3.3. Source and demo application filesBelow are the details of the files included in the article's Zip file:Sonic.Net (folder) - I named this class library as Sonic.Net (Sonic stands for speed). The namespace is also specified as Sonic.Net. All the classes that I described in this article are defined within this namespace. The folder hierarchy is described below: 2. Sonic.Net3. |4. -- Assemblies5. |6. -- Solution Files7. |8. -- Sonic.Net9. |10. -- Sonic.Net Console Demo 11. |-- Sonic.Net Demo ApplicationThe Assemblies folder contains the Sonic.Net.dll (contains the ObjectPool, Queue, ManagedIOCP, IOCPHandle, and ThreadPool classes), Sonic.Net Demo Application.exe (demo application showing the usage of ManagedIOCP and IOCPHandle classes), and Sonic.Net Console Demo.exe (console demo application showing the usage of ThreadPool and ObjectPool classes).The Solution Files folder contains the VS.NET 2003 solution file for the Sonic.Net assembly project, Sonic.Net demo application WinForms project, and Sonic.Net console demo project.The Sonic.Net folder contains the Sonic.Net assembly source code.The Sonic.Net Console Demo folder contains the Sonic.Net console demo application source code. This demo shows the usage of the Managed IOCP ThreadPool, which is explained in my Managed I/O Completion Ports - Part 2 article. This demo uses a file that will be read by the ThreadPool threads. Please change the file path to a valid one on your system. The code below shows the portion in the code to change. This code is in the ManagedIOCPConsoleDemo.cs file.public static void ReadData(){ StreamReader sr = File.OpenText(@"C:\aditya\downloads\lgslides.pdf"); string st = sr.ReadToEnd(); st = null; sr.Close(); Thread.Sleep(100);}The Sonic.Net Demo Application folder contains the Sonic.Net demo application source code.Win32IOCPDemo (folder) - This folder contains the WinForms based demo application for demonstrating Win32 IOCP usage using PInvoke. When compiled, the Win32IOCPDemo.exe will be created in the Win32IOCPDemo\bin\debug or Win32IOCPDemo\bin\Release folder based on the current build configuration you selected. The default build configuration is set to Release mode. 4. Inside Managed IOCPThis section discusses the how and why part of the core logic that is used to implement Managed IOCP.4.1. Waiting and retrieving objects in Managed IOCPManaged IOCP provides a thread safe object dispatch and retrieval mechanism. This could have been achieved by a simple synchronized queue. But with synchronized queue, when a thread (thread-A) dispatches (enqueues) an object onto the queue, for another thread (thread-B) to retrieve that object, it has to continuously monitor the queue. This technique is inefficient as thread-B will be continuously monitoring the queue for arrival of objects, irrespective of whether the objects are present in the queue. This leads to heavy CPU utilization and thread switching in the application when multiple threads are monitoring the same queue, thus degrading the performance of the system.Managed IOCP deals with this situation by attaching an auto reset event to each thread that wants to monitor the queue for objects and retrieve them. This is why any thread that wants to wait on a Managed IOCP queue and retrieve objects from it has to register with the Managed IOCP instance using its 'Register' method. The registered threads wait for the object arrival and retrieve them using the 'Wait' method of the IOCPHandle instance. The IOCPHandle instance contains an AutResetEvent that will be set by the Managed IOCP instance when any thread dispatches an object onto its queue. There is an interesting problem in this technique. Let us say that there are three threads, thread-A dispatching the objects, and thread-B and thread-C waiting on object arrival and retrieving them. Now, say if thread-A dispatches 10 objects in its slice of CPU time. Managed IOCP will set the AutoResetEvent of thread-B and thread-C, thus informing them of the new object arrival. Since it is an event, it does not have an indication of how many times it has been set. So if thread-B and thread-C just wake up on the event set and retrieve one object each from the queue and again waits on the event, there would be 8 more objects left over in the queue unattended. Also, this mechanism would waste the CPU slice given to thread-B and thread-C as they are trying to go into waiting mode after processing a single object from the Managed IOCP queue.So in Managed IOCP, when thread-B and thread-C call the 'Wait' method on their respective IOCPHandle instances, the method first tries to retrieve an object from the Managed IOCP instance queue before waiting on its event. If it was able to successfully retrieve the object, it does

初学服务器知识,该选择什么书籍?

windows平台可以看看《Windows Server 2003服务器架设与管理》。

作为推荐,这里我主要还是推荐自己仔细研读过的书,而我读过的书中某些可能已经很老了,甚至内容已经过时了,因此,这只算是抛砖引玉,大家可以选择类似的更新、更权威的书籍学习。

基础知识主要包括:语言。这里之所以拿出来说,是因为某些语言可能导致你使用完全不同的思路来设计服务器。一般来说,C/C++、Java、C# 之类可以归为一类,而 Go、Erlang 和他们区别很显著。关于语言的经典书籍太多,这里就不说了。

网络。想了解协议底层实现,可以看《TCP/IP 详解 卷2》。上层开发时,Windows 下可以看《Network Programming for Microsoft Windows》,Linux 下可以直接看看官方相关的 API 文档。Windows 下 IOCP,Linux 下 epoll(其他类 Unix 下的技术类似)是需要掌握的。实际开发中,网络库可能自己实现,也可以基于某些开源库开发,所以,无论是出于学习还是使用目的,都可以研究一下开源库,例如 libuv、libevent、boost ASIO。

操作系统相关。类 Unix 系统比较好的书籍就是《Advanced Programming in the UNIX Environment》了,Windows 下则可以看看《Windows核心编程》。系统相关的最核心的东西可能就是线程、进程,以及相关同步的内容了,看看《Multithreading applications in Win32》挺不错(包括类 Unix 系统下的开发者)

数据库。主要看你选择 SQL 还是 NOSQL。基本上搞搞 mysql、redis 不会错。

一些思想。光会各种技术是不能写出好的代码的,可能还需要学习一些更抽象的知识,所谓的一些”哲学“或者开发的指导思想。比较推荐的是《UNIX 编程艺术》,还有一本书《精益思想》,这本书和编程完全无直接关系。

《网络游戏核心技术与实战》日本人写的网络游戏服务器端开发的书, 结合游戏开发讲到了服务器端的方方面面, 但是相对的可能不够深入. 适合入门, 对游戏和技术有更好的理解. 然后就可以针对特定的技术领域, 比如网络, 数据存储等找专门的书来看了. 单纯从技术角度而言, 基础的东西并没有太多的不同. 只是网络游戏服务器端的应用领域相对狭窄, 所以市面上专门的书籍并不多。


网页标题:go语言和iocp比较 go语言和java比较
文章网址:http://6mz.cn/article/ddgoggi.html

其他资讯