+static int get_next_tid(int pid, DIR **dir)
+{
+ int error;
+
+ if (*dir == NULL) {
+ char path[64];
+
+ snprintf(path, sizeof(path), "/proc/%d/task/", pid);
+ *dir = opendir(path);
+ if (!*dir) {
+ error = errno;
+ printf("Failed to open %s directory: %s\n",
+ path,
+ strerror(error));
+ return -1;
+ }
+ }
+
+ return parse_pid(dir);
+}
+
+static int get_next_pid(void)
+{
+ static DIR *dir = NULL;
+ int error;
+
+ if (!dir) {
+ dir = opendir("/proc");
+ if (!dir) {
+ error = errno;
+ printf("Failed to open /proc directory: %s\n",
+ strerror(error));
+ return -1;
+ }
+ }
+
+ return parse_pid(&dir);
+}
+
+static void read_pageframe_with_threads(int pid,
+ struct pageframe *pageframe,
+ struct process **process_list,
+ struct parse_opts *opts)
+{
+ DIR *dir = NULL;
+ int tid;
+
+ while (1) {
+ tid = get_next_tid(pid, &dir);
+
+ if (tid <= 0)
+ return;
+
+ read_pageframe(pid, pid, pageframe, process_list, opts);
+ }
+}
+