epoll系统调用

epoll的水平触发(LT)和边缘触发模式(ET)详解 详解epoll的LT水平触发和ET边缘触发模式的工作原理 阅读详情

        epoll是Linux系统特有的I/O复用函数。它和select和poll有很大的区别,首先它使用的是一组函数来完成任务而不是一个函数;其次,epoll是将用户关心的文件描述符的事件放在内核中的一个事件表中,从而无须像select和poll每次重复传入文件描述符或者事件集,但是epoll需要一个额外的文件描述符用来唯一标识内核中的事件表。epoll针对的是同一时刻成千上万的文件描述符需要监听的情况,毕竟像select和poll那种轮询的方式来监听文件描述符的话,效率是极其低下的。

epoll有两种模式,一种是默认工作模式LT模式,另外一种是高效工作模式ET模式。

LT模式:某一个文件描述符上一旦有数据就绪,就提醒应用程序,一次没有处理完或者未处理的话,会继续提醒,直到处理完。

ET模式:某一个文件描述符上一旦有数据,就提醒应用程序一次,数据没有处理或者没有处理完全,下一轮epoll也不会再提醒,除非是第二波的数据到了,再提醒;因此epoll在ET模式下,数据就绪后只提醒应用程序一次。

先来说一下LT模式,不论是LT模式还是ET模式都会使用以下三个函数实现操作:

int epoll_create(int size);//创建内核事件表;size的作用只是告诉内核事件表的大小,其返回值是用来唯一标识该事件表的文件描述符,即下面函数的第一个参数epfd。

int epoll_ctl(int epfd,int op,int fd,struct epoll_event*event);//添加文件描述符&移除文件描述符;第二个参数是指定操作类型,一般有三种操作:

EPOLL_CTL_ADD: 往事件表中注册fd上的事件

EPOLL_CTL_DEL :删除fd上注册的事件

EPOLL_CTL_MOD:修改fd上的注册事件

event指定事件类型,其本身是一个结构体:

struct epoll_event
{   
    unsigned int event;
    epoll_data_t data;
 };

int epoll_wait(int epfd,struct epoll_event*events,int max,int timeout);//检查就绪文件描述符,返回给应用程序,只返回就绪的文件描述符。该函数返回成功时,返回就绪文件描述符的个数,失败则返回-1,max指定监听的最大个数,timeout是超时设定,单位是毫秒。

下面利用这些函数写一个简单的服务器,感受一些效果。

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<unistd.h>
#include<sys/select.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<sys/epoll.h>

#define MAX 10
int sock_create();

void epoll_add(int epfd,int fd)
{
	struct epoll_event ev;
	ev.events = EPOLLIN;
	ev.data.fd = fd;
	if(epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&ev) == -1)
	{
		perror("epoll_ctl error");
	}
	
}
void epoll_del(int epfd,int fd)
{
	if(epoll_ctl(epfd,EPOLL_CTL_DEL,fd,NULL) == -1)
	{
		perror("epoll_ctl error");
	}
}

int main()
{
	int sockfd = sock_create();

	int epfd = epoll_create(MAX);
	assert(epfd != -1);
	
	epoll_add(epfd,sockfd);

	struct epoll_event fds[MAX];

	while(1)
	{
		int n = epoll_wait(epfd,fds,MAX,5000);
		if(n == -1)
		{
			perror("epoll_wait error");
			continue;
		}
		if(n == 0)
		{
			printf("time out\n");
			continue;
		}
		else
		{
			int i = 0;
			for(;i<n;i++)
			{
				if(fds[i].events & EPOLLIN)
				{
					if(fds[i].data.fd == sockfd)
					{
						struct sockaddr_in caddr;
						int len = sizeof(caddr);
						int c = accept(fds[i].data.fd,(struct sockaddr*)&caddr,&len);
						assert(c != -1);
						epoll_add(epfd,c);
						continue;
					}
					else
					{
						char buff[128]={0};
						if(recv(fds[i].data.fd,buff,1,0)>0)
						{
							printf("read:%s\n",buff);
							memset(&buff,0,128);
							send(fds[i].data.fd,"ok",2,0);
							sleep(2);
							continue;
						}
						else
						{
							printf("%d client over!\n",fds[i].data.fd);
							int fd_ = fds[i].data.fd;
							epoll_del(epfd,fds[i].data.fd);
							close(fd_);
							continue;
						}
					}
				}
			}
		}
	}
}

