c++ - 在 C++ 中将 unsigned char bloomfilter 写入 fifo
问题描述
我想知道如何安全地将 unsigned char* 数组写入 fifo。我曾尝试将数组转换为 char*,但我不认为内容是安全地写入或读取的,因为我得到的结果不正确。该数组是一个布隆过滤器。
我想要做的是将bloomfilter从子进程转移到父进程,以便父进程可以拥有相同的确切bloomfilter。
(来自 Wiki:Bloom 过滤器是一种节省空间的概率数据结构,用于测试元素是否是集合的成员。可能会出现误报匹配,但不会出现误报 - 换句话说,查询返回“可能在集合中”或“绝对不在集合中”)
子进程有一个列表,比如说病毒列表,每个节点都有一个bloomfilter。父进程对每个子进程都有一个类,用于将bloomfilter存储在每个病毒节点中
这是我到目前为止在子进程中的代码:
void send_bloomfilters(int write_fd, int buf_size)
{
char buffer[5000]; // buffer used for concatination of bloomfilters
char temp[200];
VirusNode* current = virus_list.head;
strcpy(buffer, (char *)¤t->bf[0]); // coping the bloomfilter to buffer
while(current != NULL) // sending all bloomfilter in one buffer like so: bf1#bf2#bf3#...
{
sprintf(temp, "#%s", (char *)¤t->bf[0]);
strcat(buffer,temp);
current = current->next;
}
send_message(write_fd, buffer, buf_size); //finally sending the buffer to parent
}
// Sends <message> to file descriptor <fd> per <buf_size> characters
void send_message(int fd, char *message, int buf_size)
{
// writing the length of the message to be received
int length = strlen(message);
char buffer[10];
memset(buffer, 0, 10);
sprintf(buffer, "%d@", length);
write(fd, buffer, 9); // sending the number of bytes reader is about to read
int buffer_size = buf_size;
char * input_write = new char[length + 1];
strcpy(input_write,message); // coping the message we want to send
char * str = input_write; // pointer to the array
int bytes_written = 0, total_bytes = 0; // We might need to write less or more bytes
buffer_size = length < buffer_size ? length : buffer_size; // than <buf_size>
while(total_bytes < length)
{
str += bytes_written; // move str pointer
bytes_written = write(fd, str, buffer_size); //and write the next <buffer_size> characters
total_bytes += bytes_written; // adding them to the total amount of bytes written altogether
if((total_bytes + buffer_size) > length)
buffer_size = length - total_bytes; // reading exactly the amount that's left
}
delete [] input_write;
}
这是父进程的代码:
void receive_bloomfilters(Children *children_info, int num_children, int buf_size)
{
struct pollfd filedescs[num_children];
char * buf;
char * bloomfilter;
// while there are still monitors left to send a message
while(children_sent_remain(m_info, num_children)) //function checks how many children are left to read from
{
for (int i = 0; i < num_children; i++)
{
filedescs[i].fd = children_info[i].read_fd;
filedescs[i].events = POLLIN;
}
if(poll(filedescs,num_children,10) < 0 ) printf("poll blocked\n");
for (int i = 0; i < num_children; i++)
{
if((filedescs[i].revents & POLLIN))
{
if(filedescs[i].fd == children_info[i].read_fd)
{
buf = read_message(children_info[i].read_fd, buf_size);
VirusNode* current = m_info[i].virus_list->head;
bloomfilter = strtok (buf,"#");
while (bloomfilter != NULL && current != NULL)
{
current->InsertBloom(bloomfilter);
current = current->next;
bloomfilter = strtok (NULL, "#");
}
delete [] buf;
m_info[i].not_sent_yet = false; // flag=false now that we read the message from <i> child
}
}
}
}
}
void VirusNode::InsertBloom(char* bloomfilter) {
monitor_bloom->bf = (unsigned char*)bloomfilter;
}
// Reads a message from <fd> and returns it.
char *read_message(int read_end_fd, int buf_size)
{
char buffer[10];
int fifo_buffer_size = buf_size;
read(read_end_fd, buffer, 9);
char * tok = strtok(buffer, "@");
int length = atoi(tok); // how many characters will be received
char * input_read = new char[length + 1];
char * str = input_read;
int bytes_read = 0, total_bytes = 0; // We might need to read less or more bytes
fifo_buffer_size = length < fifo_buffer_size ? length : fifo_buffer_size; // than <buf_size>
while(total_bytes < length)
{
str += bytes_read; // move str pointer
bytes_read = read(read_end_fd, str, fifo_buffer_size); //and read the next <buf_size> characters
total_bytes += bytes_read; // adding them to the total amount of bytes read altogether
if((total_bytes + fifo_buffer_size) > length)
fifo_buffer_size = length - total_bytes; // reading exactly the amount that's left
}
input_read[length] = '\0';
return input_read;
}
我是一个初学者学习编码,所以任何帮助将不胜感激。有什么我做错了吗?
解决方案
您正在使用str-
函数来操作字节缓冲区。他们假设字符串是以 0 结尾的,因此如果您的过滤器中有 0 字节,这将不起作用。
切换到memcpy
, realloc
。或者更好 - 使用vector
s.
推荐阅读
- azure-ad-b2c - Azure B2C 自定义注册页面验证问题
- arrays - Swift 2 从包含字符串的数组中删除对象
- regex - 从文本文件中删除行号
- excel - 同时更改循环VBA的变量
- file - Netlogo 文件打开错误“预期 nlogo 文件有 12 个部分,这有 1 个”
- apache-kafka - 以最小延迟向 Kafka 添加元素的快速协议?
- ios - UICollectionView 自定义布局单元格类型相关属性
- java - 骰子 JAVAFX 的最后一个值
- android - 应用程序快捷方式:minSdk<25 的 Lint 警告
- scala - 带有 Ordering 和 ClassTag 的 scala 多态类型签名