proc.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <dirent.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/wait.h>
  6. #include <unistd.h>
  7. int main(int argc, char** argv) {
  8. struct dirent* dirent;
  9. DIR* dir;
  10. dir = opendir("/proc/1");
  11. if (!dir) {
  12. perror("opendir /proc/1");
  13. exit(1);
  14. }
  15. while ((dirent = readdir(dir))) {
  16. printf("/proc/1/%s\n", dirent->d_name);
  17. }
  18. closedir(dir);
  19. // Children end up inheriting junk if we don't flush here.
  20. fflush(stdout);
  21. /* This code tickles a bug in exit/wait for PIDs/IPC; created an issue (#532), will revisit after
  22. * landing some related IPC fixes that are pending. */
  23. #if 0
  24. for (int i = 0 ; i < 3 ; i++) {
  25. pid_t pid = fork();
  26. if (pid < 0) {
  27. perror("fork");
  28. exit(1);
  29. }
  30. if (pid) {
  31. waitpid(pid, NULL, 0);
  32. exit(0);
  33. }
  34. }
  35. #endif
  36. dir = opendir("/proc");
  37. if (!dir) {
  38. perror("opendir /proc");
  39. exit(1);
  40. }
  41. while ((dirent = readdir(dir))) {
  42. printf("/proc/%s\n", dirent->d_name);
  43. }
  44. closedir(dir);
  45. return 0;
  46. }