fseek 64位 linux,fseek char linux device

/* Makefile */

obj-m += char.o

all:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

/* char device char.c */

// to complie:

// # make

// # insmod char.ko

// # dmesg

// (to get majornumber)

// # mknod /dev/dp0 c majornumber 0

// # ./test

// # dmesg

#include

#include

#include

#include

#include

#include

#include

#include

#include

int char_init (void);

void char_exit (void);

int char_open (struct inode *, struct file *);

int char_release (struct inode *, struct file *);

ssize_t char_read (struct file *, char __user *, size_t, loff_t *);

ssize_t char_write (struct file *, const char __user *, size_t, loff_t *);

loff_t char_llseek (struct file *, loff_t, int);

int char_open (struct inode *inode, struct file *fp)

{

printk ("f_mode=%d f_pos=%dn", (int) fp->f_mode, (int)fp->f_pos);

return 0;

}

int char_release (struct inode *inode, struct file *fp)

{

return 0;

}

ssize_t char_read (struct file *fp, char __user *buff, size_t count, loff_t *fpos)

{

printk ("char_read() fp->f_pos=%dn", (int) fp->f_pos);

return count;

}

ssize_t char_write (struct file *fp, const char __user *buff, size_t count, loff_t *fpos)

{

printk ("char_write() fp->f_pos=%dn", (int) fp->f_pos);

return count;

}

loff_t char_llseek (struct file *fp, loff_t fpos, int whe)

{

printk ("char_llseek() fpos=%dn ", (int) fpos);

fp->f_pos = fpos;

return fpos;

}

struct file_operations char_fops =

{

.owner = THIS_MODULE,

.open = char_open,

.release = char_release,

.read = char_read,

.write = char_write,

.llseek = char_llseek,

.ioctl = NULL,

};

/* the char device */

struct cdev cdev;

int char_init ()

{

dev_t mm;

int mj, mi, ret;

mi = 0;

ret = alloc_chrdev_region (&mm, mi, 1, "dp0");

mj = MAJOR(mm);

cdev_init (&cdev, &char_fops);

ret = cdev_add (&cdev, mm, 1);

if (ret<0)

{

printk ("char: unable to add devicen");

return -1;

}

printk ("char: device added major=%dn", mj);

return 0;

}

void char_exit ()

{

cdev_del(&cdev);

printk ("char: device deletedn");

}

module_init(char_init);

module_exit(char_exit);

/* test program test.c */

#include

#include

int main (int argc, char *argv[])

{

int ret;

FILE *fp;

fp = fopen ("/dev/dp0", "r+");

if (fp == NULL)

{

printf ("open failedn");

return -1;

}

int x;

x= fseek (fp, 1, SEEK_SET);

printf("ret fseek=%dn", x);

fseek (fp, 1, SEEK_SET);

fwrite (" ", 1, 1, fp);

fseek (fp, 2, SEEK_SET);

fread (&ret, 1, 1, fp);

printf ("DONEn");

fclose (fp);

return 0;

}

// this is my dmesg:

f_mode=31 f_pos=0 // this is my fopen

char_llseek() fpos=0 // this is my first fseek but why fpos=0? it should be 1

char_read() fp->f_pos=0 // what's that?

char_llseek() fpos=0 //this is my second fseek but why fpos is 0 again?

char_read() fp->f_pos=0 // I suppose this is my fread fpos=0?

char_llseek() fpos=-4095 // what's that?

char_llseek() fpos=-4095 // What's that too?

char_llseek() fpos=-4095 // What's that too?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值