1.要求实现AB进程对话
a.A进程先发送一句话给B进程,B进程接收后打印
b.B进程再回复一句话A进程,A程接收后打印
c.重复1.2步骤,当收到quit后,要结束AB进程
提示:用一个消息队列,两种类型即可
a.当对方输入quit后,退出AB进程删除消息队列;
//*****************A进程****************************************//
#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[128];
};
int main(int argc, const char *argv[])
{
// 计算key值;
key_t key = ftok("../8day", 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);
// system("ipcs -q");
struct msgbuf snd;
struct msgbuf rcv;
while (1)
{
// 发送消息*********************************
printf("请输入消息类型>>");
scanf("%ld", &snd.mtype);
getchar();
if (0 == snd.mtype || snd.mtype < 0)
break;
printf("请输入消息内容>>");
scanf("%s", snd.mtext);
getchar();
if (0 == strcmp(snd.mtext, "quit"))
{
msgctl(msqid, IPC_RMID, NULL);
break;
}
// 发送消息到消息队列中
if (msgsnd(msqid, &snd, sizeof(snd.mtext), 0))
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
// 接收消息***********************************
if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 2, 0) < 0)
{
perror("msgrcv");
return -1;
}
if (0 == strcmp(rcv.mtext, "quit"))
{
msgctl(msqid, IPC_RMID, NULL);
break;
}
printf("type>>%ld mtext>>%s\n", rcv.mtype, rcv.mtext);
}
system("ipcs -q");
return 0;
}
//******************B进程*******************************//
#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[128];
};
int main(int argc, const char *argv[])
{
key_t key = ftok("../8day", 1);
if (key < 0)
{
perror("ftok");
return -1;
}
printf("%#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);
// system("ipcs -q");
struct msgbuf rcv;
struct msgbuf snd;
while (1)
{
//接收消息*********************************************************
//if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,0)<0)
//if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,IPC_NOWAIT)<0)
if(msgrcv(msqid,&snd,sizeof(snd.mtext),1,0)<0)
{
perror("msgrcv");
return -1;
}
if (0 == strcmp(snd.mtext, "quit"))
{
msgctl(msqid,IPC_RMID,NULL);
break;
}
printf("类型>>%ld 内容>>%s\n",snd.mtype,snd.mtext);
//发送消息*********************************************************
printf("请输入消息类型>>");
scanf("%ld", &rcv.mtype);
getchar();
if (0 == rcv.mtype || rcv.mtype < 0)
break;
printf("请输入消息内容>>");
scanf("%s", rcv.mtext);
getchar();
if (0 == strcmp(rcv.mtext, "quit"))
{
break;
msgctl(msqid,IPC_RMID,NULL);//删除消息队列
}
// 发送消息到消息队列中
if (msgsnd(msqid, &rcv, sizeof(rcv.mtext), 0))
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
}
system("ipcs -q");
return 0;
}
输出结果为:
///在第一个基础上利用多进程实现两端自由通信
2、—个进程对共享内存中的数据打印,另一个进程对共享内存中的数据倒置。
提示:共享内存中存储: flag+字符串
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#define SHMSIZE 8 // 共享内存大小
int main(int argc, const char *argv[])
{
int flag = 0;
key_t key = ftok("./", 1); // 获取key值
if (key < 0)
{
perror("ftok");
return -1;
}
printf("key=%#x\n", key);
// 创建共享内存的shmid
// int shmid=shmget(key,SHMSIZE,IPC_CREAT|0664);
int shmid = shmget(key, SHMSIZE, IPC_CREAT | 0664);
if (shmid < 0)
{
perror("shmget");
return -1;
}
printf("shmid=%d\n", shmid);
// 将共享内存映射到用户空间中使用 shmat
void *addr = shmat(shmid, NULL, 0);
if ((void *)-1 == addr)
{
perror("shmat");
return -1;
}
// printf("addr=%p\n",addr);
strcpy((char *)addr, "1234567");
pid_t pid = fork(); // 创建子进程
if (pid < 0)
{
perror("fork");
return -1;
}
else if (0 == pid) // 子进程:逆序打印字符串
{
//if(1 == flag)
//{
sleep(1);
printf("child will print reserved string:\n");
for (int i = SHMSIZE - 2; i >= 0; i--)
{
putchar(*((char *)addr + i));
}
putchar('\n');
//}
}
else // 父进程:顺序打印字符串
{
// if(0 == flag)
//{
printf("parent will print original string:\n");
printf("%s\n", (char *)addr);
flag = 1;
if (-1 == waitpid(pid, NULL, 0))
{
perror("waitpid");
return -1;
}
if (-1 == shmdt(addr))
{
perror("shmdt");
return -1;
}
if (-1 == shmctl(shmid, IPC_RMID, NULL))
{
perror("shmctl");
return -1;
}
// }
}
return 0;
}
输出结果为: