消息对列的操作
来源:互联网 发布:linux 格式化硬盘 编辑:程序博客网 时间:2024/05/22 19:27
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
struct msgbuf
{
int type;
char ptr[0];
};
int main(int argc,char *argv[])
{
key_t key;
key=ftok(argv[1],100);
int msgid;
msgid=msgget(key,IPC_CREAT|0600);
pid_t pid;
pid=fork();
if(pid==0) //send
{
while(1)
{
printf("pls input msg to send:");
char buf[128];
fgets(buf,128,stdin);
struct msgbuf *ptr=malloc(sizeof(struct msgbuf)+strlen(buf)+1);
ptr->type=2; //send msg type=2
memcpy(ptr->ptr,buf,strlen(buf)+1);
msgsnd(msgid,ptr,strlen(buf)+1,0);
free(ptr);
}
}
else
{
struct msgbuf{
int type;
char ptr[1024];
};
while(1)
{
struct msgbuf mybuf;
memset(&mybuf,'\0',sizeof(mybuf));
msgrcv(msgid,&mybuf,1024,1,0); //recv msg type=2
printf("recv msg:%s\n",mybuf.ptr);
}
}
}
////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
struct msgbuf
{
int type;
char ptr[0];
};
int main(int argc,char *argv[])
{
key_t key;
key = ftok(argv[1],100);
int msgid;
msgid = msgget(key, IPC_CREAT|0600);
pid_t pid;
pid=fork();
if(pid==0)
{
while(1)
{
printf("pls input msg to send:");
char buf[128];
fgets(buf,128,stdin);
struct msgbuf *ptr = malloc(sizeof(struct msgbuf)+strlen(buf)+1);
ptr->type=1;
memcpy(ptr->ptr, buf, strlen(buf)+1);
msgsnd(msgid,ptr,strlen(buf)+1,0);
free(ptr);
}
}
else
{
struct msgbuf
{
int type;
char ptr[1024];
};
while(1)
{
struct msgbuf mybuf;
memset(&mybuf,'\0',sizeof(mybuf));
msgrcv(msgid,&mybuf,1024,2,0);
printf("recv msg:%s\n",mybuf.ptr);
}
}
}
////////////////////////////////////////////////////////////////////////////////
两进程通过消息队列收发消息
(1)发送消息队列程序
msgsnd.c源代码如下:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <time.h>
#define TEXT_SIZE 512
struct msgbuf
{
long mtype ;
int status ;
char time[20] ;
char mtext[TEXT_SIZE] ;
};
char *getxtsj()
{
time_t tv ;
struct tm *tmp ;
static char buf[20] ;
tv = time( 0 ) ;
tmp = localtime(&tv) ;
sprintf(buf,"%02d:%02d:%02d",tmp->tm_hour , tmp->tm_min,tmp->tm_sec);
return buf ;
}
int main(int argc, char **argv)
{
int msqid ;
struct msqid_ds info ;
struct msgbuf buf ;
struct msgbuf buf1 ;
int flag ;
int sendlength, recvlength ;
int key ;
key = ftok("msg.tmp", 0x01 ) ;
if ( key < 0 )
{
perror("ftok key error") ;
return -1 ;
}
msqid = msgget( key, 0600|IPC_CREAT ) ;
if ( msqid < 0 )
{
perror("create message queue error") ;
return -1 ;
}
buf.mtype = 1 ;
buf.status = 9 ;
strcpy(buf.time, getxtsj()) ;
strcpy(buf.mtext, "happy new year!") ;
sendlength = sizeof(struct msgbuf) - sizeof(long) ;
flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
if ( flag < 0 )
{
perror("send message error") ;
return -1 ;
}
buf.mtype = 3 ;
buf.status = 9 ;
strcpy(buf.time, getxtsj()) ;
strcpy(buf.mtext, "good bye!") ;
sendlength = sizeof(struct msgbuf) - sizeof(long) ;
flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
if ( flag < 0 )
{
perror("send message error") ;
return -1 ;
}
system("ipcs -q") ;
return 0 ;
}
(2)接收消息队列程序
msgrcv.c源代码如下:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define TEXT_SIZE 512
struct msgbuf
{
long mtype ;
int status ;
char time[20] ;
char mtext[TEXT_SIZE] ;
} ;
int main(int argc, char **argv)
{
int msqid ;
struct msqid_ds info ;
struct msgbuf buf1 ;
int flag ;
int recvlength ;
int key ;
int mtype ;
key = ftok("msg.tmp", 0x01 ) ;
if ( key < 0 )
{
perror("ftok key error") ;
return -1 ;
}
msqid = msgget( key, 0 ) ;
if ( msqid < 0 )
{
perror("get ipc_id error") ;
return -1 ;
}
recvlength = sizeof(struct msgbuf) - sizeof(long) ;
memset(&buf1, 0x00, sizeof(struct msgbuf)) ;
mtype = 1 ;
flag = msgrcv( msqid, &buf1, recvlength ,mtype,0 ) ;
if ( flag < 0 )
{
perror("recv message error\n") ;
return -1 ;
}
printf("type=%d,time=%s, message=%s\n", buf1.mtype, buf1.time, buf1.mtext) ;
system("ipcs -q") ;
return 0 ;
}
- 消息对列的操作
- ActiveMQ的消息对列的使用
- 对mysql列的操作
- NetMQ 消息对列
- oracle 对表的操作(列)
- PL/SQL对列的操作
- sql 对列 的常见操作
- oracle_____对列的常用操作
- 信号量 消息对列 共享内存的删除办法
- oracle 性能优化操作一:避免对列的操作
- oracle 性能优化操作一:避免对列的操作
- 对表的某一列进行操作的SQL命令
- 用php操作消息隊列,RabbitMQ消息队列在PHP下的应用大全
- 对GridView模板列内控件的操作代码
- 对GridView模板列内控件的操作代码
- 对GridView模板列内控件的操作代码
- 数据库表查询-对查询的列进行操作
- Sql Server 使用脚本对表的列进行操作
- spark 自带的例子在eclipse下运行的方法
- AndroidStudio 快速集成fastjson和注解
- 2016"百度之星" - 测试赛 1002 列变位法解密 (模拟)
- 取数游戏 贪心
- 水波纹效果
- 消息对列的操作
- C++新特性
- 增强学习Reinforcement Learning经典算法梳理1:policy and value iteration
- LintCode:最长上升子序列
- 【OpenCV】CvvImage类显示图像到控件
- 【C语言】反序数
- 老罗的Android系统分析(三横三纵)
- java hessian
- Contiki教程——进程