int sock_create()
{
	int sockfd = socket(AF_INET,SOCK_STREAM,0);
	assert(sockfd != -1);

	struct sockaddr_in saddr;
	memset(&saddr,0,sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(6000);
	saddr.sin_addr.s_addr = inet_addr("192.168.31.17");

	int res = bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
	assert(res != -1);

	listen(sockfd,5);
	return sockfd;
}
另外再写一个通用的客户端向该服务器发送数据,客户端由用户从键盘接收数据,发送到服务器,服务器接收数据并打印这些数据,并发送确认数据"ok",表示已接收到来自客户端的数据。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<assert.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<pthread.h>

int main()
{
	int sockfd = socket(AF_INET,SOCK_STREAM,0);
	assert(sockfd != -1);
	
	struct sockaddr_in saddr;
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(6000);
	saddr.sin_addr.s_addr = inet_addr("192.168.31.17");
	
	int res = connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
	assert(res != -1);
	
	while(1)
	{
		printf("input:\n");
		char buff[128]={0};
		fgets(buff,128,stdin);
		if(strncmp(buff,"end",3) == 0)
		{
			break;
		}
		send(sockfd,buff,strlen(buff),0);
		memset(buff,0,128);
		recv(sockfd,buff,127,0);
		printf("buff = %s\n",buff);
	}
	close(sockfd);
}
这里只说明一次未处理完来自客户端的数据,服务器的处理方式,例如服务器一次只接收一个字符。


测试用例结果显示,服务端对于来自客户端的数据,分六次读取完毕,并且向客户端发送了一个确认信息"ok",这证明了前面我说的epoll的LT模式的特点是对的,并且如果客户端在服务端未读完数据时就关闭连接,服务端会依次读完数据并打印客户端结束连接的提醒。


隔了几秒后:


以上就是epoll LT模式,接下来说ET模式:

       ET模式是epoll的高效工作模式,前面说了ET模式的特点,但是也有个问题:那么一次未处理完的数据,下次读到的还是有效的吗?若是,谁知道下次是什么时候,难道我就为了处理完第一次的数据我就得多发n次消息,才能处理?

开玩笑,若是这么理解的话,epollET模式的作用还有个喵啊!在这里我们能想到的是,我为何不一次就把它处理完,省的麻烦,若是这样的话,那跟使用多线程、多进程处理这类问题还有个毛区别,万一没有数据,阻塞了咋办,IO复用函数实现的服务器只有一个主线程啊,咩。所有引入了一个新的概念“非阻塞的套接字”,这样就保证一次读完数据而不会阻塞服务端。那下面验证一下呗。下面是ET模式的源码:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<unistd.h>
#include<sys/select.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<sys/epoll.h>
#include<fcntl.h>

#define MAX 10
int sock_create();

void setnonblock(int fd)//设置非阻塞文件描述符
{
	int oldfl = fcntl(fd,F_GETFL);
	int newfl = oldfl | O_NONBLOCK;
	if(fcntl(fd,F_SETFL,newfl) == -1)
	{
		perror("fcntl error");
	}
}


void epoll_add(int epfd,int fd)
{
	struct epoll_event ev;
	ev.events = EPOLLIN|EPOLLET;
	ev.data.fd = fd;
	if(epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&ev) == -1)
	{
		perror("epoll_ctl error");
	}
	setnonblock(fd);//将fd设置为非阻塞文件描述符
}
void epoll_del(int epfd,int fd)
{
	if(epoll_ctl(epfd,EPOLL_CTL_DEL,fd,NULL) == -1)
	{
		perror("epoll_ctl error");
	}
}

