为了便于观察,只开启一个子进程
vi /etc/php-fpm.d/www.conf
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 1
systemctl restart php-fpm
master进程ID存放地址
/var/run/php-fpm/php-fpm.pid
ps -ef |grep php-fpm
root 2753 1 0 11:58 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
apache 2754 2753 0 11:58 ? 00:00:00 php-fpm: pool www
master进程
ll /proc/2753/fd
总用量 0
lrwx------. 1 root root 64 7月 20 11:58 0 -> /dev/null
lrwx------. 1 root root 64 7月 20 11:58 1 -> /dev/null
l-wx------. 1 root root 64 7月 20 11:58 2 -> /var/log/php-fpm/error.log
lrwx------. 1 root root 64 7月 20 11:58 3 -> /tmp/.ZendSem.RN2DDT (deleted)
l-wx------. 1 root root 64 7月 20 11:58 4 -> /var/log/php-fpm/error.log
lrwx------. 1 root root 64 7月 20 11:58 5 -> socket:[31960]
lrwx------. 1 root root 64 7月 20 11:58 6 -> socket:[31961]
lrwx------. 1 root root 64 7月 20 11:58 7 -> socket:[31962]
lrwx------. 1 root root 64 7月 20 11:58 8 -> anon_inode:[eventpoll]
worker进程
ll /proc/2754/fd
总用量 0
lrwx------. 1 root root 64 7月 20 14:03 0 -> /dev/null
lrwx------. 1 root root 64 7月 20 14:03 1 -> /dev/null
lrwx------. 1 root root 64 7月 20 14:03 2 -> /dev/null
lrwx------. 1 root root 64 7月 20 14:03 3 -> /tmp/.ZendSem.RN2DDT (deleted)
lrwx------. 1 root root 64 7月 20 14:03 9 -> socket:[31962]
使用strace分别监控2753,2754。
监控worker进程
strace -f -p2754
阻塞在这里,
accept(9,
仔细观察socket后面的编号就会发现,worker进程的9就是master进程的7,理解网络编程的同学就知道,master中的8是epoll_create产生的,用来实现epoll用的。
master进程先创建sockert监听9000端口,得到7,然后fork子进程的时候,每个子进程都会继承7;然后子进程去accept来自nginx的连接。
发送请求 http://192.168.56.101/info.php
当有请求过来时,打印信息:
accept(9, {sa_family=AF_INET, sin_port=htons(51040), sin_addr=inet_addr("127.0.0.1")}, [16]) = 4
poll([{fd=4, events=POLLIN}], 1, 5000) = 1 ([{fd=4, revents=POLLIN}])
times({tms_utime=0, tms_stime=0, tms_cutime=0, tms_cstime=0}) = 432194947
read(4, "\1\1\0\1\0\10\0\0", 8) = 8
read(4, "\0\1\0\0\0\0\0\0", 8) = 8
read(4, "\1\4\0\1\2\216\2\0", 8) = 8
read(4, "\17\22SCRIPT_FILENAME/data/web/info."..., 656) = 656
read(4, "\1\4\0\1\0\0\0\0", 8) = 8
lstat("/data/web/info.php", 0x7fff6909d2e0) = -1 EACCES (Permission denied)
stat("/data/web", {st_mode=S_IFDIR|0777, st_size=38, ...}) = 0
stat("/data", {st_mode=S_IFDIR|0777, st_size=28, ...}) = 0
stat("", 0x7fff6909f740) = -1 ENOENT (No such file or directory)
rt_sigaction(SIGPROF, NULL, {0x5611fd07ca10, ~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], SA_RESTORER|SA_SIGINFO, 0x7fbb7cd19400}, 8) = 0
rt_sigaction(SIGHUP, NULL, {0x5611fd07ca10, ~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], SA_RESTORER|SA_SIGINFO, 0x7fbb7cd19400}, 8) = 0
rt_sigaction(SIGINT, NULL, {0x5611fd07ca10, ~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], SA_RESTORER|SA_SIGINFO, 0x7fbb7cd19400}, 8) = 0
rt_sigaction(SIGQUIT, NULL, {0x5611fd07ca10, ~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], SA_RESTORER|SA_SIGINFO, 0x7fbb7cd19400}, 8) = 0
rt_sigaction(SIGTERM, NULL, {0x5611fd07ca10, ~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], SA_RESTORER|SA_SIGINFO, 0x7fbb7cd19400}, 8) = 0
rt_sigaction(SIGUSR1, NULL, {0x5611fd07ca10, ~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], SA_RESTORER|SA_SIGINFO, 0x7fbb7cd19400}, 8) = 0
rt_sigaction(SIGUSR2, NULL, {0x5611fd07ca10, ~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], SA_RESTORER|SA_SIGINFO, 0x7fbb7cd19400}, 8) = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={60, 0}}, NULL) = 0
rt_sigaction(SIGPROF, {0x5611fd07ca10, ~[ILL TRAP ABRT BUS FPE KILL SEGV CONT STOP TSTP TTIN TTOU SYS RTMIN RT_1], SA_RESTORER|SA_SIGINFO, 0x7fbb7cd19400}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [PROF], NULL, 8) = 0
times({tms_utime=0, tms_stime=0, tms_cutime=0, tms_cstime=0}) = 432194948
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
fcntl(3, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = 0
write(4, "\1\7\0\1\0\27\1\0Primary script unknown\n\0"..., 168) = 168
shutdown(4, SHUT_WR) = 0
recvfrom(4, "\1\5\0\1\0\0\0\0", 8, 0, NULL, NULL) = 8
recvfrom(4, "", 8, 0, NULL, NULL) = 0
close(4) = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
accept(9,
worker进程接收连接,处理请求,响应请求。
监控master进程
strace -f -p2753
重复的打印如下信息,而不论是否有请求过来:
epoll_wait(8, [], 1, 740) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 1000) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 1000) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 1000) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 1000) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 1000) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 1000) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 1000) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 1000) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 1000) = 0
getsockopt(7, SOL_TCP, TCP_INFO, "\n\0\0\0\0\0\0\0@B\17\0\0\0\0\0\30\2\0\0\0\0\0\0\0\0\0\0\200\0\0\0"..., [104]) = 0
epoll_wait(8, [], 1, 244) = 0
socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 9
getsockopt(9, SOL_SOCKET, SO_SNDBUF, [212992], [4]) = 0
setsockopt(9, SOL_SOCKET, SO_SNDBUFFORCE, [8388608], 4) = -1 EPERM (Operation not permitted)
setsockopt(9, SOL_SOCKET, SO_SNDBUF, [8388608], 4) = 0
getuid() = 0
geteuid() = 0
getgid() = 0
getegid() = 0
sendmsg(9, {msg_name(21)={sa_family=AF_LOCAL, sun_path="/run/systemd/notify"}, msg_iov(1)=[{"READY=1\nSTATUS=Processes active:"..., 84}], msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 84
close(9)
master进程不维护连接,只维护进程池,进程信息等。
子进程的信息,状态等各种数据都是通过共享内存的方式与master,以及其他子进程共享的。
关于php-fpm源码解读:
https://blog.csdn.net/raoxiaoya/article/details/107469567