--- os2sdk/demos/examples/share/share.c 2018/08/09 12:25:13 1.1 +++ os2sdk/demos/examples/share/share.c 2018/08/09 12:26:17 1.1.1.2 @@ -7,8 +7,9 @@ * CONTROL-d to terminate. * * Note the mutual exclusion on access to the head and tail pointers of the - * buffer. For a discussion, see Peterson & Silberschatz, "Operating - * System Concepts", 2nd ed., chapter 9. + * buffer and on clearing and setting the full and empty semaphores. For a + * discussion, see Peterson & Silberschatz, "Operating System Concepts", + * 2nd ed., chapter 9. * * Be aware when running this that the system does keyboard buffering * underneath this program, so you can type ahead more than you might think. @@ -17,84 +18,87 @@ * * Compile as: cl -AL -G2 -Lp share.c * - * Copyright (C) Microsoft Corp. 1986 + * Created by Microsoft Corp. 1986 */ -#include -#include +#define INCL_DOSPROCESS +#define INCL_SUB +#define INCL_DOSMEMMGR + +#include +#include #include "share.h" /* common declarations between processes */ #define CHILD1PROG "SHRCHILD.EXE" /* child process */ #define CTRLd 4 /* termination character */ #define FBSZ 32 -struct KeyData KeyData; /* declared in subcalls.h */ +KBDKEYINFO KeyData; /* declared in subcalls.h */ main() { - unsigned Selector; - struct ResultCodes childID; /* child process id */ - unsigned nBytes; + SEL Selector; + RESULTCODES childID; /* child process id */ unsigned rc; /* return code */ char c, fbuf[FBSZ]; /* failing object buffer */ struct ShareRec *SmemPtr; /* shared memory pointer */ /* allocate the shared memory segment */ - if (rc = DOSALLOCSHRSEG( SHRSEGSIZE, (char *)SHRSEGNAME, - (unsigned *)&Selector )) { + if (rc = DosAllocShrSeg( SHRSEGSIZE, (PSZ)SHRSEGNAME, + &Selector )) { printf("alloc of shared memory failed, error: %d\n", rc); - DOSEXIT(1, 0); + DosExit(EXIT_PROCESS, 0); } /* Get a far pointer from a 16 bit selector */ - SmemPtr = (struct ShareRec *) GETSEGPTR(Selector, 0); + SmemPtr = (struct ShareRec *) MAKEP(Selector, 0); /* Initialize circular buffer flags */ - DOSSEMCLEAR((long)&(SmemPtr->fullsem)); - DOSSEMSET((long)&(SmemPtr->emptysem)); - DOSSEMCLEAR((long)&(SmemPtr->mutexsem)); + DosSemClear((HSEM)&(SmemPtr->fullsem)); + DosSemSet((HSEM)&(SmemPtr->emptysem)); + DosSemClear((HSEM)&(SmemPtr->mutexsem)); SmemPtr->head = 0; SmemPtr->tail = 0; /* exec asynchronously the consumer process */ - if (rc = DOSEXECPGM( - (char *) fbuf, /* ObjNameBuf */ - (unsigned) FBSZ, /* ObjNameLen */ - (unsigned) 1, /* AsyncTraceFlags */ - (char *) 0L, /* Argument Strings */ - (char *) 0L, /* Environment Strings */ - (struct ResultCodes *) &childID, + if (rc = DosExecPgm( + (PCHAR) fbuf, /* ObjNameBuf */ + FBSZ, /* ObjNameLen */ + EXEC_ASYNC, /* AsyncTraceFlags */ + (PSZ) 0L, /* Argument Strings */ + (PSZ) 0L, /* Environment Strings */ + &childID, /* ID & Termination Codes */ - (char *) CHILD1PROG )) { /* Program Filename */ + (PSZ) CHILD1PROG )) { /* Program Filename */ printf("exec of child process failed, error: %d\n", rc); - DOSEXIT(1, 0); + DosExit(EXIT_PROCESS, 0); } /* Here, we read chars from the keyboard, and put them in a */ /* circular buffer in shared memory */ - KBDCHARIN(&KeyData, 0, 0); /* read character from keyboard */ + KbdCharIn(&KeyData, 0, 0); /* read character from keyboard */ - while((c = KeyData.char_code) != CTRLd) { + while((c = KeyData.chChar) != CTRLd) { /* block if buffer full */ - DOSSEMWAIT((long)&(SmemPtr->fullsem), WAITFOREVER); + DosSemWait((HSEM)&(SmemPtr->fullsem), WAITFOREVER); - /* mutual exclusion on buffer pointers */ - DOSSEMREQUEST((long)&(SmemPtr->mutexsem), WAITFOREVER); + /* mutual exclusion on buffer pointers and semaphores */ + DosSemRequest((HSEM)&(SmemPtr->mutexsem), WAITFOREVER); SmemPtr->CircBuffer[SmemPtr->head] = c; SmemPtr->head++; /* step pointer */ SmemPtr->head %= CIRCBUFSIZE; /* wrap at end */ - if(BUFFUL(SmemPtr)) /* set semaphore if buffer full */ - DOSSEMSET((long)&(SmemPtr->fullsem)); - DOSSEMCLEAR((long)&(SmemPtr->mutexsem)); + if(BUFFUL(SmemPtr)) + DosSemSet((HSEM)&(SmemPtr->fullsem)); /* indicate buf full */ + DosSemClear((HSEM)&(SmemPtr->emptysem)); /* indicate buf !emtpy */ + DosSemClear((HSEM)&(SmemPtr->mutexsem)); - DOSSEMCLEAR((long)&(SmemPtr->emptysem)); /* indicate buf !emtpy */ - KBDCHARIN(&KeyData, 0, 0); /* read character from keyboard */ + KbdCharIn(&KeyData, 0, 0); /* read character from keyboard */ } - DOSKILLPROCESS( 1, childID.TermCode_PID ); /* kill consumer */ - DOSEXIT( 1, 0 ); /* exit, terminating all threads */ + DosKillProcess( 1, childID.codeTerminate); /* kill consumer */ + DosExit( EXIT_PROCESS, 0 ); /* exit, terminating all threads */ }