免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2800 | 回复: 5
打印 上一主题 下一主题

read中的count问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-14 15:53 |只看该作者 |倒序浏览
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main(int argc,char** argv)
{
char *buf=malloc(atoi(argv[2]));
int fn,n,lk;
//printf("sizeof(buf)=%i\n",sizeof(buf));

memset(buf,0,atoi(argv[2]));
if(argc!=4){
    printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
    exit(1);
}
lk=atoi(argv[3]);
if((fn=open(argv[1],O_RDWR))==-1){
        printf("openfilenew:open file %sfailure because %s\n",argv[1],strerror(errno));
        exit(1);
}

switch(lk){
case 1:
{
int nm=0,k,count=atoi(argv[2]);
while(nm<count){
       k=read(fn,buf,count-nm);
       if(k==-1){
           printf("error:%s",strerror(errno));exit(1);}
       nm=nm+k;
};
/*if(n==-1){
        printf("openfilenew:read file %s failure because %s \n",argv[1],strerror(errno));
        free(buf);
        close(fn);
        exit(1);
}
if(n<atoi(argv[2]))
    printf("openfilenwq:alread read %i bytes,< %i\n",atoi(argv[2]));
*/

buf[nm]=0;
printf("openfilenew:we read %i bytes[nm]==%x\n",nm,buf[nm]);
close(fn);
free(buf);
exit(0);
}
case 2:
{
printf("please input %i data of char for writing\n",atoi(argv[2])-1);
int kk=0;

//gets(buf);

fgets((char*)buf,(int)atoi(argv[2]),stdin);
printf("the act input %i bytes from stdin\n",atoi(argv[2])-1);
n=write(fn,buf,atoi(argv[2]));
if(n==-1)
    printf("openfilenew:write to %s failure\n",argv[1]);
if(n<atoi(argv[2]))
    printf("openfilenew:write bytes < the act input bytes%i\n",sizeof(buf));
close(fn);
exit(0);
    break;
}
default:
        printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
        exit(1);
}
free(buf);
return 0;
}


1)上面是测试程序,名字叫test.c.
2)在LDD3的short.c的:
ssize_t do_short_read (struct inode *inode, struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
printk(KERN_ALERT "short: count of do_short_read function = %i\n",count);
....
}
函数中只是想看看count到底等于多少.
3)运行测试程序
    test /dev/short0 10 2(表示写10个字符给/dev/short0,因为有个隐含的换行字符,所以提示输入9个字符)
    (输入9个字符,比如1 2 3 4 5 6 7 8 9 )
    test /dev/short0 6 1(表示从/dev/short0读6个字符)-------这一句可以再执行一遍,看看两次有什么区别.
执行了读6个字符之后,如果 dmesg | grep short ,应该发现short: count of do_short_read function = 6才正确,可是我执行的时候却是3

这个问题困绕了我很长一段时间,各位,请了!!!

[ 本帖最后由 dreamice 于 2009-1-14 16:00 编辑 ]

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
2 [报告]
发表于 2009-01-14 16:13 |只看该作者

回复 #1 whoisliang 的帖子

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main(int argc,char** argv)
{
char *buf=malloc(atoi(argv[2]));
int fn,n,lk;
//printf("sizeof(buf)=%i\n",sizeof(buf));


memset(buf,0,atoi(argv[2]));
if(argc!=4){
&nbsp;&nbsp;&nbsp;&nbsp;printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
&nbsp;&nbsp;&nbsp;&nbsp;exit(1);
}
lk=atoi(argv[3]);
if((fn=open(argv[1],O_RDWR))==-1){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("openfilenewpen file %sfailure because %s\n",argv[1],strerror(errno));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);
}

switch(lk){
case 1:
{
int nm=0,k,count=atoi(argv[2]);
while(nm<count){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k=read(fn,buf,count-nm);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(k==-1){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("error:%s",strerror(errno));exit(1);}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nm=nm+k;
};
/*if(n==-1){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("openfilenew:read file %s failure because %s \n",argv[1],strerror(errno));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(buf);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;close(fn);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);
}
if(n<atoi(argv[2]))
&nbsp;&nbsp;&nbsp;&nbsp;printf("openfilenwq:alread read %i bytes,< %i\n",atoi(argv[2]));
*/

buf[nm]=0;
printf("openfilenew:we read %i bytes[nm]==%x\n",nm,buf[nm]);
close(fn);
free(buf);
exit(0);
}
case 2:
{
printf("please input %i data of char for writing\n",atoi(argv[2])-1);
int kk=0;

//gets(buf);


fgets((char*)buf,(int)atoi(argv[2]),stdin);
printf("the act input %i bytes from stdin\n",atoi(argv[2])-1);
n=write(fn,buf,atoi(argv[2]));
if(n==-1)
&nbsp;&nbsp;&nbsp;&nbsp;printf("openfilenew:write to %s failure\n",argv[1]);
if(n<atoi(argv[2]))
&nbsp;&nbsp;&nbsp;&nbsp;printf("openfilenew:write bytes < the act input bytes%i\n",sizeof(buf));
close(fn);
int cnt = 0;
for (;cnt < atoi(argv[2]); cnt++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\n", buf[cnt]);
exit(0);
&nbsp;&nbsp;&nbsp;&nbsp;break;
}
default:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);
}
free(buf);
return 0;
}



