linux队列实例,Linux IPC实践(6) --System V消息队列(3)

消息队列综合案例

消息队列实现回射客户/服务器

j285MDdu9gaQBuJa.png

server进程接收时,指定msgtyp为0,从队首不断接收消息

server进程发送时, 将mtype指定为接收到的client进程的pid

client进程发送的时候, mtype指定为自己进程的pid

client进程接收时, 需要将msgtyp指定为自己进程的pid, 只接收消息类型为自己pid的消息;

// client/server进程接收/发送的数据结构

const int MSGMAX = 8192;

struct msgBuf

{

long mtype; //保存客户进程的pid(需要将pid强制转换成为long)

char mtext[MSGMAX]; //保存客户进程真实发送的数据

};

//server.cpp

void echoServer(int msgid)

{

struct msgBuf buf;

int nrcv;

while (true)

{

bzero(&buf, sizeof(buf));

if ((nrcv = msgrcv(msgid, &buf, sizeof(buf.mtext), 0, 0)) == -1)

err_exit("msgrcv error");

cout << "recv: " << buf.mtext;

if (msgsnd(msgid, &buf, strlen(buf.mtext), 0) == -1)

err_exit("msgsnd error");

}

}

int main()

{

key_t key = ftok("/tmp/echoSeed", 0x1234);

int msgid = msgget(key, IPC_CREAT|0666);

if (msgid == -1)

err_exit("msgget error");

echoServer(msgid);

}

//client.cpp

void echoServer(int msgid)

{

struct msgBuf buf;

int nrcv;

while (true)

{

bzero(&buf, sizeof(buf));

if ((nrcv = msgrcv(msgid, &buf, sizeof(buf.mtext), 0, 0)) == -1)

err_exit("msgrcv error");

cout << "recv: " << buf.mtext;

if (msgsnd(msgid, &buf, strlen(buf.mtext), 0) == -1)

err_exit("msgsnd error");

}

}

int main()

{

key_t key = ftok("/tmp/echoSeed", 0x1234);

int msgid = msgget(key, IPC_CREAT|0666);

if (msgid == -1)

err_exit("msgget error");

echoServer(msgid);

}

附-ftok用法

#include

#include

key_t ftok(const char *pathname, int proj_id);

描述信息:

The ftok() function uses the identity(象征) of the file named by the given pathname (which must refer

to an existing, accessible file[必须是一个已经存在,并且可访问的文件]) and the least significant(有效的) 8 bits[有效的最低8位] of proj_id (which must  be  nonzero)  to  generate  a  key_t  type  System V IPC key, suitable

for use with msgget(2), semget(2), or shmget(2).The resulting value is the same for all pathnames that name the same file, when the  same value  of  proj_id

is used(如果文件名与proj_id的有效位全都相同的话, 则生成的key一定也是相同的).  The value returned should be different when

the (simultaneously existing) files or the project IDs differ.

RETURN VALUEOn success, the generated key_t value is returned.  On failure -1 is returned,

with errno indicating the error as for the stat(2) system call.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux提供了多种消息队列的实现,其中比较常用的是System V消息队列和POSIX消息队列。 1. System V消息队列 System V消息队列提供了一个进程间通信的方式,它允许一个进程向消息队列中发送消息,同时其他进程可以从该消息队列中读取这些消息。使用System V消息队列需要使用以下函数: - msgget:创建或打开一个消息队列。 - msgsnd:向一个消息队列中发送消息。 - msgrcv:从一个消息队列中读取消息。 - msgctl:控制一个消息队列。 下面是一个示例程序,演示了如何使用System V消息队列进行进程间通信: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define MAX_TEXT 512 struct msgbuf { long mtype; char mtext[MAX_TEXT]; }; int main() { struct msgbuf buf; int msqid; key_t key; if ((key = ftok("msgq.txt", 'B')) == -1) { perror("ftok"); exit(1); } if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) { perror("msgget"); exit(1); } printf("Enter lines of text, ^D to quit:\n"); buf.mtype = 1; while(fgets(buf.mtext, MAX_TEXT, stdin) != NULL) { int len = strlen(buf.mtext); if (buf.mtext[len-1] == '\n') { buf.mtext[len-1] = '\0'; } if (msgsnd(msqid, &buf, len+1, 0) == -1) { perror("msgsnd"); } } if (msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(1); } return 0; } ``` 2. POSIX消息队列 POSIX消息队列Linux提供的另一种消息队列实现方式,与System V消息队列相比,POSIX消息队列更加灵活和易于使用。使用POSIX消息队列需要使用以下函数: - mq_open:创建或打开一个消息队列。 - mq_send:向一个消息队列中发送消息。 - mq_receive:从一个消息队列中读取消息。 - mq_unlink:删除一个消息队列。 下面是一个示例程序,演示了如何使用POSIX消息队列进行进程间通信: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <sys/stat.h> #include <mqueue.h> #define MAX_TEXT 512 int main() { mqd_t mqd; struct mq_attr attr; char buffer[MAX_TEXT]; int len; if ((mqd = mq_open("/test", O_CREAT | O_WRONLY, 0644, NULL)) == -1) { perror("mq_open"); exit(1); } printf("Enter lines of text, ^D to quit:\n"); while(fgets(buffer, MAX_TEXT, stdin) != NULL) { len = strlen(buffer); if (buffer[len-1] == '\n') { buffer[len-1] = '\0'; } if (mq_send(mqd, buffer, len+1, 0) == -1) { perror("mq_send"); } } if (mq_close(mqd) == -1) { perror("mq_close"); exit(1); } if (mq_unlink("/test") == -1) { perror("mq_unlink"); exit(1); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值