NIO 看破也说破(二)—— Java 中的两种BIO

上一篇Linux/IO基础我们得出结论,提供网络能力的不是Java是Linux操作系统。本文我们通过分析系统函数调用,观察不同jdk版本中BIO的实现差别。NIO看破也说破(一)- Linux/IO基础

核心结论:

  1. 不同版本jdk实现方式不一致
  2. 如果不给socket设置nonblocking,accept会阻塞直到数据到达
  3. poll的调用是阻塞的,直到注册的event发生后,返回发生事件的fd

在这里插入图片描述

环境准备

centOS 7

jdk1.5.0-jdk1.8.0

strace

测试代码

BIOServer.java

import java.io.IOException;
import java.net.ServerSocket;

public class BIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8080);
        while (true) {
            server.accept();
            System.out.println("===========");
        }
    }
}

测试步骤

1、编译BIOServer.java后,命令行启动,监听8080端口

2、模拟client端telnet localhost 8080,连通后立马断开

3、strace监听Java进程的函数调用

Java5

strace调用栈

//开启3号fd
3188 socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = 3
3189 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
//对fd3 绑定8080端口,返回成功
3190 bind(3, {sa_family=AF_INET, sin_port=htons(8080), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
//监听fd3 
3191 listen(3, 50)                           = 0
3197 gettimeofday({tv_sec=1588789268, tv_usec=224692}, NULL) = 0
3198 gettimeofday({tv_sec=1588789268, tv_usec=224993}, NULL) = 0
3199 gettimeofday({tv_sec=1588789268, tv_usec=225263}, NULL) = 0
//从fd3中接受到新的连接,放到fd5中
3200 accept(3, {sa_family=AF_INET, sin_port=htons(40862), sin_addr=inet_addr("127.0.0.1")}, [16]) = 5
3199 gettimeofday({tv_sec=1588789268, tv_usec=225263}, NULL) = 0
3201 gettimeofday({tv_sec=1588789270, tv_usec=619848}, NULL) = 0
3208 gettimeofday({tv_sec=1588789270, tv_usec=623749}, NULL) = 0
3209 write(1, "===========", 11)             = 11
3210 write(1, "\n", 1)                       = 1
3211 accept(3, 0xffe9a4ec, [16])             = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
3212 --- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL} ---
3213 futex(0xf79429c0, FUTEX_WAKE_PRIVATE, 1) = 1
3214 rt_sigreturn({mask=[QUIT]})             = 102
3215 accept(3,  <unfinished ...>)            = ?

查看man手册

If no pending connections are present on the queue, and the socket is not marked as nonblocking, accept() blocks the caller until  a
 connection is present.  If the socket is marked nonblocking and no pending connections are present on the queue, accept() fails with
 the error EAGAIN or EWOULDBLOCK.

如果没有对socket设置nonblocking,accept会一直阻塞直到一个链接出现

结论

java5中的bio是通过accept阻塞实现

Java6

strace调用栈

// 打开6号fd
13614 socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = 6
13615 fcntl(6, F_GETFL)                       = 0x2 (flags O_RDWR)
13616 fcntl(6, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
13617 setsockopt(6, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
13618 gettimeofday({tv_sec=1588790897, tv_usec=258322}, NULL) = 0
13619 gettimeofday({tv_sec=1588790897, tv_usec=277413}, NULL) = 0
13620 gettimeofday({tv_sec=1588790897, tv_usec=277603}, NULL) = 0
 //对fd6绑定8080
13621 bind(6, {sa_family=AF_INET, sin_port=htons(8080), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
//监听
13622 listen(6, 50)                           = 0
13623 gettimeofday({tv_sec=1588790897, tv_usec=278363}, NULL) = 0
13624 gettimeofday({tv_sec=1588790897, tv_usec=287641}, NULL) = 0
//先把6号fd放入poll中监听,返回1个POLLIN的fd
13625 poll([{fd=6, events=POLLIN|POLLERR}], 1, -1) = 1 ([{fd=6, revents=POLLIN}])
//调用accept把fd6中的内容读出来
13626 accept(6, {sa_family=AF_INET, sin_port=htons(40868), sin_addr=inet_addr("127.0.0.1")}, [16]) = 8
13627 fcntl(8, F_GETFL)                       = 0x2 (flags O_RDWR)
13628 fcntl(8, F_SETFL, O_RDWR)               = 0
13629 gettimeofday({tv_sec=1588790899, tv_usec=835776}, NULL) = 0
13630 gettimeofday({tv_sec=1588790899, tv_usec=837031}, NULL) = 0
13631 gettimeofday({tv_sec=1588790899, tv_usec=837294}, NULL) = 0
13632 gettimeofday({tv_sec=1588790899, tv_usec=837659}, NULL) = 0
13633 gettimeofday({tv_sec=1588790899, tv_usec=838010}, NULL) = 0
13634 write(1, "===========", 11)             = 11
13635 write(1, "\n", 1)                       = 1
// 读取完数据后,再次吧6号fd放入到poll中等待数据
13636 poll([{fd=6, events=POLLIN|POLLERR}], 1, -1 <unfinished ...>) = ?
13637 +++ exited with 130 +++

listen之后这里没有立即调用 accept,而是先调用poll把 server_sockfd 与pollfdArray[0]关联起来,然后再把pollfdArray放到poll里去,这里只有一个文件描述符。

调用poll会使得线程阻塞,当有客户端连接进来的时候,poll函数就会返回一个整数,代表了数组中有多少个socket上有数据到达。对于第一次连接这种情况,返回值就是1。

接着,先判断pollfdArray[0]上是不是有数据,如果有的话,再去调用accept去接受新的连接,新的连接创建以后,我们会把新的socket放到pollfdArray中去,继续这个循环,然后在poll中再次休眠。

先看man手册中对于poll的定义:

NAME
       poll, ppoll - wait for some event on a file descriptor

SYNOPSIS
       #include <poll.h>

       int poll(struct pollfd *fds, nfds_t nfds, int timeout);
DESCRIPTION
      poll() performs a similar task to select(2): it waits for one of a set of file descriptors to become ready to perform I/O.
  		…………
       …………
       …………
      If none of the events requested (and no error) has occurred for any of the file descriptors, then poll() blocks  until  one  of  the
       events occurs.

       The  timeout argument specifies the minimum number of milliseconds that poll() will block.  (This interval will be rounded up to the
       system clock granularity, and kernel scheduling delays mean that the blocking interval may overrun by a small amount.)  Specifying a
       negative  value  in timeout means an infinite timeout.  Specifying a timeout of zero causes poll() to return immediately, even if no
       file descriptors are ready.

man手册可以得到如下结论:

1、poll 是和 select类似的方法

2、当没有任何event到来时,poll会阻塞,直到一个event发生

3、timeout参数明确了poll在指定的毫秒内阻塞,指定一个负数表示无限超时

验证

猜想server启动后,没有客户端建立连接,系统调用应该阻塞在poll方法上

[root@f00e68119764 tmp]# tail -f out.3257
clock_gettime(CLOCK_MONOTONIC, {tv_sec=49698, tv_nsec=95678478}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=49698, tv_nsec=95961778}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=49698, tv_nsec=96243778}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=49698, tv_nsec=96531678}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=49698, tv_nsec=96818478}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=49698, tv_nsec=97112578}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=49698, tv_nsec=97404578}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=49698, tv_nsec=97692278}) = 0
clock_gettime(CLOCK_MONOTONIC, {tv_sec=49698, tv_nsec=98007178}) = 0
poll([{fd=5, events=POLLIN|POLLERR}], 1, -1

当有client与8080建立连接时,日志滚动,出现accept调用

  18228 accept(5, {sa_family=AF_INET, sin_port=htons(40884), sin_addr=inet_addr("127.0.0.1")}, [16]) = 6
  18229 fcntl(6, F_GETFL)                       = 0x2 (flags O_RDWR)
  18230 fcntl(6, F_SETFL, O_RDWR)               = 0
  18231 clock_gettime(CLOCK_MONOTONIC, {tv_sec=50008, tv_nsec=62405578}) = 0
  18232 clock_gettime(CLOCK_MONOTONIC, {tv_sec=50008, tv_nsec=62713278}) = 0
  18252 clock_gettime(CLOCK_MONOTONIC, {tv_sec=50008, tv_nsec=67718778}) = 0
  18253 write(1, "===========", 11)             = 11
  18254 clock_gettime(CLOCK_MONOTONIC, {tv_sec=50008, tv_nsec=68673978}) = 0
  18255 write(1, "\n", 1)                       = 1
  18256 poll([{fd=5, events=POLLIN|POLLERR}], 1, -1

日志继续停留在poll方法,验证猜想是正确的

结论

1、jdk6中,bio通过 poll 和 accept 的方式实现

2、poll方法是阻塞的

Java7/8

strace调用栈

18774 socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = 6
18775 fcntl(6, F_GETFL)                       = 0x2 (flags O_RDWR)
18776 fcntl(6, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
18777 setsockopt(6, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
18778 gettimeofday({tv_sec=1588793691, tv_usec=279644}, NULL) = 0
18779 gettimeofday({tv_sec=1588793691, tv_usec=279906}, NULL) = 0
18780 gettimeofday({tv_sec=1588793691, tv_usec=280172}, NULL) = 0
18781 bind(6, {sa_family=AF_INET, sin_port=htons(8080), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
18782 listen(6, 50)                           = 0
18783 gettimeofday({tv_sec=1588793691, tv_usec=281027}, NULL) = 0
18784 gettimeofday({tv_sec=1588793691, tv_usec=281295}, NULL) = 0
18785 gettimeofday({tv_sec=1588793691, tv_usec=291874}, NULL) = 0
18786 poll([{fd=6, events=POLLIN|POLLERR}], 1, -1) = 1 ([{fd=6, revents=POLLIN}])
18787 accept(6, {sa_family=AF_INET, sin_port=htons(40874), sin_addr=inet_addr("127.0.0.1")}, [16]) = 7
18788 fcntl(7, F_GETFL)                       = 0x2 (flags O_RDWR)
18789 fcntl(7, F_SETFL, O_RDWR)               = 0
18790 gettimeofday({tv_sec=1588793691, tv_usec=912271}, NULL) = 0
18791 gettimeofday({tv_sec=1588793691, tv_usec=912551}, NULL) = 0
18792 write(1, "===========", 11)             = 11
18793 gettimeofday({tv_sec=1588793691, tv_usec=913462}, NULL) = 0
18794 write(1, "\n", 1)                       = 1
18795 poll([{fd=6, events=POLLIN|POLLERR}], 1, -1 <unfinished ...>) = ?

可以看出jdk7和jdk8跟jdk6中的实现方式一致

备忘录

  • 不同版本jdk实现方式不一致
  • 如果不给socket设置nonblocking,accept会阻塞直到数据到达
  • poll的调用是阻塞的,直到注册的event发生后,返回发生事件的fd

系列

NIO看破也说破(一)—— Linux/IO基础
NIO 看破也说破(二)—— Java 中的两种BIO
NIO 看破也说破(三)—— 不同的IO模型
NIO看破也说破(四)—— Java的NIO
NIO 看破也说破(五): 搞,今天就搞,搞懂Buffer

关注我

关注我
如果您在微信阅读,请您点击链接 关注我 ,如果您在 PC 上阅读请扫码关注【小眼睛聊技术】,欢迎与我交流随时指出错误
在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值