trigapp.c

来自「RT-LINUX2.2源代码,RT-LINUX是硬实时操作系统,在实时性要求高的」· C语言 代码 · 共 72 行

C
72
字号
#include <stdio.h>#include <errno.h>#include <sys/time.h>#include <sys/types.h>#include <fcntl.h>#include <unistd.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <rtl_fifo.h>#include <math.h>void outf(int fd,double d){	char x[100];	int n = sprintf(x,"%f\n",d);	write(fd,x,n);}int main(void){	fd_set rfds;        struct timeval tv;	int fifofd[2], datafd[2];	int findex;	double d;		if ((fifofd[0] = open("/dev/rtf1", O_RDONLY)) < 0) {		fprintf(stderr, "Error opening /dev/rtf1\n");		exit(1);	}	if ((fifofd[1] = open("/dev/rtf2", O_RDONLY)) < 0) {		fprintf(stderr, "Error opening /dev/rtf2\n");		exit(1);	}	umask(0777);	if ((datafd[0] = creat("sin.data", 0777)) < 0) {		fprintf(stderr, "Error opening sin.data\n");		exit(1);	}	if ((datafd[1] = creat("cos.data", 0777)) < 0) {		fprintf(stderr, "Error opening cos.data\n");		exit(1);	}	/* now start the tasks */	printf("Starting\n");		FD_ZERO(&rfds);		FD_SET(fifofd[0], &rfds);		FD_SET(fifofd[1], &rfds);		tv.tv_sec = 1;		tv.tv_usec = 0;				while(select(FD_SETSIZE, &rfds, NULL, NULL, &tv) >0){			printf("!");			if(FD_ISSET(fifofd[0], &rfds)) findex = 0;			else findex = 1;			read(fifofd[findex], &d, sizeof(double)); 			outf(datafd[findex],d);			}	printf("Done!");	return 0;}

⌨️ 快捷键说明

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