|
1 <?xml version="1.0" encoding="utf-8"?> |
|
2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. --> |
|
3 <!-- This component and the accompanying materials are made available under the terms of the License |
|
4 "Eclipse Public License v1.0" which accompanies this distribution, |
|
5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". --> |
|
6 <!-- Initial Contributors: |
|
7 Nokia Corporation - initial contribution. |
|
8 Contributors: |
|
9 --> |
|
10 <!DOCTYPE concept |
|
11 PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd"> |
|
12 <concept id="GUID-9BFF0271-F5C3-45B6-AD4D-9179107F300A" xml:lang="en"><title>Open |
|
13 C Producer Consumer Example</title><prolog><metadata><keywords/></metadata></prolog><conbody> |
|
14 <p>OpenCProducerConsumerEx is a hybrid application that depicts solution for |
|
15 producer consumer problem using Open C. This application uses main() as an |
|
16 entry point for the application. It uses <codeph>libc</codeph> and <codeph>libpthread</codeph> </p> |
|
17 <section id="GUID-026FD8D6-6C4A-5958-B289-775D4AE1E756-GENID-1-8-1-11-1-1-5-1-3-1-11-1-14-1-2-2"><title>Download</title> <p>Click |
|
18 on the following link to download the example: <xref href="guid-6013a680-57f9-415b-8851-c4fa63356636/zips/guid-b9259541-c444-400a-8f6e-8daaa5381a79.zip" scope="external">opencproducerconsumerex.zip</xref></p><p>Click: <xref href="guid-6013a680-57f9-415b-8851-c4fa63356636/guid-b9259541-c444-400a-8f6e-8daaa5381a79.html" scope="peer">browse</xref> to view the example code. </p> </section> |
|
19 <section id="GUID-7E7C4564-B10E-41F9-8A9D-D8A6C5E9C51C-GENID-1-8-1-11-1-1-5-1-3-1-11-1-14-1-2-3"><title>Design and |
|
20 Implementation</title> <p>The following sections provide information about |
|
21 the implementation of the example. </p> <p><b>Capabilities</b> </p> The program |
|
22 capability is defined in: <filepath>ProducerConsumer.mmp</filepath> (EXE - |
|
23 application):<codeph> CAPABILITY NONE</codeph>. <p><b>Implementation details |
|
24 of EXE</b> </p>This is a simple console based application. Main thread of |
|
25 the application will create a thread named as Producer that will produce the |
|
26 item and push it on to the stack. There will be N consumers which want to |
|
27 consume some N(i) items from the Producer. Producer will produce N items at |
|
28 a time and pushes them to the stack and totally it will produce sum(N(i)) |
|
29 items. The consumer thread will try to pop items from the stack and each consumer |
|
30 will pop N(i) items from the stack. The application will create one thread |
|
31 for each consumer. Producer and all the consumer threads will be synchronized |
|
32 using a semaphore. The main thread, Producer and the consumer codes are pure |
|
33 C-code. the stack used by Producer and the consumer threads is Standard C++ |
|
34 code. Apart from these threads, there is another thread named as Observer |
|
35 that is created using Symbian platform API. Producer and the consumers will |
|
36 establish a message queue with this thread and Observer will read message |
|
37 from all other threads and will log them to a file wich is opened/written |
|
38 using Symbian APIs. Hence, this application is a hybrid application with: |
|
39 <p>Standard C code -> <filepath>Consumer.c</filepath>, <filepath>Producer.c</filepath>, <filepath>ProducerConsumer.c</filepath>, <filepath>CommanHeader.h</filepath> </p><p>Standard C++ code -> <filepath>Stack.cpp</filepath>,<filepath>Stack.h</filepath>, <filepath>CommanHeader.h</filepath></p><p>Symbian |
|
40 platform code that uses <codeph>libc</codeph> APIs and Observer.cpp.</p><p> |
|
41 The application has its own icon and can be launched from the UI both in the |
|
42 emulator and in the device. </p><p><b>API Usage</b></p><p><codeph>string APIs</codeph> </p><p><codeph>IPC |
|
43 APIs (message queue and semaphore)</codeph> </p><p> <codeph>Threading APIs |
|
44 (from pthread)</codeph> </p><p><codeph>console I/O APIs</codeph> </p><p><codeph>File |
|
45 APIs of Symbian platform</codeph></p><p> <codeph>Threading APIs of Symbian |
|
46 platform</codeph></p></section> |
|
47 <section id="GUID-67A72761-1D92-46D1-B1C9-7455978BDE4E-GENID-1-8-1-11-1-1-5-1-3-1-11-1-14-1-2-4"><title>Building and |
|
48 Using</title> To build the OpenCProducerConsumerEx application, go to the |
|
49 OpenCProducerConsumerEx\group directory and build the application. The definition |
|
50 for the whole application can be found in the<filepath> bld.inf</filepath>in |
|
51 the group subdirectory of the applications main directory. <p>The application |
|
52 is simple console based application. It has its own default icon and can be |
|
53 launched from the UI both in the emulator and in the device. The application |
|
54 can be launched by: Executing OpenCProducerConsumerEx from eshell. Clicking |
|
55 default icon with display text OpenCProducerConsumerEx within the install |
|
56 folder launcher application. </p></section> |
|
57 </conbody></concept> |