Linux中使用select函数

2014-11-24 01:04:15 · 作者: · 浏览: 5

select模型在win32API编程中也很常见,而且和linux中的select函数遵循同样的berkeley标准,所以函数原型相同。select函数原型:


int select( int maxfdp, fd_set * readfds, fd_set * writefds, fd_set * errorfds, struct timeva l * timeout);


Win32:


int select( intnfds, fd_set*readfds, fd_set*writefds, fd_set*exceptfds, const struct timeva l*timeout);


可以看到是一样的。不同点是,win32API中的select函数忽略第一个参数。而linux要求第一个参数设置成(参数2~4某个数组中文件描述符的最大值+1)。


下面是ubuntu10.0中使用select函数的一个例子,流程为:


Ubuntu10.0.4:


源代码文件select.cpp:


#include
#include
#include // waitpid
#include // waitpid
#include // strlen
/*
comment:
pipe is used between two processes on the same computer.
*/
#define TIMES 50
int main(){
int pipefds[2];
if( -1 == pipe( pipefds)){
printf( "Error when create pipes\n");
}else{
int i;
pid_t pid = fork();
if( 0 == pid){// child
printf( "child running\n");
close( pipefds[0]);
for( i = 0; i < TIMES; ++ i){
write( pipefds[1], "iamagoodguy", strlen( "iamagoodguy"));
sleep( 1);
}
}else{
printf( "parent running\n");
char buf[256];
close( pipefds[1]);
fd_set readfdset;// file descriptor set
for( i = 0; i < TIMES; ++ i){
FD_ZERO( & readfdset);
FD_SET( pipefds[0], & readfdset);// add read file descriptor
FD_SET( 0, & readfdset);// add standard input
select( pipefds[0]+1, & readfdset, NULL, NULL, NULL);
if( FD_ISSET( pipefds[0], & readfdset)){
buf[ read( pipefds[0], buf, 256)] = '\0';
printf( "Receive:%s\n", buf);
}
if( FD_ISSET( 0, & readfdset)){
buf[ read( 0, buf, 256)] = '\0';
printf( "Print:%s\n", buf);
}
}
int status;
wait( & status);
}
}
return 0;
}


还需要一个Makefile:


select: select.cpp
g++ $< -o $@


然后在命令行中键入:


make select
./select


运行截图:


Linux中使用select函数


注意点


父进程必须等待子进程结束。如果父进程先于子进程结束,子进程成为死进程。


每次调用select后参数2都会被改变,下次调用select前必须重新设置。