您的位置:首页 > 运维架构 > Linux

linux device drive 第六章代码示例-scullpipe的实验(poll和fasync方法的实现)之二

2012-11-23 21:16 447 查看
一 测试程序头文件 scull.h

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