Chinaunix首页 | 论坛 | 博客
  • 博客访问: 461167
  • 博文数量: 293
  • 博客积分: 4204
  • 博客等级: 上校
  • 技术积分: 3060
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-21 10:27
个人简介

nobody

文章分类

全部博文(293)

文章存档

2014年(27)

2013年(105)

2012年(41)

2011年(109)

2010年(11)

分类:

2011-02-28 11:22:22

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*/

阅读(976) | 评论(0) | 转发(0) |
0

上一篇:some tips

下一篇:some mac(转载)

给主人留下些什么吧!~~