《UNIX网络编程 卷2》读书笔记(二)

      如何知道进程在一个空消息队列中放入一个消息?如果阻塞在msgrcv调用中,则除了等待无法做其他事情,如果给msgrcv指定非阻塞标志(IPC_NOWAIT),尽管不阻塞了,但必须持续调用该函数来确定何时有消息到达,也就是采用轮询方式(polling),Posix消息队列允许异步事件通知来通知何时有消息放入到某个空消息队列中,有2种方式:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

1)产生一个信号

2)创建一个线程执行一个指定函数

这通过mq_notify建立。

None.gif union sigval
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
int sival_int;
InBlock.gif   
void *sival_ptr;
ExpandedBlockEnd.gif}
;
None.gif
None.gif
struct  sigevent
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
int sigev_notify;
InBlock.gif   
int sigev_signo;
InBlock.gif   union sigval sigev_value;
InBlock.gif   
void (*sigev_notify_function)(union sigval);
InBlock.gif   pthread_attr_t 
*sigev_notify_attributes;
ExpandedBlockEnd.gif}
;
None.gif

None.gif     
None.gif#include 
" unpipc.h "
None.gif
None.gif
volatile  sig_atomic_t mqflag;
None.gif
static   void  sig_usr1( int  signo)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    mqflag 
= 1;
InBlock.gif    
return;
ExpandedBlockEnd.gif}

None.gif
int  main( int  argc, char **  argv)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    mqd_t mqd;
InBlock.gif    
void * buff;
InBlock.gif    SSIZE_T n;
InBlock.gif    sigset_t zeromask,newmask,oldmask;
InBlock.gif    
struct mq_attr attr;
InBlock.gif    
struct sigevent sigev;
InBlock.gif    mqd 
= mq_open(argv[1],O_RDONLY|O_NONBLOCK);//打开消息队列
InBlock.gif
    mq_getattr(mqd,&attr);
InBlock.gif    buff 
= malloc(attr.mq_msgsize);//创建接收缓冲区
InBlock.gif
    sigemptyset(&zeromask);
InBlock.gif    sigemptyset(
&newmask);
InBlock.gif    sigemptyset(
&oldmask);
InBlock.gif    sigaddset(
&newmask,SIGUSR1);
InBlock.gif    
//初始化信号处理
InBlock.gif
    signal(SIGUSR1,sig_usr1);
InBlock.gif    sigev.sigev_notify 
= SIGEV_SIGNAL;
InBlock.gif    sigev.sigev_signo 
= SIGUSR1;
InBlock.gif    mq_notify(mqd,
&sigev);
InBlock.gif    
for(;;)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        sigprocmask(SIG_BLOCK,
&newmask,&oldmask);//阻塞SIGUSR1信号
InBlock.gif
        while(mqflag==0)sigsuspend(&zeromask);
InBlock.gif        mqflag 
= 0;
InBlock.gif        mq_notify(mqd,
&sigev);
InBlock.gif        
while((n=mq_receive(mqd,buff,attr.mq_msgsize,NULL))>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//循环接收数据
InBlock.gif
            printf("read %ld bytes\n",(long)n);
ExpandedSubBlockEnd.gif        }

InBlock.gif        sigprocmask(SIG_UNBLOCK,
&newmask,NULL);
ExpandedSubBlockEnd.gif    }

InBlock.gif    exit(
0);
InBlock.gif    
ExpandedBlockEnd.gif}

None.gif

None.gif struct  msqid_ds
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
struct ipc_perm msg_perm;
InBlock.gif  
struct msg* msg_first;
InBlock.gif  
struct msg* msg_last;
InBlock.gif  msgle_t msg_cbytes;
InBlock.gif  msgqnum_t msg_qnum;
InBlock.gif  msglen_t  msg_qbytes;
InBlock.gif  pid_t msg_lspid;
InBlock.gif  pid_t msg_lrpid;
InBlock.gif  time_t msg_stime;
InBlock.gif  time_t msg_rtime;
InBlock.gif  time_t msg_ctime;
ExpandedBlockEnd.gif}
;
None.gif

msgget创建一个新的消息队列或访问一个已经存在的消息队列

msgsnd发送一个消息,消息是下面这样的结构体:

None.gif struct  msgbuf
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
long mtype;//消息类型
InBlock.gif
 char mtext[1];//消息数据
ExpandedBlockEnd.gif
}
;
None.gif

 

但这个预定的结构一般无法满足自己的需求,因此一般定义自己的结构

None.gif typedef  struct  my_msgbuf
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
long mtype;
InBlock.gif  int16_t mshort;
//消息数据起始
InBlock.gif
  char mchar[MY_DATA];
ExpandedBlockEnd.gif}
Message;
None.gif

发送数据时可以指定flagIPC_NOWAIT,这个标志使得msgsnd调用非阻塞,

 

调用msgrcv函数时,若type指定为0,则返回消息队列第一个消息,若type小于0,则返回类型值小于或等于type绝对值的消息中类型值最小的

第一个消息

 

使用两个消息队列来实现前面的客户服务器例子,一个队列用于从客户方到服务器的消息,一个队列用于从服务器到客户的消息。

None.gif
None.gif#include 
" unpipc.h "
None.gif
#define  MQ_KEY1 1234L
None.gif
#define  MQ_KEY2 2345L
None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int readid,writeid;
InBlock.gif    readid 
= msgget(MQ_KEY1,SVMSG_MODE|IPC_CREATE);
InBlock.gif    writeid 
= msgget(MQ_KEY2,SVMSG_MODE|IPC_CREATE);
InBlock.gif    server(readid,writeid);
InBlock.gif    exit(
0);
ExpandedBlockEnd.gif}

None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int readid,writeid;
InBlock.gif    readid 
= msgget(MQ_KEY2,0);
InBlock.gif    writeid 
= msgget(MQ_KEY1,0);
InBlock.gif    client(readid,writeid);
InBlock.gif    
//删除消息队列
InBlock.gif
    msgctl(readid,IPC_RMID,NULL)
InBlock.gif    msgctl(writeid,IPC_RMID,NULL);
InBlock.gif    exit(
0);
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

8节使用一个队列来实现单服务器,多客户端的通信,但互斥,死锁可能存在,另一种思路就是为每个实体单独设置一个私有队列,消息都发送到自己的队列中,也只从自己的队列中取消息.

System V的消息队列比Posix消息队列差多了,要使用select模型同时处理网络连接和IPC连接时,必须产生子进程,使用管道作为中介,另外一个弱点是无法Peek一个消息.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值