examples/PIPS/opencproducerconsumerex/src/producer.c

00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 
00032 /* INCLUDE FILES */
00033 #include <sys/types.h>
00034 #include <sys/ipc.h>
00035 #include <sys/msg.h>
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 
00039 #include "CommanHeader.h"
00040 
00041 #define MIN(X, Y) (X < Y) ? X : Y;
00042 
00043 /*****************************************************************************
00044 *  ProducerThreadEntryPoint
00045 *  Function: Producer Thread that produces M number of items at a time 
00046 *  where M is number of Consumers.
00047 *  This thread will produce N number of items where N is total number of items 
00048 *  needed by all the Consumers.
00049 *  It also informs the Observer thread about item production
00050 *******************************************************************************/
00051 
00052 void* ProducerThreadEntryPoint( void* aParam ) 
00053 {
00054         ThreadParam* args = (ThreadParam*) aParam;
00055         int yetToProduce = args->noOfItems;
00056         int produceNow = 0;
00057         int noOfConsumers = args->noOfConsumers;
00058         ProducedItem item;
00059         key_t msgQFd = -1;
00060         int ret = 0;
00061         /* Construct the message to be send thru msg q */
00062         struct msgbuf* sendMsg = (struct msgbuf*)malloc(KMAXSENDMSG);
00063         sendMsg->mtype = 1;
00064         
00065         /* Get the Handler to Observer Msg Q */
00066         msgQFd = msgget(KMSGQKEY, IPC_CREAT);
00067 
00068         item.itemNum = 1;
00069 
00070         while (yetToProduce != 0) 
00071         {
00072                 produceNow = MIN(yetToProduce, noOfConsumers);
00073                 yetToProduce -= produceNow;
00074 
00075                 /* Acquire the Lock so that Producer can Produce "produceNow" items once. */
00076                 sem_wait(&args->itemLock);
00077                 for(;produceNow != 0; produceNow--) 
00078                 {
00079                         sprintf(item.itemName, "Item-Number: #%d", item.itemNum);
00080                         /* Push this item on to Stack so that Consumetrs can take it.*/
00081                         PushOntoStack(&item);
00082                         item.itemNum++;
00083                         /* Inform the Observer about Item Production */
00084                         sprintf(sendMsg->mtext, " Producer Produced %s\0", item.itemName);
00085                         ret = msgsnd(msgQFd, sendMsg, strlen(sendMsg->mtext)+4, 0);
00086                 }
00087                 /* Release the Lock so that Consumers can consume produced items. */
00088                 sem_post(&args->itemLock);
00089         }
00090 
00091         free( sendMsg );
00092         return (int*)0;
00093 }
00094 
00095 /*  End of File */

Generated by  doxygen 1.6.2