home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / MTASK11.ZIP / MTASK.DOC next >
Encoding:
Text File  |  1988-11-12  |  16.8 KB  |  516 lines

  1. .dhMTASK 1.1 by Wayne Conrad
  2. .np Documentation written using Multi-Edit 3.01b
  3. .rm 72
  4. .df .ce -.pa-
  5. .tm6
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. .ceMTASK
  13.  
  14.  
  15. MTASK is a unit for Turbo Pascal 5.0, to allow a Turbo Pascal program to
  16. exhibit simple multi-tasking.  MTASK gives your program a
  17. non-preemptive, request driven multi-tasking capability.  I will explain
  18. what I mean by that later.
  19.  
  20. MTASK was written and donated to the public domain by Wayne E. Conrad
  21. (me) in November of 1988.  I may be contact via my BBS,
  22.  
  23.      Pascalaholics Anonymous
  24.      (602) 484-9356
  25.      300/1200/2400 bps
  26.      24 hours/day
  27.  
  28. or by mail at my home:
  29.  
  30.      2627 North 51st Ave, #219
  31.      Phoenix, AZ  85035
  32.  
  33. I am interested in any modifications, bug reports, or comments you have.
  34.  
  35. If you modify this package, please keep my name and the name of any
  36. other programmers who've worked on it intact.  Please distribute the
  37. complete ARC file, with documentation and demonstration programs
  38. included.
  39.  
  40. 1.1 INTRODUCTION
  41.  
  42.  
  43. MTASK allows your Turbo Pascal program to do simple multi-tasking.  I
  44. call MTASK's brand of multi-tasking "non-preemptive, request driven."
  45.  
  46. Preemptive is a fairly common phrase, meaning that the switch from one
  47. task to another can happen at almost any time.  Most preemptive systems
  48. have an interrupt driver hooked to a hardware timer, which causes a task
  49. switch every time the hardware timer goes off.  The advantage of this
  50. kind of multi-tasking is that your programs don't have to be written
  51. with multi-tasking in mind, and don't even have to know that its taking
  52. place.  Also, no program can hog the system for long, because the
  53. interrupt driver switches from one program to another fairly often.
  54. Desqview and Double-DOS are programs that do preemptive,
  55. interrupt-driven multi-tasking.  The disadvantage of this kind of
  56. multi-tasking is that it can be complex to write and difficult in the
  57. extreme to debug.  These difficulties are compounded in an MS-DOS
  58. environment because MS-DOS was not meant to be used in a multi-tasking
  59. environment.
  60.  
  61. On the other hand, non-preemptive multi-tasking only switches tasks at
  62. certain, well defined times.  There is no interrupt driver that forces
  63. task switches.  In the original Macintosh operating system, for example,
  64. task switches only occured when a task called the operating system.  The
  65. advantage of non-preemptive multi-tasking is that it is much simpler to
  66. write and debug than preemptive multi-tasking, because the system has
  67. total control of when task-switches occur.  The disadvantage to this
  68. form of multi-tasking is that a task must request a task switch often if
  69. the other tasks are to receive their chance to execute.  If a task does
  70. not request a task switch for a long time, the other tasks will appear
  71. to pause.  What's worse, if a task crashes, it won't be able to call the
  72. operating system to let the other tasks execute, so they'll all be hung
  73. too.
  74.  
  75. MTASK implements a very simple method of non-preemptive multi-tasking
  76. that I call "request driven."  Request driven means that task switches
  77. occur only when the current task calls MTASK and requests a switch.
  78. (The sole exception is that a task switch occurs when the current task
  79. terminates itself).  This is about the simplest form of multi-tasking
  80. that I can envision.  It is so simple that the entire MTASK unit
  81. compiles to only about 1400 bytes with stack checking and range checking
  82. turned off, or less if you don't use all of its procedures.  This
  83. simplicity also made MTASK easy to write and debug.  MTASK was written
  84. and debugged in one day!
  85.  
  86.  
  87.  
  88. 1.2 WHAT ARE MTASK'S LIMITS?
  89.  
  90.  
  91. MTASK allows your program to set up multiple tasks within itself.  These
  92. tasks will execute concurrently.  However, it does not effect anything
  93. outside of your program.  It does not allow you to run multiple
  94. programs, multiple copies of COMMAND.COM, or anything else like that.
  95. It simply allows your program to do several things concurrently without
  96. stumbling over itself.
  97.  
  98. As far as DOS is concerned, your program using MTASK is still just a
  99. simple program.  All of the gymnastics to keep track of multiple tasks
  100. are done by MTASK, withing your program, without the knowledge or
  101. consent of DOS or anything else outside of your program.  Because MTASK
  102. is so simple, it will coexist fine with any "real" multi-tasking DOS you
  103. have set up, such as DesqView or Double-DOS.  Whenever the DOS gives
  104. your program some time, your program will dole out that time to its
  105. tasks.
  106.  
  107. Your program must continue to execute for its tasks to execute.  If any
  108. task in your program exits to DOS for any reason, including a run-time
  109. error, all tasks stop executing.  If one of your tasks shells out by
  110. using Turbo's Exec function, then the other tasks in your program are
  111. suspended until control returns from the Exec function to your program.
  112.  
  113. MTASK must not be made into an overlay.  Any of the tasks it controls
  114. may be overlays, although that may be unwise.  You could end up loading
  115. an overlay from disk during each task switch!
  116.  
  117. 2.1 SUMMARY OF PROCEDURES AND FUNCTIONS
  118.  
  119.  
  120. To use MTASK, include it in your program's USES statements.  MTASK will
  121. initialize itself automatically, making your main program task #1.  Your
  122. program can then use the following procedures and functions to create
  123. and control tasks:
  124.  
  125.  
  126.      create_task         Create another task
  127.  
  128.      terminate_task      Destroy a task
  129.  
  130.     switch_task        Switch to another task
  131.  
  132.     current_task_id    Return the task ID of the current task
  133.  
  134.     number_of_tasks    Return the current number of tasks
  135.  
  136.     get_task_info        Get information about all tasks
  137.  
  138. 2.1.1 PROCEDURE CREATE_TASK
  139.  
  140.  
  141. PROCEDURE create_task
  142.   (
  143.   task      : task_proc;
  144.   VAR param ;
  145.   stack_size: Word;
  146.   VAR id    : Word;
  147.   VAR result: Word
  148.   );
  149.  
  150.  
  151.      TASK is the procedure to make into a task.  It must match type
  152.     task_proc, having a single variable as its parameter.
  153.  
  154.     PARAM is the parameter to pass to new_task.  It may be a variable of any
  155.     type, so long its what the task expects.  For example, if you pass a
  156.     Word and the task expects a LongInt, the task will get invalid data.
  157.  
  158.     STACK_SIZE is the size of the new task's stack.  A stack will be
  159.     allocated from the heap.  The minimum stack size is 512 bytes, because
  160.     Turbo's stack-check procedure flags a "stack overflow" error if less
  161.     than 512 bytes of stack remain.
  162.  
  163.     ID is set to the task ID of the newly created task.  If the task is not
  164.     created because of an error, then id is not set.
  165.  
  166.     RESULT is the result code, which is set to one of these values:
  167.  
  168.  
  169.         0                No error, task created ok
  170.         heap_full            Unable to allocate heap for the task's
  171.                         stack
  172.         too_many_tasks        Maximum number of tasks are already
  173.                         running
  174.  
  175.  
  176. The new task is created and added to the end of the task list.  The new
  177. task will be executed when the task before it calls switch_task.
  178.  
  179. 2.1.2 PROCEDURE TERMINATE_TASK
  180.  
  181.  
  182. PROCEDURE terminate_task (id: Word; VAR result: Word);
  183.  
  184.  
  185.      ID is the task id of the task you want to terminate.  If ID = 0,
  186.     then the current task will be terminated.
  187.  
  188.     RESULT is the result code, which is set to one of these values.
  189.  
  190.           0                   No error, task deleted ok
  191.         invalid_task_id    There is no task with that ID number
  192.  
  193.  
  194. The designated task will be removed from the task list.  If its stack
  195. was allocated from the heap, it is returned to the heap.
  196.  
  197. If the terminated task is the current task and there is another task in
  198. the task list, a task switch occurs.  On the other hand, if the
  199. terminated task is the current task and there are no other tasks in the
  200. task list, then the program exits to DOS.
  201.  
  202. A task may terminate itself by returning from its main procedure.  For
  203. example, when this task is executed, it will immediately display a
  204. message and then terminate itself.
  205.  
  206.  
  207.      PROCEDURE task (VAR param);
  208.     BEGIN
  209.       Writeln ('We just started, but already we're terminating');
  210.     END;
  211.  
  212. 2.1.3 PROCEDURE switch_task
  213.  
  214.  
  215.     PROCEDURE switch_task;
  216.  
  217.  
  218. This procedure causes an immediate switch to the next task in the task
  219. list.  The task list is always scanned as a circular list.  For
  220. example, if there are three tasks in the list -- task 1, task 2, and
  221. task 3 -- then they will be executed in this order:
  222.  
  223.  
  224.     1, 2, 3, 1, 2, 3, 1, 2, 3 . . .
  225.  
  226.  
  227. If the current task is the only task, then no task switch occurs.
  228.  
  229. The stack pointer is switched to its position in the new task's stack.
  230. If the new task has just been created, then its main procedure will be
  231. executed from the beginning.  On the other hand, if the new task had put
  232. itself to sleep by asking for a task switch, then control will return to
  233. the point where it called switch_task.
  234.  
  235.  
  236.  
  237. 2.1.4 FUNCTION CURRENT_TASK_ID
  238.  
  239.  
  240.     FUNCTION current_task_id: task_id;
  241.  
  242.  
  243. This function returns the task ID number of the currently executing
  244. task.  When calling an MTASK procedure to do something to a task, the
  245. task ID number is always used to identify the task.
  246.  
  247. A task is assigned its ID number when it is created.  A task's ID number
  248. belongs to it as long as that task exists, and will not be changed or
  249. reassigned until the task terminates.
  250.  
  251.  
  252.  
  253. 2.1.5 FUNCTION NUMBER_OF_TASKS
  254.  
  255.  
  256.     FUNCTION number_of_tasks: task_number;
  257.  
  258.  
  259. This function returns the number of tasks in the task list.  There will
  260. always be at least one task.
  261.  
  262. 2.1.6 PROCEDURE get_task_info
  263.  
  264.  
  265.     PROCEDURE get_task_info
  266.       (
  267.       VAR info: task_info_array;
  268.       VAR n   : task_number
  269.       );
  270.  
  271.  
  272. INFO is an array of task information.  You receive a copy of the actual
  273. task information array, not the original.  See MTASK.PAS for the
  274. definition and description of task_info_array.
  275.  
  276. I really don't expect that I'll ever use this procedure, but it's there
  277. if your program ever needs to know what the current state of MTASK is.
  278.  
  279. 3.1 TRICKS AND TRAPS
  280.  
  281.  
  282. This section focuses on some of the tricks and traps of programming in
  283. this multi-tasking environment.  Like all multi-tasking environments,
  284. strange things can happen.  You'll learn how to watch for problems with
  285. shared data, and crunched parameters.
  286.  
  287. I will only give a few examples of the problems that can occur in
  288. multi-tasking environments.  There are other problems that can occur
  289. when using MTASK, and many more problems that can occur when using a
  290. preemptive multi-tasker such as UNIX.  This section should help you to
  291. begin thinking like a real-time programmer, giving you an idea of the
  292. kinds of problems to watch for.  For a real education on concurrent
  293. programming, head to your library or book store and look for a book on
  294. operating systems.
  295.  
  296.  
  297.  
  298. 3.1.1 PASSING PARAMETERS TO TASKS
  299.  
  300.  
  301. When you create a task, you can pass a parameter to it.  For example, a
  302. BBS program needs to tell a task which node it is, so that the task
  303. knows which serial port to use for i/o.  The parameter you pass is
  304. "untyped," meaning that it can be any type of variable.  You must be
  305. familiar with how Turbo handles untyped variables.
  306.  
  307. The sample program TEST1.PAS shows how to pass a word variable to a
  308. task.  You can pass any kind of variable, including records, arrays, and
  309. even files.
  310.  
  311. One thing to remember is that when you pass an untyped parameter to a
  312. task, you are actually passing the address of the parameter, not the
  313. parameter itself.  Therefore, if you pass the task a paramater and then
  314. modify the parameter, the task may see the new value instead of the old
  315. value.  It will all depend upon where task switches occur.
  316.  
  317. As a general rule, parameters you pass to a task should be global
  318. variables or typed constants.  Global variables and typed constants are
  319. both in the data segment.  Local variables are declared on the current
  320. task's stack, and cannot be assured of existing for very long.  If you
  321. a procedure passes one of its local variables to a task that it's
  322. creating, and then the procedure returns, that local variable is "thrown
  323. away" and its space can be reused by other procedures.  That would cause
  324. the value of the parameter you passed to the task to change
  325. unpredictably.
  326.  
  327. 3.1.2 SHARED DATA
  328.  
  329.  
  330. Problems can occur when two or more tasks are using the same global
  331. variables.  If two or more tasks have access to the same variable, you
  332. need to consider carefully what will happen if two tasks access the
  333. variable concurrently.  This pseudo-code example shows two tasks.
  334. task_a is computing the sum of an array of Reals.  Task_b is clearing
  335. the values in the array.
  336.  
  337.  
  338.  
  339.     CONST
  340.       data_size = 1000;
  341.     VAR
  342.       data: ARRAY [1..data_size] OF Real;
  343.  
  344.  
  345.     PROCEDURE task_a;
  346.        .
  347.        .
  348.        .
  349.     sum := 0.0;
  350.     FOR i := 1 TO data_size DO
  351.       BEGIN
  352.       sum := sum + data [i];
  353.       switch_task;
  354.       END;
  355.        .
  356.        .
  357.        .
  358.     END;
  359.  
  360.  
  361.     PROCEDURE task_b;
  362.        .
  363.        .
  364.        .
  365.     FOR i := data_size DOWNTO 1 DO
  366.       BEGIN
  367.       data [i] := 0.0;
  368.       switch_task;
  369.       END;
  370.        .
  371.        .
  372.        .
  373.     END;
  374.  
  375.  
  376. Do you see what happens if task_a is computing the average at the same
  377. time task_b is clearing the array?  The average will end up being
  378. incorrect, because the data being averaged is being changed while the
  379. average is being computed.  Obviously, this example is contrived.
  380. Nobody in their right mind would call switch_task inside those
  381. loops.  That causes many more context switches than are necessary.
  382.  
  383. One way to avoid the problem in this particular example is not to call
  384. switch_task inside either of the loops.  Then you could be sure that an
  385. average would not take place while you were clearing the array, and
  386. array clearing would not take place during an average.
  387.  
  388. You cannot always avoid calling switch_task, however.  Suppose that
  389. floating point addition on your computer was so slow that it took many
  390. seconds to compute the average.  You may have other tasks that cannot
  391. afford to be denied CPU time for more than a fraction of a second.  What
  392. do you do?
  393.  
  394. The solution here is to create a flag that indicates when a task is
  395. using the data array.  When one task is using the data array the flag
  396. will be set to True, indicating that no other task should access it.
  397.  
  398.  
  399.     CONST
  400.       flag: Boolean = False;
  401.  
  402.  
  403.     PROCEDURE task_a;
  404.        .
  405.        .
  406.        .
  407.     WHILE flag DO
  408.       switch_task;
  409.     flag := True;
  410.     sum := 0.0;
  411.     FOR i := 1 TO data_size DO
  412.       BEGIN
  413.       sum := sum + data [i];
  414.       switch_task;
  415.       END;
  416.     flag := False;
  417.        .
  418.        .
  419.        .
  420.     END;
  421.  
  422.     PROCEDURE task_b;
  423.        .
  424.        .
  425.        .
  426.     WHILE flag DO
  427.       switch_task;
  428.     flag := True;
  429.     FOR i := data_size DOWNTO 1 DO
  430.       BEGIN
  431.       data [i] := 0.0;
  432.       switch_task;
  433.       END;
  434.     flag := True;
  435.        .
  436.        .
  437.        .
  438.     END;
  439.  
  440.  
  441. Do you see what's going on here?  Before task_a does an average, it
  442. checks the flag to see whether someone else is messing with the data
  443. array.  If someone is, then it waits until the data structure is
  444. available, sets the flag to indicate that it now "owns" the data array,
  445. and proceeds to compute the average.  When the average is finished,
  446. task_a resets the flag, to allow any other task which is waiting for the
  447. data array to have access.  task_b is doing exactly the same thing.
  448.  
  449. Now both tasks can go on calling switch_task even while messing with the
  450. data array, without concern that some other task will access the data
  451. array at the same time.  This technique will work for any number of
  452. tasks.
  453.  
  454.  
  455.  
  456. 3.1.3 WHEN TO SWITCH TASKS?
  457.  
  458.  
  459. Obviously, the examples in 3.1.2 switch tasks far too often.  The
  460. program will spend more time bouncing from one task to another than it
  461. will doing anything useful!  If your loop is too time consuming to leave
  462. out task switches, and switching tasks during every iteration of the
  463. loop is too often, try something like this:
  464.  
  465.  
  466.     FOR i := 1 TO 10000 DO
  467.       BEGIN
  468.       IF i MOD 100 = 0 THEN
  469.         switch_tasks;
  470.       do_something_useful;
  471.       END;
  472.  
  473.  
  474. This will switch tasks every hundreth iteration of the loop.
  475.  
  476. If your program is going to do something that takes a while, like disk
  477. i/o, it should probably switch tasks before doing so to let the other
  478. tasks get some time before the long delay occurs.  In fact, if you are
  479. doing several lengthy disk operations in a row, call switch_task before
  480. every one.
  481.  
  482.  
  483.     Assign (inf, 'INPUT.DAT');
  484.     switch_tasks;
  485.     Reset (inf);
  486.     Assign (outf, 'OUTPUT.DAT');
  487.     switch_tasks;
  488.     Rewrite (outf);
  489.  
  490.  
  491. Many programs have to wait for input at some point.  Input loops are a
  492. perfect place to switch tasks.  In fact, any time a task cannot proceed
  493. because its input is not ready, or for any other reason, it should
  494. switch tasks.
  495.  
  496.  
  497.     WHILE NOT KeyPressed DO
  498.       switch_tasks;
  499.     ch := ReadKey;
  500.  
  501.  
  502. It is a matter of judgement where task switches should occur.  It will
  503. depend upon the program and circumstances around each operation.
  504.  
  505.  
  506.  
  507. 4.1 REVISION HISTORY
  508.  
  509.  
  510. Version 1.0, MTASK10.ARC.  Original release by Wayne E. Conrad
  511.  
  512. Version 1.1, MTASK11.ARC.  Minor changes to documentation, including 
  513. using spaces instead of tabs.  ARC file now includes the original 
  514. documentation in Multi-Edit format, as well as the printable file.
  515.  
  516.