2
0

pipe_fds.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. g++ pipe_fds.cpp -g -O0 -o pipe_fds
  3. About the limits:
  4. [winlin@dev6 srs]$ ulimit -n
  5. 1024
  6. [winlin@dev6 srs]$ sudo lsof -p 21182
  7. pipe_fds 21182 winlin 0u CHR 136,4 0t0 7 /dev/pts/4
  8. pipe_fds 21182 winlin 1u CHR 136,4 0t0 7 /dev/pts/4
  9. pipe_fds 21182 winlin 2u CHR 136,4 0t0 7 /dev/pts/4
  10. pipe_fds 21182 winlin 3r FIFO 0,8 0t0 464543 pipe
  11. pipe_fds 21182 winlin 1021r FIFO 0,8 0t0 465052 pipe
  12. pipe_fds 21182 winlin 1022w FIFO 0,8 0t0 465052 pipe
  13. So, all fds can be open is <1024, that is, can open 1023 files.
  14. The 0, 1, 2 is opened file, so can open 1023-3=1020files,
  15. Where 1020/2=512, so we can open 512 pipes.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <string.h>
  22. int main(int argc, char** argv)
  23. {
  24. if (argc <= 1) {
  25. printf("Usage: %s <nb_pipes>\n"
  26. " nb_pipes the pipes to open.\n"
  27. "For example:\n"
  28. " %s 1024\n", argv[0], argv[0]);
  29. exit(-1);
  30. }
  31. int nb_pipes = ::atoi(argv[1]);
  32. for (int i = 0; i < nb_pipes; i++) {
  33. int fds[2];
  34. if (pipe(fds) < 0) {
  35. printf("failed to create pipe. i=%d, errno=%d(%s)\n",
  36. i, errno, strerror(errno));
  37. break;
  38. }
  39. }
  40. printf("Press CTRL+C to quit, use bellow command to show the fds opened:\n");
  41. printf(" sudo lsof -p %d\n", getpid());
  42. sleep(-1);
  43. return 0;
  44. }