当前位置:网站首页>PHP interprocess pass file descriptor
PHP interprocess pass file descriptor
2022-06-25 23:47:00 【ndrandy】
What are the practical application scenarios for passing file descriptors between processes ?
- Recently I saw golang Server smooth hot restart reload when , Need long links not to be broken , need fork A sub process comes out , Then all of the parent process socket The file of The descriptor ( namely : all hold Client connections for ) Are passed to the child process . No research linux Before passing file descriptors between processes , All I have in mind is fork when , happen copy-on-write, The parent and child processes can share the connection directly fd 了 , But at this point , If the parent process exits , All the connections were cut off , Because the parent and child processes share the connection resources . So we need to fd The associated resources are complete clone Into child processes , Make the parent-child process correspond to fd Completely divorced , however fd From the parent process copy After the child process , to want to fd value (int) All in line , Is unlikely to , Because the child process needs to be reallocated fd Of .
How to implement interprocess transfer fd Well ?
- Probably through unixsockt, one end sendmsg, The other end recvmsg, But it is not a simple transmission of message content , You need to tell the kernel that the file descriptor is transferred SCM_RIGHTS. Let's go straight to php Version of demo
<?php
$fds = [];
$ret = socket_create_pair(AF_UNIX, SOCK_DGRAM, 0, $fds);
if (!$ret) {
exit(socket_strerror(socket_last_error()));
}
$pid = pcntl_fork();
if ($pid > 0) {
socket_close($fds[0]);
$handle = fopen(__DIR__ . '/atomic.txt', 'a+');
var_dump($handle);
socket_sendmsg($fds[1], [
'control' => [
[
'level' => SOL_SOCKET,
'type' => SCM_RIGHTS,
'data' => [$handle]
]
]
], 0);
sleep(1);
} else {
socket_close($fds[1]);
$data = [
'controllen' => socket_cmsg_space(SOL_SOCKET, SCM_RIGHTS, 1)
];
$result = socket_recvmsg($fds[0], $data, 0);
var_dump("recv ok\n");
var_dump($data['control'][0]['data'][0]);
fwrite($data['control'][0]['data'][0], 'passing fd in PHP' . PHP_EOL);
}In the above code , The parent process var_dump($handle), And subprocesses var_dump($data['control'][0]['data'][0]); Twice printed resource The resource type int The value has changed , explain fd It's complete clone In the past , The parent process exits , It will not affect the subprocess fd 了 .
边栏推荐
- STL教程5-STL基本概念及String和vector使用
- QT Chinese and English use different fonts respectively
- Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
- The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high
- IDEA中如何生成get/set方法
- C# IO Stream 流(二)扩展类_封装器
- 提取系统apk
- 达梦数据库修改字段信息采坑记
- The software test interview has been suspended. The interviewer always says that the logical thinking is chaotic. What should I do?
- B. Box Fitting-CodeCraft-21 and Codeforces Round #711 (Div. 2)
猜你喜欢

如何设计产品的roadmap?

YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21

SSL/TLS、对称加密和非对称加密和TLSv1.3

解析产品开发失败的5个根本原因

unsigned与signed之大白话

STL教程5-STL基本概念及String和vector使用

How does excel translate Chinese words into English automatically? This formula teaches you

提取系统apk

The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high

ACM. Hj16 shopping list ●●
随机推荐
iomanip头文件在实战中的作用
(serial port Lora module) centrida rf-al42uh private protocol test at instruction test communication process
二叉排序树
The package name of the manifest file in the library project and the app project are not the same
后序线索二叉树
unsigned与signed之大白话
Record the ideas and precautions for QT to output a small number of pictures to mp4
Px4 multi computer simulation (gazebo)
CSDN add on page Jump and off page specified paragraph jump
库项目和App项目中清单文件的包名不要相同
社招两年半10个公司28轮面试面经(含字节、拼多多、美团、滴滴......)
CSDN添加页内跳转和页外指定段落跳转
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
两种块级元素居中的方式
php进程间传递文件描述符
如何进行流程创新,以最经济的方式提升产品体验?
UE4 learning records create a role and control its movement
B. Box Fitting-CodeCraft-21 and Codeforces Round #711 (Div. 2)
Use of xinchida ble 5.0 Low Power Bluetooth module (at command serial port transparent transmission) rsbrs02abr
oracle写一个先插入一条数据,在将该数据中一个字段更新的触发器的坑