【发布时间】:2021-04-21 06:36:43
【问题描述】:
我已经有 3 个程序,
分别通过 TLS 获取传感器数据并将其发送到我的远程服务器。
我想减少 TLS 标头,
所以我决定将上述程序分开
3 * (传感器数据获取程序) + 1 * (tls 只发送程序),
使用命名管道作为进程间通信(不是套接字)。
但是现在我想知道哪个更好
-
使用 1 个命名管道和 3 个写入器 + 1 个读取器
#!/bin/sh mkfifo /tmp/tls/pipe /app/sensor1 & >> /tmp/tls/pipe /app/sensor2 & >> /tmp/tls/pipe /app/sensor3 & >> /tmp/tls/pipe /app/tls_sender /tmp/tls/pipe -
使用每个写入器的 3 个命名管道 + 在读取器中使用像
select()这样的 IO 多路复用?#!/bin/sh mkfifo /tmp/tls/pipe1 mkfifo /tmp/tls/pipe2 mkfifo /tmp/tls/pipe3 /app/sensor1 & >> /tmp/tls/pipe1 /app/sensor2 & >> /tmp/tls/pipe2 /app/sensor3 & >> /tmp/tls/pipe3 /app/tls_sender /tmp/tls/pipe1 /tmp/tls/pipe2 /tmp/tls/pipe3
与 tls 发件人类似
#define SENSOR_NUM 3
int tls_flags[SENSOR_NUM];
char huge_buffer[HUGE_NUM];
int tls_send(int idx, char* buf) {
// set flags for each readfds index
// if all index counts to some threshold, send huge_buffer at once
}
int main(int argc, char *argv[]) {
...
int fd[SENSOR_NUM];
int state;
char buf[255];
fd_set readfds;
FD_ZERO(&readfds);
for(int i=0; i<SENSOR_NUM; i++)
{
fd[i] = open(argv[i+1], O_RDONLY);
FD_SET(fd[i], &readfds);
}
while(1) {
state = select(fd[2]+1, &readfs, NULL, NULL, NULL);
switch(state)
{
// case -1: error exception
// case 0 : no send
default:
for (int i=0; i<SENSOR_NUM; i++){
if (FD_ISSET(fd[i], &readfds))
read(fd[i], buf, 255);
tls_send(i, buf);
}
break;
}
}
...
}
我猜前者会更容易实现和更快,但后者会更稳定,但我不确定。
不使用信号量之类的,前者是否足够稳定?
还是后者更快或更容易受到攻击?
甚至没有 PIPE 的共享内存方法就足够了?
哪个更好,值得推荐?
谢谢。
【问题讨论】:
-
您是否需要跟踪数据的来源(哪个传感器)?除非传感器程序输出的传感器数据在其输出中包含该数据,否则第一种选择将无法判断数据的来源。
-
实际上每个传感器应用程序都将数据编码和屏蔽数据包,数据包协议有自己的传感器ID!谢谢关心。
-
/app/sensor1 & >> /tmp/tls/pipe&通常放在末端,比如首选/app/sensor1 >> /tmp/tls/pipe &,以免与/app/sensor1 &>> /tmp/tls/pipe混淆。