×
Namespaces

Variants
Actions
Revision as of 04:57, 20 October 2011 by hamishwillee (Talk | contribs)

Message Queues Example

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Keywords: RMessageQueue
Created: spark55 (01 Oct 2007)
Last edited: hamishwillee (20 Oct 2011)
How to communicate between two threads or processes (Message Queues Description)
{{{width}}}
16 Sep
2009

The following code shows the message queues usage (follows the steps involved in the above link):

#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
 
#define MSG_KEY 1000
pthread_t threadid;
 
void* MsgDSndThread(void* arg)
{
struct msgbuf *snd = malloc(16);
key_t fd;
snd->mtype = 1;
strcpy(snd->mtext, "message");
fd = msgget(MSG_KEY, IPC_CREAT);
msgsnd(fd, snd, 12, 0);
free(snd);
return 0;
}
 
void CreateThreadL()
{
pthread_create(&threadid,(pthread_attr_t *)NULL,MsgDSndThread, NULL);
}
 
int main()
{
struct msgbuf *rcv = malloc(16);
key_t fd = msgget(MSG_KEY, IPC_CREAT);
CreateThreadL();
msgrcv(fd, rcv, 6, 0, 0);
msgctl(fd, IPC_RMID, NULL);
free(rcv);
return 0;
}
94 page views in the last 30 days.