int main()
{
	int sockfd = sock_create();

	int epfd = epoll_create(MAX);
	assert(epfd != -1);
	
	epoll_add(epfd,sockfd);

	struct epoll_event fds[MAX];

	while(1)
	{
		int n = epoll_wait(epfd,fds,MAX,5000);
		if(n == -1)
		{
			perror("epoll_wait error");
			continue;
		}
		if(n == 0)
		{
			printf("time out\n");
			continue;
		}
		else
		{
			int i = 0;
			for(;i<n;i++)
			{
				if(fds[i].events & EPOLLIN)
				{
					if(fds[i].data.fd == sockfd)
					{
						struct sockaddr_in caddr;
						int len = sizeof(caddr);
						int c = accept(fds[i].data.fd,(struct sockaddr*)&caddr,&len);
						assert(c != -1);
						epoll_add(epfd,c);
						continue;
					}
					else
					{
						while(1)//这里跟LT模式有了区别了
						{
							char buff[128]={0};
							int num = recv(fds[i].data.fd,buff,1,0);
							if(num == -1)
							{
								send(fds[i].data.fd,"ok",2,0);
								break;
							}
							else if(num == 0)
							{
								int fd = fds[i].data.fd;
								epoll_del(epfd,fds[i].data.fd);
								close(fd);
							}
							else
							{
								printf("read(%d):%s\n",fds[i].data.fd,buff);
							}
						}
					}
				}
			}
		}
	}
}

