我正在尝试使用字符驱动程序实现FIFO。然而,在写入设备时,它似乎无法正常工作。它似乎并没有结束循环。任何帮助或链接表示赞赏。我从很多方面获得了帮助,所以当前的代码很多东西都不应该像现在这样混乱。
static ssize_t dev_write(struct file *filp, const char *buff, size_t len, loff_t *off) {
int mode;
int ind;
ssize_t count = -ENOMEM;
printk(KERN_ALERT "to be written : %s\n", buff);
mode = iminor(filp->f_dentry->d_inode);
printk(KERN_ALERT "Device minor : %d\n", mode);
if ((mode == 1) || (mode ==3))
return -EINVAL;
if (mode == 0){
count = 0;
ind = 0;
if (buff[ind] == NULL) {
return -ENOMEM;
}
printk(KERN_ALERT "Write position1 : %d\n", writePos1);
while(ind<=len) { //loop untill we have something to writer
if (down_interruptible(&buffer1_e)) { //taking flag first isn't right because that won't allow other guyto give access to our turn.
printk(KERN_ALERT "buffer1 flag didn't work\t %d", buffer1_e.count);
return -ERESTARTSYS;
}
else {
if (down_interruptible(&flag1)){
up(&buffer1_e); //must because we couldn't write it properly
return -EINVAL;
}
else {
queue1[writePos1] = buff[ind];
printk(KERN_ALERT "Write %d %c\n",ind,queue1[writePos1]);
if (writePos1 == 9){
writePos1 = 0;
}
else
writePos1++;
count++;
}
up(&flag1);
}
up(&buffer1_f);
off += count;
ind++;
}
printk(KERN_ALERT "Write position1 now: %d\t and count%d\n", writePos1,count);
return count-1;
}
答案 0 :(得分:2)
我刚刚编写了自己的模块而我怀疑你的问题是调用dev_write的进程希望dev_write返回写入的字节数。如果你没有返回正确的数字(我看到你正在返回count - 1),dev_write会一次又一次地被调用。
dev_read我发现它是相似的 - 直到它返回0,进程将重复调用它 - 期望有更多字符被检索(这是有意义的)。
我已经编写/修改了一个更简单的模块,该模块说明了将模块用作字符缓冲区(抱歉,它写得很匆匆)。它应该允许您回显一个字符串,并在您捕获或以其他方式读取该字符串时返回该字符串。当您make run
时会证明这一点。我相信你将能够轻松修改它成为FIFO。
对于崩溃内核或其他问题,我不承担任何责任,而且无论如何都应该使用VM。
很长,所以我的github:
git clone https://github.com/n-hutton/tempRepo
cd tempRepo
make
make run
答案 1 :(得分:0)
最后我纠正了完整的代码。这是非常不成熟,不合逻辑的少数逻辑仍然有效,我想这样做。 @Nathan Hutton帮助改善了它并使其发挥作用。我为此感谢他。虽然有很多东西我仍然有一个主要的疑问,如果你可以在内核日志中跟踪它,每当你用一个设备写入时都会添加一个额外的换行符(echo&#34; test&#34; | cat&gt; ;的/ dev / FIFO0) 要正确运行它,您还需要创建4个主设备号为240的辅助设备,次要设备为0,1,2,3。 mknod可以用作: &#34; mknod / dev / fifo(0,1,2,3)c 240(0,1,2,3)-m 777&#34; // 0,1,2,3一次可以修改777以获得粒度。最后工作代码:
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/time.h>
#include <linux/semaphore.h>
#define DEVNO 240
#define DEVNAME "fifo"
MODULE_LICENSE("GPL");
DECLARE_WAIT_QUEUE_HEAD(writing1);
//static short writeFlag1=0;
static DEFINE_SEMAPHORE(flag1);
static DEFINE_SEMAPHORE(flag2);
static struct semaphore buffer1_f;
static struct semaphore buffer2_f;
static struct semaphore buffer1_e;
static struct semaphore buffer2_e;
DECLARE_WAIT_QUEUE_HEAD(writing2);
//static short writeFlag2=0;
static char queue1[10]={0};
static short readPos1=0;
static short writePos1=0;
//static short qsize1=0;
static char queue2[10]={0};
static short readPos2=0;
static short writePos2=0;
//static short qsize2=0;
static int times=0;
static int dev_open(struct inode *,struct file *);
static int dev_rls(struct inode *,struct file *);
static ssize_t dev_read(struct file *,char *,size_t,loff_t *);
static ssize_t dev_write(struct file *,const char *,size_t,loff_t *);
static struct file_operations fops={
.read=dev_read,
.write=dev_write,
.open=dev_open,
.release=dev_rls,
};
int init_module(void){
unsigned int devno = 240;
char *devname = "fifo";
int t;
sema_init(&buffer1_f,0);
sema_init(&buffer2_f,0);
sema_init(&buffer1_e,10);
sema_init(&buffer2_e,10);
memset(queue1,0,10);
memset(queue2,0,10);
t=register_chrdev(devno,devname,&fops);
if(t<0) printk(KERN_ALERT "device reg failed. \n");
else printk(KERN_ALERT "Device registered. \n");
return t;
}
void cleanup_module(void) {
unregister_chrdev(240,"fifo");
printk(KERN_ALERT "Device has been removed");
}
static int dev_open(struct inode *inod, struct file *fil){
times++;
printk(KERN_ALERT "Device opened %d times\n",times);
return 0;
}
static ssize_t dev_read(struct file *filep, char *buff, size_t len, loff_t *off) {
int mode = iminor((filep->f_dentry->d_inode));
short count;
printk(KERN_ALERT "Device minor when read : %d\n", mode);
if ((mode == 0) || (mode ==2))
return -EINVAL;
else if (mode == 1){
count = 0;
printk(KERN_ALERT "Read position1 when read: %d\n", readPos1);
while(len) { //loop untill we have something to write or empty buffer
if (readPos1==writePos1){
printk(KERN_ALERT "Returning chars put to buffer: %d\n", count);
return count;
}
if (down_interruptible(&buffer1_f)) {
printk(KERN_ALERT "flag1 didn't work");
return -ERESTARTSYS;
}
else {
if (down_interruptible(&flag1)){
return -EINVAL;
}
else {
printk(KERN_ALERT "Read %c\n",queue1[readPos1]);
put_user(queue1[readPos1],buff++);
if (writePos1==-1) writePos1=readPos1;
if (readPos1 == 9) readPos1 = 0;
else readPos1++;
count++;
}
up(&flag1);
}
up(&buffer1_e);
}
printk(KERN_ALERT "Read position1 now: %d\t and count%d\n", readPos1,count);
return count;
}
else if (mode == 3){
count = 0;
printk(KERN_ALERT "Read position2 when read: %d\n", readPos2);
while(len) { //loop untill we have something to write or empty buffer
if (readPos2==writePos2){
printk(KERN_ALERT "Returning chars put to buffer: %d\n", count);
return count;
}
if (down_interruptible(&buffer2_f)) {
printk(KERN_ALERT "flag2 didn't work");
return -ERESTARTSYS;
}
else {
if (down_interruptible(&flag2)){
return -EINVAL;
}
else {
printk(KERN_ALERT "Read %c\n",queue2[readPos2]);
put_user(queue2[readPos2],buff++);
if (writePos2==-1) writePos2=readPos2;
if (readPos2 == 9) readPos2 = 0;
else readPos2++;
count++;
}
up(&flag2);
}
up(&buffer2_e);
}
printk(KERN_ALERT "Read position2 now: %d\t and count%d\n", readPos2,count);
return count;
}
else {
printk(KERN_ALERT "Not correct mode\n");
return -1;
}
}
static char Message[100] = "Initial message\n";
static ssize_t dev_write(struct file *filp, const char *buff, size_t len, loff_t *off) {
int mode;
int ind;
ssize_t count = -ENOMEM;
int i;
//Let's copy the message onto our stack so we can be clear what we are getting
for (i = 0; i < 99 && i < len; i++){
char getChar;
get_user(getChar, buff + i);
Message[i] = getChar;
}
Message[i] = '\0';
printk(KERN_ALERT "to be written : %s\n", Message);
mode = iminor(filp->f_dentry->d_inode);
printk(KERN_ALERT "Device minor : %d\n", mode);
if ((mode == 1) || (mode ==3))
return -EINVAL;
else if (mode == 0){
count = 0;
ind = 0;
if (( buff == NULL) || (*buff == 0)) {
return -ENOMEM;
}
printk(KERN_ALERT "Write position1 : %d\n", writePos1);
while(ind<len) { //loop untill we have something to writer
if (down_interruptible(&buffer1_e)) { //taking flag first isn't right because that won't allow other guyto give access to our turn.
printk(KERN_ALERT "buffer1 flag didn't work\t %d", buffer1_e.count);
return -ERESTARTSYS;
}
else {
if (down_interruptible(&flag1)){
up(&buffer1_e); //must because we couldn't write it properly
return -EINVAL;
}
else {
queue1[writePos1] = buff[ind];
printk(KERN_ALERT "Write ind:%d writepos:%d readpos;%d char:%c\tascii%d\n",ind,writePos1,readPos1,queue1[writePos1],(int)queue1[writePos1]);
if (readPos1==((writePos1+1)%10)) {
writePos1=-1;
}
else if (writePos1 == 9){
writePos1 = 0;
}
else
writePos1++;
count++;
}
printk(KERN_ALERT "writepos:%d",writePos1);
up(&flag1);
}
up(&buffer1_f);
off += count;
ind++;
}
printk(KERN_ALERT "Write position1 now: %d\t and count%d\n", writePos1,(int)count);
printk(KERN_ALERT "Note: our allowable buffer length was %d\n", (int)len);
return count;
}
else if (mode == 2){
count = 0;
ind = 0;
if (( buff == NULL) || (*buff == 0)) {
return -ENOMEM;
}
printk(KERN_ALERT "Write position2 : %d\n", writePos2);
while(ind<len) { //loop untill we have something to writer
if (down_interruptible(&buffer2_e)) { //taking flag first isn't right because that won't allow other guyto give access to our turn.
printk(KERN_ALERT "buffer2 flag didn't work\t %d", buffer2_e.count);
return -ERESTARTSYS;
}
else {
if (down_interruptible(&flag2)){
up(&buffer2_e); //must because we couldn't write it properly
return -EINVAL;
}
else {
queue2[writePos2] = buff[ind];
printk(KERN_ALERT "Write ind:%d writepos2:%d readpos2;%d char:%c\tascii%d\n",ind,writePos2,readPos2,queue2[writePos2],(int)queue2[writePos2]);
if (readPos2==((writePos2+1)%10)) {
writePos2=-1;
}
else if (writePos2 == 9){
writePos2 = 0;
}
else
writePos2++;
count++;
}
printk(KERN_ALERT "writepos:%d",writePos2);
up(&flag2);
}
up(&buffer2_f);
off += count;
ind++;
}
printk(KERN_ALERT "Write position2 now: %d\t and count%d\n", writePos2,(int)count);
printk(KERN_ALERT "Note: our allowable buffer length was %d\n", (int)len);
return count;
}
else {
printk(KERN_ALERT "This meant wrong device minor accessed\n");
return -1;
}
}
static int dev_rls(struct inode *inod, struct file *fil) {
printk(KERN_ALERT "Device is closed\n");
return 0;
}