K
K
Kirill Batalin2016-12-10 02:45:13
linux
Kirill Batalin, 2016-12-10 02:45:13

Why does an asynchronous read complete earlier than it should?

Hello. I call aio_read, and the signal handler immediately fires. aio_error says the read is complete, aio_return returns 0
i.e. there is no data and the read has already ended

//установка обработчика сигнала
struct sigaction settings;
memset(&settings, 0, sizeof(settings));
settings.sa_flags = SA_SIGINFO;
settings.sa_sigaction = SigHandler;
sigaction(SIGUSR1, &settings, NULL);

//Чтение
memset(&readAio, 0, sizeof(readAio));
readAio.aio_fildes = socket;
readAio.aio_buf = buffer;
readAio.aio_nbytes = BUFFER_SIZE;

sigevent event;
memset(&event, 0, sizeof(event));
event.sigev_notify = SIGEV_SIGNAL;
event.sigev_signo = SIGUSR1;
event.sigev_value.sival_int = socket;
readAio.aio_sigevent = event;

aio_read(&readAio);

What is the reason for this behavior? How to read data from a socket?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Evseev, 2016-12-10
@IlyaEvseev

1) Show the code for the SigHandler function
2) aio_read does not actually read, but tells the system what to do when there is data to read.
After calling it, the program can either do other tasks, or wait until the reading is completed.
Use aio_return , aio_error and aio_suspend to test and wait.
Examples:
- https://software.intel.com/en-us/node/524452 (for Windows, but better than nothing)
- https://gist.github.com/rsms/771059 (EINPROGRESS checks ugly, better use aio_suspend)
Tutorials:
- books.google.com => Embedded Linux System Design and Development => chapter 7.3.8
-https://www3.physnet.uni-hamburg.de/physnet/Tru64-... (for Unix, so there may be minor differences in Linux)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question