23,116
社区成员
发帖
与我相关
我的任务
分享
static ssize_t uart_read(struct file *filp, char __user *buf, size_t size,loff_t *ppos)
{
int ret = 0;
struct my_dev *dev = filp->private_data; /*获得设备结构体指针*/
if (NULL == dev)
{
printk(KERN_ERR "dev is NULL\n");
return -ENXIO;
}
/*
* 读取函数假若获取不到信号量,若为不可中断读取,该函数一直不返回,若为可中断函数该函数
* 会立即返回,若系统调用为慢速系统调用,则根据实际需求返回后会重启系统调用。
* 第一个线程到这里来执行的时候,它首先获得了信号量,进入后续的代码并从磁盘读取数据。
* 之后的线程陆续到这里,全部卡在这里,它们一直在等待信号量,并没有尝试读取磁盘。
* 至于你说的死锁,我觉得好像不会出现。
* 另外进入读取函数内部可能会出现睡眠,因为使用DMA读取数据的时候是CPU是等待状态。
*/
if (down_interruptible(&dev->sem))
{
return -ERESTARTSYS;
}
/*add your code at here*/
up(&dev->sem);
return ret;
}
这是我的理解,打错无需笑话
/*
* asynctest.c: use async notification to read stdin
*
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2001 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files. The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates. No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*/
#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[4096];
int main(int argc, char **argv)
{
int count;
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = sighandler;
action.sa_flags = 0;
sigaction(SIGIO, &action, NULL);
fcntl(STDIN_FILENO, F_SETOWN, getpid());
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | FASYNC);
while(1) {
/* this only returns if a signal arrives */
sleep(86400); /* one day */
if (!gotdata)
continue;
count=read(0, buffer, 4096);
/* buggy: if avail data is more than 4kbytes... */
write(1,buffer,count);
gotdata=0;
}
}
另外给你一个aio_read的例子
#include <sys/types.h>
#include <aio.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int SIZE_TO_READ = 100;
int main()
{
// open the file
int file = open("blah.txt", O_RDONLY, 0);
if (file == -1)
{
perror("Unable to open file!");
return 1;
}
// create the buffer
char* buffer = new char[SIZE_TO_READ];
// create the control block structure
aiocb cb;
memset(&cb, 0, sizeof(aiocb));
cb.aio_nbytes = SIZE_TO_READ;
cb.aio_fildes = file;
cb.aio_offset = 0;
cb.aio_buf = buffer;
// read!
if (aio_read(&cb) == -1)
{
cout << "Unable to create request!" << endl;
close(file);
}
cout << "Request enqueued!" << endl;
// wait until the request has finished
while(aio_error(&cb) == EINPROGRESS)
{
cout << "Working..." << endl;
}
// success?
int numBytes = aio_return(&cb);
if (numBytes != -1)
{
cout << "Success!readed " << numBytes << " bytes" << endl;
cout << buffer << endl;
}
else
{
cout << "Error!" << endl;
}
// now clean up
delete[] buffer;
close(file);
return 0;
}
void* pfunc(void *p)
{
inputfile.read(100000000);
.... ....
}
for(int i=0;i<300;i++)
{ ... ....
pthread_create(,,pfunc,..);
}