消息队列和共享空间(8.6)

目录

1.要求AB进程做通信(使用队列)

A进程发送一句话,B进程接收打印

然后B进程发送给A进程一句话,A进程接收打印

重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程

2.在第一题的基础上实现,AB进程能够随时收发数据(附加题)

A进程

B进程

A进程写入一个整型,在该整型后,写入一个字符串

B进程将共享内存中的整型以及字符串读取出来;

写入共享内存的进程

 输出内容的进程


 

1.要求AB进程做通信(使用队列)

  1. A进程发送一句话,B进程接收打印

  2. 然后B进程发送给A进程一句话,A进程接收打印

  3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程

2.在第一题的基础上实现,AB进程能够随时收发数据(附加题)

A进程

#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>

//消息包结构体
struct msgbuf
{
	long mtype; 		//消息类型,必须大于0 
	char mtext[128]; 	//消息内容,该成员类型可变
};


off_t len;

struct msgbuf rcv;
ssize_t res =0;
struct msgbuf snd;
int  msqid;
int msqid2;

void* callBack1(void* arg)
{
	while(1)
	{
		//发送消息包

		printf("请输入消息类型>>>");
		scanf("%ld", &snd.mtype);
		getchar(); 	//吸收垃圾字符

		printf("请输入>>>");
		fgets(snd.mtext, sizeof(snd.mtext), stdin);

		snd.mtext[strlen(snd.mtext)-1] = 0;
	if(strcasecmp(snd.mtext, "quit") == 0)
		{
			exit(0);
		}
		//阻塞方式发送,消息队列满了,阻塞
		if(msgsnd(msqid, &snd, sizeof(snd.mtext), 0) < 0)
		{
			perror("msgsnd");
			return NULL;
		}
	
		printf("A消息包发送成功\n");
	}
}


void* callBack2(void* arg)
{
	while(1)
	{
		res=msgrcv(msqid2,&rcv,sizeof(rcv.mtext),0,0);
		if(strcasecmp(snd.mtext, "quit") == 0)
		{
			exit(0);
		}
		if(res<0)
		{
			perror("msgrcv");
			return NULL;
		}
		printf("B res=%ld [%ld : %s]\n",res,rcv.mtype,rcv.mtext);


	}
}






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);

	//计算key值
	key_t key2 = ftok("./",2);
	if(key2 < 0)
	{
		perror("ftok");
		return -1;
	}
	printf("key2 = %#x\n", key2);
	//创建一个消息队列
	msqid = msgget(key, IPC_CREAT|0664);
	if(msqid < 0)
	{
		perror("msgget");
		return -1;
	}
	printf("msqid = %d\n", msqid);

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

	//创建一个线程
	pthread_t tid1;
	if(pthread_create(&tid1, NULL, callBack1,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	//创建一个线程,
	pthread_t tid2;
	if(pthread_create(&tid2, NULL, callBack2, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}


	pthread_join(tid1, NULL);
	pthread_join(tid2,NULL);

	return 0;
}

B进程

#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>

//消息包结构体
struct msgbuf
{
	long mtype; 		//消息类型,必须大于0 
	char mtext[128]; 	//消息内容,该成员类型可变
};
off_t len;

struct msgbuf rcv;
ssize_t res =0;
struct msgbuf snd;
int  msqid;
int msqid2;


void* callBack1(void* arg)
{
	while(1)
	{
		res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),0,0);
	if(strcasecmp(snd.mtext, "quit") == 0)
	{
		exit(0);
	}	

	if(res<0)
	{
		perror("msgrcv");
		return NULL;
	}
	printf("A res=%ld [%ld : %s]\n",res,rcv.mtype,rcv.mtext);
	}


}

void* callBack2(void* arg)
{
	while(1)
	{
		//发送消息包

	printf("请输入消息类型>>>");
		scanf("%ld", &snd.mtype);
		getchar(); 	//吸收垃圾字符

		printf("请输入>>>");
		fgets(snd.mtext, sizeof(snd.mtext), stdin);
		snd.mtext[strlen(snd.mtext)-1] = 0;
	if(strcasecmp(snd.mtext, "quit") == 0)
		{
			exit(0);
		}	
		//阻塞方式发送,消息队列满了,阻塞
		if(msgsnd(msqid2, &snd, sizeof(snd.mtext), 0) < 0)
		{
			perror("msgsnd");
			return NULL;
		}
	
		printf("B消息包发送成功\n");
	}
}



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);


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


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




	//创建一个消息队列
	msqid2 = msgget(key2, IPC_CREAT|0664);
	if(msqid2 < 0)
	{
		perror("msgget");
		return -1;
	}
	printf("msqid2 = %d\n", msqid2);
	//创建一个线程
	pthread_t tid1;
	if(pthread_create(&tid1, NULL, callBack1,NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}

	//创建一个线程
	pthread_t tid2;
	if(pthread_create(&tid2, NULL, callBack2, NULL) != 0)
	{
		perror("pthread_create");
		return -1;
	}


	pthread_join(tid1, NULL);
	pthread_join(tid2,NULL);

	return 0;
}

3.

  1. A进程写入一个整型,在该整型后,写入一个字符串

  2. B进程将共享内存中的整型以及字符串读取出来;

写入共享内存的进程

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, const char *argv[])
{


	//创建key值
	key_t key = ftok("./", 'a');
	if(key < 0)
	{
		perror("ftok");
		return -1;
	}
	printf("key = %#x\n", key);
	
	//创建共享内存
	int shmid = shmget(key, 32, IPC_CREAT|0664);
	if(shmid < 0)
	{
		perror("shmget");
		return -1;
	}
	printf("shmid = %d\n", shmid);

	//共享内存的映射
	void* shmaddr = shmat(shmid, NULL, 0);

	int* pint = (int*)shmaddr;
	*pint=1;
	char* pstr = (char*)(pint+1);
	strcpy(pstr, "hello world");



	system("ipcs -m");

	return 0;
}

 输出内容的进程

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, const char *argv[])
{
	//创建key值
	key_t key = ftok("./", 'a');
	if(key < 0)
	{
		perror("ftok");
		return -1;
	}
	printf("key = %#x\n", key);
	
	//创建共享内存
	int shmid = shmget(key, 32, IPC_CREAT|0664);
	if(shmid < 0)
	{
		perror("shmget");
		return -1;
	}
	printf("shmid = %d\n", shmid);

	//共享内存的映射
	void* shmaddr = shmat(shmid,NULL ,0);
    int* pint=(int*)shmaddr;
	char* pstr = (char*)(pint+1);
//	strcpy(pstr, "hello world");
    
    printf("%d   ",*pint);
	printf("%s\n",pstr);



	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值