繁体   English   中英

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

[英]Mmap() Segmentation Fault Error While Reading The Stored Valued

我正在用 C 编写一个程序,并使用 mmap() 使进程将计算返回到它们的主进程。

这是代码:

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

映射不受任何文件的支持; 它的内容被初始化为零。 fd 参数被忽略; 但是,如果指定了 MAP_ANONYMOUS(或 MAP_ANON),某些实现要求 fd 为 -1,并且便携式应用程序应确保这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM