userthreads.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Test multiple user level threads inside a process. The program
  31. * forks 3 threads off 2 to functions, each of which displays a string
  32. * every once in a while.
  33. *
  34. * This won't do much of anything unless you implement user-level
  35. * threads.
  36. *
  37. * It also makes various assumptions about the thread API. In
  38. * particular, it believes (1) that you create a thread by calling
  39. * "threadfork()" and passing the address for execution of the new
  40. * thread to begin at, (2) that if the parent thread exits any child
  41. * threads will keep running, and (3) child threads will exit if they
  42. * return from the function they started in. If any or all of these
  43. * assumptions are not met by your user-level threads, you will need
  44. * to patch this test accordingly.
  45. *
  46. * This is also a rather basic test and you'll probably want to write
  47. * some more of your own.
  48. */
  49. #include <unistd.h>
  50. #include <stdio.h>
  51. #define NTHREADS 3
  52. #define MAX 1<<25
  53. /* counter for the loop in the threads :
  54. This variable is shared and incremented by each
  55. thread during his computation */
  56. volatile int count = 0;
  57. /* the 2 threads : */
  58. void ThreadRunner(void);
  59. void BladeRunner(void);
  60. int
  61. main(int argc, char *argv[])
  62. {
  63. int i;
  64. (void)argc;
  65. (void)argv;
  66. for (i=0; i<NTHREADS; i++) {
  67. if (i)
  68. threadfork(ThreadRunner);
  69. else
  70. threadfork(BladeRunner);
  71. }
  72. printf("Parent has left.\n");
  73. return 0;
  74. }
  75. /* multiple threads will simply print out the global variable.
  76. Even though there is no synchronization, we should get some
  77. random results.
  78. */
  79. void
  80. BladeRunner()
  81. {
  82. while (count < MAX) {
  83. if (count % 500 == 0)
  84. printf("Blade ");
  85. count++;
  86. }
  87. }
  88. void
  89. ThreadRunner()
  90. {
  91. while (count < MAX) {
  92. if (count % 513 == 0)
  93. printf(" Runner\n");
  94. count++;
  95. }
  96. }