root/user/testbin/userthreads/userthreads.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. BladeRunner
  3. ThreadRunner

   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 /*
  31  * Test multiple user level threads inside a process. The program
  32  * forks 3 threads off 2 to functions, each of which displays a string
  33  * every once in a while.
  34  *
  35  * This won't do much of anything unless you implement user-level
  36  * threads.
  37  *
  38  * It also makes various assumptions about the thread API. In
  39  * particular, it believes (1) that you create a thread by calling
  40  * "threadfork()" and passing the address for execution of the new
  41  * thread to begin at, (2) that if the parent thread exits any child
  42  * threads will keep running, and (3) child threads will exit if they
  43  * return from the function they started in. If any or all of these
  44  * assumptions are not met by your user-level threads, you will need
  45  * to patch this test accordingly.
  46  *
  47  * This is also a rather basic test and you'll probably want to write
  48  * some more of your own.
  49  */
  50 
  51 
  52 #include <unistd.h>
  53 #include <stdio.h>
  54 
  55 #define NTHREADS  3
  56 #define MAX       1<<25
  57 
  58 /* counter for the loop in the threads : 
  59    This variable is shared and incremented by each 
  60    thread during his computation */
  61 volatile int count = 0;
  62 
  63 /* the 2 threads : */
  64 void ThreadRunner(void);
  65 void BladeRunner(void);
  66 
  67 int
  68 main(int argc, char *argv[])
  69 {
  70     int i;
  71 
  72     (void)argc;
  73     (void)argv;
  74 
  75     for (i=0; i<NTHREADS; i++) {
  76         if (i)
  77             threadfork(ThreadRunner);
  78         else
  79             threadfork(BladeRunner);
  80     }
  81 
  82     printf("Parent has left.\n");
  83     return 0;
  84 }
  85 
  86 /* multiple threads will simply print out the global variable.
  87    Even though there is no synchronization, we should get some 
  88    random results.
  89 */
  90 
  91 void
  92 BladeRunner()
  93 {
  94     while (count < MAX) {
  95         if (count % 500 == 0)
  96             printf("Blade ");
  97         count++;
  98     }
  99 }
 100 
 101 void
 102 ThreadRunner()
 103 {
 104     while (count < MAX) {
 105         if (count % 513 == 0)
 106             printf(" Runner\n");
 107         count++;
 108     }
 109 }
 110     

/* [<][>][^][v][top][bottom][index][help] */