/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);
}
}