test09.c

来自「minix操作系统最新版本(3.1.1)的源代码」· C语言 代码 · 共 63 行

C
63
字号
/* * Test name: test09.c * * Objetive: The purpose of this test is to make sure that select works * when working with the terminal. * * Description:  This tests wait entry from stdin using select and displays * it again in stdout when it is ready to write (which is always) *  * Jose M. Gomez */#include <sys/types.h>#include <fcntl.h>#include <unistd.h>#include <sys/select.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>void main(void) {	fd_set fds_read, fds_write;	int retval;	char data[1024];	FD_ZERO(&fds_read);	FD_ZERO(&fds_write);	FD_SET(0, &fds_read);		/* stdin */	FD_SET(1, &fds_write);		/* stdout */	while(1) {		printf("Input some data: ");		fflush(stdout);		retval=select(3, &fds_read, NULL, NULL, NULL); 		if (retval < 0) {			fprintf(stderr, "Error while executing select\n");			exit(-1);		}		printf("select retval: %d\n", retval);		if (!FD_ISSET(0, &fds_read)) {			fprintf(stderr, "Error: stdin not ready (?)\n");			exit(-1);		}		printf("gets..\n");		gets(data);		printf("gets done..\n");		if (!strcmp(data, "exit"))			exit(0);		printf("Try to write it back\n");		retval=select(3, NULL, &fds_write, NULL, NULL);		if (retval < 0) {			fprintf(stderr, "Error while executing select\n");			exit(-1);		}		if (!FD_ISSET(1, &fds_write)) {			fprintf(stderr, "Error: stdout not ready (?)\n");			exit(-1);		}		printf("Data: %s\n", data);	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?