使用消息队列完成两个进程之间相互通信
代码:
/*******************************************/
文件名:snd.c
/*******************************************/
#include <myhead.h>
sem_t sem;
//要发送的消息类型
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define SIZE sizeof(struct msgbuf) - sizeof(long)
void *task1(void *arg)
{
//创建key值,用于生产信息队列
key_t key = ftok("/", 'k');
if (key == -1)
{
perror("ftok error");
return NULL;
}
//创建一个消息队列
int msqid = msgget(key, IPC_EXCL | 0664);
if (msqid == -1)
{
perror("msgget error");
return NULL;
}
//向消息队列中存放消息
struct msgbuf buf2;
while (1)
{
sem_wait(&sem);
buf2.mtype=1;
printf("请输入消息正文:");
fgets(buf2.mtext, SIZE, stdin); //从终端获取数据
buf2.mtext[strlen(buf2.mtext) - 1] = 0; //将换行换成 '\0'
//将消息发送到消息队列中
msgsnd(msqid, &buf2, SIZE, 0);
printf("发送成功\n");
if (strcmp(buf2.mtext, "quit") == 0)
{
break;
}
}
pthread_exit(NULL); //退出线程
}
void *task2(void *arg)
{
//创建key值,用于生产消息队列
key_t key2 = ftok("/", 't');
if (key2 == -1)
{
perror("ftok error");
return NULL;
}
printf("key = %#x\n", key2);
//通过key值创建一个消息队列
int msqid = msgget(key2, IPC_CREAT | 0664);
if (msqid == -1)
{
perror("msgget error");
return NULL;
}
printf("msqid = %d\n", msqid); //id号
//从消息队列中读取消息
struct msgbuf buf;
while (1)
{
msgrcv(msqid, &buf, SIZE, 0, 0);
if (strcmp(buf.mtext, "quit") == 0)
{
sem_post(&sem);
break;
}
printf("收到消息为:%s\n", buf.mtext);
sem_post(&sem);
}
pthread_exit(NULL); //退出线程
}
int main(int argc, char const *argv[])
{
//初始化无名信号量
sem_init(&sem, 0, 1);
//定义变量存储线程号
pthread_t tido = -1;
if (pthread_create(&tido, NULL, task1, NULL) != 0)
{
printf("pthread_create error\n");
return -1;
}
//定义变量存储线程号
pthread_t tido2 = -1;
if (pthread_create(&tido2, NULL, task2, NULL) != 0)
{
printf("pthread_create error\n");
return -1;
}
//阻塞回收线程的资源
pthread_join(tido, NULL);
pthread_join(tido2, NULL);
//销毁无名信号量
sem_destroy(&sem);
return 0;
}
/*******************************************/
文件名:rvs.c
/*******************************************/
#include <myhead.h>
sem_t sem;
//要发送的消息类型
struct msgbuf
{
long mtype;
char mtext[1024];
};
#define SIZE sizeof(struct msgbuf) - sizeof(long)
void *task2(void *arg)
{
//创建key值,用于生产信息队列
key_t key2 = ftok("/", 't');
if (key2 == -1)
{
perror("ftok error");
return NULL;
}
//创建一个消息队列
int msqid2 = msgget(key2, IPC_EXCL | 0664);
if (msqid2 == -1)
{
perror("msgget error");
return NULL;
}
//向消息队列中存放消息
struct msgbuf buf;
while (1)
{
sem_wait(&sem);
buf.mtype=2;
printf("请输入消息正文:");
fgets(buf.mtext, SIZE, stdin); //从终端获取数据
buf.mtext[strlen(buf.mtext) - 1] = 0; //将换行换成 '\0'
//将消息发送到消息队列中
msgsnd(msqid2, &buf, SIZE, 0);
printf("发送成功\n");
usleep(10);
if (strcmp(buf.mtext, "quit") == 0)
{
break;
}
}
pthread_exit(NULL); //退出线程
}
void *task1(void *arg)
{
//创建key值,用于生产消息队列
key_t key = ftok("/", 'k');
if (key == -1)
{
perror("ftok error");
return NULL;
}
printf("key = %#x\n", key);
//通过key值创建一个消息队列
int msqid = msgget(key, IPC_CREAT | 0664);
if (msqid == -1)
{
perror("msgget error");
return NULL;
}
printf("msqid = %d\n", msqid); //id号
//从消息队列中读取消息
struct msgbuf buf2;
while (1)
{
usleep(10);
msgrcv(msqid, &buf2, SIZE, 0, 0);
if(buf2.mtype==2){
continue;
}
if (strcmp(buf2.mtext, "quit") == 0)
{
sem_post(&sem);
break;
}
printf("收到消息为:%s\n", buf2.mtext);
sem_post(&sem);
}
pthread_exit(NULL); //退出线程
}
int main(int argc, char const *argv[])
{
//初始化无名信号量
sem_init(&sem, 0, 0);
//定义变量存储线程号
pthread_t tido = -1;
if (pthread_create(&tido, NULL, task1, NULL) != 0)
{
printf("pthread_create error\n");
return -1;
}
//定义变量存储线程号
pthread_t tido2 = -1;
if (pthread_create(&tido2, NULL, task2, NULL) != 0)
{
printf("pthread_create error\n");
return -1;
}
//阻塞回收线程的资源
pthread_join(tido, NULL);
pthread_join(tido2, NULL);
//销毁无名信号量
sem_destroy(&sem);
return 0;
}