home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / MAILBOX.ZIP / MPTSKEX2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-27  |  5.9 KB  |  224 lines

  1. /****************************************************************************/
  2. /* mptskex2.c  -- EXAMPLE OF MAILBOX MULTITASKING    (IMPLEMENTATION)    */
  3. /* Created:  6/25/88        Release:  0.7        Version:  6/27/88   */
  4. /****************************************************************************
  5. (c) Copyright 1987, 1988 by Michael Benjamin Parker     (USA SS# 557-49-4130)
  6.  
  7. All Rights Reserved unless specified in the following include files: */
  8. #include "mptsk.cpy" /*
  9.  
  10. DO NOT REMOVE OR ALTER THIS NOTICE AND ITS PROVISIONS.
  11. ****************************************************************************/
  12. /* OVERVIEW:
  13.  
  14. WARNING WARNING:    This code has not been debugged.  It is
  15. presented only for educational purposes and will likely not work.  It
  16. will be completed by release 1.0.
  17.  
  18. This is a second example of the MPTSK cluster in action.  It presents
  19. a "shell"-like command prompt and allows the user to run predefined
  20. procedures in the background.  Two procedures are given, both which take
  21. up a lot of computing time (to give the feel of background processing).
  22. Several instances of the same procedure can be running at the same
  23. time.
  24.  
  25. When the procedures are run, they signal the user for arguments.  The user
  26. may then ask the shell task to send an argument at the user's convenience.
  27.  
  28. The example illustrates:
  29.     task control yielding
  30.     message passing
  31.     dynamic task creation
  32.     dynamic task destruction
  33.  
  34.     how simple resource management is in a non-preemptive nor
  35.         multiprocessing environment.
  36.  
  37. */
  38. /****************************************************************************/
  39. #include <stdio.h>
  40. #include <io.h>
  41. #include <sys/ioctl.h>
  42. #include "mptsk.h"
  43. #define    NDEBUG    1
  44. #include "mpbug.h"
  45.  
  46.  
  47. #define    mptsk_printf(ARGUMENTS)                        \
  48. {                                    \
  49.     printf("\nMPTSK %ld SAYS:\t",ptr_2int(mptsk_me()));        \
  50.     printf(ARGUMENTS);                        \
  51. }
  52.  
  53. MPTHDID    mpthdfn_nthprime(MPTHDID mpthd)
  54. {
  55.     UINT    n;
  56.     UINT    curn;
  57.     UINT    prime=    1;
  58.     MPMSGID    mpmsg=    0;
  59.     do {
  60.         if (mpmsg)    free(mpmsg);
  61.         sleep(3);
  62.         mptsk_printf("Hey, User!  This is nthprime.  I need a numeric argument.");
  63.          mpmsg=    mptsk_recv(mptsk_mympbox());
  64.     } until(sscanf(&mpmsg[1],"%ld",&n)==1);
  65.     free(mpmsg);
  66.  
  67.     for(curn= 0; curn < n; n++) {
  68.         do {
  69.             UINT    div;
  70.             mptsk_yield();
  71.             for (div= 2; div < prime; div++)
  72.                 if (prime % div)    break;
  73.             prime++;
  74.         } while(div < prime);    /* WHILE NOT PRIME */
  75.     }
  76.     mptsk_printf(("The %ldth Prime Is %ld.",n,prime));
  77.     mptsk_recv(mptsk_mpbox(mptsk_death));
  78. }
  79.  
  80. MPTHDID    mpthdfn_wordcount(MPTHDID mpthd)
  81. {
  82.     char    filespec[100];
  83.     FILE    *file;
  84.     char    word[100];
  85.     UINT    wordcount=    0;
  86.     MPMSGID    mpmsg=    0;
  87.     do {
  88.         if (mpmsg)    free(mpmsg);
  89.         sleep(2);
  90.         mptsk_printf("Hey, User!  This is wordcount.  I need a file specification.");
  91.          mpmsg=    mptsk_recv(mptsk_mympbox());
  92.         if (sscanf(&mpmsg[1],"%s",filespec)!=1)    continue;
  93.     } until(file= fopen(filespec,"r"));
  94.     free(mpmsg);
  95.  
  96.  
  97.     while (fscanf(file,"%s",word) > 0) {
  98.         wordcount++;
  99.         mptsk_yield();
  100.     }
  101.     fclose(file);
  102.     mptsk_printf(("File %s has %ld words.",wordcount));
  103.     mptsk_recv(mptsk_mpbox(mptsk_death));
  104.  
  105. }
  106.  
  107.  
  108. #define    mptsk_waitfor(COND)        until(COND)    mptsk_yield()
  109.  
  110. #ifdef    __MSDOS__
  111. #define    mptsk_inputavail(handle)    (ioctl(handle,6) & 0x00FF)
  112. #endif
  113. #ifdef    unix
  114. int    mptsk_inputavail(int handle)
  115. {
  116.     int    charsavail=    lseek(handle,0,L_INCR);    /* GET CUR FILE POS */
  117.     /* GET THE IMMEDIATELY READABLE charsavail FROM CUR FILE POS */
  118.     ioctl(handle,FIONREAD,&charsavail);
  119.     return(charsavail);
  120. }
  121. #endif
  122.  
  123.  
  124. #define    mptsk_input(handle,C_INPUT_CALL)                \
  125.     (    mptsk_waitfor(mptsk_inputavail(handle)),        \
  126.         (C_INPUT_CALL)                        \
  127.     )
  128. #define    mptsk_finput(stream,C_FINPUT_CALL)                \
  129.     mptsk_input(fileno(stream),C_FINPUT_CALL)
  130.  
  131. #define    mptsk_read(handle,buf,nbyte)                    \
  132.     mptsk_input(handle,read(handle,buf,nbyte))
  133.  
  134. #define    mptsk_fgetc(stream)                        \
  135.     mptsk_finput(stream,fgetc(stream))
  136.  
  137. void    mptsk_fgetline(char PTR line)
  138. {
  139.      char    ch;
  140.     until((*line= mptsk_fgetc(stdin)) == '\n')
  141.         if (*line!='\b')    ++line
  142.     *(++line)=    0;
  143. }
  144.  
  145.  
  146. typedef struct {
  147.     char    name[50];
  148.     MPTHDFN    mpthdfn;
  149.     UINT    size;
  150. } ENTRY;
  151.  
  152.  
  153. #define    ENTRIES    (2)
  154. ENTRY    cat[ENTRIES]= {
  155.     {"nthprime", mpthdfn_nthprime,1500},
  156.     {"wordcount",mpthdfn_wordcount,1000}
  157. };
  158.  
  159.  
  160. void    main() {
  161. mpbug_init(2); {
  162.     mpthd_setup();
  163.     mptsk_setup();
  164.     mptsk_printf(("\nmptskex1.c  --  Multitasking Example 2\n"));
  165.  
  166. for(ever) {
  167.     char    line[100];
  168.     char    arg1[50],arg2[50],arg3[50];
  169.     int    args;
  170.     mptsk_fprint("Enter Your Command: ");
  171.     mptsk_fgetline(line);
  172.     if (args= sscanf(line,"%s%s%s",arg1,arg2,arg3)) {
  173.         if (!strcmp(arg1,"run") && args >= 2) {
  174.             int    entryno=    ENTRIES;
  175.             while(entryno--)
  176.                 if (!strcmp(arg2,cat[entryno].name))    break;
  177.             if (++entryno) {
  178.                 MPTSKID    mptsk=
  179. mptsk_init(    malloc(sizeof(MPTSK)),
  180.         mpthd_init(malloc(cat[entryno].size),cat[entryno].size,cat[entryno].mpthdfn),
  181.                 mpscd_backgroundtasks);
  182.                 mptsk_printf(("A %s Task Started with ID %ld",
  183.                         arg2,ptr_2int(mptsk)));
  184.             } else {
  185.                 mptsk_printf(("Task %s Unknown!",arg2));
  186.             }
  187.                 } else if (!strcmp(arg1,"send") && args >= 3) {
  188.             INT    mptskid;
  189.             if (sscanf(arg2,"%ld",&mptskid)==1) {
  190.                 MPMSGID    mpmsg=  mprng_init(malloc(sizeof(MPMSG)+50));
  191.                 strcpy(&mpmsg[1],arg3);
  192.                 mptsk_send(mptsk_mpbox((MPTSKID)int_2ptr(mptskid)),
  193.                         mpmsg);
  194.             }
  195.         } else if (!strcmp(arg1,"tasks")) {
  196.             int    entryno=    ENTRIES;
  197.             mptsk_printf(("Available tasks to run..."));
  198.             while (entryno--)
  199.                 mptsk_printf(("\t%s",cat[entryno].name));
  200.         } else if (!strcmp(arg1,"dir")) {
  201.             mptsk_printf(("Available commands..."));
  202.             mptsk_printf(("\trun <task name>"));
  203.             mptsk_printf(("\tsend <task id> <argument>"));
  204.             mptsk_printf(("\ttasks"));
  205.             mptsk_printf(("\tdir"));
  206.             mptsk_printf(("\tquit"));
  207.         } else if (!strcmp(arg1,"quit"))    break;
  208.         else    mptsk_printf(("Unrecognized command %s.  Type `dir'.",
  209.                         arg1));
  210.         }
  211. }
  212.     mptsk_printf(("Quiting.  Bye!\n"));
  213. }}
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.