Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试使用FIFO发送消息时出错_C++_Process_Operating System_Pipe_Fork - Fatal编程技术网

C++ 尝试使用FIFO发送消息时出错

C++ 尝试使用FIFO发送消息时出错,c++,process,operating-system,pipe,fork,C++,Process,Operating System,Pipe,Fork,我是这类编程的新手,所以如果这是一个垃圾问题,我很抱歉。我正在尝试做一个非常简单的任务,但我似乎不知道哪里出了问题 我有一个创建多个子进程的父进程,通过使用FIFO,我想向所有子进程发送一条消息(例如“hi”),进程接收到get消息,但无论我做什么,都会出现错误,似乎无法找到错误 以下是父级的主要功能: int main(int argc, char *argv[]) { int num_monitors, buf_size; //command line arguments num_m

我是这类编程的新手,所以如果这是一个垃圾问题,我很抱歉。我正在尝试做一个非常简单的任务,但我似乎不知道哪里出了问题

我有一个创建多个子进程的父进程,通过使用FIFO,我想向所有子进程发送一条消息(例如“hi”),进程接收到get消息,但无论我做什么,都会出现错误,似乎无法找到错误

以下是父级的主要功能:

int main(int argc, char *argv[])
{
  int num_monitors, buf_size; //command line arguments
  num_monitors = stoi(argv[1]);
  buf_size = stoi(argv[2]);

  // Structures to store monitor info
  Monitors *m_info = new Monitors[num_monitors]; // Stores monitor pid & pipe ends

  create_n_monitors(m_info,num_monitors,buf_size,input_dir_path);
  sleep(1); // making sure all pipes get created

  for(int i=0; i<num_monitors; i++) // opening the write end now that the monitors have been created
  {
    m_info[i].write_fd = open(m_info[i].write_p, O_WRONLY | O_NONBLOCK);
    if (m_info[i].write_fd == -1){perror("open @ 27 main parent");exit(1);}
  }

  for(int i=0; i<num_monitors; i++)
    send_message(m_info[i].write_fd, (char*)"hi", buf_size);

  delete [] m_info;
  return 0;
}
int create_unique_fifo(char* read_fifo)
{
  int read_fd;
    struct stat stat_temp;

  // Create fifos
    if(stat(read_fifo, &stat_temp) == -1){
        if (mkfifo(read_fifo,0666) < 0 ){perror("mkfifo @ unique_fifo 37");exit(1);}
    }   

    read_fd = open(read_fifo, O_RDONLY);  // Open named pipe for reading
  if (read_fd == -1){perror("open @ monitor.cpp 1"); exit(1);}
  return read_fd;
}


int main(int argc, char* argv[])
{
  int buf_size = stoi(argv[1]);  // Process command line args
  char read_fifo[100], write_fifo[100];
  strcpy(write_fifo, argv[2]); // parent read - monitor write
  strcpy(read_fifo, argv[3]);  // parent write - monitor read

  int read_fd, write_fd;
  read_fd = create_unique_fifo(read_fifo);

  write_fd = open(write_fifo, O_WRONLY | O_NONBLOCK);  // Open named pipe for writing
  if (write_fd == -1){perror("open @ monitor.cpp 2"); exit(1);}

  char* message;
  message = read_message(read_fd, buf_size);
  cout << "2:" << message << endl;
  return 0;
}
以下是我如何创建流程和管道(FIFO):


知道为什么会这样吗?我在创建进程后添加了sleep(1),以确保所有FIFO都能及时创建,但仍然会出现此错误。。。非常感谢任何帮助

发送消息
中,您总是从
缓冲区
写入9个字节,但并非所有这些字节都写入了值。这是因为填充
缓冲区的
sprintf
只写入前几个位置,可能只有3个

解决方案是将它们全部初始化为0

char buffer[10] = 0;
或者只写入字符串中的字节数。从
sprintf
返回的值很容易看出这一点

auto buf_len = sprintf(buffer, "%d@", length);
write(fd, buffer, buf_len); 

你说得对!缺少将缓冲区初始化为0。非常感谢您祝您度过愉快的一天:)
==29783== Syscall param write(buf) points to uninitialised byte(s)
==29783==    at 0x4B691E7: write (write.c:26)
==29783==    by 0x10B85B: send_message(int, char*, int) (in /home/sofia/Desktop/syspro-2/travelMonitor)
==29783==    by 0x10A80F: main (in /home/sofia/Desktop/syspro-2/travelMonitor)
==29783==  Address 0x1ffefffc81 is on thread 1's stack
==29783==  in frame #1, created by send_message(int, char*, int) (???:)
==29783== 
2:hi
2:hi
2:hi
2:hi
2:hi
==29783== 
==29783== HEAP SUMMARY:
==29783==     in use at exit: 0 bytes in 0 blocks
==29783==   total heap usage: 4 allocs, 4 frees, 138,864 bytes allocated
==29783== 
==29783== All heap blocks were freed -- no leaks are possible
==29783== 
==29783== Use --track-origins=yes to see where uninitialised values come from
==29783== For lists of detected and suppressed errors, rerun with: -s
==29783== ERROR SUMMARY: 5 errors from 1 contexts (suppressed: 0 from 0)
char buffer[10] = 0;
auto buf_len = sprintf(buffer, "%d@", length);
write(fd, buffer, buf_len);