I/O阻塞函数非阻塞模式的实现

1. I/O中有哪些阻塞函数

1)读操作中的read、recv、recvfrom

2)写操作中的write、sendto

3)其他操作:accept、connect

2. 阻塞函数在什么情况下阻塞

1)读阻塞,以read函数为例

进程调用read函数从套接字中上读取数据,当套接字的接收缓冲区中还没有数据可读时,函数read将发生阻塞。它会一直阻塞,等待套接字的接收缓冲区中有数据可读。经过一段时间后,缓冲区内接收到数据,于是内核便去唤醒内核,通过read访问这些数据。如果在进程阻塞过程中,对方发生阻塞,那这个进程将永远阻塞下去。

2)写阻塞

在写操作时发生阻塞的情况比读操作少,主要发生在要写入的缓冲区的大小小于要写入的数据量的情况下。这时,写作操不进形任何拷贝工作,将发生阻塞。一旦发送缓冲区内有足够的空间,内核将唤醒进程,将数据从用户缓冲区中拷贝到相应的发送数据缓冲区。注:UDP不用等待确认,没有实际的发送缓冲区,所以UDP协议中不存在发送缓冲区满的情况,在UDP套接字上执行的写操作永远不会发生阻塞。

3. 非阻塞模式I/O

当我们将一个套接字设置为非阻塞模式时,我们相当于告诉系统的内核:当我请求I/O操作不能马上完成,你想让我的进程进行休眠等待的时候,不要这么做,请马上返回一个错误值给我。当一个应用程序使用了非阻塞模式的套接字,它需要使用一个循环来不停的测试一个文件描述符是否可读(称作polling或轮询)

3. 阻塞函数非阻塞模式的实现

fcntl函数

一开始建立套接字时,系统内核将其设置为阻塞IO模式,可以使用函数fcntl设置一个套接字的标志位O_NONBLOCK来实现非阻塞。

头文件:#include <unistd.h>

       #include <fcntl.h>

函数原型:int fcntl(int fd, int cmd, ... /* arg*/ );

实现过程如下:

Int flag;

fcntl(socketfd, F_GETFL, 0);

flag = O_NONBLOCK;

fcntl(socketfd, F_SETFL,flag);

示例代码:

/*************************************************************************

 @Author: wanghao

 @Created Time : Fri 25 May 2018 08:09:19 PM PDT

 @File Name: fcntl.c

 @Description:

 ************************************************************************/

#include <stdio.h>

#include <sys/types.h>     

#include <sys/socket.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <string.h>

#include <pthread.h>

#include <unistd.h>

#include <fcntl.h>

#define N 36

#define MAXSIZE 128

int main(int argc, const char *argv[])

{

       intsocketfd;

       intclient_fd;

       intaddrlen;

       intflag;

       u_shortport;

       pthread_ta_thread;

       charbuf[MAXSIZE] = {0};

       structsockaddr_in my_addr;

       structsockaddr_in client_addr;

       if(argc< 3)

       {

              printf("usage:%s <IPadd> <Port>\n",argv[0]);

              return-1;

       }

       socketfd= socket(PF_INET, SOCK_DGRAM, 0);

       if(socket< 0)

       {

              perror("");

              return-1;

       }

       port= (u_short)atol(argv[2]);

       my_addr.sin_family= PF_INET;

       my_addr.sin_port= htons(port);

       my_addr.sin_addr.s_addr= inet_addr(argv[1]);

       addrlen= sizeof(struct sockaddr_in);

       if(bind(socketfd,(struct sockaddr *)(&my_addr), addrlen) < 0)

       {

              perror("");

              return-2;

       }

       fcntl(socketfd,F_GETFL, 0);

       flag= O_NONBLOCK;

       fcntl(socketfd,F_SETFL,flag);

       while(1)

       {

                     if(recvfrom(socketfd,buf, sizeof(buf), 0, (struct sockaddr*)(&client_addr), &addrlen) <0)

                     {

                            perror("receive error!\n");

                            sleep(2);

                            continue;

                     }

                     buf[strlen(buf)]= '\0';

                     printf("clientip = %s port = %d\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

                     printf("%s",buf);

                     sleep(1);

                     printf("helloworld\n");

       }

       close(socketfd);

       return0;

}

在没有任何client发送数据给server时,server打印如下:

receive error!

: Resource temporarily unavailable

receive error!

: Resource temporarily unavailable

receive error!

: Resource temporarily unavailable

……


猜你喜欢

转载自blog.csdn.net/weixin_42048417/article/details/80558862
今日推荐