linux消息队列的收发
#include<stdio.h>#include<sys/types.h>#include<sys/msg.h>#include<sys/ipc.h>intmain()...
#include <stdio.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
int main()
{
int msqid;
char mrecv;
key_t msgkey;
struct msgbuf {
long mtype;
char mdata[256];
};
struct msgbuf msgdata , *p ;
p = &msgdata ;
printf("Enter message :" );
fflush(stdin); /* 刷新标准输入缓冲区 */
gets(p->mdata ); /* 输入字符串 */
p->mtype = getpid();
msgkey = ftok ( mrecv , 'a' ); /* 计算标识符 */
msqid = msgget( msgkey , IPC_CREAT | 0666 ) ; /* 建立消息队列 */
msgsnd( msqid , p , sizeof(p->mdata) , 0 ); /* 发送消息 */
while(1) {
msgrcv( msqid , p , sizeof(p->mdata) , 0 , 0 ) ; /* 接收消息 */
printf("Message received from %ld\n%s\n" , p->mtype , p->mdata );
}
return 0;
}
运行之后是Enter message :hello
Message received from 26203
hello
那这句话是什么意思Message received from 26203 展开
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
int main()
{
int msqid;
char mrecv;
key_t msgkey;
struct msgbuf {
long mtype;
char mdata[256];
};
struct msgbuf msgdata , *p ;
p = &msgdata ;
printf("Enter message :" );
fflush(stdin); /* 刷新标准输入缓冲区 */
gets(p->mdata ); /* 输入字符串 */
p->mtype = getpid();
msgkey = ftok ( mrecv , 'a' ); /* 计算标识符 */
msqid = msgget( msgkey , IPC_CREAT | 0666 ) ; /* 建立消息队列 */
msgsnd( msqid , p , sizeof(p->mdata) , 0 ); /* 发送消息 */
while(1) {
msgrcv( msqid , p , sizeof(p->mdata) , 0 , 0 ) ; /* 接收消息 */
printf("Message received from %ld\n%s\n" , p->mtype , p->mdata );
}
return 0;
}
运行之后是Enter message :hello
Message received from 26203
hello
那这句话是什么意思Message received from 26203 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询