Я звоню aio_read
в новом потоке, где я устанавливаю все поля структуры aiocb
и путь к функции aio_read
. Проблема в том, что функция, которую я установил в качестве обработчика, не вызывается после чтения. Пожалуйста, помогите!
AsyncIO.h:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <aio.h>
#include <signal.h>
#include <fcntl.h>
#include <string>
class AsyncIO
{
public:
virtual void AsyncIOManager();
static void* FileReader(void* params);
int result_file_desc = open("./Result.txt", O_APPEND);
std::string files_to_read[3] = {"./File1.txt", "./File2.txt", "./File3.txt"};
const int files_count = 3;
unsigned char buff_to_read[10000];
struct sigaction read_compleate_signal;
struct sigaction write_compleate_signal;
int readding_file_index = -1;
};
AsyncIO. cpp:
#include "AsyncIO.h"
AsyncIO* asyncIO = new AsyncIO();
void* FileReader(void* params);
void read_handler(int);
void write_handler(int);
void AsyncIO::AsyncIOManager()
{
memset(&asyncIO->read_compleate_signal, 0, sizeof(asyncIO->read_compleate_signal));
asyncIO->read_compleate_signal.sa_flags = SA_SIGINFO;
asyncIO->read_compleate_signal.sa_handler = write_handler;
sigemptyset(&asyncIO->read_compleate_signal.sa_mask);
memset(&asyncIO->write_compleate_signal, 0, sizeof(asyncIO->write_compleate_signal));
asyncIO->write_compleate_signal.sa_flags = SA_SIGINFO;
asyncIO->write_compleate_signal.sa_handler = read_handler;
sigemptyset(&asyncIO->write_compleate_signal.sa_mask);
sigaction(SIGUSR1, &asyncIO->read_compleate_signal, NULL);
sigaction(SIGIO, &asyncIO->write_compleate_signal, NULL);
pthread_t thread;
int status = pthread_create(&thread, NULL, &AsyncIO::FileReader, asyncIO);
sleep(2);
}
void* AsyncIO::FileReader(void* params)
{
int file = open(asyncIO->files_to_read[asyncIO->readding_file_index].c_str(), O_RDWR);
struct aiocb _aiocb;
memset(asyncIO->buff_to_read, 0xaa, 10000);
_aiocb.aio_fildes = file;
_aiocb.aio_buf = asyncIO->buff_to_read;
_aiocb.aio_nbytes = 10000;
_aiocb.aio_offset = 0;
_aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
_aiocb.aio_sigevent.sigev_signo = SIGUSR1;
_aiocb.aio_sigevent.sigev_value.sival_ptr = &asyncIO->read_compleate_signal;
if(aio_read(&_aiocb) == -1)
{
printf("aio_read error\n");
}
sleep(1);
}
void write_handler(int a)
{
printf("write_handle\n");
if (asyncIO->readding_file_index < asyncIO->files_count+1)
{
struct aiocb _aiocb;
memset(asyncIO->buff_to_read, 0xaa, 10000);
memset(&asyncIO->buff_to_read, 0, sizeof(struct aiocb));
_aiocb.aio_fildes = asyncIO->result_file_desc;
_aiocb.aio_buf = asyncIO->buff_to_read;
_aiocb.aio_nbytes = 10000;
_aiocb.aio_lio_opcode = LIO_WRITE;
_aiocb.aio_offset = (intptr_t)-1;
_aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
_aiocb.aio_sigevent.sigev_signo = SIGIO;
_aiocb.aio_sigevent.sigev_value.sival_ptr = &asyncIO->write_compleate_signal;
if(aio_read(&_aiocb) == -1)
{
printf("aio_read error\n");
}
}
}