home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* mptskex2.c -- EXAMPLE OF MAILBOX MULTITASKING (IMPLEMENTATION) */
- /* Created: 6/25/88 Release: 0.7 Version: 6/27/88 */
- /****************************************************************************
- (c) Copyright 1987, 1988 by Michael Benjamin Parker (USA SS# 557-49-4130)
-
- All Rights Reserved unless specified in the following include files: */
- #include "mptsk.cpy" /*
-
- DO NOT REMOVE OR ALTER THIS NOTICE AND ITS PROVISIONS.
- ****************************************************************************/
- /* OVERVIEW:
-
- WARNING WARNING: This code has not been debugged. It is
- presented only for educational purposes and will likely not work. It
- will be completed by release 1.0.
-
- This is a second example of the MPTSK cluster in action. It presents
- a "shell"-like command prompt and allows the user to run predefined
- procedures in the background. Two procedures are given, both which take
- up a lot of computing time (to give the feel of background processing).
- Several instances of the same procedure can be running at the same
- time.
-
- When the procedures are run, they signal the user for arguments. The user
- may then ask the shell task to send an argument at the user's convenience.
-
- The example illustrates:
- task control yielding
- message passing
- dynamic task creation
- dynamic task destruction
-
- how simple resource management is in a non-preemptive nor
- multiprocessing environment.
-
- */
- /****************************************************************************/
- #include <stdio.h>
- #include <io.h>
- #include <sys/ioctl.h>
- #include "mptsk.h"
- #define NDEBUG 1
- #include "mpbug.h"
-
-
- #define mptsk_printf(ARGUMENTS) \
- { \
- printf("\nMPTSK %ld SAYS:\t",ptr_2int(mptsk_me())); \
- printf(ARGUMENTS); \
- }
-
- MPTHDID mpthdfn_nthprime(MPTHDID mpthd)
- {
- UINT n;
- UINT curn;
- UINT prime= 1;
- MPMSGID mpmsg= 0;
- do {
- if (mpmsg) free(mpmsg);
- sleep(3);
- mptsk_printf("Hey, User! This is nthprime. I need a numeric argument.");
- mpmsg= mptsk_recv(mptsk_mympbox());
- } until(sscanf(&mpmsg[1],"%ld",&n)==1);
- free(mpmsg);
-
- for(curn= 0; curn < n; n++) {
- do {
- UINT div;
- mptsk_yield();
- for (div= 2; div < prime; div++)
- if (prime % div) break;
- prime++;
- } while(div < prime); /* WHILE NOT PRIME */
- }
- mptsk_printf(("The %ldth Prime Is %ld.",n,prime));
- mptsk_recv(mptsk_mpbox(mptsk_death));
- }
-
- MPTHDID mpthdfn_wordcount(MPTHDID mpthd)
- {
- char filespec[100];
- FILE *file;
- char word[100];
- UINT wordcount= 0;
- MPMSGID mpmsg= 0;
- do {
- if (mpmsg) free(mpmsg);
- sleep(2);
- mptsk_printf("Hey, User! This is wordcount. I need a file specification.");
- mpmsg= mptsk_recv(mptsk_mympbox());
- if (sscanf(&mpmsg[1],"%s",filespec)!=1) continue;
- } until(file= fopen(filespec,"r"));
- free(mpmsg);
-
-
- while (fscanf(file,"%s",word) > 0) {
- wordcount++;
- mptsk_yield();
- }
- fclose(file);
- mptsk_printf(("File %s has %ld words.",wordcount));
- mptsk_recv(mptsk_mpbox(mptsk_death));
-
- }
-
-
- #define mptsk_waitfor(COND) until(COND) mptsk_yield()
-
- #ifdef __MSDOS__
- #define mptsk_inputavail(handle) (ioctl(handle,6) & 0x00FF)
- #endif
- #ifdef unix
- int mptsk_inputavail(int handle)
- {
- int charsavail= lseek(handle,0,L_INCR); /* GET CUR FILE POS */
- /* GET THE IMMEDIATELY READABLE charsavail FROM CUR FILE POS */
- ioctl(handle,FIONREAD,&charsavail);
- return(charsavail);
- }
- #endif
-
-
- #define mptsk_input(handle,C_INPUT_CALL) \
- ( mptsk_waitfor(mptsk_inputavail(handle)), \
- (C_INPUT_CALL) \
- )
- #define mptsk_finput(stream,C_FINPUT_CALL) \
- mptsk_input(fileno(stream),C_FINPUT_CALL)
-
- #define mptsk_read(handle,buf,nbyte) \
- mptsk_input(handle,read(handle,buf,nbyte))
-
- #define mptsk_fgetc(stream) \
- mptsk_finput(stream,fgetc(stream))
-
- void mptsk_fgetline(char PTR line)
- {
- char ch;
- until((*line= mptsk_fgetc(stdin)) == '\n')
- if (*line!='\b') ++line
- *(++line)= 0;
- }
-
-
- typedef struct {
- char name[50];
- MPTHDFN mpthdfn;
- UINT size;
- } ENTRY;
-
-
- #define ENTRIES (2)
- ENTRY cat[ENTRIES]= {
- {"nthprime", mpthdfn_nthprime,1500},
- {"wordcount",mpthdfn_wordcount,1000}
- };
-
-
- void main() {
- mpbug_init(2); {
- mpthd_setup();
- mptsk_setup();
- mptsk_printf(("\nmptskex1.c -- Multitasking Example 2\n"));
-
- for(ever) {
- char line[100];
- char arg1[50],arg2[50],arg3[50];
- int args;
- mptsk_fprint("Enter Your Command: ");
- mptsk_fgetline(line);
- if (args= sscanf(line,"%s%s%s",arg1,arg2,arg3)) {
- if (!strcmp(arg1,"run") && args >= 2) {
- int entryno= ENTRIES;
- while(entryno--)
- if (!strcmp(arg2,cat[entryno].name)) break;
- if (++entryno) {
- MPTSKID mptsk=
- mptsk_init( malloc(sizeof(MPTSK)),
- mpthd_init(malloc(cat[entryno].size),cat[entryno].size,cat[entryno].mpthdfn),
- mpscd_backgroundtasks);
- mptsk_printf(("A %s Task Started with ID %ld",
- arg2,ptr_2int(mptsk)));
- } else {
- mptsk_printf(("Task %s Unknown!",arg2));
- }
- } else if (!strcmp(arg1,"send") && args >= 3) {
- INT mptskid;
- if (sscanf(arg2,"%ld",&mptskid)==1) {
- MPMSGID mpmsg= mprng_init(malloc(sizeof(MPMSG)+50));
- strcpy(&mpmsg[1],arg3);
- mptsk_send(mptsk_mpbox((MPTSKID)int_2ptr(mptskid)),
- mpmsg);
- }
- } else if (!strcmp(arg1,"tasks")) {
- int entryno= ENTRIES;
- mptsk_printf(("Available tasks to run..."));
- while (entryno--)
- mptsk_printf(("\t%s",cat[entryno].name));
- } else if (!strcmp(arg1,"dir")) {
- mptsk_printf(("Available commands..."));
- mptsk_printf(("\trun <task name>"));
- mptsk_printf(("\tsend <task id> <argument>"));
- mptsk_printf(("\ttasks"));
- mptsk_printf(("\tdir"));
- mptsk_printf(("\tquit"));
- } else if (!strcmp(arg1,"quit")) break;
- else mptsk_printf(("Unrecognized command %s. Type `dir'.",
- arg1));
- }
- }
- mptsk_printf(("Quiting. Bye!\n"));
- }}
-
-
-
-
-
-
-
-
-
-