int sock_create()
{
	int sockfd = socket(AF_INET,SOCK_STREAM,0);
	assert(sockfd != -1);

	struct sockaddr_in saddr;
	memset(&saddr,0,sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(6000);
	saddr.sin_addr.s_addr = inet_addr("192.168.31.17");

	int res = bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
	assert(res != -1);

	listen(sockfd,5);
	return sockfd;
}

看起来跟前面的LT没什么区别,但是效率已经高了很多了,如果是默认的文件描述符的话,你最多也只能读到一个'h'就阻塞了,总的来说,epoll的ET模式是综合了多线程多进程读数据的方式,又包含select和poll那样处理文件描述符的方式,及两家之长,在Linux环境下,已经是一种很高效的方法了。

好了,以上就是我的理解,也许太基础,但会随着项目的深入,我对epoll的理解会更深一个层次。

杀青!!!

epoll的LT与ET模式以及阻塞和非阻塞 阅读详情

相关推荐

Linux网络编程(epoll的ET模式和LT模式)

本篇文章主要来讲解epoll的ET模式和LT模式,epoll中有两种模式可以选择一种是ET模式(边缘触发模式),另一种是LT模式(水平触发模式)本篇文章就讲解到这里。

m0_49476241的博客 1447

epoll的LT模式(水平触发)和ET模式(边沿触发)

epoll的触发模式是个引发讨论非常多的话题,网络上这方面总结的文章也很多,首先从名字上就不是很统一,LT模式常被称为水平触发、电平触发、条件触发,而ET模式常被称为边缘触发、边沿触发等,这些都是从英文翻译过来的,只不过翻译的时候有些差异,LT全称 level-triggered,ET全称 edge-triggered ...

AlbertS Home of Technology 1万+

在Docker中集成GPU监控体系

先上效果图 等文档整理出来再更

tamako0v0的博客 2477

认识epoll模型的两种工作方式 以及 ET模式必须设为非阻塞的原因

了解EPOLL模型中的ET模式以及使用ET模式的两个要求

challenglistic的博客 3889

eda、gnm、anm究竟是个啥?

eda:主成分分析 gnm:高斯网络模型 anm:各向异性网络模型

药研猿的博客 1252

QCM6490平台DDR测试:从QDUTT配置到眼图分析的实战指南

本文详细介绍了QCM6490平台DDR测试的全流程,从QDUTT工具安装配置到眼图分析,提供了实战操作指南和常见问题解决方案。通过具体案例分享,帮助工程师掌握DDR测试技巧,确保内存稳定性,特别适用于物联网和边缘计算设备的开发调试。

weixin_33910191的博客 469

linux 系统调用epoll_create epoll_ctl epoll_wait

epoll是Linux内核为处理大批量句柄而作了改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。 epoll主要涉及epoll_create epoll_ctl epoll_wait三个基本系统调用,及增加功能的epoll_create1,epoll_pwait   epoll相对poll

永无止境 4538

新手必看!一步步教您如何获取可用的D音Cookie(为什么我复制的D音Cookie无法正常使用?)

新手必看!一步步教您如何获取可用的抖音Cookie(为什么我复制的抖音Cookie无法正常使用,无法下载无水印视频?)

XiaoqiangClub的博客 1505

Linux内核epoll系统调用以及内核实现

一、epoll基本知识一、epoll基本知识epoll 是 Linux 系统下实现 IO 多路复用的关键技术,用于网络编程中高效管理大量文件描述符(fd)。它能监听多个 fd 的事件(如读取、写入事件),当特定事件发生时,内核会主动通知应用程序处理,无需像传统 select/poll 那样轮询所有 fd,还支持非阻塞监听,极大提升 IO 处理效率。二、应用层api。

m0_74186706的博客 1065

12027.linux flash驱动之IS25LP01G/IS25WWP01G

一页:512字节 --> 扇区 4096字节 --> 块 32k,64k, 层层递进关系。

写给自己的笔记 2296

基于Python的FTA协定内容爬虫实战:从数据抓取到智能分析

本文将详细介绍如何使用Python构建一个高效的FTA(自由贸易协定)内容爬虫,涵盖最新技术如Playwright、异步IO、智能解析等。通过本教程,您将学会如何从各类政府网站和国际组织平台抓取结构化FTA数据,并对其进行清洗、存储和分析。文章包含完整的代码实现和最佳实践指南,适合中高级Python开发者学习网络爬虫开发。

2201_76125261的博客 999

Linux内核学习之 -- epoll()一族系统调用分析笔记

linux 4.19epoll()也是一种I/O多路复用的技术,但是完全不同于select()/poll()。更加高效,高效的原因其他博客也都提到了,这篇笔记主要是从源码的角度来分析一下实现过程。作为自己的学习笔记,分析都在代码注释中,后续回顾的时候看注释好一点。对于epoll()的分析建立在《Linux内核学习之 – 系统调用open()和write()的实现笔记》 + 《Linux内核学习之 – 系统调用poll()分析笔记》的基础上,因为有很多通用的步骤和环节。

xi_xix_i的博客 1417

Windows 11 安装 JDK 25:下载、安装、配置环境变量、验证

本文详细介绍了在 Windows 11 系统上安装 JDK 25 的完整流程,包括从 Oracle 官网下载压缩包、解压至自定义目录、配置 JAVA_HOME 与 Path 环境变量,并验证安装是否成功。文章推荐使用 ZIP 压缩包而非 EXE 安装程序,以实现绿色便携、多版本共存和灵活管理。最后通过一个使用 Java 25 新特性的示例程序,验证 JDK 的编译与运行环境。

寒水馨 · 轻量级技术文档 · hanshuixin.org 7742

epoll系统调用及内核实现

epoll系统调用及内核实现

qq_53111905的博客 1096

手把手教你学Simulink——基于传统电网建模与稳定性分析的场景实例:基于同步发电机的单机无穷大系统建模与稳定性分析

本文详细介绍了使用MATLAB/Simulink进行单机无穷大系统(SMIB)建模与稳定性分析的方法。教程从系统结构设计入手,包含同步发电机、输电线路、无穷大母线等核心模块的建模步骤和参数配置示例。重点讲解了七个关键步骤:项目创建、模块配置、控制系统集成、负载添加(可选)以及仿真运行与结果分析,并提供了具体的MATLAB代码示例。最后通过性能评估指标(功角差、电压跌落、频率偏差)分析系统稳定性,为电力系统动态特性研究提供了实用指导,强调准确参数设置对仿真结果的重要性。

MHD0815的博客 310

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

IO复用之epoll系统调用

IO复用的第三个系统调用就是epoll。 epoll并不是由一个函数来实现,而是一组函数。 一.epoll()函数 **I.**epoll函数的创建: #include&lt;sys/epoll.h&gt; int epoll_create(int size); 唯一参数size只是给内核一个提示,告诉它需要多大的事件表; **II.**epoll内核事件表的操作: #inc...

Zkr吖頭~的博客 361

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

epoll系列系统调用

需要包含的头文件#include 1. int epoll_create(int size): 在内核新建一个事件表(这是与select和poll不同的地方),返回这个事件表的文件描述符,这个文件描述符将会作为epoll系统调用的第一个参数。另外size参数并不起 作用,只是给内核一个提示,它的事件表需要多大。 2.int epoll_ctl(int epfd,int op,int fd,

secret 615

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

epoll原理以及系统调用案例分析

一、epoll原理一、epoll原理二、epoll系统调用使用c库封装的3个epoll系统调用,具体源码如下:此系统调用对文件描述符epfd引用的epoll实例执行控制操作,它要求操作op对目标文件描述符fd执行。op参数有效值为:EPOLL_CTL_ADD/EPOLL_CTL_MOD/EPOLL_CTL_DEL.等待 epoll 文件描述符上的 I/O 事件。epfd 引用的 epoll 实例上的事件,事件所指向的存储区域将包含供调用者使用的事件。

m0_74186706的博客 1432

epoll_wait系统调用及示例

epoll_wait是Linux系统中用于高效I/O多路复用的核心函数,用于等待epoll实例中的文件描述符就绪事件。其原型为int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout),参数包括epoll实例描述符、事件数组、最大事件数和超时时间(-1阻塞/0非阻塞/正整数毫秒)。函数返回就绪事件数量,0表示超时,-1表示错误。示例代码演示了基本用法、错误处理、信号中断和事件处理等场景,展示了如何创建epo

