examples/PIPS/opencproducerconsumerex/src/producerconsumer.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 <pthread.h>
00034 #include <errno.h>
00035 #include <unistd.h>
00036 #include <fcntl.h>
00037 #include <sys/stat.h>
00038 #include <sys/types.h>
00039 #include <sys/ioctl.h>
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 //#include <staticlibinit_gcce.h>
00043 #include "CommanHeader.h"
00044 
00045 int main() 
00046 {
00047         ThreadParam threadParams[MAX_CLIENTS + 1];
00048         pthread_t threadID[MAX_CLIENTS + 1];
00049         int i = 0;
00050         int noOfConsumers = 0;
00051         int noOfItems = 0;
00052         int totalItems = 0;
00053         int ret = 0;
00054         int exitReason = 0;
00055         sem_t semLock;
00056         pthread_attr_t threadAttr;
00057 
00058         if( sem_init( &semLock, 0, 1 ) != 0 ) 
00059         {
00060                 printf("Error Creating semaphore");
00061                 exit(-1);
00062         }
00063 
00064         /* Read all the inputs needed */
00065         printf("Enter Number of Consumers : ");
00066         scanf("%d", &noOfConsumers);
00067 
00068         if(MAX_CLIENTS < noOfConsumers) 
00069         {
00070                 noOfConsumers = MAX_CLIENTS;
00071         }
00072 
00073         for(i=1; i<=noOfConsumers; i++) 
00074         {
00075                 printf("Items Needed by Consumer-%d : ", i);
00076                 scanf("%d", &noOfItems);
00077                 totalItems += noOfItems;
00078 
00079                 threadParams[i].noOfItems = noOfItems;
00080                 threadParams[i].itemLock = semLock;
00081                 threadParams[i].noOfConsumers = i;
00082         }
00083 
00084         /* For the Producer      */
00085         threadParams[0].noOfItems = totalItems;
00086         threadParams[0].noOfConsumers = noOfConsumers;
00087         threadParams[0].itemLock = semLock;
00088 
00089         pthread_attr_init( &threadAttr );
00090         pthread_attr_setdetachstate( &threadAttr, PTHREAD_CREATE_JOINABLE );
00091         
00092         /* Create a Producer thread now */
00093         ret = pthread_create( &threadID[0], &threadAttr, ProducerThreadEntryPoint, 
00094                 (void*)&threadParams[0] );
00095 
00096         if(ret != 0) 
00097         {
00098                 printf("Error Creating Producer Thread");
00099                 exit(-1);
00100         }
00101 
00102         /* Create a Consumer Threads now */
00103         for(i=1; i <= noOfConsumers; i++) 
00104         {
00105                 ret = pthread_create( &threadID[i], &threadAttr, ConsumerThreadEntryPoint, 
00106                         (void*)&threadParams[i] );
00107 
00108                 if(ret != 0) 
00109                 {
00110                         printf("Error Creating Consumer Thread %d", i);
00111                         exit(-1);
00112                 }
00113         }
00114 
00115         /* Call C++ function that creats Observer Thread */
00116         CreateObserverThread (totalItems * 2);
00117 
00118         /* Wait for the completion of all the threads */
00119         for(i=0; i<=noOfConsumers; i++) 
00120         {
00121                 ret = pthread_join(threadID[i], (void**)&exitReason );
00122         }
00123 
00124         /* Destroy the semaphore */
00125         sem_destroy( &semLock );
00126         printf("Completed the Production/Consumption..\n Press Any Key to Exit");
00127         getchar();
00128         getchar();
00129         return 0;
00130 }
00131 
00132 /*  End of File */

Generated by  doxygen 1.6.2