/*lab2.c
*author: zxh
*system propraming lab2
*/
#include <stdio.h>
#include <signal.h>
#include <aio.h>
#include <string.h>
#include <malloc.h>
#include <error.h>
#define BUF_SIZE 4096
int aio_completion_handler(int, siginfo_t*, void *);
int get_file_size(const char *);
int finished = 0;
int filesize = -1;
int timesToRead = -1;
int timesRead = 0;
struct aiocb my_aiocb;
int main(void)
{
printf("\nWelcome to my simple aio!\n");
int fd,ret;
struct sigaction sig_act;
printf("Buffer size is :%d\n", BUF_SIZE);
/* Get the file size */
filesize = get_file_size("./testfile");
printf("The file size is %d\n", filesize);
timesToRead = filesize / BUF_SIZE;
printf("Times to read: %d\n", timesToRead + 1);
/* Open the file, readonly */
if((fd = open("./testfile", O_RDONLY)) < 0){
perror("open error!\n");
}
/* Set up the signal handler */
sigemptyset(&sig_act.sa_mask);
sig_act.sa_flags = SA_SIGINFO;
sig_act.sa_sigaction = aio_completion_handler;
/* Set up the AIO request */
bzero( (char *)&my_aiocb, sizeof(struct aiocb) );
my_aiocb.aio_fildes = fd;
my_aiocb.aio_buf = malloc(BUF_SIZE+1);
my_aiocb.aio_nbytes = BUF_SIZE;
my_aiocb.aio_offset = 0;
/* Link the AIO request with the Signal Handler */
my_aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
my_aiocb.aio_sigevent.sigev_signo = SIGIO;
my_aiocb.aio_sigevent.sigev_value.sival_ptr = &my_aiocb;
/* Map the Signal to the Signal Handler */
ret = sigaction( SIGIO, &sig_act, NULL );
if(ret < 0)
perror("sigaction:\n");
/* Call asynchronous I/O functions */
ret = aio_read(&my_aiocb);
if(ret < 0)
perror("aio_read request:\n");
else
printf("Start asynchronous I/O reading...\n");
/* Continue processing, in this case just sleep */
while(!finished){
printf("sleeping...\n");
sleep(1);
}
/* Asynchronous I/O finished */
if((ret = aio_return (&my_aiocb)) > 0)
{
printf("Asynchronous I/O OK, file size: %d\n", timesToRead * BUF_SIZE + ret);
close(fd);
}
printf("Time to say goodbye!\n\n");
}
/* aio_completion_handler
* deliver the SIGIO signal
* params: signo info context
* ret;
*/
int aio_completion_handler( int signo, siginfo_t *info, void *context )
{
struct aiocb * req;
int ret;
/* Ensure it's our signal */
if (info->si_signo == SIGIO) {
req = (struct aiocb*)info->si_value.sival_ptr;
if(aio_error(req) == 0){
timesRead ++;
printf("read %d times\n", timesRead);
}
if(timesRead > timesToRead){
finished = 1;
return timesRead * BUF_SIZE;
}
else{
my_aiocb.aio_offset += BUF_SIZE;
/* Call asynchronous I/O functions */
ret = aio_read(&my_aiocb);
if(ret < 0)
perror("aio_read request:\n");
}
}
return ret;
}
/* get_file_size
* get the size of a file
* params:file path, const char
* ret: file size, int
* error:-1
*/
int get_file_size(const char *path)
{
int filesize = -1;
struct stat statbuff;
if(stat(path, &statbuff) < 0){
perror("get file size error!\n");
return filesize;
}else{
filesize = statbuff.st_size;
}
return filesize;
}
linux_aio.zip_LINUX下开启AIO_aio glibc_aio 两种方式_linux aio_linux_aio
版权申诉
150 浏览量
2022-09-20
20:51:32
上传
评论
收藏 4KB ZIP 举报
刘良运
- 粉丝: 66
- 资源: 1万+
最新资源
- 基于STM32使用HAL库实现USB组合设备之多路CDC源码+说明文档.zip
- 金融贸易项目springboot
- mybatis动态sqlSQL 映射 XML 文件是所有 sql 语句
- 基于基于STM32的智能家居系统源码+qt上位机源码.zip
- 深圳房地产资源数据报告
- 基于stm32的智能门禁系统源码+设计文档+演示视频.zip
- cef + chromium 完整源码支持h265和h264
- 基于SpringBoot的API管理平台源代码+数据库,以项目的形式管理API文档,可以进行API的编辑、测试、Mock等操作
- protobuf 3.11版本,静态编译
- 2023NOC创客智慧编程赛项真题图形化-选拔赛(有解析)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