synchronous, asynchronous, blocking, non-blocking

Source: Internet
Author: User
Tags htons

Synchronous Asynchrony is for the interaction of the application and the kernel. For example: When a process triggers an IO operation,

Synchronization: The process needs to determine when the IO operation is complete and the process itself needs constant queries to determine whether the task is complete. (during which process can do other things)

Async: The process does not need to determine when the IO operation is complete, and the kernel notifies the process when the task is completed.

Blocking: The process stops at the current operation and does not perform subsequent operations until the current operation is complete. (blocking refers to the operating mode of the current operation)

Non-blocking: The process does not stop in the current operation, the current operation returns a result immediately, and then the subsequent operation. (non-blocking refers to the operating mode of the current operation)

Synchronous blocking: The process stops during the current operation, while the process continuously queries whether the current operation is complete.

Synchronous non-blocking: The process does not stop at the current operation A, perform other actions later B ..., but during the continuous query operation a is completed.

Asynchronous blocking: The process stops at the current operation A, does not perform subsequent operations, and so after operation a completes, the kernel notifies the process, and the process performs the subsequent operations.

Asynchronous non-blocking: The process does not stop at the current operation A, instead of performing the subsequent operation, after operation a completes, the kernel notifies the process operation a to complete.

For non-blocking: if operation A is not completed, but the current process has finished executing, will the process stop waiting for operation A to complete? What is the status of the waiting period?

For non-blocking, operation a returns the result immediately.

The following is a recvfrom in the network to specifically describe the difference between blocking and non-blocking.

For network IO, it involves two objects, the kernel and the process of calling Io, generally divided into two phases:

1. Wait for data preparation (waiting for the

2. Copy the data from the kernel to the process (Copying the "kernel to the")

Blocking IO mode:

Non-blocking mode:

Asynchronous mode:

Use a UDP client server program to explain the difference between synchronous blocking and non-blocking: (its client is the same, the main difference is the server)

Client:

#include <stdio.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h>int main (int argc, char **argv) {    int sockfd;    struct sockaddr_in servaddr;    SOCKFD = socket (pf_inet, SOCK_DGRAM, 0);    Bzero (&servaddr, sizeof (SERVADDR));    servaddr.sin_family = af_inet;    Servaddr.sin_port = htons (50001);    SERVADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1");    Char sendline[100];    sprintf (Sendline, "Hello, world!");    SendTo (SOCKFD, Sendline, strlen (Sendline), 0, (struct sockaddr *) &servaddr, sizeof (SERVADDR));    Close (SOCKFD);    return 1;}

Service-side blocking:

#include <stdio.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h>int main (int argc, char **argv) {    int sockfd;    struct sockaddr_in servaddr;    SOCKFD = socket (pf_inet, SOCK_DGRAM, 0);    Bzero (&servaddr, sizeof (SERVADDR));    servaddr.sin_family = af_inet;    SERVADDR.SIN_ADDR.S_ADDR = htonl (inaddr_any);    Servaddr.sin_port = htons (50001);    Bind (SOCKFD, (struct sockaddr *) &servaddr, sizeof (SERVADDR));    int n;    Char recvline[1024];    Recvfrom (SOCKFD, Recvline, 1024x768, 0, NULL, NULL);    printf ("%s\n", recvline);    Close (SOCKFD);}

Service-side non-blocking:

#include <stdio.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h>int main (int argc, char **argv) {    int sockfd;    struct sockaddr_in servaddr;    SOCKFD = socket (pf_inet, SOCK_DGRAM, 0);    Bzero (&servaddr, sizeof (SERVADDR));    servaddr.sin_family = af_inet;    SERVADDR.SIN_ADDR.S_ADDR = htonl (inaddr_any);    Servaddr.sin_port = htons (50001);    Bind (SOCKFD, (struct sockaddr *) &servaddr, sizeof (SERVADDR));    int n;    Char recvline[1024];    int result =  Recvfrom (sockfd, Recvline, 1024x768, msg_dontwait, NULL, NULL);    while (Result! =-1) {        result = Recvfrom (SOCKFD, Recvline, 1024x768, msg_dontwait, NULL, NULL);        printf ("Continue recvfrom");    }    printf ("%s\n", recvline);    Close (SOCKFD);}     

In blocking mode, the server waits for data and does not perform the following operations.

In nonblocking mode, the server returns the result immediately, in this case, no data received, returns 1, so in non-blocking mode, the service side will always output "continue recvfrom" until there is data, loop stop.

Synchronous, asynchronous, blocking, non-blocking

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.