上面是我给你加了两行代码的情况,我这样测试的:
[root@localhost junyong]# ./a.out tt.txt 10 2
please input 9 data of char for writing
1 2 3 4 5 6 7 8 8
the act input 9 bytes from stdin
49
32
50
32
51
32
52
32
53
0
tt.txt里面的结果如下:
1 2 3 4 5^@
[root@localhost junyong]# ./a.out tt.txt 6 1
openfilenew:we read 6 bytes[nm]==0
所以,你好好分析一下你的代码,为什么会这样?你这个输入应该是有问题的。
ps:上面我的输出是用的%d,用%c的话就是1,2,3,4,5……

论坛徽章:
0
3 [报告]
发表于 2009-01-14 18:10 |只看该作者

回复 #2 dreamice 的帖子

刚才写一个进程调度的体会去了,现在看到你的回复,先谢了!

我明白你的意思了.就算我输入时不带空格输入123456789,读6个字符的时候,驱动程序read函数中 count应该等于6才对吧,我没有用C库调用,直接采用系统调用的方式,这是你以前说的,应该不会错.可是结果依然不理解?????????

论坛徽章:
0
4 [报告]
发表于 2009-01-15 14:09 |只看该作者
无人回答吗?
不会这么个简单问题都回答不出来吧?
各位。。。。。。。。。。。。。。。????????????

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
5 [报告]
发表于 2009-01-15 15:17 |只看该作者

回复 #4 whoisliang 的帖子

完全是你的代码写得有问题,细心点啊,大哥:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main(int argc,char** argv)
{
char *buf=malloc(atoi(argv[2]));
int fn,n,lk;
//printf("sizeof(buf)=%i\n",sizeof(buf));


memset(buf,0,atoi(argv[2]));
if(argc!=4){
    printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
    exit(1);
}
lk=atoi(argv[3]);
if((fn=open(argv[1],O_RDWR))==-1){
        printf("openfilenew:open file %sfailure because %s\n",argv[1],strerror(errno));
        exit(1);
}

switch(lk){
case 1:
{
int nm=0,k,count=atoi(argv[2]);
while(nm<count){
       k=read(fn,buf,count-nm);
       if(k==-1){
           printf("error:%s",strerror(errno));exit(1);}
       nm=nm+k;
};
/*if(n==-1){
        printf("openfilenew:read file %s failure because %s \n",argv[1],strerror(errno));
        free(buf);
        close(fn);
        exit(1);
}
if(n<atoi(argv[2]))
    printf("openfilenwq:alread read %i bytes,< %i\n",atoi(argv[2]));
*/

buf[nm]=0;
printf("openfilenew:we read %i bytes[nm]==%x\n",nm,buf[nm]);
int read_cnt;
for(read_cnt = 0; read_cnt < nm; read_cnt++)
        printf("%c\n", buf[read_cnt]);
close(fn);
free(buf);
exit(0);
}
case 2:
{
printf("please input %i data of char for writing\n",atoi(argv[2])-1);
int kk=0;

//gets(buf);


fgets((char*)buf,(int)atoi(argv[2]),stdin);
printf("the act input %i bytes from stdin\n",atoi(argv[2])-1);
n=write(fn,buf,atoi(argv[2]));
if(n==-1)
    printf("openfilenew:write to %s failure\n",argv[1]);
if(n<atoi(argv[2]))
    printf("openfilenew:write bytes < the act input bytes%i\n",sizeof(buf));
close(fn);
int cnt = 0;
for (;cnt < atoi(argv[2]); cnt++)
        printf("%c\n", buf[cnt]);
exit(0);
    break;
}
default:
        printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
        exit(1);
}
free(buf);
return 0;
}


自己好好测试一下吧
[root@localhost junyong]# ./a.out tt.txt 10 2
please input 9 data of char for writing
abcdefg12
the act input 9 bytes from stdin
a
b
c
d
e
f
g
1
2

[root@localhost junyong]# ./a.out tt.txt 9 1
openfilenew:we read 9 bytes[nm]==0
a
b
c
d
e
f
g
1
2

论坛徽章:
0
6 [报告]
发表于 2009-01-20 17:07 |只看该作者

回复 #5 dreamice 的帖子

我不是说读和写的问题,而是说当读时,设备驱动程序的file相应read函数里边count反应出来的问题:
ssize_t do_short_read (struct inode *inode, struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
printk(KERN_ALERT "short: count of do_short_read function = %i\n",count);
....
}

针对测试程序:
case 1:
{
int nm=0,k,count=atoi(argv[2]);
while(nm<count){
       k=read(fn,buf,count-nm);
       if(k==-1){
           printf("error:%s",strerror(errno));exit(1);}
       nm=nm+k;
};
这里面的read的count和驱动程序里面do_short_read的count不一致.

[ 本帖最后由 whoisliang 于 2009-1-20 17:08 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP