神奇的linux:同时监听多个IO

本文介绍了如何在Win32API编程中利用select函数实现并发I/O操作,通过创建子进程和管道,实现实时数据交换与用户交互。详细解释了函数原型差异、关键代码实现及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Ubuntu:

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

Win32:

int select( int nfds, fd_set*readfds, fd_set*writefds, fd_set*exceptfds, const struct timeval*timeout);

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


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

  1. 创建子进程
  2. 子进程通过管道定时发送数据给父进程
  3. 父进程通过select同时监听管道数据和用户输入
  4. 父进程等待子进程退出
Ubuntu10.0.4:
源代码文件select.cpp:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>// waitpid
#include <sys/types.h>// waitpid
#include <string.h>// 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

运行截图:


注意点
  1. 父进程必须等待子进程结束。如果父进程先于子进程结束,子进程成为死进程。
  2. 每次调用select后参数2都会被改变,下次调用select前必须重新设置。

iamagoodguy :D

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值