IO 模型知多少 | 代码篇 (2)

要想验证应用使用的何种IO模型,只需要确定应用运行时发起了哪些系统调用即可。对于Linux系统来说,我们可以借助strace命令来跟踪指定应用发起的系统调用和信号。

验证同步阻塞I/O发起的系统调用

可以使用VSCode Remote 连接到自己的Linux系统上,然后新建项目Io.Demo,以上面非阻塞IO的代码进行测试,执行以下启动跟踪命令:

shengjie@ubuntu:~/coding/dotnet$ ls Io.Demo shengjie@ubuntu:~/coding/dotnet$ strace -ff -o Io.Demo/strace/io dotnet run --project Io.Demo/ Press any key to start! 服务端已启动(127.0.0.1:5001)-等待连接... 127.0.0.1:36876-已连接 127.0.0.1:36876-开始接收数据... 127.0.0.1:36876-接收数据:1

另起命令行,执行nc localhost 5001模拟客户端连接。

shengjie@ubuntu:~/coding/dotnet/Io.Demo$ nc localhost 5001 1 received:1

使用netstat 命令查看建立的连接。

shengjie@ubuntu:/proc/3763$ netstat -natp | grep 5001 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 127.0.0.1:5001 0.0.0.0:* LISTEN 3763/Io.Demo tcp 0 0 127.0.0.1:36920 127.0.0.1:5001 ESTABLISHED 3798/nc tcp 0 0 127.0.0.1:5001 127.0.0.1:36920 ESTABLISHED 3763/Io.Demo

另起命令行,执行 ps -h | grep dotnet 抓取进程Id。

shengjie@ubuntu:~/coding/dotnet/Io.Demo$ ps -h | grep dotnet 3694 pts/1 S+ 0:11 strace -ff -o Io.Demo/strace/io dotnet run --project Io.Demo/ 3696 pts/1 Sl+ 0:01 dotnet run --project Io.Demo/ 3763 pts/1 Sl+ 0:00 /home/shengjie/coding/dotnet/Io.Demo/bin/Debug/netcoreapp3.0/Io.Demo 3779 pts/2 S+ 0:00 grep --color=auto dotnet shengjie@ubuntu:~/coding/dotnet$ ls Io.Demo/strace/ # 查看生成的系统调用文件 io.3696 io.3702 io.3708 io.3714 io.3720 io.3726 io.3732 io.3738 io.3744 io.3750 io.3766 io.3772 io.3782 io.3827 io.3697 io.3703 io.3709 io.3715 io.3721 io.3727 io.3733 io.3739 io.3745 io.3751 io.3767 io.3773 io.3786 io.3828 io.3698 io.3704 io.3710 io.3716 io.3722 io.3728 io.3734 io.3740 io.3746 io.3752 io.3768 io.3774 io.3787 io.3699 io.3705 io.3711 io.3717 io.3723 io.3729 io.3735 io.3741 io.3747 io.3763 io.3769 io.3777 io.3797 io.3700 io.3706 io.3712 io.3718 io.3724 io.3730 io.3736 io.3742 io.3748 io.3764 io.3770 io.3780 io.3799 io.3701 io.3707 io.3713 io.3719 io.3725 io.3731 io.3737 io.3743 io.3749 io.3765 io.3771 io.3781 io.3800

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfjwg.html