。
多线程实现通信双方互发消息
一个程序本身可以看做是一个进程,因而只需在程序内建立两个线程,用来接收和发送消息即可,退出时,可以通过一个线程控制另一个线程结束
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#define MSGKEY 1234
pthread_t tid1, tid2;
struct msgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[64]; /* message data */
};
void *mythread(void *arg)
{
struct msgbuf mbuf;
int msgid = *(int *)arg;
int old;
int ret;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&old);
while(1)
{
memset(&mbuf, 0, sizeof(mbuf));
mbuf.mtype = 1;
scanf("%s",mbuf.mtext);
ret = msgsnd(msgid,&mbuf,sizeof(mbuf.mtext), 0);
if(ret == -1)
{
perror("msgsnd");
exit(1);
}
if(!strcmp(mbuf.mtext,"bye"))
{
pthread_cancel(tid2);
break;
}
}
}
void *mythread1(void *arg)
{
struct msgbuf mbuf;
int msgid = *(int *)arg;
int old;
int ret;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&old);
while(1)
{
ret = msgrcv(msgid,&mbuf,sizeof(mbuf.mtext), 2, 0);
if(ret == -1)
{
perror("msgsnd");
exit(1);
}
if(!strcmp(mbuf.mtext,"bye"))
{
pthread_cancel(tid1);
break;
}
printf("\t\t\t%s\n",mbuf.mtext);
}
}
int main()
{
int ret;
int msgid = msgget(MSGKEY,IPC_CREAT | IPC_EXCL);
if(msgid == -1)
{
perror("msgget");
exit(1);
}
ret = pthread_create(&tid1, NULL, mythread, &msgid);
if(ret != 0)
{
perror("pthread_create");
exit(1);
}
ret = pthread_create(&tid2, NULL, mythread1, &msgid);
if(ret != 0)
{
perror("pthread_create");
exit(1);
}
void *status;
ret = pthread_join(tid1,&status);
ret = pthread_join(tid2,&status);
sleep(1);
msgctl(msgid,IPC_RMID, NULL);
return 0;
}
另一端:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#define MSGKEY 1234
pthread_t tid1, tid2;
struct msgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[64]; /* message data */
};
void *mythread(void *arg)
{
struct msgbuf mbuf;
int msgid = *(int *)arg;
int old;
int ret;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&old);
while(1)
{
memset(&mbuf, 0, sizeof(mbuf));
mbuf.mtype = 2;
scanf("%s",mbuf.mtext);
ret = msgsnd(msgid,&mbuf,sizeof(mbuf.mtext), 0);
if(ret == -1)
{
perror("msgsnd");
exit(1);
}
if(!strcmp(mbuf.mtext,"bye"))
{
pthread_cancel(tid2);
break;
}
}
}
void *mythread1(void *arg)
{
struct msgbuf mbuf;
int msgid = *(int *)arg;
int old;
int ret;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&old);
while(1)
{
ret = msgrcv(msgid,&mbuf,sizeof(mbuf.mtext), 1, 0);
if(ret == -1)
{
perror("msgsnd");
exit(1);
}
if(!strcmp(mbuf.mtext,"bye"))
{
pthread_cancel(tid1);
break;
}
printf("\t\t\t%s\n",mbuf.mtext);
}
}
int main()
{
int ret;
int msgid = msgget(MSGKEY,0);
if(msgid == -1)
{
perror("msgget");
exit(1);
}
ret = pthread_create(&tid1, NULL, mythread, &msgid);
if(ret != 0)
{
perror("pthread_create");
exit(1);
}
ret = pthread_create(&tid2, NULL, mythread1, &msgid);
if(ret != 0)
{
perror("pthread_create");
exit(1);
}
void *status;
ret = pthread_join(tid1,&status);
ret = pthread_join(tid2,&status);
return 0;
}