1184 views|0 replies

1140

Posts

0

Resources
The OP
 

Asynchronous IO principles and corresponding functions [Copy link]

What is asynchronous IO?

(1) It can be considered that asynchronous IO is an interrupt response system implemented by software in the operating system.
(2) The working method of asynchronous IO is: our current process registers an asynchronous IO event (use signal to register a signal
SIGIO processing function), and then the current process can handle its own affairs normally. When an asynchronous event occurs, the current process
will receive a SIGIO signal and execute the bound processing function to handle this asynchronous event. In fact, all signals
are a kind of interrupt mechanism implemented by software, so asynchronous IO actually uses the software interrupt mechanism of signals to work
. The workflow is as follows:
1): Set the device file fd to have the function of receiving IO
2): Set the receiving process of asynchronous IO events
3): Register the signal processing function (that is, binding)

Functions involved:
(1) fcntl (F_GETFL, F_SETFL, O_ASYNC, F_SETOWN)
F_GETFL: Get the operation attributes of the device file fd
F_SETFL: Set the operation attributes of the device file fd
O_ASYNC: Used to set fd to have the function of receiving asynchronous IO signals
F_SETOWN: Set the receiving process of asynchronous IO events
(2) signal or sigaction (SIGIO)

Sample code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd .h>
#include <signal.h>


#define FILE "/dev/input/mouse0"

static int fd = -1;

static inline void funcIO(int sig);

int main(void)
{
int flag = -1;
int sig_ret = -1;
int read_ret = -1;
char buf[100] = {0};
struct sigaction sig = {
.sa_handler = funcIO,
};

//Open device file
fd = open(FILE, O_RDONLY);
if (-1 == fd)
{
perror("open error");
exit(-1);
}

//Set the mouse's fd to receive asynchronous IO signals
flag = fcntl(fd, F_GETFL); //Read the fd's attributes
flag |= O_ASYNC; //Add the O_ASYNC attribute to enable it to receive asynchronous IO signals
fcntl(fd, F_SETFL, flag); //Set the attributes

//Set the current process as the receiving process of asynchronous IO events
fcntl(fd, F_SETOWN, getpid());

//Bind asynchronous IO event processing function
sig_ret = sigaction(SIGIO, &sig, NULL); //Indicates not to get the old processing function
if (-1 == sig_ret)
{
perror("sigaction error");
exit(-1);
}

/*
//The keyboard reads in a blocking manner
(0, buf, sizeof(buf)/2);
printf("The keyboard data read: %s.\n", buf);
*/


while (1)
{
memset(buf, 0, sizeof(buf));
read_ret = read(0, buf, sizeof(buf)/2);
if (0 < read_ret)
{
printf("Read keyboard data: %s.\n", buf);
}
}


//Close the file descriptor
close(fd);

return 0;
}

static inline void funcIO(int sig)
{
char buf[100] = {0};

if (SIGIO != sig)
return;
read(fd, buf, sizeof(buf)/2);
printf("Read mouse data: %s.\n", buf);
}

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list