微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 C++ 中将无符号字符布隆过滤器写入 fifo

如何解决在 C++ 中将无符号字符布隆过滤器写入 fifo

我想知道如何安全地将 unsigned char* 数组写入 fifo。我尝试将数组转换为 char*,但我认为内容不能安全地写入或读取,因为我得到的结果不正确。该数组是一个布隆过滤器。

我想要做的是将布隆过滤器从子进程转移到父进程,这样父进程就可以拥有完全相同的布隆过滤器。

(来自 Wiki:布隆过滤器是一种节省空间的概率数据结构,用于测试元素是否是集合的成员。误报匹配是可能的,但漏报不是 - 在换句话说,查询返回“可能在集合中”或“绝对不在集合中”)

子进程有一个列表,比如说病毒列表,每个节点都有一个布隆过滤器。父进程为每个子进程派生一个类,用于在每个病毒节点中存储布隆过滤器

这是我目前在子进程中的代码

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 *)&current->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 *)&current->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,10);
  sprintf(buffer,"%d@",length);
  write(fd,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,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,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 字节,这将不起作用。

切换到memcpyrealloc。或者更好 - 使用 vectors。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。