proc.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <dirent.h>
  5. #include <fcntl.h>
  6. #include <sys/wait.h>
  7. int main(int argc, char ** argv)
  8. {
  9. struct dirent * dirent;
  10. DIR * dir;
  11. dir = opendir("/proc/1");
  12. if (!dir) {
  13. perror("opendir /proc/1");
  14. exit(1);
  15. }
  16. while ((dirent = readdir(dir)))
  17. printf("/proc/1/%s\n", dirent->d_name);
  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
  22. * revisit after 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. closedir(dir);
  44. return 0;
  45. }