要求实现AB进程对话
A进程先发送一句话给B进程,B进程接收后打印
B进程再回复一句话给A进程,A进程接收后打印
重复1.2步骤,当收到quit后,要结束AB进程
提示:两根管道
提示:用一个消息队列,两种类型即可
当对方输入quit后,退出AB进程删除消息队列;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf
{
long mtype;
char mtext[32];
};
int main(int argc, const char *argv[])
{
//创建消息队列
key_t key = ftok("/home/ubuntu/",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
//通过key值找到对应的消息队列
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msgget success msqid = %d\n",msqid);
struct msgbuf snd;
struct msgbuf rcv;
int flag = 0;
while(1)
{
if(1 != flag)
{
snd.mtype = 1;
if(0 == snd.mtype)
break;
printf("请输入消息内容:");
scanf("%s",snd.mtext);
//发送数据到消息队列中
if(msgsnd(msqid,&snd,sizeof(snd.mtext),0))
{
perror("msgsnd");
return -1;
}
flag = 1;
}
else if(0 != flag)
{
if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),2,0) < 0)
{
perror("msgrcv");
return -1;
}
printf("B说:%s\n",rcv.mtext);
flag = 0;
}
}
system("ipcs -q");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MSG_EXCEPT 020000
struct msgbuf
{
long mtype;
char mtext[32];
};
int main(int argc, const char *argv[])
{
//创建消息队列
key_t key = ftok("/home/ubuntu/",1);
if(key < 0)
{
perror("ftok");
return -1;
}
printf("key = %#x\n",key);
//通过key值找到对应的消息队列
int msqid = msgget(key,IPC_CREAT|0664);
if(msqid < 0)
{
perror("msgget");
return -1;
}
printf("msgget success msqid = %d\n",msqid);
struct msgbuf rcv;
struct msgbuf snd;
int flag = 0;
while(1)
{
if(1 != flag)
{
if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,0) < 0)
{
perror("msgrcv");
return -1;
}
printf("A说:%s\n",rcv.mtext);
flag = 1;
}
else if(0 != flag)
{
snd.mtype = 2;
if(0 == snd.mtype)
break;
printf("请输入消息内容:");
scanf("%s",snd.mtext);
//发送数据到消息队列中
if(msgsnd(msqid,&snd,sizeof(snd.mtext),0))
{
perror("msgsnd");
return -1;
}
flag = 0;
}
}
system("ipcs -q");
return 0;
}
一个进程对共享内存中的数据打印,另一个进程对共享内存中的数据倒置。
提示:共享内存中存储:flag+字符串
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
key_t key = ftok("./",'a');
if(key < 0)
{
perror("ftok");
return -1;
}
//创建获取共享内存的shmid
int shmid = shmget(key,32,IPC_CREAT|0664);
if(shmid < 0)
{
perror("shmget");
return -1;
}
//将共享内存映射到用户空间中使用shmat
void *addr = shmat(shmid,NULL,0);
if((void *)-1 == addr)
{
perror("shmat");
return -1;
}
char str[32] = "1234567";
char *ptr = (char *)addr +4;
while(1)
{
*(int *)addr = 0;
int left = 0;
int right = strlen(str)-1;
while(left < right)
{
str[left] = str[left] ^ str[right];
str[right] = str[left] ^ str[right];
str[left] = str[left] ^ str[right];
left++;
right--;
}
*(int *)addr = 1;
strcpy(ptr,str);
sleep(1);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
key_t key = ftok("./",'a');
if(key < 0)
{
perror("ftok");
return -1;
}
//创建获取共享内存的shmid
int shmid = shmget(key,32,IPC_CREAT|0664);
if(shmid < 0)
{
perror("shmget");
return -1;
}
//将共享内存映射到用户空间中使用shmat
void *addr = shmat(shmid,NULL,0);
if((void *)-1 == addr)
{
perror("shmat");
return -1;
}
while(1)
{
if(1 == *(int *)addr)
printf("%s\n",(char *)addr+4);
}
return 0;
}