00001
00014 # include <pthread.h>
00015 # include <stdio.h>
00016 # include <string.h>
00017 # include <unistd.h>
00018
00019 # include "MsgQLib.h"
00020
00021 ULONG q1 = 1;
00022 ULONG q2 = 2;
00023 const int KMaxMsg = 10;
00024
00029 void ThreadFunction(int *id) {
00030 int i = 2;
00031 int err = 0;
00032 int result = 0;
00033 int pri = MSG_PRI_NORMAL;
00034 int timeout = NO_WAIT;
00035 int len = 100;
00036 int nBytes;
00037 char smsg[] = "Sending Some Data through MsgQ";
00038 char rmsg[100];
00039 ULONG queueOne = q1;
00040 ULONG queueTwo = q2;
00041
00042
00043 if(*id == q2) {
00044 queueOne = q2;
00045 queueTwo = q1;
00046 }
00047
00048 printf("Thread %d: Started..\n", *id);
00049
00050
00051
00052 result = MsgQCreate(queueOne, KMaxMsg, MSG_Q_FIFO, &err);
00053 printf("Thread %d: Q CREATE result = %d\n", *id, result);
00054
00055 while(i>=0) {
00056
00057 nBytes = strlen(smsg);
00058
00059 result = MsgQSend(queueOne, smsg, nBytes, pri, timeout, &err);
00060 printf("Thread %d: Q SEND result = %d\n", *id, result);
00061
00062 sleep(1);
00063 rmsg[0] = '\0';
00064
00065 result = MsgQReceive(queueTwo, rmsg, len, timeout, &err);
00066 rmsg[result] = '\0';
00067 printf("Thread %d: Q RECEIVE result = %d\n", *id, result);
00068 printf("Thread %d: Message Received from Message Queue 2 is : %s\n", *id, rmsg);
00069 i--;
00070 }
00071 sleep(2);
00072
00073
00074 result=MsgQDelete(queueOne, &err);
00075 printf("Thread %d: Q DELETE result = %d\n", *id, result);
00076 }
00077
00078
00079 int main() {
00080 int result;
00081 int err;
00082 pthread_t thread1;
00083 pthread_t thread2;
00084
00085 printf("Main Thread: Started..\n");
00086
00087
00088 result = MsgQCreate(q1, KMaxMsg, MSG_Q_FIFO, &err);
00089 printf("Main Thread: Q CREATE result = %d\n",result);
00090
00091 result = MsgQCreate(q2, KMaxMsg, MSG_Q_FIFO, &err);
00092 printf("Main Thread: Q CREATE result = %d\n",result);
00093
00094
00095
00096 if(pthread_create(&thread1, NULL, (thread_begin_routine)ThreadFunction, (void*) &q1) != 0) {
00097 printf("Main Thread: Failed to create the threadOne\n");
00098 }
00099
00100 if(pthread_create(&thread2, NULL, (thread_begin_routine)ThreadFunction, (void*) &q2) != 0) {
00101 printf("Main Thread: Failed to create the threadTwo\n");
00102 }
00103
00104
00105 pthread_join(thread1, NULL);
00106 pthread_join(thread2, NULL);
00107 printf("Main Thread: Exiting from Main() \nMain Thread: Enter a Key to Exit");
00108 getchar();
00109 return 0;
00110 }