home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / TASK.ZIP / TESTS.TXT < prev    next >
Encoding:
Text File  |  1989-08-28  |  6.9 KB  |  286 lines

  1. Test 1.  The multitasking core.
  2.  
  3.  
  4. #include "usual.hpp"
  5. #include <stream.hpp>
  6. #include "task.hpp"
  7.  
  8. bool done= FALSE;
  9.  
  10. void task1 (void* a)
  11. {
  12. int loop= 100;
  13. while (loop--)
  14.    cout << "I'm here!\n";
  15. done= TRUE;
  16. }
  17.  
  18. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  19.  
  20. static void dummy(){}
  21.  
  22. static long two_count;
  23.  
  24. void task2 (void* a)
  25. {
  26. while (!done) {
  27.    for (int loop= 0;  loop < 10;  loop++) dummy();  //delay
  28.    two_count++;
  29.    }
  30. }
  31.  
  32. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  33.  
  34. int _stack= 30000;  //used by Zortech to set stack size
  35.  
  36. main()
  37. {
  38. unsigned st[3000];
  39.    /* Zortech's Large Model uses seperate stack and data segments, so I
  40.       could put the task's stack anywhere.  However, the system crashed
  41.       somewhere in the ostream operator<< function on my first try, when
  42.       the stack was in the data segment.  Making the stacks auto variables
  43.       puts the stack somewhere in the real stack.  I want to be able to
  44.       put stacks in the heap, but gotta watch out for existing code in the
  45.       library. */
  46. task t1 (task1, 2, st, 1024);
  47. task t2 (task2, 2, st+512, 1024);
  48. scheduler();
  49.  
  50. cout << "two_count= " << two_count << "\n";  //prove it did something
  51. }
  52.  
  53. =============================================================================
  54.  
  55. Test 2.  Semaphores to insure mutual exclusion.
  56. Two tasks are both using DOS.
  57.  
  58. #include "usual.hpp"
  59. #include <stream.hpp>
  60. #include "task.hpp"
  61. #include "sem.hpp"
  62.  
  63. bool done= FALSE;
  64.  
  65. semaphore DOS;
  66.  
  67. void task1 (void* a)
  68. {
  69. int loop= 100;
  70. while (loop--) {
  71.    DOS.wait();  //gain exclusive access
  72.    cout << "I'm here!\n";
  73.    DOS.signal();  //release
  74.    }
  75. done= TRUE;
  76. }
  77.  
  78. void task1b (void* a)
  79. {
  80. // do this the same as the first one, but use a resource lock instead.
  81. int loop= 80;
  82. while (loop--) {
  83.    resource_lock x(DOS);
  84.    cout << "I'm there!\n";
  85.    // any exit from this block causes the lock to be released,
  86.    // with very little room for programmer error.
  87.    }
  88. }
  89.  
  90. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  91.  
  92. static void dummy(){}
  93.  
  94. static long two_count;
  95.  
  96. void task2 (void* a)
  97. {
  98. while (!done) {
  99.    for (int loop= 0;  loop < 10;  loop++) dummy();  //delay
  100.    two_count++;
  101.    }
  102. }
  103.  
  104. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  105.  
  106. int _stack= 30000;  //used by Zortech to set stack size
  107.  
  108. main()
  109. {
  110. unsigned st1[512],st2[512],st3[512];
  111. task t1 (task1, 2, st1, 1024);
  112. task t2 (task1b, 2, st2, 1024);
  113. task t3 (task2, 2, st3, 1024);
  114. scheduler();
  115.  
  116. cout << "two_count= " << two_count << "\n";
  117. }
  118.  
  119. =============================================================================
  120.  
  121. Test 3.
  122. "Hello World" the hard way.  This is a test of messages.  It shows how
  123. several tasks can pass data around.  Specificly, one task is reading from
  124. and two tasks are writing to a shared message buffer.  Task1 prints whatever
  125. it gets.  The hard part is shutting down task1 when the other tasks are
  126. done.  The last one to finish must do so.  So task1 itself keeps track of
  127. how many users it has, and shuts down itself when the last one is finished.
  128.  
  129.  
  130. #include "usual.hpp"
  131. #include <stream.hpp>
  132. #include "task.hpp"
  133. #include "sem.hpp"
  134. #include "mess.hpp"
  135.  
  136. message m1;  //strings for the print server
  137.  
  138. void task1 (void* a)   // A print server
  139. {
  140. char* s;
  141. int user_count= 0;
  142.  
  143. for (;;) {
  144.    m1 >> s;
  145.    if (*s == '#') {
  146.       m1.done();
  147.       user_count++;  //new client logged in
  148.       }
  149.    else if (*s == '*') {
  150.       m1.done();
  151.       user_count--;
  152.       if (user_count == 0) break;  //all done
  153.       }
  154.    else {  //something to print
  155.       cout << s;
  156.       m1.done();
  157.       }
  158.    }
  159. }
  160.  
  161. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  162.  
  163. void task2 (void* a)
  164. {
  165. m1 << "#";
  166. m1 << "Hello from task 2\n";
  167. m1 << "Hello again from task 2!\n";
  168. m1 << "*";
  169. }
  170.  
  171. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  172.  
  173. void task3 (void* a)
  174. {
  175. m1 << "#";
  176. m1 << "Hello from task 3\n";
  177. m1 << "Hello again from task 3!\n";
  178. m1 << "*";
  179. }
  180.  
  181. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  182.  
  183. int _stack= 30000;  //used by Zortech to set stack size
  184.  
  185. main()
  186. {
  187. unsigned st1[512],st2[512],st3[512];
  188. task t1 (task1, 2, st1, 1024);
  189. task t2 (task2, 2, st2, 1024);
  190. task t3 (task3, 2, st3, 1024);
  191. scheduler();
  192. }
  193.  
  194. =============================================================================
  195.  
  196. Test 4.
  197. Pipes and child tasks.
  198. This shows one reader and two writers on a pipe.  All three tasks are
  199. child tasks of another, which creates the pipe and all tasks that use it.
  200. This is a more typical situation than the other tests.
  201.  
  202.  
  203. #include "usual.hpp"
  204. #include <stream.hpp>
  205. #include "task.hpp"
  206. #include "sem.hpp"
  207. #include "mess.hpp"
  208. #include "pipe.hpp"
  209. #include <string.h>
  210.    //need strlen()
  211.  
  212. static char pipe_buf[100];
  213. pipe P (pipe_buf, sizeof pipe_buf);
  214.  
  215. semaphore master(0);  //used so master task can wait for sub-tasks to finish
  216.  
  217. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  218.  
  219. void task1 (pipe_reader* p)
  220. {
  221. char c;
  222. /* loop until the task is killed.  When the task is killed, finish
  223.    printing what is in the pipe.  Also, drop out if receive() fails which
  224.    means the pipe was destroyed.  */
  225. while (!(active_task->iskilled() && p->contents()==0) && p->receive (&c, 1))
  226.    cout.put(c);
  227. }
  228.  
  229. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  230.  
  231. void task2 (pipe_writer* p)
  232. {
  233. static char* strings[]= { "This is task2 reporting in\n",
  234.    "Task2 is still on the line\n", "Task2 sending third string\n",
  235.    "Task2 running out of things to say\n",
  236.    "This is task2 again, really having a hard time coming up with material\n"
  237.    "Task2 saying bye-bye\n",  NULL };
  238. for (int loop= 0; strings[loop]; loop++)
  239.    p->send(strings[loop], strlen(strings[loop]));
  240.  
  241. master.signal();  //inform master that I'm done
  242. }
  243.  
  244. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  245.  
  246. void task3 (pipe_writer* p)
  247. {
  248. static char* strings[]= { "This is task3 reporting in\n",
  249.    "Task3 is still on the line\n", "Task3 sending third string\n",
  250.    "Task3 running out of things to say\n",
  251.    "This is task3 again, really having a hard time coming up with material\n"
  252.    "Task3 saying bye-bye\n",  NULL };
  253. for (int loop= 0; strings[loop]; loop++)
  254.    p->send(strings[loop], strlen(strings[loop]));
  255.  
  256. master.signal();  //inform master that I'm done
  257. }
  258.  
  259. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  260.  
  261. void master_task (void*)
  262. {
  263. preemptable(FALSE);
  264. unsigned st1[512],st2[512],st3[512];  //stacks
  265. static char pipe_buf[100];
  266. pipe P (pipe_buf, sizeof pipe_buf);
  267.  
  268. task t1 (task1, 2, st1, 1024, (pipe_reader*)&P);
  269. task t2 (task2, 2, st2, 1024, (pipe_writer*)&P);
  270. task t3 (task3, 2, st3, 1024, (pipe_writer*)&P);
  271. master.wait();  //wait for one writer to finish
  272. master.wait();  //wait for other writer to finish
  273. }
  274.  
  275. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  276.  
  277. int _stack= 30000;  //used by Zortech to set stack size
  278.  
  279. main()
  280. {
  281. unsigned stack_space[2048];
  282. task master (master_task, 20, stack_space, sizeof stack_space);
  283. scheduler();
  284. }
  285.  
  286.