2. 要求用消息队列实现AB进程对话:
1)A进程发送一句话,B进程接收后打印;
2)B进程接着再发送一句话,A进程接收打印;
3)重复上述步骤,当A进程或者B进程接收到quit后退出AB进程。
//A进程
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
struct msgbuf
{
long mtype;
char mtext[10];
}send,rcv;
int main(int argc, const char *argv[])
{
key_t key=ftok("./",2);//创建IPC对象
if(key==-1)
{
perror("ftok");
return -1;
}
int msqid=msgget(key,IPC_CREAT|0664);//通过key值创建消息队列
if(msqid==-1)
{
perror("msgget");
return -1;
}
while(1)
{
send.mtype=1;//该进程发送类型为1的消息到消息队列中
printf("A进程发送:");
scanf("%s",send.mtext);
if(msgsnd(msqid,&send,sizeof(send.mtext),IPC_NOWAIT)==-1)//以非阻塞的方式发送
{
perror("msgsnd");
return -1;
}
if(strcmp(send.mtext,"quit")==0)
{
break;
}
if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),2,0)<0)//该进程从消息队列接受类型为2的消息
{
perror("msgrcv");
return -1;
}
printf("%s\n",rcv.mtext);
if(strcmp(rcv.mtext,"quit")==0)
{
break;
}
}
printf("退出A进程\n");
return 0;
}
//B进程
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
struct msgbuf
{
long mtype;
char mtext[10];
};
int main(int argc, const char *argv[])//
{
key_t key=ftok("./",2);//创建IPC对象
if(key==-1)
{
perror("ftok");
return -1;
}
int msqid=msgget(key,IPC_CREAT|0664);//通过key值创建消息队列
if(msqid==-1)
{
perror("msgget");
return -1;
}
struct msgbuf rcv;
struct msgbuf send;
while(1)
{
if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,0)<0)//该进程从消息队列接受类型为1的消息
{
perror("msgrcv");
return -1;
}
if(strcmp(rcv.mtext,"quit")==0)
{
break;
}
printf("%s\n",rcv.mtext);
send.mtype=2;//该进程发送类型为2的消息到消息队列中
printf("B进程发送:");
scanf("%s",send.mtext);
if(msgsnd(msqid,&send,sizeof(send.mtext),IPC_NOWAIT)==-1)//以非阻塞的方式发送
{
perror("msgsnd");
return -1;
}
if(strcmp(send.mtext,"quit")==0)
{
break;
}
}
msgctl(msqid,IPC_RMID,NULL);//
printf("退出B进程并删除消息队列\n");
system("ipcs -q");
return 0;
}
创建两个进程A、B,以及一个共享内存,共享内存中存储char str[] = "123456",在不考虑进程退
出的情况下,要求如下:
1. A进程 循环 打印str字符串。
2. B进程 循环 倒置str字符串,不使用辅助数组。注意是循环倒置,要把字符串倒过来,倒回
去。
3. 要求A进程打印出来的结果是有序的,例如:"123456" 或者 "654321",不允许出
现"623451",,,等无序情况
提示:将flag + str一起写到共享内存中,当flag=0,打印 当flag=1 ,倒置
//A进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
key_t key=ftok("./",'a');//计算key值
if(key==-1)
{
perror("ftok");
return -1;
}
int shmid=shmget(key,32,IPC_CREAT|0775);//通过key值创建共享内存
if(shmid==-1)
{
perror("shmget");
return -1;
}
void *shmaddr=shmat(shmid,NULL,0);//将共享内存映射到用户空间中
if(shmaddr==(void *)-1)
{
perror("shmaddr");
return -1;
}
int *pa=(int *)shmaddr;
*pa=0;//在共享内存中定义一个标志位
char *pb=(char *)pa+4;
strcpy(pb,"1234567");
while(1)
{
if(*pa==0)//标志位为0进行逆置的操作
{
char *head=pb;//定义头指针
char *tail=pb+strlen(pb)-1;//定义尾指针
while(head<tail)
{
*head ^= *tail;
*tail ^= *head;
*head ^= *tail;
head++;
tail--;
}
*pa=1;
}
}
return 0;
}
//B进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
key_t key=ftok("./",'a');//计算key值
if(key==-1)
{
perror("ftok");
return -1;
}
int shmid=shmget(key,32,IPC_CREAT|0775);//通过key值创建共享内存
if(shmid==-1)
{
perror("shmget");
return -1;
}
void *shmaddr=shmat(shmid,NULL,0);//将共享内存映射到用户空间中
if(shmaddr==(void *)-1)
{
perror("shmaddr");
return -1;
}
int *pa=(int *)shmaddr;
while(1)
{
if(*pa==1)//标志位为1进行打印的操作
{
printf("%s\n",(char *)shmaddr+4);//
*pa=0;
}
}
shmdt(shmaddr);//断开共享内存与用户空间的映射
shmctl(shmid,IPC_RMID,NULL);//删除共享内存
return 0;
}
本文介绍使用消息队列实现两个进程间的对话功能,并通过共享内存实现字符串的循环打印与倒置操作。A进程和B进程利用消息队列进行交互,发送与接收信息。同时,共享内存用于存储字符串及状态标记,实现A进程对字符串的循环打印和B进程对字符串的循环倒置。
876

被折叠的 条评论
为什么被折叠?



