使用消息队列完成两个进程之间相互通信(多进程)
#include<myhead.h>
//定义结构体
struct buf
{
long mtype;
char mtest[1024];
};
#define SIZE (sizeof(struct buf)-sizeof(long))
//进程
int main(int argc, const char *argv[])
{
//创建key
key_t key1 = ftok("/",'k');
if(key1 == -1)
{
perror("ftok");
return -1;
}
//创建消息队列
int msgid1 = msgget(key1,IPC_CREAT|0664);
if(msgid1 == -1)
{
perror("msgid1");
return -1;
}
//定义结构体
struct buf mbuf;
//创建子进程
pid_t pid = fork();
if(pid == 0)
{
//子进程1发送
while(1)
{
printf("请输入类型:");
scanf("%ld",&mbuf.mtype);
printf("请输入内容:");
scanf("%s",mbuf.mtest);
msgsnd(msgid1,&mbuf,SIZE,0);
if(strcmp(mbuf.mtest,"quit") == 0)
{
break;
}
}
exit(EXIT_SUCCESS);
}
else if(pid > 0)
{
//父进程2接受
while(1)
{
msgrcv(msgid1,&mbuf,SIZE,20,0);
printf("传输的数据为%s\n",mbuf.mtest);
if(strcmp(mbuf.mtest,"quit") == 0)
{
break;
}
}
waitpid(pid,NULL,0);
}
else
{
perror("fork");
return -1;
}
return 0;
}
#include<myhead.h>
//定义结构体
struct buf
{
long mtype;
char mtest[1024];
};
#define SIZE (sizeof(struct buf)-sizeof(long))
//进程
int main(int argc, const char *argv[])
{
//创建key
key_t key1 = ftok("/",'k');
if(key1 == -1)
{
perror("ftok");
return -1;
}
//创建消息队列
int msgid1 = msgget(key1,IPC_CREAT|0664);
if(msgid1 == -1)
{
perror("msgid1");
return -1;
}
//定义结构体
struct buf mbuf;
//创建子进程
pid_t pid = fork();
if(pid == 0)
{
//子进程1接受
while(1)
{
msgrcv(msgid1,&mbuf,SIZE,10,0);
printf("传输的数据为%s\n",mbuf.mtest);
if(strcmp(mbuf.mtest,"quit") == 0)
{
break;
}
}
exit(EXIT_SUCCESS);
}
else if(pid > 0)
{
//父进程2发送
while(1)
{
printf("请输入类型:");
scanf("%ld",&mbuf.mtype);
printf("请输入内容:");
scanf("%s",mbuf.mtest);
msgsnd(msgid1,&mbuf,SIZE,0);
if(strcmp(mbuf.mtest,"quit") == 0)
{
break;
}
}
waitpid(pid,NULL,0);
}
else
{
perror("fork");
return -1;
}
//删除消息队列
if(msgctl(msgid1,IPC_RMID,NULL)== -1)
{
perror("msgtrl");
return -1;
}
return 0;
}
效果图:
使用消息队列完成两个进程之间相互通信(多线程)
#include<myhead.h>
//定义结构体
struct buf
{
long mtype;
char mtest[1024];
};
#define SIZE (sizeof(struct buf)-sizeof(long))
//发送
void* func1(void* arg)
{
int msgid1 = *((int*)arg);
//定义结构体
struct buf mbuf;
while(1)
{
printf("请输入类型:");
scanf("%ld",&mbuf.mtype);
printf("请输入内容:");
scanf("%s",mbuf.mtest);
msgsnd(msgid1,&mbuf,SIZE,0);
if(strcmp(mbuf.mtest,"quit") == 0)
{
pthread_exit(NULL);
}
}
}
//接受
void* func2(void* arg)
{
int msgid2 = *((int*)arg);
//定义结构体
struct buf mbuf;
while(1)
{
msgrcv(msgid2,&mbuf,SIZE,40,0);
printf("传输的数据为%s\n",mbuf.mtest);
if(strcmp(mbuf.mtest,"quit") == 0)
{
pthread_exit(NULL);
}
}
}
int main(int argc, const char *argv[])
{
//创建key
key_t key1 = ftok("/",'k');
if(key1 == -1)
{
perror("ftok");
return -1;
}
//创建消息队列
int msgid1 = msgget(key1,IPC_CREAT|0664);
if(msgid1 == -1)
{
perror("msgid1");
return -1;
}
//创建两个线程
pthread_t tid1;
pthread_t tid2;
if(pthread_create(&tid1,NULL,func1,&msgid1) != 0)//msgid1
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,func2,&msgid1) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
#include<myhead.h>
//定义结构体
struct buf
{
long mtype;
char mtest[1024];
};
#define SIZE (sizeof(struct buf)-sizeof(long))
//发送
void* func1(void* arg)
{
int msgid2 = *((int*)arg);
//定义结构体
struct buf mbuf;
while(1)
{
printf("请输入类型:");
scanf("%ld",&mbuf.mtype);
printf("请输入内容:");
scanf("%s",mbuf.mtest);
msgsnd(msgid2,&mbuf,SIZE,0);
if(strcmp(mbuf.mtest,"quit") == 0)
{
pthread_exit(NULL);
}
}
}
//接受
void* func2(void* arg)
{
int msgid1 = *((int*)arg);
//定义结构体
struct buf mbuf;
while(1)
{
msgrcv(msgid1,&mbuf,SIZE,30,0);
printf("传输的数据为%s\n",mbuf.mtest);
if(strcmp(mbuf.mtest,"quit") == 0)
{
pthread_exit(NULL);
}
}
}
int main(int argc, const char *argv[])
{
//创建key
key_t key1 = ftok("/",'k');
if(key1 == -1)
{
perror("ftok");
return -1;
}
//创建消息队列
int msgid1 = msgget(key1,IPC_CREAT|0664);
if(msgid1 == -1)
{
perror("msgid1");
return -1;
}
//创建两个线程
pthread_t tid1;
pthread_t tid2;
if(pthread_create(&tid1,NULL,func1,&msgid1) != 0)//msgid1
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid2,NULL,func2,&msgid1) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
if(msgctl(msgid1,IPC_RMID,NULL) == -1)
{
perror("msgctl");
return -1;
}
return 0;
}
效果图: