00001
00015
00016 #include <sys/types.h>
00017 #include <sys/ipc.h>
00018 #include <sys/msg.h>
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021
00022 #include "CommanHeader.h"
00023
00024 #define MIN(X, Y) (X < Y) ? X : Y;
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 void* ProducerThreadEntryPoint( void* aParam )
00036 {
00037 ThreadParam* args = (ThreadParam*) aParam;
00038 int yetToProduce = args->noOfItems;
00039 int produceNow = 0;
00040 int noOfConsumers = args->noOfConsumers;
00041 ProducedItem item;
00042 key_t msgQFd = -1;
00043 int ret = 0;
00044
00045 struct msgbuf* sendMsg = (struct msgbuf*)malloc(KMAXSENDMSG);
00046 sendMsg->mtype = 1;
00047
00048
00049 msgQFd = msgget(KMSGQKEY, IPC_CREAT);
00050
00051 item.itemNum = 1;
00052
00053 while (yetToProduce != 0)
00054 {
00055 produceNow = MIN(yetToProduce, noOfConsumers);
00056 yetToProduce -= produceNow;
00057
00058
00059 sem_wait(&args->itemLock);
00060 for(;produceNow != 0; produceNow--)
00061 {
00062 sprintf(item.itemName, "Item-Number: #%d", item.itemNum);
00063
00064 PushOntoStack(&item);
00065 item.itemNum++;
00066
00067 sprintf(sendMsg->mtext, " Producer Produced %s\0", item.itemName);
00068 ret = msgsnd(msgQFd, sendMsg, strlen(sendMsg->mtext)+4, 0);
00069 }
00070
00071 sem_post(&args->itemLock);
00072 }
00073
00074 free( sendMsg );
00075 return (int*)0;
00076 }
00077
00078