msgserver.c
来自「基于linux下的故障录波前置程序」· C语言 代码 · 共 88 行
C
88 行
#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include<signal.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<error.h>
//int MsgSnd ( MsgQid , MsgContent , MsgKey , MsgLen )
//int MsgQid ;
//char *MsgContent ;
//long MsgKey ;
//int MsgLen ;
//{
// struct
// {
// long pMsgKey ;
// char pMsgContent[ MAX_QUE_PACKSIZE ] ;
// } Message ;
//
// int SndLen ;
//
// Message.pMsgKey = MsgKey ;
// memset ( Message.pMsgContent , NULL , MAX_QUE_PACKSIZE ) ;
// memcpy ( Message.pMsgContent , MsgContent , MsgLen ) ;
//
// SndLen = msgsnd ( (int)MsgQid , (const void *)&Message , (unsigned long)MsgLen , (int)IPC_NOWAIT ) ;
//
// if ( SndLen < 0 )
// return ERROR ;
// else
// return SUCCESS ;
//}
typedef struct _Msg
{
long type;
char buffer[1024];
}Msg;
int main()
{
key_t key;
int msgid;
Msg msg;
int RcvLen;
struct msqid_ds aa;
int i;
key = ftok("/root/aa",'Y');
msgid = msgget(key,IPC_CREAT|0666);
msgctl(msgid,IPC_RMID,0);
msgid = msgget(key,IPC_CREAT|0666);
if(msgid<0 )
{
perror("open msg error:");
return -1;
}
msg.type = 1;
strcpy(msg.buffer,"yangyi love huangyuanyuan");
//while(1)
for(i=0;i<10;i++)
{
RcvLen = msgsnd( (int)msgid , (void *)&msg , strlen(msg.buffer) , IPC_NOWAIT ) ;
printf("%d\n",RcvLen);
if ( RcvLen < 0 && errno == EAGAIN)
{
msgctl(msgid,IPC_STAT,&aa);
printf("%d!\n",aa.msg_qnum);
// continue ;
}
else
{
msgctl(msgid,IPC_STAT,&aa);
printf("%d!\n",aa.msg_qnum);
}
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?