home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *+
- ** Module Name: MOS.C
- **
- ** Description: Multi-tasking
- ** Operating
- ** System
- ** Simulation
- **
- ** Moss is a simulation of a multi-tasking operating system
- ** which uses many of the C standard algorithms.
- **
- ** Written by: John Tal
- **
- **
- **
- ** Modification History:
- **
- ** Date Programmer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 04-JUL-1991 J. Tal V1.0-000 New
- ** 01-DEC-1991 J. Tal V2.0-000 New Version, enhanced semaphores,
- ** messaging, swapping control
- **
- *-
- */
-
- #ifdef C_ANSI
- #include <string.h>
- #include <stdlib.h>
- #endif
-
- #include <stdio.h>
-
- #include <fio.h>
-
-
-
- /*
- ** FioGetFile
- **
- ** Is called by a reader to check for existance of a file.
- ** SeedPathName = directory + filename + extension.
- ** The extension increments starting from .000
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY
- FioGetFile(PCHAR pcSeedPathName)
- #else
- SHORT APIENTRY
- FioGetFile(pcSeedPathName)
- PCHAR pcSeedPathName;
- #endif
- {
- C_DEF_MODULE("FioGetFile")
-
- struct stat stStatMain;
-
-
- /* check if file exists */
-
- C_STATUS = stat(pcSeedPathName,&stStatMain);
- if(C_STATUS)
- C_SET_STATUS(FIO_NO_FILE)
- else
- C_SET_STATUS(FIO_OK);
-
- C_RETURN
- }
-
-
-
- /*
- ** FioRemoveFile
- **
- ** Delete a file. To be called by a READER only
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY
- FioRemoveFile(PCHAR pcSeedPathName)
- #else
- SHORT APIENTRY
- FioRemoveFile(pcSeedPathName)
- PCHAR pcSeedPathName;
- #endif
- {
- C_DEF_MODULE("FioRemoveFile")
-
- C_STATUS = unlink(pcSeedPathName);
-
- C_RETURN
- }
-
-
- /*
- ** FioIncSeedPathName
- **
- ** Increment the extension of the rolling seed name.
- ** Wraps to 000 if 999
- ** Called by writer to next filename of next file to write.
- ** Called by reader to get next filename to read.
- ** NOTE: Beware of situations where files a reader is looking
- ** for do not have their response files created in the
- ** exact same order (asynchronus process on one side
- ** of the connection). The reader may have to maintain
- ** a list of all filenames it is waiting for and be
- ** able to accept them out of order. This is only necessary
- ** if there is asynchronus processing on one side AND the
- ** queue/pipeline of communication files is greater than one
- ** at a time.
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY
- FioIncSeedPathName(PCHAR pcSeedPathName)
- #else
- SHORT APIENTRY
- FioIncSeedPathName(pcSeedPathName)
- PCHAR pcSeedPathName;
- #endif
- {
- C_DEF_MODULE("FioIncSeedPathName")
-
- SHORT iPos;
- CHAR szExtension[FIO_EXTENSION_LEN + 2];
- SHORT iExtension;
- CHAR szWorkStr[128];
-
- iPos = FioFindChar(pcSeedPathName,FIO_PERIOD);
-
- if(iPos == FIO_NO_CHAR)
- return(iPos);
-
- strcpy(szExtension,&pcSeedPathName[iPos + 1]);
- iExtension = atoi(szExtension);
- iExtension++;
- if(iExtension > FIO_MAX_EXTENSION)
- iExtension = 0;
-
- sprintf(szExtension,"%d",iExtension);
- while(strlen(szExtension) < FIO_EXTENSION_LEN)
- {
- strcpy(szWorkStr,"0");
- strcat(szWorkStr,szExtension);
- strcpy(szExtension,szWorkStr);
- }
-
- strcpy(&pcSeedPathName[iPos + 1],szExtension);
-
- C_RETURN
- }
-
-
-
- /*
- ** FioFindChar
- **
- ** Look for a character in a string.
- ** Returns a 0-based offset
- **
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY
- FioFindChar(PCHAR pcString, CHAR cChar)
- #else
- SHORT APIENTRY
- FioFindChar(pcString,cChar)
- PCHAR pcString;
- CHAR cChar;
- #endif
- {
- C_DEF_MODULE("FioFindChar")
-
- SHORT iPos = -1;
- SHORT i;
- SHORT iLen;
-
- iLen = strlen(pcString);
- for(i = 0; i < iLen; i++)
- if(pcString[i] == cChar)
- break;
-
- if(i < iLen)
- C_SET_STATUS(i)
- else
- C_SET_STATUS(FIO_NO_CHAR)
-
- C_RETURN
- }
-
-
- /*
- ** FioReplaceExt
- **
- ** Replace the default .xxx extension
- ** Usefull if restarting one side of the communcation session
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY
- FioReplaceExt(PCHAR pcSeedPathName, PCHAR pcNewExt)
- #else
- SHORT APIENTRY
- FioReplaceExt(pcSeedPathName,pcNewExt)
- PCHAR pcSeedPathName;
- PCHAR pcNewExt;
- #endif
- {
- C_DEF_MODULE("FioReplaceExt")
-
- SHORT iPos;
-
- iPos = FioFindChar(pcSeedPathName,'.');
-
- if(iPos != FIO_NO_CHAR)
- {
- strcpy(&pcSeedPathName[iPos +1],pcNewExt);
- }
-
- C_RETURN
- }
-
-
-
-
-
-
- #ifdef TEST
-
- /* *******************************************************************
- T E S T C O D E
- ******************************************************************* */
-
- #include <memlib.h>
-
- #include <mos.h>
-
-
- CHAR szReaderSeedPathName[] = "Testing.000";
- CHAR szWriterSeedPathName[] = "Testing.000";
-
-
-
- /* Reader Proc */
-
- #ifdef C_ANSI
- SHORT
- FioProc1(PROC_P pstProc)
- #else
- SHORT
- FioProc1(pstProc)
- PROC_P pstProc;
- #endif
- {
- C_DEF_MODULE("FioProc1")
-
- C_STATUS = FioGetFile(szReaderSeedPathName);
-
- if(!C_STATUS)
- {
- printf("Reader: Working on file %s\n",szReaderSeedPathName);
- FioRemoveFile(szReaderSeedPathName);
- FioIncSeedPathName(szReaderSeedPathName);
- }
- else
- {
- pstProc -> lWakeTime = MosCurTime()+0;
-
- C_STATUS = MosSleep(pstProc);
- }
-
- C_RETURN
-
- }
-
-
-
- #define TEMP_FILE_NAME "Temp.Dat"
-
-
- /* Writer Proc */
-
- #ifdef C_ANSI
- SHORT
- FioProc2(PROC_P pstProc)
- #else
- SHORT
- FioProc2(pstProc)
- PROC_P pstProc;
- #endif
- {
- C_DEF_MODULE("FioProc2")
-
- FILE *fp;
-
- fp = fopen(TEMP_FILE_NAME,"w");
- fclose(fp);
-
-
- /* It is highly recommended that you write out the file as a temporary file
- and rename it after it is closed. This will prevent the reader from
- dealing with situations where the data has not been flushed out to
- disk by the operating system (i.e. Unix).
- */
-
- rename(TEMP_FILE_NAME,szWriterSeedPathName);
-
- printf("Writer: Created file %s\n",szWriterSeedPathName);
- FioIncSeedPathName(szWriterSeedPathName);
-
- pstProc -> lWakeTime = MosCurTime()+0; /* yield cpu */
-
- C_STATUS = MosSleep(pstProc);
-
- C_RETURN
- }
-
-
- #ifdef C_ANSI
- main(VOID)
- #else
- main()
- #endif
- {
- C_DEF_MODULE("FioTestMain")
-
- C_STATUS = MosInit();
-
- C_STATUS = MosProcCreate(FioProc1,"FIO_PROC_1",10);
- C_STATUS = MosProcCreate(FioProc2,"FIO_PROC_2",10);
-
- C_STATUS = MosScheduler();
-
- }
-
-
-
- #endif
-
-
-
-
-