读取存储值时出现 Mmap() 分段错误

Mmap() Segmentation Fault Error While Reading The Stored Valued

我正在用 C 语言编写一个程序,并使用 mmap() 进行进程 return 计算到它们的主进程。

代码如下:

int *results = mmap ( NULL, count*sizeof(int),
 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );

for (i = 0; i < count; i++)
   {
      pid_t pid = fork();
      if (pid == 0)
      {
         /// some calculations in the child.
         }
         
         // I save the result to ith element of mmap
         results[i] = calculation;
      }
      else{
         wait(NULL);
      }
      
   }
    
   // The following part arises an error called segmentation fault.
    for (i = 0; i < count; i++)
   {
      printf(" results[%d] = %d.\n", i, results[i]);
   }

当我尝试访问结果时,出现错误。我该如何解决?

感谢任何帮助。

引用man mmap:

MAP_ANONYMOUS

The mapping is not backed by any file; its contents are initialized to zero. The fd argument is ignored; however, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this.