POSIX message queues brief usage
2008-12-12 15:31
197 查看
Code from
http://mij.oltrelinux.com/devel/unixprg/
// a little error in the original version, changed a little
More information on POSIX message queue
http://www.linuxhowtos.org/manpages/7/mq_overview.htm
And one problem I found is when I try to open a queue not under '/' directory, an error shows "Permision denied".
dropone.c
/*
* dropone.c
*
* drops a message into a #defined queue, creating it if user
* requested. The message is associated a priority still user
* defined
*
*
* Created by Mij <mij@bitchx.it> on 07/08/05.
* Original source file available on http://mij.oltrelinux.com/devel/unixprg/
*
*/
#include <stdio.h>
/* mq_* functions */
#include <mqueue.h>
#include <sys/stat.h>
/* exit() */
#include <stdlib.h>
/* getopt() */
#include <unistd.h>
/* ctime() and time() */
#include <time.h>
/* strlen() */
#include <string.h>
/* name of the POSIX object referencing the queue */
#define MSGQOBJ_NAME "/myqueue123"
/* max length of a message (just for this process) */
#define MAX_MSG_LEN 70
int main(int argc, char *argv[]) {
mqd_t msgq_id;
unsigned int msgprio = 0;
pid_t my_pid;
char msgcontent[MAX_MSG_LEN];
int create_queue = 0;
char ch; /* for getopt() */
time_t currtime;
opterr = 0;
my_pid = getpid();
/* accepting "-q" for "create queue", requesting "-p prio" for message priority */
while ((ch = getopt(argc, argv, "qp:")) != -1) {
switch (ch) {
case 'q': /* create the queue */
printf("get q option/n");
create_queue = 1;
break;
case 'p': /* specify client id */
printf("get p option/n");
msgprio = (unsigned int)strtol(optarg, (char **)NULL, 10);
printf("I (%d) will use priority %d/n", my_pid, msgprio);
break;
default:
printf("Usage: %s [-q] -p msg_prio, getopt/n", argv[0]);
exit(1);
}
}
/* forcing specification of "-i" argument */
if (msgprio == 0) {
printf("Usage: %s [-q] -p msg_prio/n", argv[0]);
exit(1);
/* opening the queue -- mq_open() */
if (create_queue) {
/* mq_open() for creating a new queue (using default attributes) */
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, NULL);
} else {
/* mq_open() for opening an existing queue */
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR);
}
if (msgq_id == (mqd_t)-1) {
perror("In mq_open()");
exit(1);
}else {
printf("msg queuse %s opened/n", MSGQOBJ_NAME);
}
/* producing the message */
currtime = time(NULL);
snprintf(msgcontent, MAX_MSG_LEN, "Hello from process %u (at %s).", my_pid, ctime(&currtime));
/* sending the message -- mq_send() */
mq_send(msgq_id, msgcontent, strlen(msgcontent)+1, msgprio);
/* closing the queue -- mq_close() */
mq_close(msgq_id);
return 0;
}
takeone.c
/*
* takeone.c
*
* simply request a message from a queue, and displays queue
* attributes.
*
*
* Created by Mij <mij@bitchx.it> on 07/08/05.
* Original source file available on http://mij.oltrelinux.com/devel/unixprg/
*
*/
#include <stdio.h>
/* mq_* functions */
#include <mqueue.h>
/* exit() */
#include <stdlib.h>
/* getopt() */
#include <unistd.h>
/* ctime() and time() */
#include <time.h>
/* strlen() */
#include <string.h>
/* name of the POSIX object referencing the queue */
#define MSGQOBJ_NAME "/myqueue123"
/* max length of a message (just for this process) */
#define MAX_MSG_LEN 10000
int main(int argc, char *argv[]) {
mqd_t msgq_id;
char msgcontent[MAX_MSG_LEN];
int msgsz;
unsigned int priority;
struct mq_attr msgq_attr;
/* opening the queue -- mq_open() */
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR);
if (msgq_id == (mqd_t)-1) {
perror("In mq_open()");
exit(1);
}
/* getting the attributes from the queue -- mq_getattr() */
mq_getattr(msgq_id, &msgq_attr);
printf("Queue /"%s/":/n/t- stores at most %ld messages/n/t- large at most %ld bytes each/n/t- currently holds %ld messages/n", MSGQOBJ_NAME, msgq_attr.mq_maxmsg, msgq_attr.mq_msgsize, msgq_attr.mq_curmsgs);
/* getting a message */
msgsz = mq_receive(msgq_id, msgcontent, MAX_MSG_LEN, &priority);
if (msgsz == -1) {
perror("In mq_receive()");
exit(1);
}
printf("Received message (%d bytes) from %d: %s/n", msgsz, priority, msgcontent);
/* closing the queue -- mq_close() */
mq_close(msgq_id);
return 0;
}
http://mij.oltrelinux.com/devel/unixprg/
// a little error in the original version, changed a little
More information on POSIX message queue
http://www.linuxhowtos.org/manpages/7/mq_overview.htm
And one problem I found is when I try to open a queue not under '/' directory, an error shows "Permision denied".
dropone.c
/*
* dropone.c
*
* drops a message into a #defined queue, creating it if user
* requested. The message is associated a priority still user
* defined
*
*
* Created by Mij <mij@bitchx.it> on 07/08/05.
* Original source file available on http://mij.oltrelinux.com/devel/unixprg/
*
*/
#include <stdio.h>
/* mq_* functions */
#include <mqueue.h>
#include <sys/stat.h>
/* exit() */
#include <stdlib.h>
/* getopt() */
#include <unistd.h>
/* ctime() and time() */
#include <time.h>
/* strlen() */
#include <string.h>
/* name of the POSIX object referencing the queue */
#define MSGQOBJ_NAME "/myqueue123"
/* max length of a message (just for this process) */
#define MAX_MSG_LEN 70
int main(int argc, char *argv[]) {
mqd_t msgq_id;
unsigned int msgprio = 0;
pid_t my_pid;
char msgcontent[MAX_MSG_LEN];
int create_queue = 0;
char ch; /* for getopt() */
time_t currtime;
opterr = 0;
my_pid = getpid();
/* accepting "-q" for "create queue", requesting "-p prio" for message priority */
while ((ch = getopt(argc, argv, "qp:")) != -1) {
switch (ch) {
case 'q': /* create the queue */
printf("get q option/n");
create_queue = 1;
break;
case 'p': /* specify client id */
printf("get p option/n");
msgprio = (unsigned int)strtol(optarg, (char **)NULL, 10);
printf("I (%d) will use priority %d/n", my_pid, msgprio);
break;
default:
printf("Usage: %s [-q] -p msg_prio, getopt/n", argv[0]);
exit(1);
}
}
/* forcing specification of "-i" argument */
if (msgprio == 0) {
printf("Usage: %s [-q] -p msg_prio/n", argv[0]);
exit(1);
/* opening the queue -- mq_open() */
if (create_queue) {
/* mq_open() for creating a new queue (using default attributes) */
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, NULL);
} else {
/* mq_open() for opening an existing queue */
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR);
}
if (msgq_id == (mqd_t)-1) {
perror("In mq_open()");
exit(1);
}else {
printf("msg queuse %s opened/n", MSGQOBJ_NAME);
}
/* producing the message */
currtime = time(NULL);
snprintf(msgcontent, MAX_MSG_LEN, "Hello from process %u (at %s).", my_pid, ctime(&currtime));
/* sending the message -- mq_send() */
mq_send(msgq_id, msgcontent, strlen(msgcontent)+1, msgprio);
/* closing the queue -- mq_close() */
mq_close(msgq_id);
return 0;
}
takeone.c
/*
* takeone.c
*
* simply request a message from a queue, and displays queue
* attributes.
*
*
* Created by Mij <mij@bitchx.it> on 07/08/05.
* Original source file available on http://mij.oltrelinux.com/devel/unixprg/
*
*/
#include <stdio.h>
/* mq_* functions */
#include <mqueue.h>
/* exit() */
#include <stdlib.h>
/* getopt() */
#include <unistd.h>
/* ctime() and time() */
#include <time.h>
/* strlen() */
#include <string.h>
/* name of the POSIX object referencing the queue */
#define MSGQOBJ_NAME "/myqueue123"
/* max length of a message (just for this process) */
#define MAX_MSG_LEN 10000
int main(int argc, char *argv[]) {
mqd_t msgq_id;
char msgcontent[MAX_MSG_LEN];
int msgsz;
unsigned int priority;
struct mq_attr msgq_attr;
/* opening the queue -- mq_open() */
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR);
if (msgq_id == (mqd_t)-1) {
perror("In mq_open()");
exit(1);
}
/* getting the attributes from the queue -- mq_getattr() */
mq_getattr(msgq_id, &msgq_attr);
printf("Queue /"%s/":/n/t- stores at most %ld messages/n/t- large at most %ld bytes each/n/t- currently holds %ld messages/n", MSGQOBJ_NAME, msgq_attr.mq_maxmsg, msgq_attr.mq_msgsize, msgq_attr.mq_curmsgs);
/* getting a message */
msgsz = mq_receive(msgq_id, msgcontent, MAX_MSG_LEN, &priority);
if (msgsz == -1) {
perror("In mq_receive()");
exit(1);
}
printf("Received message (%d bytes) from %d: %s/n", msgsz, priority, msgcontent);
/* closing the queue -- mq_close() */
mq_close(msgq_id);
return 0;
}
相关文章推荐
- Usage sample of unix Message Queues
- semaphore & ipc_message posix message queue & ipc_shared Memory
- Messages and Message Queues
- Posix message queue 消息队列
- 091021(星期三)MFC线程消息循环4,Using Messages and Message Queues的msdn描述
- 消息队列 posix message queue
- linux message queues
- Using Messages and Message Queues
- 消息和消息队列(About Message and Message Queues)
- 消息和消息队列(Using Messages and Message Queues)
- About Messages and Message Queues
- VC++ Messages and Message Queues
- Brief Intro to Operations and Operation Queues of Foundation Framework
- fn project Message Queues 配置
- About Messages and Message Queues
- Using Messages and Message Queues
- Using Messages and Message Queues (Windows)
- Optimizing Data Access and Messaging - Idempotency for Windows Azure Message Queues
- POSIX message queue
- Windows Message Codes