pipering.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <u.h>
  2. #include <lib9.h>
  3. int nring = 32;
  4. int niter = 1024;
  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. fprint(2, "Running with %d pipes %d times\n", nring, niter);
  31. int *pfd = malloc(sizeof(int)* 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++ ) fprint(2, "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) fprint(2, "Forked. pid %d\n", getpid());
  46. for(i = 0; debug && i < nring; i++ ) fprint(2, "pfd[%d,%d]\n", pfd[i*2],pfd[i*2+1]);
  47. for(i = 0; i < niter; i++) {
  48. if (debug) fprint(2, "%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. print("PASS\n");
  58. exits("PASS");
  59. }