tprimes.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <thread.h>
  12. int quiet;
  13. int goal;
  14. int buffer;
  15. int (*fn)(void(*)(void*), void*, uint) = threadcreate;
  16. void
  17. primethread(void *arg)
  18. {
  19. Channel *c, *nc;
  20. int p, i;
  21. c = arg;
  22. p = recvul(c);
  23. if(p > goal)
  24. threadexitsall(nil);
  25. if(!quiet)
  26. print("%d\n", p);
  27. nc = chancreate(sizeof(ulong), buffer);
  28. (*fn)(primethread, nc, 1024);
  29. for(;;){
  30. i = recvul(c);
  31. if(i%p)
  32. sendul(nc, i);
  33. }
  34. }
  35. void
  36. threadmain(int argc, char **argv)
  37. {
  38. int i;
  39. Channel *c;
  40. ARGBEGIN{
  41. case 'q':
  42. quiet = 1;
  43. break;
  44. case 'b':
  45. buffer = atoi(ARGF());
  46. break;
  47. case 'p':
  48. fn=proccreate;
  49. break;
  50. }ARGEND
  51. if(argc>0)
  52. goal = atoi(argv[0]);
  53. else
  54. goal = 100;
  55. c = chancreate(sizeof(ulong), buffer);
  56. threadcreate(primethread, c, 1024);
  57. for(i=2;; i++)
  58. sendul(c, i);
  59. }