Nokia N97 SDK
Example Applications Guide

consumer.c File Reference


Detailed Description

Part of : Open C / ProducerConsumer

Description : ProducerConsumer Problem solution Version : 0.1

Copyright (c) 2007 Nokia Corporation. This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia Corporation.

Definition in file consumer.c.

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include "CommanHeader.h"

Go to the source code of this file.


Functions

void *  ConsumerThreadEntryPoint (void *aParam)


Function Documentation

void* ConsumerThreadEntryPoint (  void *  aParam  ) 
 

Declaration of Consumer Thread Entry Point

Parameters:
aParam  - argument to thread Entry function
Returns:
thread return value

Definition at line 30 of file consumer.c.

References FreeItem(), ThreadParam::itemLock, ThreadParam::noOfConsumers, ThreadParam::noOfItems, and PopFromStack().

00031 {
00032         ThreadParam* args = (ThreadParam*) aParam;
00033         int yetToConsume = args->noOfItems;
00034         int clientId = args->noOfConsumers;
00035         ProducedItem* consumeItem;
00036         int ret = 0;
00037         key_t msgQFd = -1;
00038 
00039         /* Construct the message to be send thru msg q */
00040         struct msgbuf* sendMsg = (struct msgbuf*)malloc(KMAXSENDMSG);
00041         sendMsg->mtype = 1;
00042         
00043         /* Get the Handler to Observer Msg Q */
00044         msgQFd = msgget(KMSGQKEY, IPC_CREAT);
00045 
00046         while (yetToConsume != 0) 
00047         {
00048                 /* Acquire the Lock before checking for items available from Producer */
00049                 sem_wait(&args->itemLock);
00050                 /* Pop item from Producved Item-Stack */
00051                 consumeItem = PopFromStack();
00052                 if (consumeItem == NULL) 
00053                 {
00054                         /* No items in the Stack */
00055                         sem_post(&args->itemLock);
00056                         continue;
00057                 }
00058                 yetToConsume--;
00059                 /* Inform the Observer about Item Consumption */
00060                 sprintf(&sendMsg->mtext, " Consumer#%d Consuming Produced %s\0", clientId, consumeItem->itemName);
00061                 ret = msgsnd(msgQFd, sendMsg, strlen(sendMsg->mtext)+4, 0);
00062 
00063                 /* Release the Lock once after getting an item */
00064                 sem_post(&args->itemLock);
00065                 FreeItem(consumeItem);
00066         }
00067 
00068         free( sendMsg );
00069         return (int*)0;
00070 }


© Nokia 2009

Back to top