Name
msgget - gets a message queue identifier
Library
libc.lib
Synopsis
|
int
msgget (key_t key, int msgflg);
|
Return values
Upon successful completion a positive message queue identifier is returned.
Otherwise, -1 is returned and the global variable
errno
is set to indicate the error.
Detailed description
The
msgget
function
returns the message queue identifier associated with
key.
A message queue identifier is a unique integer greater than zero.
A message queue is created if either
key
is equal to
IPC_PRIVATE,
or
key
does not have a message queue identifier associated with it, and the
IPC_CREAT
bit is set in
msgflg.
If a new message queue is created, the data structure associated with it (the
msqid_ds
structure, see
msgctl
is initialized as follows:
-
msg_perm.cuid
and
msg_perm.uid
are set to the effective uid of the calling process.
In the current implementation,
uid
is set to root.
-
msg_perm.gid
and
msg_perm.cgid
are set to the effective gid of the calling process.
In the current implementation,
gid
is set to root.
-
msg_perm.mode
is set to the lower 9 bits of
msgflg.
-
msg_cbytes,
msg_qnum,
msg_lspid,
msg_lrpid,
msg_rtime,
and
msg_stime
are set to 0.
-
msg_qbytes
is set to the system wide maximum value for the number of bytes in a queue
(Dv MSGMNB).
-
msg_ctime
is set to the current time.
Examples
Example code to create a message queue using
msgget
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define MESSAGE_Q_KEY 1000
int main(void)
{
int msq_id, len;
struct {
long mtype;
char mtext[128];
} msg_buf;
/*
* Create a message queue with a given key
*/
if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
printf("Message Q create failed with errno %d\n", errno);
return -1;
}
msg_buf.mtype = 1; /* message identifier */
strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
len = strlen(msg_buf.mtext)+1;
/*
* Put the message in the queue
*/
if (msgsnd(msq_id, (struct msgbuf *)&msg_buf, len, 0) == -1) {
printf("Message Q send failed with errno %d\n", errno);
}
return 0;
}
Example code to return an existing message queue with
msgget
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define MESSAGE_Q_KEY 1000
int main(void)
{
int msq_id;
int msg_len = 128;
int msg_type = 0; /* Any type of message */
struct {
long mtype;
char mtext[128];
} msg_buf;
/*
* Get the message queue id for the given key
*/
if ((msq_id = msgget(MESSAGE_Q_KEY, 0)) == -1) {
printf("Message Q get id failed with errno %d\n", errno);
return -1;
}
/*
* Get the message from the queue
*/
if (msgrcv(msq_id, (struct msgbuf *)&msg_buf, msg_len, msg_type, 0) == -1) {
printf("Message Q recv failed with errno %d\n", errno);
}
/*
* Remove the message queue
*/
if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
printf("Message Q delete failed with errno %d\n", errno);
return -1;
}
return 0;
}
Errors
[EACCES]
|
|
A message queue is already associated with
key
and the caller has no permission to access it.
|
[EEXIST]
|
|
Both
IPC_CREAT
and
IPC_EXCL
are set in
msgflg,
and a message queue is already associated with
key.
|
[ENOSPC]
|
|
A new message queue could not be created because the system limit for
the number of message queues has been reached.
|
[ENOENT]
|
|
IPC_CREAT
was not set in
msgflg
and no message queue associated with
key
was found.
|
See also
msgctl,
msgrcv,
msgsnd
Feedback
For additional information or queries on this page send feedback
© 2005-2007 Nokia
|
|