8.5消息列表与共享内存的练习

     1.使用消息列表完成AB程序间通信。

     2.A进程写入一个整型,在该整型后,写入一个字符串,B进程将共享内存中的整型以及         字符串读取出来。

 题1,本次使用进程来解决AB的通信。代码如下;

进程A:

#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
//消息包结构体
struct msgbuf
{
	long mtype; 		//消息类型,必须大于0
	char mtext[128]; 	//消息内容,该成员类型可变
};
int main(int argc, const char *argv[])
{	
	//计算key1值
	key_t key1,key2;
	key1= ftok("./",1);
	if(key1 < 0)
	{
		perror("ftok");
		return -1;
	}
	printf("key1 = %#x\n", key1);

	//计算key2值
	key2 = ftok("./",2);
	if(key2 < 0)
	{
		perror("ftok");
		return -1;
	}
	printf("key2 = %#x\n", key2);

	//创建一个消息队列1
	int msqid1 = msgget(key1, IPC_CREAT|0664);
	if(msqid1 < 0)
	{
		perror("msgget");
		return -1;
	}
	printf("msqid = %d\n", msqid1);

	//创建一个消息队列2
	int msqid2 = msgget(key2, IPC_CREAT|0664);
	if(msqid2 < 0)
	{
		perror("msgget");
		return -1;
	}
	printf("msqid2 = %d\n", msqid2);

    //创建父子进程
	
	pid_t pid = fork();
	if(pid > 0)
	{
		//向B进程发送消息包
		struct msgbuf snd;
		while(1)
		{
			snd.mtype=1;

			printf("请输入>>>");
			fgets(snd.mtext, sizeof(snd.mtext), stdin);
			snd.mtext[strlen(snd.mtext)-1] = 0;

			//阻塞方式发送,消息队列满了,阻塞
			if(msgsnd(msqid1, &snd, sizeof(snd.mtext), 0) < 0)
			{
				perror("msgsnd");
				return -1;
			}
			printf("A消息包发送成功\n");
		
		if(0 == strcmp(snd.mtext,"quit"))
		{
			printf("A进程退出\n");
			exit(0);
		}
		sleep(1);
		}
	}


	//子进程
	if(0 == pid)
	{
		sleep(1);
		int msqid2 = msgget(key2,IPC_CREAT|0664);
		if(msqid2 <0)
		{
			perror("msgget");
			return -1;
		}
		printf("msgget = %d\n",msqid2);

		//接受消息包
		struct msgbuf rcv;
		ssize_t res =0;
		while(1)
		{
			//以阻塞方式读取消息队列中的消息,msgtyp==0,先进先出原则
			res = msgrcv(msqid2,&rcv,sizeof(rcv.mtext),0,0);
			if(res <0)
			{
				perror("msgrcv");
				return -1;
			}

			printf("res = %ld [%ld : %s]\n",res,rcv.mtype,rcv.mtext);
			printf("B消息包接受成功\n");
		}
		if(strcmp(rcv.mtext,"quit") == 0)
		{
			printf("收到B进程退出,进程退出\n");
			_exit(0);
		}

	

	system("ipcs -q");
	}



	return 0;
}

进程B:

#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
//消息包结构体
struct msgbuf
{
	long mtype; 		//消息类型,必须大于0
	char mtext[128]; 	//消息内容,该成员类型可变
};
int main(int argc, const char *argv[])
{	
	//计算key1值
	key_t key1,key2;
	key1= ftok("./",1);
	if(key1 < 0)
	{
		perror("ftok");
		return -1;
	}
	printf("key1 = %#x\n", key1);

	//计算key2值
	key2 = ftok("./",2);
	if(key2 < 0)
	{
		perror("ftok");
		return -1;
	}
	printf("key2 = %#x\n", key2);

	//创建一个消息队列B1
	int msqid1 = msgget(key1, IPC_CREAT|0664);
	if(msqid1 < 0)
	{
		perror("msgget");
		return -1;
	}
	printf("msqid = %d\n", msqid1);

	//创建一个消息队列B2
	int msqid2 = msgget(key2, IPC_CREAT|0664);
	if(msqid2 < 0)
	{
		perror("msgget");
		return -1;
	}
	printf("msqid2 = %d\n", msqid2);

    //创建父子进程
	pid_t pid = fork();
	if(pid > 0)
	{
		//向A进程发送消息包
		struct msgbuf snd;
		while(1)
		{
			snd.mtype=1;

			printf("请输入>>>");
			fgets(snd.mtext, sizeof(snd.mtext), stdin);
			snd.mtext[strlen(snd.mtext)-1] = 0;

			//阻塞方式发送,消息队列满了,阻塞
			if(msgsnd(msqid2, &snd, sizeof(snd.mtext), 0) < 0)
			{
				perror("msgsnd");
				return -1;
			}
			if(0 == strcmp(snd.mtext,"quit"))
			{
				printf("B进程退出\n");
				exit(0);
			}
			printf("B消息包发送成功\n");
			sleep(1);

		}
	}


	//子进程
	if(0 == pid)
	{
		sleep(1);
		int msqid2 = msgget(key2,IPC_CREAT|0664);
		if(msqid2 <0)
		{
			perror("msgget");
			return -1;
		}
		printf("msgget = %d\n",msqid2);

		//接受A消息包
		struct msgbuf rvc;
		ssize_t res =0;
		while(1)
		{
			//以阻塞方式读取消息队列中的消息,msgtyp==0,先进先出原则
			res = msgrcv(msqid1,&rvc,sizeof(rvc.mtext),0,0);
			if(res <0)
			{
				perror("msgrcv");
				return -1;
			}
			printf("res = %ld [%ld : %s]\n",res,rvc.mtype,rvc.mtext);
			if(0 ==strcmp(rvc.mtext,"quit"))
			{
				printf("收到A进程退出,B进程退出\n");
				exit(0);
			}
			printf("A消息包接受成功\n");
		}

		system("ipcs -q");
	}
	
	

	return 0;
}

运行结果:

 题2:

代码如下:

进程A

#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<string.h>
int main(int argc, const char *argv[])
{
	int a=10;
	char c[10]="gf";
	key_t key = ftok("./",1);
	int sh = shmget(key,128,IPC_CREAT|0664);
	int*p=(int*)shmat(sh,NULL,0);
	*p=a;
	 char*pc= (char*)shmat(sh,NULL,0);
	strcpy(pc,c);
	return 0;
}

进程B

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
int main(int argc, const char *argv[])
{
	key_t key = ftok("./",1);
	int sh = shmget(key,50,IPC_CREAT|0644);
	int*p=(int*)shmat(sh,NULL,0);
    char *pc=(char*)shmat(sh,NULL,0);	
	printf("整形:%d   ",*p);
 
	 printf("字符串%s   \n",pc);
	
 
 
 
 
	return 0;
}

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值