1 mq_creat()
#include
#include
#include /* For O_* constants */
#include /* For mode constants */
#include
#include
void perror(const char *s);
#include
extern int errno;
struct mq_attr attr;
extern char *optarg;
extern int optind, opterr, optopt;
#define FILE_MODE (S_IRUSR|S_IWUSR) //注意这个宏,否则可能在调用mq_send()时permision denied.
int main(int argc,char **argv)
{
int c,flags;
mqd_t mqd ;
flags = O_RDWR | O_CREAT;
while((c = getopt(argc,argv,"em:z:"))!=-1){
switch(c)
{
case 'e':
flags |= O_EXCL;
break;
case 'm':
attr.mq_maxmsg = atoi(optarg);
attr.mq_msgsize = atoi(optarg);
break;
case 'z':
attr.mq_msgsize = atoi(optarg);
break;
}
}
if(optind != argc -1)
{fprintf(stdout," usage;\n");exit(1);}
if((attr.mq_maxmsg!=0&&attr.mq_msgsize == 0)||(attr.mq_maxmsg == 0 &&attr.mq_msgsize != 0))
{fprintf(stdout,"msgsize/maxmsg\n");exit(2);}
mqd = mq_open(argv[optind],flags,FILE_MODE,(attr.mq_maxmsg!=0)?(&attr):NULL);
if(-1==mqd)
{fprintf(stdout,"mq_open() error :%s \n",strerror(errno));exit(1);}
mq_close(mqd);
exit(0);
}
/*gcc THIS_FILE -lrt -o mq_creat*/
[jim@jim c]$ ./mq_creat -e -m 1024 -z 1024 /jim_mq
mq_open() error :Invalid argument
Invalid argument
[jim@jim c]$ ./mq_creat -e -m 1024 -z 1024 /mq
mq_open() error :Invalid argument
Invalid argument
//看来哪里有问题
man mq_overview
[jim@jim mqueue]$ pwd
/proc/sys/fs/mqueue
[jim@jim mqueue]$ cat msg_max
10
[jim@jim mqueue]$ cat msgsize_max
8192
[jim@jim mqueue]$ cat queues_max
256
[jim@jim c]$ ./mq_creat -e -m 6 -z 1024 /mq
[jim@jim c]$ echo $?
0 // seems OK now
2 mq_getattr()
#include
#include
#include /* For O_* constants */
#include /* For mode constants */
#include
struct mq_attr attr;
extern char *optarg;
extern int optind, opterr, optopt;
extern char * optarg;
extern int optind;
int main(int argc,char**argv)
{
mqd_t mqd;
struct mq_attr attr;
if(2!=argc)
{fprintf(stdout,"usage:%s ",argv[0]);}
mqd = mq_open(argv[1],O_RDONLY);
if(-1==mqd)
{fprintf(stdout,"mq_open() error \n");exit(1);}
mq_getattr(mqd,&attr);
fprintf(stdout,"max msgs = %ld,max #bytes/msg = %ld,#currently on queue = %ld\n",attr.mq_maxmsg,attr.mq_msgsize,attr.mq_curmsgs);/*fprintf(),vs printf()*/
mq_close(mqd);
exit(0);
}
/*gcc THIS_FILE -lrt -o mq_getattr*/
[jim@jim c]$ ./mq_getattr /mq
max msgs = 6,max #bytes/msg = 1024,#currently on queue = 0
3 mq_send()
#include
#include
#include /* For O_* constants */
#include /* For mode constants */
#include
#include
void perror(const char *s);
#include
extern int errno;
struct mq_attr attr;
extern char *optarg;
extern int optind, opterr, optopt;
#define FILE_MODE (S_IRUSR|S_IWUSR)
int main(int argc,char **argv)
{
int c,flags;
mqd_t mqd ;
flags = O_RDWR | O_CREAT;
while((c = getopt(argc,argv,"em:z:"))!=-1){
switch(c)
{
case 'e':
flags |= O_EXCL;
break;
case 'm':
attr.mq_maxmsg = atoi(optarg);
attr.mq_msgsize = atoi(optarg);
break;
case 'z':
attr.mq_msgsize = atoi(optarg);
break;
}
}
if(optind != argc -1)
{fprintf(stdout," usage;\n");exit(1);}
if((attr.mq_maxmsg!=0&&attr.mq_msgsize == 0)||(attr.mq_maxmsg == 0 &&attr.mq_msgsize != 0))
{fprintf(stdout,"msgsize/maxmsg\n");exit(2);}
mqd = mq_open(argv[optind],flags,FILE_MODE,(attr.mq_maxmsg!=0)?(&attr):NULL);
if(-1==mqd)
{fprintf(stdout,"mq_open() error :%s \n",strerror(errno));exit(1);}
mq_close(mqd);
exit(0);
}
/*gcc THIS_FILE -lrt -o mq_send*/
阅读(973) | 评论(0) | 转发(0) |