Annotation of os2sdk/demos/examples/share/share.c, revision 1.1.1.2

1.1       root        1: /*
                      2:  * This example illustrates the use of shared memory and ram semaphores
                      3:  * between separate processes.  One process reads and stores keystrokes
                      4:  * into a circular buffer in shared memory, and another reads the buffer
                      5:  * and writes to standard output. The output program throttles output to
                      6:  * one character per sec so the effect of buffering can be seen. Type
                      7:  * CONTROL-d to terminate.
                      8:  *
                      9:  * Note the mutual exclusion on access to the head and tail pointers of the
1.1.1.2 ! root       10:  * buffer and on clearing and setting the full and empty semaphores.  For a
        !            11:  * discussion, see Peterson & Silberschatz, "Operating System Concepts",
        !            12:  * 2nd ed., chapter 9.
1.1       root       13:  *
                     14:  * Be aware when running this that the system does keyboard buffering
                     15:  * underneath this program, so you can type ahead more than you might think.
                     16:  *
                     17:  * Needs shrchild.c as companion program.
                     18:  *
                     19:  * Compile as: cl -AL -G2 -Lp share.c
                     20:  *
1.1.1.2 ! root       21:  * Created by Microsoft Corp. 1986
1.1       root       22:  */
                     23: 
1.1.1.2 ! root       24: #define INCL_DOSPROCESS
        !            25: #define INCL_SUB
        !            26: #define INCL_DOSMEMMGR
        !            27: 
        !            28: #include <os2def.h>
        !            29: #include <bse.h>
1.1       root       30: #include "share.h"             /* common declarations between processes */
                     31: 
                     32: #define        CHILD1PROG      "SHRCHILD.EXE"          /* child process */
                     33: #define        CTRLd           4                       /* termination character */
                     34: #define FBSZ           32
                     35: 
1.1.1.2 ! root       36: KBDKEYINFO KeyData;        /* declared in subcalls.h */
1.1       root       37: 
                     38: main()
                     39: {
1.1.1.2 ! root       40:        SEL             Selector;
        !            41:        RESULTCODES     childID;     /* child process id */
1.1       root       42:        unsigned        rc;             /* return code */
                     43:        char            c,
                     44:                        fbuf[FBSZ];     /* failing object buffer */
                     45:        struct ShareRec *SmemPtr;       /* shared memory pointer */
                     46: 
                     47:        /* allocate the shared memory segment */
1.1.1.2 ! root       48:        if (rc = DosAllocShrSeg( SHRSEGSIZE, (PSZ)SHRSEGNAME,
        !            49:                                &Selector ))  {
1.1       root       50:                printf("alloc of shared memory failed, error: %d\n", rc);
1.1.1.2 ! root       51:                DosExit(EXIT_PROCESS, 0);
1.1       root       52:        }
                     53: 
                     54:        /* Get a far pointer from a 16 bit selector */
1.1.1.2 ! root       55:        SmemPtr = (struct ShareRec *) MAKEP(Selector, 0);
1.1       root       56: 
                     57:        /* Initialize circular buffer flags */
1.1.1.2 ! root       58:        DosSemClear((HSEM)&(SmemPtr->fullsem));
        !            59:        DosSemSet((HSEM)&(SmemPtr->emptysem));
        !            60:        DosSemClear((HSEM)&(SmemPtr->mutexsem));
1.1       root       61:        SmemPtr->head = 0;
                     62:        SmemPtr->tail = 0;
                     63: 
                     64:        /* exec asynchronously the consumer process */
1.1.1.2 ! root       65:        if (rc = DosExecPgm(
        !            66:                (PCHAR) fbuf,                   /* ObjNameBuf           */
        !            67:                FBSZ,                           /* ObjNameLen           */
        !            68:                EXEC_ASYNC,                     /* AsyncTraceFlags      */
        !            69:                (PSZ) 0L,                       /* Argument Strings     */
        !            70:                (PSZ) 0L,                       /* Environment Strings  */
        !            71:                &childID,
1.1       root       72:                                                /* ID & Termination Codes */
1.1.1.2 ! root       73:                (PSZ) CHILD1PROG )) {           /* Program Filename     */
1.1       root       74:            
                     75:                printf("exec of child process failed, error: %d\n", rc);
1.1.1.2 ! root       76:                DosExit(EXIT_PROCESS, 0);
1.1       root       77:        }
                     78: 
                     79:        /* Here, we read chars from the keyboard, and put them in a */
                     80:        /* circular buffer in shared memory */
                     81: 
1.1.1.2 ! root       82:        KbdCharIn(&KeyData, 0, 0);      /* read character from keyboard */
1.1       root       83: 
1.1.1.2 ! root       84:        while((c = KeyData.chChar) != CTRLd) {
1.1       root       85: 
                     86:            /* block if buffer full */
1.1.1.2 ! root       87:            DosSemWait((HSEM)&(SmemPtr->fullsem), WAITFOREVER);
1.1       root       88: 
1.1.1.2 ! root       89:            /* mutual exclusion on buffer pointers and semaphores */
        !            90:            DosSemRequest((HSEM)&(SmemPtr->mutexsem), WAITFOREVER);
1.1       root       91:            SmemPtr->CircBuffer[SmemPtr->head] = c;
                     92:            SmemPtr->head++;                /* step pointer */
                     93:            SmemPtr->head %= CIRCBUFSIZE;   /* wrap at end  */
1.1.1.2 ! root       94:            if(BUFFUL(SmemPtr))
        !            95:                DosSemSet((HSEM)&(SmemPtr->fullsem)); /* indicate buf full   */
        !            96:            DosSemClear((HSEM)&(SmemPtr->emptysem));  /* indicate buf !emtpy */
        !            97:            DosSemClear((HSEM)&(SmemPtr->mutexsem));
1.1       root       98: 
                     99: 
1.1.1.2 ! root      100:            KbdCharIn(&KeyData, 0, 0);  /* read character from keyboard */
1.1       root      101:        }
1.1.1.2 ! root      102:        DosKillProcess( 1, childID.codeTerminate);      /* kill consumer */
        !           103:        DosExit( EXIT_PROCESS, 0 );        /* exit, terminating all threads */
1.1       root      104: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.