Synchronous, synchronous, and asynchronous operations

Source: Internet
Author: User

The concepts "synchronous" and "Asynchronous" emphasize whether I/O operations need to wait for a certain time before returning, while "synchronous" emphasizes what events must occur. For example, four write operations are as follows:

  1. "Synchronization" + "synchronization": The operation will return after the data is flushed into the disk. For example, the file is opened in o_sync mode;
  2. "Synchronous" + "Asynchronous": Although the operation will return when the request is inserted into the queue, this operation will ensure that the data is flushed into the disk.
  3. "Asynchronization" + "synchronization": data is returned only when it is written to the kernel buffer, but this operation only affects the kernel buffer. This is a common method;
  4. "Asynchronization" + "Asynchronization": the insert Request queue will be returned immediately.
Why do I/O operations need to be asynchronous?
  1. If you do not want to be blocked during the operation, can you think that the I/O operation overhead is 0?
  2. Separate the insert queue, I/O commit kernel, and receive operation completion notifications.
A structure that is indispensable for AIO is aiocb. The specific definition is as follows:
struct aiocb{
int aio_fildes; /* File desriptor. */
int aio_lio_opcode; /* Operation to be performed. */
int aio_reqprio; /* Request priority offset. */
volatile void *aio_buf; /* Location of buffer. */
size_t aio_nbytes; /* Length of transfer. */
struct sigevent aio_sigevent; /* Signal number and value. */

/* Internal members. */
struct aiocb *__next_prio;
int __abs_prio;
int __policy;
int __error_code;
__ssize_t __return_value;
#ifndef __USE_FILE_OFFSET64
__off_t aio_offset; /* File offset. */
char __pad[sizeof (__off64_t) - sizeof (__off_t)];
#else
__off64_t aio_offset; /* File offset. */
#endif
char __unused[32];
};
The following is an informal example (which does not reflect the characteristics of AIO, but just shows how to program and what should be noted ):

Int main (){
Int FD, RET;
Struct aiocb my_aiocb;
Bzero (char *) & my_aiocb, sizeof (struct aiocb ));

FD = open ("test", o_rdonly );
If (FD <0 ){
Printf ("failed to open the file! \ N ");
} Else {
Printf ("file opened successfully! \ N ");
}

My_aiocb.aio_buf = malloc (bufsize + 1 );
If (my_aiocb.aio_buf <= 0 ){
Printf ("failed to allocate cache! \ N ");
} Else {
Printf ("cache allocation successful! \ N ");
}

My_aiocb.aio_fildes = FD;
My_aiocb.aio_nbytes = bufsize;
My_aiocb.aio_offset = 0;

Ret = aio_read (& my_aiocb );
If (Ret <0 ){
Printf ("Asynchronous reading failed! \ N ");
} Else {
Printf ("Asynchronous reading successful! \ N ");
}

While (aio_error (& my_aiocb) = einprogress );

If (ret = aio_return (& my_aiocb)> 0 ){
Printf ("data reaches the user buffer! \ N ");
} Else {
Printf ("read failed! \ N ");
}
Return 0;
}
It should be noted that bzero should be called to clear unnecessary bits, otherwise, the request queue will fail to be inserted (maybe during implementation, each flag bit will be checked and the legitimacy of each flag bit will be checked and whether there is a conflict between the signs, similar to do_fork ?). Einprogress should include the header file errno. h. In the program, you can use the suspend function to implement the pending operation. If one of the waiting arrays is complete, the pending state ends. The following is a simple example:

Int main (){
Int ret;
Struct aiocb AIO, * PTR;
PTR = & AIO;
Bzero (char *) & AIO, sizeof (AIO ));
AIO. aio_buf = malloc (bufsize + 1 );
AIO. aio_fildes = stdin_fileno;
AIO. aio_nbytes = bufsize;
AIO. aio_offset = 0;

Ret = aio_read (& AIO );

Ret = aio_suspend (& PTR, 1, null );
Printf ("the suspension ends. The current status is: % d \ n", aio_error (& AIO ));

// While (aio_error (& AIO) = einprogress );
If (ret = aio_return (& AIO)> 0 ){
Printf ("% s", (char *) AIO. aio_buf );
}
Return 0;
}
When the signal is used as the AIO request notification, a data structure is sigaction, which controls the action of the process when it receives the signal:

struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
}
You can call the following function when you need to change it:

int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
The usage of each attribute in this structure is as follows:
  1. Sa_handler: Do not set it at the same time as sa_sigaction. When processing signals, you only need one parameter: signo, which is the default processing function;
  2. Sa_sigaction: The processing function when flags sets sa_siginfo. The first parameter of the processing function is also signo, the second parameter is siginfo_t, which contains some signal information, and the third parameter is void *;
  3. Sa_mask: Specifies the signal. Should it be blocked when processing the signal?
  4. Sa_flags: Specifies the behavior of the signal processing process:
    1. Sa_siginfo, which is the most common function. When this value is set, the sa_sigaction function is called;
    2. Sa_nocldstop: Ignore the sigstop signal (this is not ignored by default );
    3. Sa_nocldwait, when receiving the sigstop signal, do not Program Sub-processes as "zombie processes ";
    4. Sa_resethand, reset to the default status;
    5. Sa_onstack: the stack set by sigaltstack is preferred;
    6. Sa_restart, which provides behavior compatible with BSD signals;
    7. Sa_nodefer, will the signal reception be blocked during signal processing?
  5. Sa_restorer has been deprecated.
The data structure to be set in an aiocb is sigevent:

struct sigevent {    
int sigev_notify;
int sigev_signo;
union sigval sigev_value;
void (*sigev_notify_function) (union sigval);
void *sigev_notify_attributes;
};
The meaning of each attribute in the structure is as follows:
  1. Sigev_notify specifies the notification method:
    1. Sigev_none: Is the current process to be notified?
    2. Sigev_signal: Signal callback notification
    3. Sigev_thread: thread callback notification
  2. Sigev_signo: Notification signal;
  3. Sigval: the data transmitted by the notification, but a pointer or integer;
  4. Sigev_policy_function: The function address of the thread callback notification;
  5. Sigev_policy_sttributes: Features of thread functions;
Below is a simple example of using signals as AIO notifications:

# Define bufsize 10

Void do_something (INT signo, siginfo_t * info, void * context ){
Struct aiocb * PTR;
Int ret;
Printf ("signal processing function! \ N ");
If (Info-> si_signo = sigio ){
PTR = (struct aiocb *) Info-> si_value.sival_ptr;
If (aio_error (PTR) = 0 ){
Ret = aio_return (PTR );
}
}
}

Int main (){
Int ret, I;
Struct aiocb in;
Struct sigaction sigact;

Sigemptyset (& sigact. sa_mask );
Sigact. sa_flags = sa_siginfo;
Sigact. sa_sigaction = do_something;

Bzero (char *) & in, sizeof (struct aiocb ));

In. aio_fildes = stdin_fileno;
In. aio_buf = malloc (bufsize + 1 );
In. aio_nbytes = bufsize;
In. aio_offset = 0;
In. aio_lio_opcode = lio_read;

In. aio_sigevent.sigev_notify = sigev_signal;
In. aio_sigevent.sigev_signo = sigio;
In. aio_sigevent.sigev_value.sival_ptr = & in;

Ret = sigaction (sigio, & sigact, null); // Changes the processing method after a process receives a signal
Ret = aio_read (& in );
Sleep (10 );
Printf ("the main function ends! \ N ");

Return 0;
}
A simple example of using a thread callback notification is as follows:

# Define bufsize 10

Void do_something (sigval_t sigval ){
Struct aiocb * PTR = (struct aiocb *) sigval. sival_ptr;
Int ret;
Printf ("signal processing function, thread ID: % d \ n", (INT) pthread_self ());
If (aio_error (PTR) = 0 ){
Ret = aio_return (PTR );
}
}

Int main (){
Int ret, I;
Struct aiocb in;
Struct sigaction sigact;

Bzero (char *) & in, sizeof (struct aiocb ));

In. aio_fildes = stdin_fileno;
In. aio_buf = malloc (bufsize + 1 );
In. aio_nbytes = bufsize;
In. aio_offset = 0;
In. aio_lio_opcode = lio_read;

In. aio_sigevent.sigev_notify = sigev_thread;
In. aio_sigevent.sigev_policy_function = do_something;
In. aio_sigevent.sigev_policy_attributes = NULL;
In. aio_sigevent.sigev_value.sival_ptr = & in;

Printf ("Main Function thread ID: % d \ n", (INT) pthread_self ());

Ret = aio_read (& in );
Sleep (10 );
Printf ("the main function ends! \ N ");

Return 0;
}

The specific principle of running the program also needs to be tested by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.