大家好,我是码农先森。
引言
我们日常的程序开发大多数都是以业务为主,很少会接触到底层逻辑。对于我们程序员来说,了解程序的底层运行逻辑,更有助于提升我们对程序的理解。我相信大多数的人,每天基本上都是完成业务需求。当然,完成业务需求并没有错,因为业务是第一生产力。但是,对应我们 PHP 程序员来说,只会 PHP 语法知识是远远不够的。那么,我今天的主要任务,就是分享 PHP 网络通信的底层原理。通过原理的讲解,能够让大家看到程序到底如何进行通信交互的。
段子:
面试官:看你简历有 5 年开滴滴的经验?你开什么车比较多?
我:嗯,平常开的比亚迪。
面试官:那你说下比亚迪汽车从点火到启动的整个过程?
我:按下点火按钮,车辆完成自检,火花塞点火,发动机顺利启动。
面试官:嗯,那你之前工作中有做过启动优化吗?改过发动机内核吗?
我:额,没有。
面试官:额,那你还开过其他的车吗?
我:我之前还摸过大奔。
面试官:那你说下大奔的运行原理?
我:额...
整体示意图
strace 强大的进程追踪工具
简介
strace
常用来跟踪进程执行时的系统调用和所接收的信号。Linux
查看进程不能直接访问硬件设备,当需要访问硬件设备,比如:读取磁盘文件、接收网络数据等,必须由用户态模式切换至内核态模式,通过系统调用访问硬件设备。strace
可以跟踪到一个进程产生的系统调用,包括:参数、返回值、执行耗时。
参数
-c # 统计每一系统调用的所执行的时间,次数和出错的次数等。
-d # 输出strace关于标准错误的调试信息。
-f # 跟踪由fork调用所产生的子进程。
-ff # 如果提供-o filename,则所有进程的跟踪结果输出到相应的filename.pid中,pid是各进程的进程号。
-F # 尝试跟踪vfork调用,在-f时,vfork不被跟踪。
-h # 输出简要的帮助信息。
-i # 输出系统调用的入口指针。
-q # 禁止输出关于脱离的消息。
-r # 打印出相对时间关于,每一个系统调用。
-t # 在输出中的每一行前加上时间信息。
-tt # 在输出中的每一行前加上时间信息,微秒级。
-ttt # 微秒级输出,以秒了表示时间。
-T # 显示每一调用所耗的时间。
-v # 输出所有的系统调用,一些调用关于环境变量,状态、输入输出等调用由于使用频繁,默认不输出。
-V # 输出strace的版本信息。
-x # 以十六进制形式输出非标准字符串。
-xx # 所有字符串以十六进制形式输出。
-a # column 设置返回值的输出位置,默认 为40。
-e # expr 指定一个表达式,用来控制如何跟踪
-o # filename 将strace的输出写入文件filename
-p # pid 跟踪指定的进程pid. 一般用于跟踪后台程序
-s # strsize 指定输出的字符串的最大长度.默认为32.
-u # username 以username 的UID和GID执行被跟踪的命令
常见的系统函数
fork # 创建一个新进程
clone # 按指定条件创建子进程
execve # 运行可执行文件
exit # 中止进程
nanosleep # 使进程睡眠指定的时间
pause # 挂起进程,等待信号
wait # 等待子进程终止
waitpid # 等待指定子进程终止
socket # 建立socket
bind # 绑定socket到端口
connect # 连接远程主机
accept # 响应socket连接请求
send # 通过socket发送信息
recv # 通过socket接收信息
mmap # 映射虚拟内存页
munmap # 去除内存页映射
mremap # 重新映射虚拟内存地址
msync # 将映射内存中的数据写回磁盘
listen # 监听socket端口
select # 对多路同步I/O进行轮询
shutdown # 关闭socket上的连接
readv # 从文件读入数据到缓冲数组中
writev # 将缓冲数组里的数据写入文件
pread # 对文件随机读
pwrite # 对文件随机写
lseek # 移动文件指针
dup # 复制已打开的文件描述字
flock # 文件加/解锁
poll # I/O多路转换
truncate # 截断文件
Nginx 进程追踪
Nginx 配置文件
[root@iZwz925ohgv150w36b86ndZ ~]# cd /www/server/panel/vhost/nginx
[root@iZwz925ohgv150w36b86ndZ nginx]# cat demo.yxh.top.conf
server
{
# ............................. 省略
#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-74.conf;
#PHP-INFO-END
# ............................. 省略
}
Nginx 中与 PHP 相关的配置文件
[root@iZwz925ohgv150w36b86ndZ ~]# cd /www/server/nginx/conf
## enable-php-74.conf 配置文件
[root@iZwz925ohgv150w36b86ndZ conf]# cat enable-php-74.conf
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi-74.sock; # 连接 PHP-FPM
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}
## fastcgi.conf 配置文件
[root@iZwz925ohgv150w36b86ndZ conf]# cat fastcgi.conf
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
## pathinfo.conf 配置文件
[root@iZwz925ohgv150w36b86ndZ conf]# cat pathinfo.conf
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
查看 Nginx 服务进程号
[root@iZwz925ohgv150w36b86ndZ ~]# ps -aux | grep nginx
root 644868 0.0 0.1 150276 2376 ? Ss 11:25 0:00 nginx: master process /www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf
www 644869 0.0 1.5 199040 28480 ? S 11:25 0:00 nginx: worker process
Nginx 进程追踪
strace -f -s 65500 -i -T -o output_nginx.log -p 644869
进程追踪信息
[root@iZwz925ohgv150w36b86ndZ ~]# cat output_nginx.log
# epoll_wait 返回值为 1,表示有 1 个描述符存在可读/写事件,这里是可读事件
644869 [00007f78754c10bb] epoll_wait(27, [{EPOLLIN, {u32=1809981680, u64=140155182784752}}], 512, -1) = 1 <27.452704>
644869 [00007f78754c272b] accept4(22, {sa_family=AF_INET, sin_port=htons(57690), sin_addr=inet_addr("203.168.15.87")}, [112->16], SOCK_NONBLOCK) = 29 <0.000015>
644869 [00007f78754c17ae] epoll_ctl(27, EPOLL_CTL_ADD, 29, {EPOLLIN|EPOLLRDHUP|EPOLLET, {u32=1809982352, u64=140155182785424}}) = 0 <0.000007>
# accept4 接受该请求,返回的数字 30 表示 socket 的文件描述符
644869 [00007f78754c272b] accept4(22, {sa_family=AF_INET, sin_port=htons(57669), sin_addr=inet_addr("203.168.15.87")}, [112->16], SOCK_NONBLOCK) = 30 <0.000005>
# epoll_ctl把 accept4 建立的 socket 套接字(参数 30)加入到事件监听机制里
644869 [00007f78754c17ae] epoll_ctl(27, EPOLL_CTL_ADD, 30, {EPOLLIN|EPOLLRDHUP|EPOLLET, {u32=1809982576, u64=140155182785648}}) = 0 <0.000004>
644869 [00007f78754c272b] accept4(22, 0x7ffe9d0e8d00, [112], SOCK_NONBLOCK) = -1 EAGAIN (Resource temporarily unavailable) <0.000009>
644869 [00007f78754c10bb] epoll_wait(27, [{EPOLLIN, {u32=1809982576, u64=140155182785648}}], 512, 60000) = 1 <0.007657>
# recvfrom 从发生可读事件的 socket 文件描述符内读取数据,读取的数据存在第二个参数内,读取了 366 个字节
644869 [00007f7876bf5b1e] recvfrom(30, "GET /index.php HTTP/1.1\r\nHost: demo.yxh.top\r\nUpgrade-Insecure-Requests: 1\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15\r\nAccept-Language: zh-cn\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\n\r\n", 32768, 0, NULL, NULL) = 366 <0.000008>
# stat 判断客户端请求的 php 文件是否存在,返回值为 0 表示存在
644869 [00007f78754b0a79] stat("/www/wwwroot/demo.yxh.top/index.php", {st_mode=S_IFREG|0644, st_size=30, ...}) = 0 <0.000009>
644869 [00007f78754c17ae] epoll_ctl(27, EPOLL_CTL_MOD, 30, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=1809982576, u64=140155182785648}}) = 0 <0.000005>
644869 [00007f78754c1edb] getsockname(30, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("172.30.82.40")}, [112->16]) = 0 <0.000004>
644869 [00007f78754c244b] socket(AF_UNIX, SOCK_STREAM, 0) = 31 <0.000011>
644869 [00007f78754b762b] ioctl(31, FIONBIO, [1]) = 0 <0.000004>
644869 [00007f78754c17ae] epoll_ctl(27, EPOLL_CTL_ADD, 31, {EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=1809982800, u64=140155182785872}}) = 0 <0.000008>
# connect 与 php-cgi 建立连接
644869 [00007f7876bf5a75] connect(31, {sa_family=AF_UNIX, sun_path="/tmp/php-cgi-74.sock"}, 110) = 0 <0.000060>
644869 [00007f78754c1f0e] getsockopt(31, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 <0.000011>
# writev 向 php-cgi socket 写入数据
644869 [00007f78754b7718] writev(31, [{iov_base="\1\1\0\1\0\10\0\0\0\1\0\0\0\0\0\0\1\4\0\1\3\220\0\0\17&SCRIPT_FILENAME/www/wwwroot/demo.yxh.top/index.php\f\0QUERY_STRING\16\3REQUEST_METHODGET\f\0CONTENT_TYPE\16\0CONTENT_LENGTH\v\nSCRIPT_NAME/index.php\v\nREQUEST_URI/index.php\f\nDOCUMENT_URI/index.php\r\34DOCUMENT_ROOT/www/wwwroot/demo.yxh.top\17\10SERVER_PROTOCOLHTTP/1.1\16\4REQUEST_SCHEMEhttp\21\7GATEWAY_INTERFACECGI/1.1\17\fSERVER_SOFTWAREnginx/1.18.0\v\rREMOTE_ADDR203.168.15.87\v\5REMOTE_PORT57669\v\fSERVER_ADDR172.30.82.40\v\2SERVER_PORT80\v\17SERVER_NAMEdemo.yxh.top\17\3REDIRECT_STATUS200\17&SCRIPT_FILENAME/www/wwwroot/demo.yxh.top/index.php\v\nSCRIPT_NAME/index.php\t\0PATH_INFO\t\17HTTP_HOSTdemo.yxh.top\36\1HTTP_UPGRADE_INSECURE_REQUESTS1\v?HTTP_ACCEPTtext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\17wHTTP_USER_AGENTMozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15\24\5HTTP_ACCEPT_LANGUAGEzh-cn\24\rHTTP_ACCEPT_ENCODINGgzip, deflate\17\nHTTP_CONNECTIONkeep-alive\1\4\0\1\0\0\0\0\1\5\0\1\0\0\0\0", iov_len=952}], 1) = 952 <0.000027>
644869 [00007f78754c10bb] epoll_wait(27, [{EPOLLOUT, {u32=1809982576, u64=140155182785648}}, {EPOLLOUT, {u32=1809982800, u64=140155182785872}}], 512, 59992) = 2 <0.000020>
644869 [00007f78754c10bb] epoll_wait(27, [{EPOLLIN|EPOLLOUT, {u32=1809982800, u64=140155182785872}}], 512, 59992) = 1 <0.000879>
# recvfrom 接受来至 php-cgi 发送的数据
644869 [00007f7876bf5b1e] recvfrom(31, "\1\6\0\1\0006\2\0Content-type: text/html; charset=UTF-8\r\n\r\nHello World!\0\0\1\3\0\1\0\10\0\0\0\0\0\0\086d", 65536, 0, NULL, NULL) = 80 <0.000019>
644869 [00007f78754c10bb] epoll_wait(27, [{EPOLLIN|EPOLLOUT|EPOLLRDHUP, {u32=1809982800, u64=140155182785872}}], 512, 59991) = 1 <0.000029>
644869 [00007f78754b7668] readv(31, [{iov_base="", iov_len=65456}], 1) = 0 <0.000017>
644869 [00007f7876bf5955] close(31) = 0 <0.000014>
# writev 向客户端的 scoket 写入数据,这里表示的是:将数据响应给浏览器客户端
644869 [00007f78754b7718] writev(30, [{iov_base="HTTP/1.1 200 OK\r\nServer: nginx\r\nDate: Sun, 11 Jul 2021 03:27:44 GMT\r\nContent-Type: text/html; charset=UTF-8\r\nTransfer-Encoding: chunked\r\nConnection: keep-alive\r\nVary: Accept-Encoding\r\nContent-Encoding: gzip\r\n\r\n", iov_len=210}, {iov_base="20\r\n", iov_len=4}, {iov_base="\37\213\10\0\0\0\0\0\0\3\363H\315\311\311W\10\317/\312IQ\4\0\243\34)\34\f\0\0\0", iov_len=32}, {iov_base="\r\n0\r\n\r\n", iov_len=7}], 4) = 253 <0.000038>
644869 [00007f7876bf5815] write(13, "203.168.15.87 - - [11/Jul/2021:11:27:44 +0800] \"GET /index.php HTTP/1.1\" 200 43 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15\"\n", 206) = 206 <0.000025>
644869 [00007f78754c23ee] setsockopt(30, SOL_TCP, TCP_NODELAY, [1], 4) = 0 <0.000021>
644869 [00007f78754c10bb] epoll_wait(27, <detached ...>
[root@iZwz925ohgv150w36b86ndZ ~]#
PHP-FPM 进程追踪
查看 PHP-FPM 进程号
[root@iZwz925ohgv150w36b86ndZ ~]# ps -aux | grep fpm
root 581176 0.0 0.5 299964 9212 ? Ss Jul10 0:01 php-fpm: master process (/www/server/php/74/etc/php-fpm.conf)
www 581177 0.0 1.8 330388 32960 ? S Jul10 0:00 php-fpm: pool www
PHP-FPM 进程追踪
strace -f -s 65500 -i -T -o output_fpm.log -p 581177
进程追踪信息
[root@iZwz925ohgv150w36b86ndZ ~]# cat output_fpm.log
581177 [00007ff7eb6d8d48] accept(6, {sa_family=AF_UNIX}, [112->2]) = 9 <9.001650>
581177 [00007ff7eb6cca08] poll([{fd=9, events=POLLIN}], 1, 5000) = 1 ([{fd=9, revents=POLLIN}]) <0.000059>
581177 [00007ff7eb6a399b] times({tms_utime=17, tms_stime=3, tms_cutime=0, tms_cstime=0}) = 2593846756 <0.000017>
581177 [00007ff7eb6c85a5] read(9, "\1\1\0\1\0\10\0\0", 8) = 8 <0.000018>
581177 [00007ff7eb6c85a5] read(9, "\0\1\0\0\0\0\0\0", 8) = 8 <0.000004>
581177 [00007ff7eb6c85a5] read(9, "\1\4\0\1\3\220\0\0", 8) = 8 <0.000004>
# 读取 Nginx socket 的数据
581177 [00007ff7eb6c85a5] read(9, "\17&SCRIPT_FILENAME/www/wwwroot/demo.yxh.top/index.php\f\0QUERY_STRING\16\3REQUEST_METHODGET\f\0CONTENT_TYPE\16\0CONTENT_LENGTH\v\nSCRIPT_NAME/index.php\v\nREQUEST_URI/index.php\f\nDOCUMENT_URI/index.php\r\34DOCUMENT_ROOT/www/wwwroot/demo.yxh.top\17\10SERVER_PROTOCOLHTTP/1.1\16\4REQUEST_SCHEMEhttp\21\7GATEWAY_INTERFACECGI/1.1\17\fSERVER_SOFTWAREnginx/1.18.0\v\rREMOTE_ADDR203.168.15.87\v\5REMOTE_PORT57669\v\fSERVER_ADDR172.30.82.40\v\2SERVER_PORT80\v\17SERVER_NAMEdemo.yxh.top\17\3REDIRECT_STATUS200\17&SCRIPT_FILENAME/www/wwwroot/demo.yxh.top/index.php\v\nSCRIPT_NAME/index.php\t\0PATH_INFO\t\17HTTP_HOSTdemo.yxh.top\36\1HTTP_UPGRADE_INSECURE_REQUESTS1\v?HTTP_ACCEPTtext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\17wHTTP_USER_AGENTMozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15\24\5HTTP_ACCEPT_LANGUAGEzh-cn\24\rHTTP_ACCEPT_ENCODINGgzip, deflate\17\nHTTP_CONNECTIONkeep-alive", 912) = 912 <0.000004>
581177 [00007ff7eb6c85a5] read(9, "\1\4\0\1\0\0\0\0", 8) = 8 <0.000004>
581177 [00007ff7eb6c7b29] lstat("/www/wwwroot/demo.yxh.top/index.php", {st_mode=S_IFREG|0644, st_size=30, ...}) = 0 <0.000006>
581177 [00007ff7eb6c7b29] lstat("/www/wwwroot/demo.yxh.top", {st_mode=S_IFDIR|0755, st_size=91, ...}) = 0 <0.000006>
581177 [00007ff7eb6c7b29] lstat("/www/wwwroot", {st_mode=S_IFDIR|0755, st_size=175, ...}) = 0 <0.000006>
581177 [00007ff7eb6c7b29] lstat("/www", {st_mode=S_IFDIR|0755, st_size=83, ...}) = 0 <0.000004>
581177 [00007ff7eb6c7a79] stat("/www/wwwroot/demo.yxh.top/.user.ini", {st_mode=S_IFREG|0644, st_size=48, ...}) = 0 <0.000005>
581177 [00007ff7eb6c82f2] openat(AT_FDCWD, "/www/wwwroot/demo.yxh.top/.user.ini", O_RDONLY) = 10 <0.000007>
581177 [00007ff7eb6cdd0c] ioctl(10, TCGETS, 0x7ffe5057ae00) = -1 ENOTTY (Inappropriate ioctl for device) <0.000004>
581177 [00007ff7eb6c7ad7] fstat(10, {st_mode=S_IFREG|0644, st_size=48, ...}) = 0 <0.000004>
581177 [00007ff7eb6c7ad7] fstat(10, {st_mode=S_IFREG|0644, st_size=48, ...}) = 0 <0.000004>
581177 [00007ff7eb6c85a5] read(10, "open_basedir=/www/wwwroot/demo.yxh.top/:/tmp/", 4096) = 48 <0.000006>
581177 [00007ff7eb6cd67b] close(10) = 0 <0.000005>
581177 [00007ff7eb6124d4] rt_sigaction(SIGPROF, NULL, {sa_handler=0xb0abd2, sa_mask=~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7ff7eb612400}, 8) = 0 <0.000004>
581177 [00007ff7eb6124d4] rt_sigaction(SIGHUP, NULL, {sa_handler=0xb0abd2, sa_mask=~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7ff7eb612400}, 8) = 0 <0.000006>
581177 [00007ff7eb6124d4] rt_sigaction(SIGINT, NULL, {sa_handler=0xb0abd2, sa_mask=~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7ff7eb612400}, 8) = 0 <0.000005>
581177 [00007ff7eb6124d4] rt_sigaction(SIGQUIT, NULL, {sa_handler=0xb0abd2, sa_mask=~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7ff7eb612400}, 8) = 0 <0.000005>
581177 [00007ff7eb6124d4] rt_sigaction(SIGTERM, NULL, {sa_handler=0xb0abd2, sa_mask=~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7ff7eb612400}, 8) = 0 <0.000004>
581177 [00007ff7eb6124d4] rt_sigaction(SIGUSR1, NULL, {sa_handler=0xb0abd2, sa_mask=~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7ff7eb612400}, 8) = 0 <0.000004>
581177 [00007ff7eb6124d4] rt_sigaction(SIGUSR2, NULL, {sa_handler=0xb0abd2, sa_mask=~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7ff7eb612400}, 8) = 0 <0.000004>
581177 [00007ff7eb695e3b] setitimer(ITIMER_PROF, {it_interval={tv_sec=0, tv_usec=0}, it_value={tv_sec=60, tv_usec=0}}, NULL) = 0 <0.000005>
581177 [00007ff7eb6124d4] rt_sigaction(SIGPROF, {sa_handler=0xb0abd2, sa_mask=~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7ff7eb612400}, NULL, 8) = 0 <0.000004>
581177 [00007ff7eb612641] rt_sigprocmask(SIG_UNBLOCK, [PROF], NULL, 8) = 0 <0.000004>
581177 [00007ff7eb6c8fa2] getcwd("/www/wwwroot/admin.yxh.top/easyadmin/public", 4095) = 47 <0.000005>
581177 [00007ff7eb6c8ecb] chdir("/www/wwwroot/demo.yxh.top") = 0 <0.000007>
581177 [00007ff7eb695e3b] setitimer(ITIMER_PROF, {it_interval={tv_sec=0, tv_usec=0}, it_value={tv_sec=300, tv_usec=0}}, NULL) = 0 <0.000005>
581177 [00007ff7eb6cd6c4] fcntl(3, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET, l_start=1, l_len=1}) = 0 <0.000006>
581177 [00007ff7eb6c7a79] stat("/www/wwwroot/demo.yxh.top/index.php", {st_mode=S_IFREG|0644, st_size=30, ...}) = 0 <0.000005>
581177 [00007ff7eb6c8ecb] chdir("/www/wwwroot/admin.yxh.top/easyadmin/public") = 0 <0.000005>
581177 [00007ff7eb6a399b] times({tms_utime=17, tms_stime=3, tms_cutime=0, tms_cstime=0}) = 2593846756 <0.000004>
581177 [00007ff7eb695e3b] setitimer(ITIMER_PROF, {it_interval={tv_sec=0, tv_usec=0}, it_value={tv_sec=0, tv_usec=0}}, NULL) = 0 <0.000004>
581177 [00007ff7eb6cd6c4] fcntl(3, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = 0 <0.000004>
# 向 Nginx socket 写入数据
581177 [00007ff7eb6c8648] write(9, "\1\6\0\1\0006\2\0Content-type: text/html; charset=UTF-8\r\n\r\nHello World!\0\0\1\3\0\1\0\10\0\0\0\0\0\0\086d", 80) = 80 <0.000037>
581177 [00007ff7eb6d941b] shutdown(9, SHUT_WR) = 0 <0.000032>
581177 [00007ff7eb6d8f81] recvfrom(9, "\1\5\0\1\0\0\0\0", 8, 0, NULL, NULL) = 8 <0.000040>
581177 [00007ff7eb6d8f81] recvfrom(9, "", 8, 0, NULL, NULL) = 0 <0.000143>
581177 [00007ff7eb6c8cc8] close(9) = 0 <0.000037>
581177 [00007ff7eb6c8648] write(2, "\0fscf\0", 6) = 6 <0.000022>
581177 [00007ff7eb6d8d48] accept(6, <detached ...>
总结
我们通过对 Nginx 进程和 PHP-FPM 进程的调用信息的分析、追踪,相信大家对程序之间的通信交互有一定的了解了。如果有网络编程经验的人,可能对这块的内容会更加的容易理解。通过上述的分析,我们可以看出所有的程序交互,都是 socket 通信。socket 中的数据,就是封装了一层协议。希望这一次的分享,能够对大家在以后的编程中有所帮助。
欢迎关注、分享、点赞、收藏、在看,我是微信公众号「码农先森」作者。