Annotation of q_a/samples/bndbuf/bndbufp.c, revision 1.1

1.1     ! root        1: /**********************************************************************
        !             2: *
        !             3: *   bndbufp.c - Contains the exported RPC interfaces for inserting and
        !             4: *              removing strings from the central buffer pool.
        !             5: *
        !             6: *   Frederick Chong
        !             7: *   Microsoft Developer Support
        !             8: *   Copyright (c) 1992, 1993 Microsoft Corporation
        !             9: *
        !            10: ***********************************************************************/
        !            11: 
        !            12: 
        !            13: #include <windows.h>
        !            14: #include <stdio.h>
        !            15: #include <string.h>
        !            16: #include <stdlib.h>
        !            17: #include <rpc.h>
        !            18: #include "common.h"
        !            19: #include "bndbuf.h"
        !            20: 
        !            21: 
        !            22: // Functions prototype
        !            23: 
        !            24: void enter_item(char *input);
        !            25: void remove_item(char *output);
        !            26: 
        !            27: 
        !            28: // external variables
        !            29: 
        !            30: extern char buffer[MAX_ITEM][MAX_SIZE];     // Global buffer pool
        !            31: extern HANDLE hMutex;                      // Handle to mutex
        !            32: extern HANDLE hEmptySem;                   // Handle to empty semaphore
        !            33: extern HANDLE hFullSem;                            // Handle to full semaphore
        !            34: extern int pro_buf_pos;                    // Producer buffer position
        !            35: extern int con_buf_pos;                    // Consumer buffer position
        !            36: 
        !            37: 
        !            38: /*************************************************************************
        !            39: 
        !            40: void insert_buffer(unsigned char *item)
        !            41: 
        !            42: Synchronizes access to the central buffer pool for inserting a string.
        !            43: 
        !            44: **************************************************************************/
        !            45: 
        !            46: void insert_buffer(unsigned char *item)
        !            47: {
        !            48: 
        !            49:     WaitForSingleObject(hEmptySem, INFINITE);      // Decrease empty count
        !            50:     WaitForSingleObject(hMutex, INFINITE);         // Acquire Mutex
        !            51:     enter_item(item);                              // Put item in buffer
        !            52:     ReleaseMutex(hMutex);                          // Release Mutex
        !            53:     ReleaseSemaphore(hFullSem, 1, NULL);           // Increment full count
        !            54: 
        !            55: }
        !            56: 
        !            57: 
        !            58: 
        !            59: /*************************************************************************
        !            60: 
        !            61: void remove_buffer(unsigned char *item)
        !            62: 
        !            63: Synchronizes access to the the central buffer pool for removing a string.
        !            64: 
        !            65: **************************************************************************/
        !            66: 
        !            67: void remove_buffer(unsigned char *item)
        !            68: {
        !            69: 
        !            70:     WaitForSingleObject(hFullSem, INFINITE);       // Decrease full count
        !            71:     WaitForSingleObject(hMutex, INFINITE);         // Acquire Mutex
        !            72:     remove_item(item);                             // Take item from buffer
        !            73:     ReleaseMutex(hMutex);                          // Release Mutex
        !            74:     ReleaseSemaphore(hEmptySem, 1, NULL);          // Increment full count
        !            75: 
        !            76: }
        !            77: 
        !            78: 
        !            79: 
        !            80: /*************************************************************************
        !            81: 
        !            82: void enter_item(char *input)
        !            83: 
        !            84: Puts a string in the centralized buffer pool
        !            85: 
        !            86: We maintain two buffer pointers separately for the producer
        !            87: and the consumer so that they can keep track of where they
        !            88: are in the buffer queue.
        !            89: 
        !            90:                -----------------------------
        !            91: Producer -->   |   |   |   ...     |   |   | --> Consumer
        !            92:                |   |   |           |   |   |
        !            93:                -----------------------------
        !            94:                        ^           ^
        !            95:                        |           |
        !            96:                     current        current
        !            97:                     producer       consumer
        !            98:                     pointer        pointer
        !            99: 
        !           100: **************************************************************************/
        !           101: 
        !           102: 
        !           103: void enter_item(char *input)
        !           104: {
        !           105: 
        !           106:     strncpy(buffer[pro_buf_pos], input, MAX_SIZE);
        !           107:     printf("\nproduce    ---> %s", buffer[pro_buf_pos]);
        !           108:     pro_buf_pos = ((++pro_buf_pos) % MAX_ITEM);
        !           109: }
        !           110: 
        !           111: 
        !           112: /*************************************************************************
        !           113: 
        !           114: void remove_item(char *output)
        !           115: 
        !           116: Removes a string from the centralized buffer pool.
        !           117: 
        !           118: **************************************************************************/
        !           119: 
        !           120: void remove_item(char *output)
        !           121: {
        !           122: 
        !           123:     strncpy(output, buffer[con_buf_pos], 20);
        !           124:     printf("\nconsume    ---> %s", output);
        !           125:     con_buf_pos = ((++con_buf_pos) % MAX_ITEM);
        !           126: 
        !           127: }

unix.superglobalmegacorp.com

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