pipering.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <u.h>
  2. #include <libc.h>
  3. int nring = 32;
  4. int niter = 65536;
  5. int debug = 0;
  6. char data[8192];
  7. int i;
  8. int id;
  9. int intr = 0;
  10. void
  11. handler(void *v, char *s)
  12. {
  13. print("%d: %d iterations\n", id, i);
  14. if (intr++ < 16)
  15. noted(NCONT);
  16. exits("too many interrupts");
  17. }
  18. void
  19. main(int argc, char *argv[])
  20. {
  21. int pid;
  22. if (notify(handler)){
  23. sysfatal("notify: %r");
  24. }
  25. if (argc > 1)
  26. nring = strtoull(argv[1], 0, 0);
  27. if (argc > 2)
  28. niter = strtoull(argv[2], 0, 0);
  29. debug = argc > 3;
  30. print("Running with %d pipes %d times\n", nring, niter);
  31. int *pfd = malloc(sizeof(*pipe)* nring * 2);
  32. if (pfd == nil)
  33. sysfatal(smprint("alloc %d pipes: %r", nring*2));
  34. for(i = 0; i < nring; i++)
  35. if (pipe(&pfd[i*2]) < 0)
  36. sysfatal(smprint("create pipe pair %d of %d: %r", i, nring));
  37. for(i = 0; debug && i < nring; i++ ) print("pfd[%d,%d]\n", pfd[i*2],pfd[i*2+1]);
  38. for(i = 0, id = 0; i < nring-1; i++, id++) {
  39. pid = fork();
  40. if (pid == 0)
  41. break;
  42. if (pid < 0)
  43. sysfatal("fork");
  44. }
  45. if (debug) print("Forked. pid %d\n", getpid());
  46. for(i = 0; debug && i < nring; i++ ) print("pfd[%d,%d]\n", pfd[i*2],pfd[i*2+1]);
  47. for(i = 0; i < niter; i++) {
  48. if (debug) print("%d: write %d\n", id, pfd[(id%nring)*2+1]);
  49. int amt = write(pfd[(id%nring)*2+1], data, sizeof(data));
  50. if (amt < sizeof(data))
  51. sysfatal(smprint("%d: write only got %d of %d: %r", id, amt, sizeof(data)));
  52. if (debug) print("%d: read %d\n", id, pfd[(id%nring)*2]);
  53. amt = read(pfd[(id%nring)*2], data, sizeof(data));
  54. if (amt < sizeof(data))
  55. sysfatal(smprint("%d: read only got %d of %d: %r", id, amt, sizeof(data)));
  56. }
  57. }