Write C-Program To Create Message Queue Print Message queue id | Remove The Message Queue.

Create Print Message queue id | Remove The Message Queue.
#include&alt;sys/types.h>
#include&alt;sys/ipc.h>
#include&alt;sys/msg.h>
main()
{
key_t key = 100;
int i,msqid;
msqid = msgget(key,IPC_CREAT | 0666);
if (msqid &alt; 0)
perror ("faild to creat message q");
else
perror("creat message q");
}
Create Message Queue

Print message queue id.
#include &alt;sys/types.h>
#include &alt;sys/ipc.h>
#include &alt;sys/msg.h>
#define KEY ((key_t) 98765L)
#define PERMS 0666
main()
{
int i, msqid;
for(i = 0; i &alt; 10; i++)
{
if ( (msqid = msgget(KEY, PERMS | IPC_CREAT)) &alt; 0)
perror("cant creat message q");
printf("msqid = %d\n", msqid);
if (msgctl(msqid, IPC_RMID, (struct msqid_ds *) 0) &alt; 0 )
perror("cant remove message q");
}
}
print message queue id.

Remove the message queue.
#include&alt;sys/ipc.h>
#include&alt;stdio.h>
#include&alt;sys/msg.h>
#include&alt;errno.h>
main()
{
key_t key = 100;
int i,msqid;
if(msgctl(msqid,IPC_RMID,0)&alt;0)
{
perror("msgctl fail ");
}
} Remove the message queue.

Related post