消息队列实现AB进程间通话
使用不同的类型名区分消息来源
A进程实现
#include <stdio.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <string.h>
//消息结构体
struct msgbuf
{
long mtype;
char mtext[128];
};
int main(int argc, const char *argv[])
{
//创建key
key_t key = ftok("./",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
//创建消息队列
int msqid = msgget(key, IPC_CREAT | 0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n",msqid);
struct msgbuf sndbuf;
struct msgbuf rcvbuf;
while(1)
{
//A进程往队列中写
memset(&sndbuf,0,sizeof(sndbuf));
printf("A进程说>>>");
sndbuf.mtype = 100;
fgets(sndbuf.mtext, sizeof(sndbuf.mtext), stdin);
sndbuf.mtext[strlen(sndbuf.mtext)-1] = '\0';
if(msgsnd(msqid, &sndbuf, sizeof(sndbuf.mtext), IPC_NOWAIT) < 0)
{
perror("msgsnd");
break;
}
printf("消息发送成功\n");
if(0 == strcasecmp(sndbuf.mtext,"quit"))
{
break;
}
//A进程往队列中读
memset(&rcvbuf,0,sizeof(rcvbuf));
if(msgrcv(msqid, &rcvbuf, sizeof(rcvbuf.mtext), 101, 0) < 0)
{
perror("msgsnd");
break;
}
printf("收到B的消息: %s\n",rcvbuf.mtext);
if(0 == strcasecmp(rcvbuf.mtext,"quit"))
{
break;
}
}
if(msgctl(msqid, IPC_RMID, NULL) < 0)
{
perror("msgctl");
return -1;
}
return 0;
}
B进程实现
#include <stdio.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <string.h>
//消息结构体
struct msgbuf
{
long mtype;
char mtext[128];
};
int main(int argc, const char *argv[])
{
//创建key
key_t key = ftok("./",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
//创建消息队列
int msqid = msgget(key, IPC_CREAT | 0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n",msqid);
struct msgbuf sndbuf;
struct msgbuf rcvbuf;
while(1)
{
//B进程往队列中读
memset(&rcvbuf,0,sizeof(rcvbuf));
if(msgrcv(msqid, &rcvbuf, sizeof(rcvbuf.mtext), 100, 0) < 0)
{
perror("msgsnd");
break;
}
printf("收到A的消息: %s\n",rcvbuf.mtext);
if(0 == strcasecmp(rcvbuf.mtext,"quit"))
{
break;
}
//B进程往队列中写
memset(&sndbuf,0,sizeof(sndbuf));
printf("B进程说>>>");
sndbuf.mtype = 101;
fgets(sndbuf.mtext, sizeof(sndbuf.mtext), stdin);
sndbuf.mtext[strlen(sndbuf.mtext)-1] = '\0';
if(msgsnd(msqid, &sndbuf, sizeof(sndbuf.mtext), IPC_NOWAIT) < 0)
{
perror("msgsnd");
break;
}
printf("消息发送成功\n");
if(0 == strcasecmp(sndbuf.mtext,"quit"))
{
break;
}
}
if(msgctl(msqid, IPC_RMID, NULL) < 0)
{
perror("msgctl");
return -1;
}
return 0;
}
用共享内存的方式分别将数组打印和逆置
使用的是flag来切换功能
打印
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
char str[] ="123456";
//创建key值
key_t key = ftok("./",3);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#X\n", key);
//创建共享内存
int shmid = shmget(key, 32, IPC_CREAT|0777);
if(shmid < 0)
{
perror("shmget");
return -1;
}
printf("shmid = %d\n",shmid);
//映射到当前进程上
void* shmaddr = shmat(shmid, NULL, 0);
if((void*)-1 == shmaddr)
{
perror("shmat");
return -1;
}
printf("shmaddr = %p\n", shmaddr);
//初始化
int flag = 0;
char* pa = (char*)shmaddr+4;
*(int*)shmaddr = flag;
strcpy(pa,str);
//打印
while(1)
{
flag = *(int*)shmaddr;
if(0 == flag)
{
printf("%s\n",pa);
*(int*)shmaddr = 1;
}
sleep(1);
}
//断开映射
shmdt(shmaddr);
return 0;
}
逆置
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
//创建key值
key_t key = ftok("./",3);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#X\n", key);
//创建共享内存
int shmid = shmget(key, 32, IPC_CREAT|0777);
if(shmid < 0)
{
perror("shmget");
return -1;
}
printf("shmid = %d\n",shmid);
//映射到当前进程上
void* shmaddr = shmat(shmid, NULL, 0);
if((void*)-1 == shmaddr)
{
perror("shmat");
return -1;
}
printf("shmaddr = %p\n", shmaddr);
while(1)
{
int flag = 0;
char* pa = (char*)shmaddr+4;
char* end = pa+strlen(pa)-1;
char temp;
if(1 == (flag = *(int*)shmaddr))
{
while(pa<end)
{
temp = *pa;
*pa = *end;
*end = temp;
pa++;
end--;
}
*(int*)shmaddr = 0;
}
sleep(1);
}
//断开映射
shmdt(shmaddr);
return 0;
}