home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / TASK.ZIP / TEST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-28  |  2.4 KB  |  84 lines

  1. #include "usual.hpp"
  2. #include <stream.hpp>
  3. #include "task.hpp"
  4. #include "sem.hpp"
  5. #include "mess.hpp"
  6. #include "pipe.hpp"
  7. #include <string.h>
  8.    //need strlen()
  9.  
  10. static char pipe_buf[100];
  11. pipe P (pipe_buf, sizeof pipe_buf);
  12.  
  13. semaphore master(0);  //used so master task can wait for sub-tasks to finish
  14.  
  15. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  16.  
  17. void task1 (pipe_reader* p)
  18. {
  19. char c;
  20. /* loop until the task is killed.  When the task is killed, finish
  21.    printing what is in the pipe.  Also, drop out if receive() fails which
  22.    means the pipe was destroyed.  */
  23. while (!(active_task->iskilled() && p->contents()==0) && p->receive (&c, 1))
  24.    cout.put(c);
  25. }
  26.  
  27. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  28.  
  29. void task2 (pipe_writer* p)
  30. {
  31. static char* strings[]= { "This is task2 reporting in\n",
  32.    "Task2 is still on the line\n", "Task2 sending third string\n",
  33.    "Task2 running out of things to say\n",
  34.    "This is task2 again, really having a hard time coming up with material\n"
  35.    "Task2 saying bye-bye\n",  NULL };
  36. for (int loop= 0; strings[loop]; loop++)
  37.    p->send(strings[loop], strlen(strings[loop]));
  38.  
  39. master.signal();  //inform master that I'm done
  40. }
  41.  
  42. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  43.  
  44. void task3 (pipe_writer* p)
  45. {
  46. static char* strings[]= { "This is task3 reporting in\n",
  47.    "Task3 is still on the line\n", "Task3 sending third string\n",
  48.    "Task3 running out of things to say\n",
  49.    "This is task3 again, really having a hard time coming up with material\n"
  50.    "Task3 saying bye-bye\n",  NULL };
  51. for (int loop= 0; strings[loop]; loop++)
  52.    p->send(strings[loop], strlen(strings[loop]));
  53.  
  54. master.signal();  //inform master that I'm done
  55. }
  56.  
  57. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  58.  
  59. void master_task (void*)
  60. {
  61. preemptable(FALSE);
  62. unsigned st1[512],st2[512],st3[512];  //stacks
  63. static char pipe_buf[100];
  64. pipe P (pipe_buf, sizeof pipe_buf);
  65.  
  66. task t1 (task1, 2, st1, 1024, (pipe_reader*)&P);
  67. task t2 (task2, 2, st2, 1024, (pipe_writer*)&P);
  68. task t3 (task3, 2, st3, 1024, (pipe_writer*)&P);
  69. master.wait();  //wait for one writer to finish
  70. master.wait();  //wait for other writer to finish
  71. }
  72.  
  73. /* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ */
  74.  
  75. int _stack= 30000;  //used by Zortech to set stack size
  76.  
  77. main()
  78. {
  79. unsigned stack_space[2048];
  80. task master (master_task, 20, stack_space, sizeof stack_space);
  81. scheduler();
  82. }
  83.  
  84.