悟空看八戒烤熟了没的专栏c)4Upqt4SeVk 843

Linux网络编程(三)IO复用三 epoll系统调用

epoll是Linux特有的I/O复用函数。它在实现和使用上与poll有很大差异。

Lyajpunov的博客 1325

epoll系统调用原理

epoll系统调用原理

峰爸的博客 975

IO复用--epoll系统调用

epoll系统调用 epoll是Linux特有的I/O复用函数。它在实现和使用上与select、 poll有很大差异。首先,epoll使用一组函数来完成任务,而不是单个函数。其次,epoll把用户关心的文件描述符上的事件放在内核里的一个事件表中,从而无须像select和poll那样每次调用都要重复传入文件描述符集或事件集。但 epoll 需要使用一个额外的文件描述符,来唯一标识内核中的这个事件表。...

weixin_kab777的博客 270

linux wait 系统调用,epoll_wait 系统调用

epoll_wait, epoll_pwait - wait for an I/O event on an epoll filedescriptor以上两个系统调用等待epoll fd上的IO 事件#include int epoll_wait(int epfd, struct epoll_event *events,int maxevents, int timeout);int epo...

weixin_29351351的博客 433

linux下epoll系统调用实现I/O复用

/**************************  *      epoll系统调用  *************************/    //穿件内核事件表,返回指向被创建表的文件描述符  //作为其它epoll函数的第一个参数  #include  int epoll_create(int size)    //epoll_ctl实现对内核事件表的操作  in

RichardGao的专栏 723

epoll_ctl系统调用及示例

epoll_ctl是Linux系统中用于控制epoll实例的系统调用,可对文件描述符进行添加、修改或删除操作。其函数原型为int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event),其中op参数指定操作类型(EPOLL_CTL_ADD/MOD/DEL)。该调用常用于高性能I/O多路复用场景,如服务器程序中管理多个socket连接。示例代码演示了添加、修改、删除文件描述符等操作,并展示了错误处理机制和epoll_event结构体的详细用

悟空看八戒烤熟了没的专栏c)4Upqt4SeVk 382
上一篇: C++实现带头结点单链表
下一篇: C++继承
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值