select范例

/select server//
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
#include <sys/time.h>
#include <assert.h>
#include <string.h>
//struct timeval
//{
//	time_t tv_sec; 表示秒 
//	long tv_usec;毫秒
//};

//int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeout);
//nfds是文件描述符中最大的加1
int main()
{
	mkfifo("pipe1", 0666);
	mkfifo("pipe2", 0666);
	mkfifo("pipe3", 0666);
	
	int fd1 = open("pipe1", O_RDONLY);
	int fd2 = open("pipe2", O_RDONLY);
	int fd3 = open("pipe3", O_RDONLY);
	
	assert(fd1!=-1 && fd2!=-1 && fd3!=-1);
	
	char buff[128];
	int result;

	fd_set inputs, testfds;//设置要监视的文件描述符数组
	struct timeval timeout;//超时设置

	FD_ZERO(&inputs);
	FD_SET(fd1, &inputs);
	FD_SET(fd2, &inputs);
	FD_SET(fd3, &inputs);

	while(1)
	{
		testfds = inputs;//每次都要对select中的监视文件描述符参数进行重置
		timeout.tv_sec = 2;
		timeout.tv_usec = 500000;//对时间参数进行重置

		result = select(FD_SETSIZE, &testfds, (fd_set *)NULL, (fd_set *)NULL, &timeout);
		//进行监视可读事件,如果有事件发生就返回发生事件的个数
		//FD_SETSIZE的是表示最大数字加1
		cout<<result<<endl;		
		if (result == 0)//超时
		{
			cout<<"timeout\n";
		}
		else if(result == -1)//出错,可以通过判断来鉴别出错具体内容
		{
			perror("error select");
			exit(-1);
		}
		else//有数据可读
		{
			for (int fd=0; fd<FD_SETSIZE; ++fd)//遍历数组
			{ 
				if (FD_ISSET(fd, &testfds))//用文件符与之判断
				{
					int num = read(fd, buff, 128);//由何处而知道该文件描述符的大小是?
					cout<<"from fd"<<fd<<"  buff is:"<<buff<<endl;
					if (strncmp(buff, "exit", 4) == 0)
					{
						exit(0);
					}
				}
			}
		}
	}
	return 0;
}

//每次都要将数据从用户拷贝到内核
//外设的操作都是通过内核来进行的,内核能最早知道哪个文件描述符发生事件
//通过显示pipe来测试多文件描述符的监视
///    select client //
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
#include <sys/time.h>
#include <assert.h>
#include <string.h>

int main()
{
	int fd1 = open("./pipe1", O_WRONLY);
	int fd2 = open("./pipe2", O_WRONLY);
	int fd3 = open("./pipe3", O_WRONLY);
 	assert(fd1!=-1 && fd2!=-1 && fd3!=-1);
	int fd = 0;
	
	char buf[128];
	while(1)
	{
		cout<<"input fd ";	
		scanf("%d", &fd);
		getchar();//整型输入问题
		
		fgets(buf, 128, stdin);
		if(strncmp(buf, "exit", 4)==0){exit(0);}
		
		switch(fd)
		{
			case 0:
			{
				cout<<"bye"<<endl;
				write(fd1,"exit", 128);
				close(fd1);
				close(fd2);
				close(fd3);
				exit(0);
			}
			case 1:
				write(fd1, buf, 128);
				break;
			case 2:
				write(fd2, buf, 128);		
				break;

			case 3:
				write(fd3, buf, 128);			
				break;
			defalute:
				continue;
		}
		memset(buf,0,128);
	}
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值