Main Page | Alphabetical List | Class List | File List | Class Members | File Members

threadtest.cc

Go to the documentation of this file.
00001 // threadtest.cc 00002 // Simple test case for the threads assignment. 00003 // 00004 // 00005 // Copyright (c) 1992-1993 The Regents of the University of California. 00006 // All rights reserved. See copyright.h for copyright notice and limitation 00007 // of liability and disclaimer of warranty provisions. 00008 00009 #include "copyright.h" 00010 #include "system.h" 00011 #include "synch.h" 00012 #include <time.h> 00013 #include "list.h" 00014 00015 Semaphore SPing("Ping", 1); 00016 Semaphore SPong("Pong", 0); 00017 00018 00019 void Ping(int ch) 00020 { 00021 printf("Ping Start\n"); 00022 for(int i = 0; i < 10; i++) 00023 { 00024 SPing.P(); 00025 printf("Ping\n"); 00026 SPong.V(); 00027 } 00028 } /* Ping */ 00029 00030 00031 void Pong(int ch) 00032 { 00033 printf("Pong Start\n"); 00034 for(int i = 0; i < 10; i++) 00035 { 00036 SPong.P(); 00037 printf("Pong\n"); 00038 SPing.V(); 00039 } 00040 } /* Pong */ 00041 00042 00043 void PingPong() 00044 { 00045 Thread *t1 = new Thread("Ping"); 00046 Thread *t2 = new Thread("Pong"); 00047 00048 t1->Fork(Ping, 0); 00049 t2->Fork(Pong, 0); 00050 } /* PingPong */ 00051 00052 00053 00054 void SimpleThread1(int which) 00055 { 00056 int num4 = 1; 00057 int num = 0; 00058 int count = 0; 00059 00060 printf("^%c Started\n", which); 00061 00062 for (num = 0; num < 500; num++) 00063 { 00064 for(num4 = 0; num4 < 40000; num4++, count++) 00065 ; 00066 printf("%c", (char)which); 00067 count++; 00068 } /* for */ 00069 00070 printf("%c$Done count:%d$\n", which, count); 00071 } /* SimpleThread1 */ 00072 00073 //---------------------------------------------------------------------- 00074 // ThreadTest 00075 // Fork 7 threads and call PingPong 00076 //---------------------------------------------------------------------- 00077 00078 void ThreadTest1(int arg) 00079 { 00080 printf("Entering SimpleTest\n"); 00081 00082 00083 Thread *t1 = new Thread("A thread"); 00084 Thread *t2 = new Thread("B thread"); 00085 Thread *t3 = new Thread("C thread"); 00086 Thread *t4 = new Thread("D thread"); 00087 Thread *t5 = new Thread("E thread"); 00088 Thread *t6 = new Thread("F thread"); 00089 Thread *t7 = new Thread("G thread"); 00090 t1->Fork(SimpleThread1, 'A'); 00091 t2->Fork(SimpleThread1, 'B'); 00092 t3->Fork(SimpleThread1, 'C'); 00093 t4->Fork(SimpleThread1, 'D'); 00094 t5->Fork(SimpleThread1, 'E'); 00095 t6->Fork(SimpleThread1, 'F'); 00096 t7->Fork(SimpleThread1, 'G'); 00097 00098 printf("Doing Ping Pong\n"); 00099 PingPong(); 00100 } /* ThreadTest1 */ 00101 00102

Generated on Thu Sep 16 12:33:46 2004 for NachOS by doxygen 1.3.8