1 /* $OpenBSD: switch.c,v 1.3 2002/10/12 18:59:13 marc Exp $ */ 2 /* 3 * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors, 4 * proven@mit.edu All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Chris Provenzano, 17 * the University of California, Berkeley, and contributors. 18 * 4. Neither the name of Chris Provenzano, the University, nor the names of 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR 26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* ==== test_switch.c ======================================================== 36 * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu 37 * 38 * Description : Test context switch functionality. 39 * 40 * 1.00 93/08/04 proven 41 * -Started coding this file. 42 */ 43 44 #include <pthread.h> 45 #include <stdio.h> 46 #include <errno.h> 47 #include <unistd.h> 48 #include <stdlib.h> 49 50 #include "test.h" 51 52 const char buf[] = "abcdefghijklmnopqrstuvwxyz"; 53 char x[sizeof(buf)]; 54 int fd = 1; 55 56 volatile int ending = 0; 57 58 /* ========================================================================== 59 * usage(); 60 */ 61 void usage(void) 62 { 63 extern char *__progname; 64 printf("usage: %s [-?] [-c count]\n", __progname); 65 printf("count must be between 2 and 26\n"); 66 errno = 0; 67 } 68 69 void * 70 new_thread(arg) 71 void *arg; 72 { 73 int i; 74 75 SET_NAME("writer"); 76 while (!ending) { 77 CHECKe(write (fd, (char *) arg, 1)); 78 x[(char *)arg - buf] = 1; 79 for (i = 0; i < 999999; i += 1) 80 ; 81 } 82 return NULL; 83 } 84 85 int 86 main(argc, argv) 87 int argc; 88 char **argv; 89 { 90 pthread_t thread; 91 int count = 4; 92 int eof = 0; 93 long i; 94 95 /* Getopt variables. */ 96 extern int optind, opterr; 97 extern char *optarg; 98 99 while (!eof) 100 switch (getopt (argc, argv, "c:d?")) 101 { 102 case EOF: 103 eof = 1; 104 break; 105 case 'c': 106 count = atoi(optarg); 107 if ((count > 26) || (count < 2)) { 108 count = 2; 109 } 110 break; 111 case '?': 112 usage(); 113 return(OK); 114 default: 115 usage(); 116 return(NOTOK); 117 } 118 119 /* create the threads */ 120 for (i = 0; i < count; i++) 121 CHECKr(pthread_create(&thread, NULL, new_thread, 122 (void*)(buf+i))); 123 124 /* give all threads a chance to run */ 125 sleep (2); 126 127 ending = 1; 128 for (i = 0; i < count; i++) 129 ASSERT(x[i]); /* make sure each thread ran */ 130 131 CHECKe(write(STDOUT_FILENO, "\n", 1)); 132 SUCCEED; 133 } 134