Linux消息队列编程
2007-03-16 12:41
375 查看
文件msg为空文件,可以为任何内容,这里只是为了ftok函数使用。程序通过建立消息队列,完成进程间通信,注意msgrcv的第四个参数为消息类型,他定义了从队列中取消息的类型。
下面是msgLucy.c,是建立消息队列的
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
#include<fcntl.h>
#include<signal.h>
#include<stdlib.h>
#include<string.h>
#define PROJID 0xFF
#define LUCY 1
#define PETER 2
int mqid;
void terminate_handler(int signo)
{
msgctl(mqid,IPC_RMID,NULL);
exit(0);
}
int main()
{
char filenm[] = "msg";
key_t mqkey;
struct msgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[256]; /* message data */
}msg;
int ret;
mqkey = ftok(filenm,PROJID);
if(mqkey == -1)
{
perror("ftok error: ");
exit(-1);
}
mqid = msgget(mqkey,IPC_CREAT | IPC_EXCL | 0666);
if(mqid == -1)
{
perror("msgget error: ");
exit(-1);
}
signal(SIGINT, terminate_handler);
signal(SIGTERM, terminate_handler);
while(1)
{
printf("Lucy: ");
fgets(msg.mtext, 256, stdin);
if (strncmp("quit", msg.mtext, 4) == 0)
{
msgctl(mqid,IPC_RMID,NULL);
exit(0);
}
msg.mtext[strlen(msg.mtext)-1] = '/0';
msg.mtype = LUCY;
msgsnd(mqid,&msg,strlen(msg.mtext) + 1,0);
msgrcv(mqid,&msg,256,PETER,0);
printf("Peter: %s/n", msg.mtext);
}
}
下面的是msgPeter,是和Lucy通信的,程序先运行Lucy,再运行Peter
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
#include<fcntl.h>
#include<signal.h>
#include<string.h>
#include<stdlib.h>
#define PROJID 0xFF
#define LUCY 1
#define PETER 2
int main()
{
char filenm[] = "msg";
int mqid;
key_t mqkey;
struct msgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[256]; /* message data */
}msg;
int ret;
mqkey = ftok(filenm, PROJID);
if(mqkey == -1)
{
perror("ftok error: ");
exit(-1);
}
mqid = msgget(mqkey, 0);
if(mqid == -1)
{
perror("msgget error: ");
exit(-1);
}
while(1)
{
msgrcv(mqid,&msg,256,LUCY,0);
printf("Lucy: %s/n",msg.mtext);
printf("Peter: ");
fgets(msg.mtext,256,stdin);
if(strncmp("quit", msg.mtext, 4) == 0)
{
exit(0);
}
msg.mtext[strlen(msg.mtext)-1] = '/0';
msg.mtype = PETER;
msgsnd(mqid,&msg,strlen(msg.mtext) + 1,0);
}
}
下面是msgLucy.c,是建立消息队列的
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
#include<fcntl.h>
#include<signal.h>
#include<stdlib.h>
#include<string.h>
#define PROJID 0xFF
#define LUCY 1
#define PETER 2
int mqid;
void terminate_handler(int signo)
{
msgctl(mqid,IPC_RMID,NULL);
exit(0);
}
int main()
{
char filenm[] = "msg";
key_t mqkey;
struct msgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[256]; /* message data */
}msg;
int ret;
mqkey = ftok(filenm,PROJID);
if(mqkey == -1)
{
perror("ftok error: ");
exit(-1);
}
mqid = msgget(mqkey,IPC_CREAT | IPC_EXCL | 0666);
if(mqid == -1)
{
perror("msgget error: ");
exit(-1);
}
signal(SIGINT, terminate_handler);
signal(SIGTERM, terminate_handler);
while(1)
{
printf("Lucy: ");
fgets(msg.mtext, 256, stdin);
if (strncmp("quit", msg.mtext, 4) == 0)
{
msgctl(mqid,IPC_RMID,NULL);
exit(0);
}
msg.mtext[strlen(msg.mtext)-1] = '/0';
msg.mtype = LUCY;
msgsnd(mqid,&msg,strlen(msg.mtext) + 1,0);
msgrcv(mqid,&msg,256,PETER,0);
printf("Peter: %s/n", msg.mtext);
}
}
下面的是msgPeter,是和Lucy通信的,程序先运行Lucy,再运行Peter
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
#include<fcntl.h>
#include<signal.h>
#include<string.h>
#include<stdlib.h>
#define PROJID 0xFF
#define LUCY 1
#define PETER 2
int main()
{
char filenm[] = "msg";
int mqid;
key_t mqkey;
struct msgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[256]; /* message data */
}msg;
int ret;
mqkey = ftok(filenm, PROJID);
if(mqkey == -1)
{
perror("ftok error: ");
exit(-1);
}
mqid = msgget(mqkey, 0);
if(mqid == -1)
{
perror("msgget error: ");
exit(-1);
}
while(1)
{
msgrcv(mqid,&msg,256,LUCY,0);
printf("Lucy: %s/n",msg.mtext);
printf("Peter: ");
fgets(msg.mtext,256,stdin);
if(strncmp("quit", msg.mtext, 4) == 0)
{
exit(0);
}
msg.mtext[strlen(msg.mtext)-1] = '/0';
msg.mtype = PETER;
msgsnd(mqid,&msg,strlen(msg.mtext) + 1,0);
}
}
相关文章推荐
- linux基础编程 共享内存 通过消息队列实现同步 shmget
- UNIX/LINUX编程学习之进程通信--消息队列
- 【学习笔记】天嵌2440第三季下学期——linux消息队列编程
- linux 下消息队列编程
- 基于Linux的消息队列及多线程编程实现的聊天室(二)代码分析
- [linux系统编程]System V IPC 消息队列
- Linux进程间通信(IPC)编程实践(十二)Posix消息队列--基本API的使用
- linux消息队列编程实例
- Linux消息队列编程示例
- 【网络编程基础】Linux下进程通信方式(共享内存,管道,消息队列,Socket)
- linux网络编程之POSIX消息队列
- 基于Linux的消息队列及多线程编程实现的聊天室(一)
- Linux编程之自定义消息队列
- linux消息队列编程实例
- Linux编程实践----消息队列的演示(IPC)
- linux基础编程:进程通信之System V IPC:消息队列,信号量,共享内存
- Linux编程之自定义消息队列
- linux网络编程之System V 消息队列:消息队列实现回射客户/服务器和 msgsnd、msgrcv 函数
- linux基础编程 消息队列 msgsnd
- Linux编程学习之Posix消息队列