在/ proc上使用pthread进行分段错误

时间:2015-09-18 13:27:00

标签: c linux segmentation-fault pthreads

我在特定事件上创建了一定数量的pthread。每个pthread都会对/ proc进行扫描,但过了一段时间我会收到分段错误。如果有人帮助我,我真的很感激

在pthread函数上循环,我得到分段错误

while ((topproc_dent = readdir(topproc_dir)) != NULL)
{
  if (topproc_dent->d_name[0] < '0' || topproc_dent->d_name[0] > '9') /* Not a process */
    continue;
  pid = atoi(topproc_dent->d_name);
  /* Dont analize my_self */
  if (pid == my_pid)
    continue;

  snprintf(strPath, sizeof strPath, "%s%jd%s", "/proc/", (intmax_t) pid, "/fd");

  DIR *dp;
  struct dirent *ep;
  printf("%s\n", strPath);
  dp = opendir(strPath);
  if (dp != NULL)
  {
    while (ep = readdir(dp))
    {
      if (strcmp("..", ep->d_name) == 0)
        continue;
      if (strcmp(".", ep->d_name) == 0)
        continue;
      snprintf(strLink, sizeof strPath, "%s%s%s", strPath, "/", ep->d_name);
      printf("%s\n", strLink);
      if ((len = readlink(strLink, buf, sizeof(buf) - 1)) != -1)
        buf[len] = '\0';
      printf("%s\n", buf);
    }
    (void) closedir(dp);
  }
  else
    perror("Couldn't open the directory");
  closedir(dp);
}

0 个答案:

没有答案