注意编译时需要加-lpthread
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define msgkey 1234
pthread_t tid1, tid2; //定义两个线程全局变量
struct msgbuf { //通信所要使用的结构体
long mtype; /* message type, must be > 0 */
char mtext[100]; /* message data */
};
void *sendpthread(void *arg) //功能为发送信息的线程
{
int ret;
int msqid = *(int *)arg; //传过来的信号队列需要进行格式转换
struct msgbuf msg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old); //定义取消属性
while(1) //利用信号量进行发送的循环
{
memset(&msg, 0, sizeof(msg));
msg.mtype = 2;
scanf("%s", msg.mtext);
ret = msgsnd(msqid, &msg, sizeof(msg.mtext), 0);
if(ret == -1)
{
perror("msgsnd");
exit(1);
}
if(!strcmp(msg.mtext, "bye"))
{
pthread_cancel(tid2); //结束自身线程的同时结束接受线程
break;
}
memset(&msg, 0, sizeof(msg));
}
}
void *rcvpthread(void *arg) //功能为接受的线程
{
int ret;
int msqid = *(int *)arg;
struct msgbuf msg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while(1)
{
memset(&msg, 0, sizeof(msg));
ret = msgrcv(msqid, &msg, sizeof(msg.mtext), 1, 0);
if(ret == -1)
{
perror("msgrcv");
exit(1);
}
if(!strcmp(msg.mtext, "bye"))
{
pthread_cancel(tid1);
break;
}
printf("\t\t%s\n", msg.mtext); //打印接受到的信息
}
}
int main()
{
int ret;
struct msgbuf msg;
int msqid = msgget(msgkey, 0); //定义一个信号队列实现通讯
ret = pthread_create(&tid1, NULL, sendpthread, &msqid); //创建两个线程
if(ret != 0)
{
perror("pthread_create");
exit(1);
}
ret = pthread_create(&tid2, NULL, rcvpthread, &msqid);
if(ret != 0)
{
perror("pthread_create");
exit(1);
}
void *status; //回收线程
pthread_join(tid1, &status);
pthread_join(tid2, &status);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define msgkey 1234
pthread_t tid1, tid2;
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[100]; /* message data */
};
void *sendpthread(void *arg)
{
int ret;
int msqid = *(int *)arg;
struct msgbuf msg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while(1)
{
memset(&msg, 0, sizeof(msg));
msg.mtype = 2;
scanf("%s", msg.mtext);
ret = msgsnd(msqid, &msg, sizeof(msg.mtext), 0);
if(ret == -1)
{
perror("msgsnd");
exit(1);
}
if(!strcmp(msg.mtext, "bye"))
{
pthread_cancel(tid2);
break;
}
memset(&msg, 0, sizeof(msg));
}
}
void *rcvpthread(void *arg)
{
int ret;
int msqid = *(int *)arg;
struct msgbuf msg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while(1)
{
memset(&msg, 0, sizeof(msg));
ret = msgrcv(msqid, &msg, sizeof(msg.mtext), 1, 0);
if(ret == -1)
{
perror("msgrcv");
exit(1);
}
if(!strcmp(msg.mtext, "bye"))
{
pthread_cancel(tid1);
break;
}
printf("\t\t%s\n", msg.mtext);
}
}
int main()
{
int ret;
struct msgbuf msg;
int msqid = msgget(msgkey, 0);
ret = pthread_create(&tid1, NULL, sendpthread, &msqid);
if(ret != 0)
{
perror("pthread_create");
exit(1);
}
ret = pthread_create(&tid2, NULL, rcvpthread, &msqid);
if(ret != 0)
{
perror("pthread_create");
exit(1);
}
void *status;
pthread_join(tid1, &status);
pthread_join(tid2, &status);
return 0;
}
以上两文件运行后,可实现两个线程间的通讯。