linux查看传输数据队列,linux程序通过消息队列传输数据

发送端

#include

#include

#include

#include

#define SYS_MSG_KEY_VALUE 1002 // 消息队列键值

struct msgstru{

long mtype; //大于0

char mtext[512];

};

struct msgstru msg;

int qid = -1;

Widget::Widget(QWidget *parent) :

QWidget(parent),

ui(new Ui::Widget)

{

ui->setupUi(this);

}

Widget::~Widget()

{

delete ui;

}

void Widget::on_pushButton_clicked()

{

msg.mtype = 1;

char *c="aaaa";

memcpy(msg.mtext,c,strlen(c));

ssize_t ret_value = msgsnd(qid,&msg,sizeof(struct msgstru),IPC_NOWAIT);

if(ret_value < 0)

QMessageBox::warning(NULL,"aaaa","aaaa",QMessageBox::Yes);

}

void Widget::on_pushButton_2_clicked()

{

msgctl(qid,IPC_RMID,0);

}

void Widget::on_pushButton_3_clicked()

{

qid = msgget(SYS_MSG_KEY_VALUE,IPC_CREAT|0666);

if(qid <0 || qid ==65536)

QMessageBox::warning(NULL,"aaaa","aaaa",QMessageBox::Yes);

}

接受端

#include

#include

#include

#define SYS_MSG_KEY_VALUE 1002 // 消息队列键值

struct msgstru{

long mtype; //大于0

char mtext[512];

};

struct msgstru msg;

Widget::Widget(QWidget *parent) :

QWidget(parent),

ui(new Ui::Widget)

{

ui->setupUi(this);

}

Widget::~Widget()

{

delete ui;

}

void Widget::on_pushButton_clicked()

{

int qid = msgget(SYS_MSG_KEY_VALUE,IPC_EXCL);

if(qid < 0 || qid == 65536)

return;

while(1)

{

sleep(2);

memset(msg.mtext,0,512);

ssize_t size = msgrcv(qid,&msg,sizeof(struct msgstru),0,0);

if(size > 0)

{

printf("%s\n",msg.mtext);

fflush(stdout);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 消息队列是一种进程间通信方式,可以实现不同进程之间的数据传输。下面是消息队列的发送和接收程序的编写实例: 发送程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/msg.h> struct message { long mtype; char mtext[256]; }; int main() { int msqid; key_t key; struct message msg; // 创建消息队列 key = ftok(".", 'a'); msqid = msgget(key, 0666 | IPC_CREAT); if (msqid == -1) { perror("msgget"); exit(1); } // 设置消息类型 msg.mtype = 1; // 输入消息内容 printf("Enter message: "); fgets(msg.mtext, sizeof(msg.mtext), stdin); // 发送消息 if (msgsnd(msqid, &msg, sizeof(msg.mtext), 0) == -1) { perror("msgsnd"); exit(1); } printf("Message sent: %s\n", msg.mtext); return 0; } ``` 接收程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/msg.h> struct message { long mtype; char mtext[256]; }; int main() { int msqid; key_t key; struct message msg; // 打开消息队列 key = ftok(".", 'a'); msqid = msgget(key, 0666 | IPC_CREAT); if (msqid == -1) { perror("msgget"); exit(1); } // 接收消息 if (msgrcv(msqid, &msg, sizeof(msg.mtext), 1, 0) == -1) { perror("msgrcv"); exit(1); } printf("Message received: %s\n", msg.mtext); // 关闭消息队列 if (msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(1); } return 0; } ``` 以上代码中,发送程序先创建一个消息队列,并设置消息类型为1。然后输入消息内容,并通过 msgsnd 函数发送消息到消息队列中。 接收程序打开同样的消息队列,并通过 msgrcv 函数接收类型为1的消息。接收完消息后,通过 msgctl 函数关闭消息队列。 注意:以上代码中的错误处理部分并不完整,仅供参考。实际应用中应该根据具体情况进行完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值