proc.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <dirent.h>
  7. #include <fcntl.h>
  8. #include <sys/wait.h>
  9. int main(int argc, char ** argv)
  10. {
  11. struct dirent * dirent;
  12. DIR * dir;
  13. dir = opendir("/proc/1");
  14. if (!dir) {
  15. perror("opendir /proc/1");
  16. exit(1);
  17. }
  18. while ((dirent = readdir(dir)))
  19. printf("/proc/1/%s\n", dirent->d_name);
  20. closedir(dir);
  21. for (int i = 0 ; i < 3 ; i++) {
  22. pid_t pid = fork();
  23. if (pid < 0) {
  24. perror("fork");
  25. exit(1);
  26. }
  27. if (pid) {
  28. waitpid(pid, NULL, 0);
  29. exit(0);
  30. }
  31. }
  32. dir = opendir("/proc");
  33. if (!dir) {
  34. perror("opendir /proc");
  35. exit(1);
  36. }
  37. while ((dirent = readdir(dir)))
  38. printf("/proc/%s\n", dirent->d_name);
  39. closedir(dir);
  40. return 0;
  41. }