xcsnd.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>
struct msgbuf
{
long mtype; //数据类型/编号
char mtext[1024];
};
//线程函数
void *pthread_start(void *arg)
{
key_t key;
struct msgbuf data;
int n=100;
memset(&data, 0, sizeof(struct msgbuf));
// 获取key的值
key = ftok(".", 30);
//在内核创始消息队列,并返回消息队列的ID
int msgid = msgget(key, IPC_CREAT|0666);
if(msgid == -1)
{
printf("msgget fail\n");
return NULL;
}
//数据的编号,要保证收发端要一致
data.mtype = 30;
while (1)
{
memset(data.mtext, 0, sizeof(data.mtext));
//接受数据
2、使用线程与消息队列,实现两个进程间的互相通信(一个进程实现读写)。
最新推荐文章于 2024-07-01 22:00:14 发布
本文介绍了如何利用C语言通过线程和消息队列来实现两个进程之间的有效通信,其中一个进程负责读取,另一个进程负责写入。详细探讨了线程与消息队列在进程通信中的应用和实现方法。
摘要由CSDN通过智能技术生成