linux device drive 第六章代码示例-scullpipe的实验(poll和fasync方法的实现)之二
2012-11-23 21:16
447 查看
一 测试程序头文件 scull.h
二 测试文件pipe_test.c
三 测试文件asynctest.c
三 makefile文件
#include <linux/ioctl.h> /* needed for the _IOW etc stuff used later */ /* * Ioctl definitions */ /* Use 'k' as magic number */ #define SCULL_IOC_MAGIC 'k' /* Please use a different 8-bit number in your code */ #define SCULL_P_IOCTSIZE _IO(SCULL_IOC_MAGIC, 0) #define SCULL_P_IOCQSIZE _IO(SCULL_IOC_MAGIC, 1)
二 测试文件pipe_test.c
#include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <stropts.h> #include <linux/poll.h> #include <unistd.h> #include <errno.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include "scull.h" int main() { char buffer1[20]={0}; int pipetest0, pipetest1; int code=21, i=0; struct pollfd poll_list[2]; int retval; if ((pipetest0 = open("/dev/scullpipe0",O_RDONLY)) < 0) { printf("open scullpipe0 error! \n"); exit(1); } printf("open scullpipe0 ! \n"); if ((pipetest1 = open("/dev/scullpipe1",O_RDONLY)) < 0) { printf("open scullpipe1 error! \n"); exit(1); } printf("open scullpipe1 ! \n"); if ( ioctl(pipetest0 , SCULL_P_IOCTSIZE , code ) < 0) { printf("pipetest0 ioctl SCULL_P_IOCTSIZE error! \n"); exit(1); } printf(" SCULL_P_IOCTSIZE : scull_p_buffer0=%d !\n" ,ioctl( pipetest0 , SCULL_P_IOCQSIZE , NULL ) ); if ( ioctl(pipetest1 , SCULL_P_IOCTSIZE , code ) < 0) { printf("pipetest1 ioctl SCULL_P_IOCTSIZE error! \n"); exit(1); } printf(" SCULL_P_IOCTSIZE : scull_p_buffer1=%d !\n" ,ioctl( pipetest1 , SCULL_P_IOCQSIZE , NULL ) ); close(pipetest0); printf("close pipetest0 ! \n"); close(pipetest1); printf("close pipetest1 ! \n"); if ((pipetest0 = open("/dev/scullpipe0",O_RDONLY)) < 0) { printf("reopen scullpipe0 error! \n"); exit(1); } printf("reopen scullpipe0 ! \n"); if ((pipetest1 = open("/dev/scullpipe1",O_RDONLY)) < 0) { printf("reopen scullpipe1 error! \n"); exit(1); } printf("reopen scullpipe1 ! \n"); poll_list[0].fd = pipetest0; poll_list[1].fd = pipetest1; poll_list[0].events = POLLIN|POLLRDNORM; poll_list[1].events = POLLIN|POLLRDNORM; while(1) { retval = poll(poll_list,(unsigned long)2,-1); /* retval 鎬绘槸澶т簬0鎴栦负-1锛屽洜涓烘垜浠湪闃诲涓伐浣?*/ if(retval < 0) { fprintf(stderr,"poll閿欒: %s\n",strerror(errno)); return -1; } if(poll_list[0].revents&(POLLIN|POLLRDNORM)) { if ((code=read(pipetest0 , buffer1 , 20)) != 20) printf("read from pipetest0 error! code=%d\n",code); else printf("read from pipetest0 ok! code=%d \n",code); for(i=0;i<20;i+=5) printf("[%d]=%c [%d]=%c [%d]=%c [%d]=%c [%d]=%c\n",i,buffer1[i],i+1,buffer1[i+1],i+2,buffer1[i+2],i+3,buffer1[i+3],i+4,buffer1[i+4]); } if(poll_list[1].revents&(POLLIN|POLLRDNORM)) { if ((code=read(pipetest1 , buffer1 , 20)) != 20) printf("read from pipetest1 error! code=%d \n",code); else printf("read from pipetest1 ok! code=%d \n",code); for(i=0;i<20;i+=5) printf("[%d]=%c [%d]=%c [%d]=%c [%d]=%c [%d]=%c\n",i,buffer1[i],i+1,buffer1[i+1],i+2,buffer1[i+2],i+3,buffer1[i+3],i+4,buffer1[i+4]); } } close(pipetest0 ); printf("close pipetest0 ! \n"); close(pipetest1 ); printf("close pipetest1 ! \n"); printf("\n"); exit(0); }
三 测试文件asynctest.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <signal.h> #include <fcntl.h> int gotdata=0; void sighandler(int signo) { if (signo==SIGIO) gotdata++; return; } char buffer[21]; int main(int argc, char **argv) { int pipetest0; int count; struct sigaction action; if ((pipetest0 = open("/dev/scullpipe0",O_RDONLY)) < 0) { printf("open scullpipe0 error! \n"); exit(1); } memset(&action, 0, sizeof(action)); action.sa_handler = sighandler; action.sa_flags = 0; sigaction(SIGIO, &action, NULL); fcntl(pipetest0, F_SETOWN, getpid()); fcntl(pipetest0, F_SETFL, fcntl(pipetest0, F_GETFL) | FASYNC); while(1) { /* this only returns if a signal arrives */ sleep(86400); /* one day */ if (!gotdata) continue; count=read(pipetest0, buffer, 21); /* buggy: if avail data is more than 4kbytes... */ write(1,buffer,count); gotdata=0; break; } close(pipetest0 ); printf("close pipetest0 ! \n"); printf("exit !\n"); exit(0); }
三 makefile文件
CROSS_COMPILE =/home/tekkaman/working/crosstool-gcc410-k26222/gcc-4.1.0-glibc-2.3.2/arm-9tdmi-linux-gnu/bin/arm-9tdmi-linux-gnu- CC = $(CROSS_COMPILE)gcc all : pipe_test.o asynctest.o $(CC) -o pipe_test pipe_test.o $(CC) -o asynctest asynctest.o install : cp pipe_test asynctest /home/tekkaman/working/rootfs/tmp/ clean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
相关文章推荐
- linux device drive 第六章代码示例-scullpipe的实验(poll和fasync方法的实现)之一
- linux device drive 第六章代码示例-scullpipe的实验(poll和fasync方法的实现)之三
- linux expect实现登陆远程主机并执行命令示例代码
- js实现的map方法示例代码
- Linux Poll 使用方法和实现
- 2.3 linux实现服务端与多个客户端间通讯-线程Socket-pthread的方法(实验一)
- ★实验 9-2 1. 补全代码,实现以下功能 a) 调用pipe()创建无名管道 b) 调用fork创建一个子进程 c) 在父进程中向管道写入 “helloworld”, 同时打印出PID和写入
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- Linux高级字符设备驱动 poll方法(select多路监控原理与实现)
- java中,父类是抽象类不能通过工厂输出时,如何通过其他类实现输出的示例代码(工厂方法种类(1))
- Vue多种方法实现表头和首列固定的示例代码
- Linux高级字符设备驱动 poll方法(select多路监控原理与实现)
- 如何实现在Windows上运行Linux程序,附示例代码
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- Android实现IP地址输入框的方法示例代码
- 嵌入式 linux下cp带进度条的实现示例代码
- 一键Git实现方法和示例代码
- js实现的map方法示例代码
- 如何实现在Windows上运行Linux程序,附示例代码
- android 指纹识别调用实现方法及示例代码