whalemating.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2001, 2002, 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. * Driver code for whale mating problem
  31. */
  32. #include <types.h>
  33. #include <lib.h>
  34. #include <thread.h>
  35. #include <test.h>
  36. #define NMATING 10
  37. static
  38. void
  39. male(void *p, unsigned long which)
  40. {
  41. (void)p;
  42. kprintf("male whale #%ld starting\n", which);
  43. // Implement this function
  44. }
  45. static
  46. void
  47. female(void *p, unsigned long which)
  48. {
  49. (void)p;
  50. kprintf("female whale #%ld starting\n", which);
  51. // Implement this function
  52. }
  53. static
  54. void
  55. matchmaker(void *p, unsigned long which)
  56. {
  57. (void)p;
  58. kprintf("matchmaker whale #%ld starting\n", which);
  59. // Implement this function
  60. }
  61. // Change this function as necessary
  62. int
  63. whalemating(int nargs, char **args)
  64. {
  65. int i, j, err=0;
  66. (void)nargs;
  67. (void)args;
  68. for (i = 0; i < 3; i++) {
  69. for (j = 0; j < NMATING; j++) {
  70. #ifdef UW
  71. switch(i) {
  72. case 0:
  73. err = thread_fork("Male Whale Thread", NULL,
  74. male, NULL, j);
  75. break;
  76. case 1:
  77. err = thread_fork("Female Whale Thread", NULL,
  78. female, NULL, j);
  79. break;
  80. case 2:
  81. err = thread_fork("Matchmaker Whale Thread", NULL,
  82. matchmaker, NULL, j);
  83. break;
  84. }
  85. #else
  86. switch(i) {
  87. case 0:
  88. err = thread_fork("Male Whale Thread",
  89. male, NULL, j, NULL);
  90. break;
  91. case 1:
  92. err = thread_fork("Female Whale Thread",
  93. female, NULL, j, NULL);
  94. break;
  95. case 2:
  96. err = thread_fork("Matchmaker Whale Thread",
  97. matchmaker, NULL, j, NULL);
  98. break;
  99. }
  100. #endif /* UW */
  101. if (err) {
  102. panic("whalemating: thread_fork failed: %s)\n",
  103. strerror(err));
  104. }
  105. }
  106. }
  107. return 0;
  108. }