20230405作业(内存共享,消息队列)

作业1

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

int msqid=0;

pthread_t tid1,tid2;
struct msgbuf
{
	long mtype;
	char mtext[128];
};

void* Fun_Msgsnd(void* arg)
{
	struct msgbuf snd;
	while(1)
	{
		//确认要发送到消息队列中的信息包的数据类型和内容
		snd.mtype=22;
	//	getchar();
	//	printf("请输入消息内容:");
		scanf("%s",snd.mtext);
		getchar();

		//将信息包发送到消息队列中
		if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
		{
			perror("msgsnd");
			return NULL;
		}
	//	printf("发送成功\n");
		if(0==strcmp(snd.mtext,"quit"))
		{
			break;
		}
	}
	pthread_cancel(tid2);
	pthread_exit(NULL);
}

void* Fun_Msgrcv(void* arg)
{
	struct msgbuf rcv;
	while(1)
	{
	
		//读取A类型的数据信息的内容,并打印,阻塞形式
		if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),11,0)<0)
		{
			if(errno!=43)
			{
				//	printf("errno=%d\n",errno);
				perror("msgrcv");
				return NULL;
			}
		}
		printf("B说:%s\n",rcv.mtext);
		if(0==strcmp(rcv.mtext,"quit"))
		{	
			pthread_cancel(tid1);

			if(-1==msgctl(msqid,IPC_RMID,NULL))
			{
				perror("msgctl");
				return NULL;
			}
			break;
		}
	}
	exit(0);
//	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	//用ftok函数,计算key值
	key_t key=ftok("./",1);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
//	printf("key=%#x\n",key);

	//通过key值找到对应的消息队列
	msqid=msgget(key,IPC_CREAT|0664);
	if(msqid<0)
	{
		perror("msgget");
		return -1;
	}
//	printf("msgget success, msgqid=%d\n",msqid);

	if(pthread_create(&tid1,NULL,Fun_Msgsnd,NULL)!=0)
	{
		printf("line=%d\n",__LINE__);
		printf("pthread_create failed!\n");
		return -1;
	}

	if(pthread_create(&tid2,NULL,Fun_Msgrcv,NULL)!=0)
	{
		printf("line=%d\n",__LINE__);
		printf("pthread_create failed!\n");
		return -1;
	}

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

	printf("对话结束!\n");

	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <pthread.h>
#include <errno.h>

int msqid=0;

pthread_t tid1,tid2;
struct msgbuf
{
	long mtype;
	char mtext[128];
};

void* Fun_Msgsnd(void* arg)
{
	struct msgbuf snd;
	while(1)
	{
		//确认要发送到消息队列中的信息包的数据类型和内容
		snd.mtype=11;
	//	getchar();
	//	printf("请输入消息内容:");
		scanf("%s",snd.mtext);
		getchar();

		//将信息包发送到消息队列中
		if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
		{
			perror("msgsnd");
			return NULL;
		}
	//	printf("发送成功\n");
		if(0==strcmp(snd.mtext,"quit"))
		{
			pthread_cancel(tid2);
			break;
		}
	}
	pthread_exit(NULL);
}

void* Fun_Msgrcv(void* arg)
{
	struct msgbuf rcv;
	while(1)
	{	
		//读取A类型的数据信息的内容,并打印,阻塞形式
		if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),22,0)<0)
		{
			perror("msgrcv");
			return NULL;
	
		}
		printf("A说:%s\n",rcv.mtext);
		if(0==strcmp(rcv.mtext,"quit"))
		{	
			pthread_cancel(tid1);
			if(-1==msgctl(msqid,IPC_RMID,NULL))
			{
				perror("msgctl");
				return NULL;
			}
			break;
		}
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	//用ftok函数,计算key值
	key_t key=ftok("./",1);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
//	printf("key=%#x\n",key);

	//通过key值找到对应的消息队列
	msqid=msgget(key,IPC_CREAT|0664);
	if(msqid<0)
	{
		perror("msgget");
		return -1;
	}
//	printf("msgget success, msgqid=%d\n",msqid);

	if(pthread_create(&tid1,NULL,Fun_Msgsnd,NULL)!=0)
	{
		printf("line=%d\n",__LINE__);
		printf("pthread_create failed!\n");
		return -1;
	}

	if(pthread_create(&tid2,NULL,Fun_Msgrcv,NULL)!=0)
	{
		printf("line=%d\n",__LINE__);
		printf("pthread_create failed!\n");
		return -1;
	}
	pthread_join(tid1,NULL);


	printf("对话结束!\n");

	return 0;
}

作业2:

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

int main(int argc, const char *argv[])
{
	key_t key=ftok("./",6);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	printf("key=%#x\n",key);

	int shmid=shmget(key,64,IPC_CREAT|0664);
	if(shmid<0)
	{
		perror("shmget");
		return -1;
	}
	printf("shmid=%d\n",shmid);

	void* addr=shmat(shmid,NULL,0);
	if((void*)-1==addr)
	{
		perror("shmat");
		return -1;
	}
	printf("addr=%p\n",addr);

	char* buf=(char*)addr+4;
	while(1)
	{
		if(1==*(int*)addr)
		{
			int i=0;
			int j=strlen(buf)-1;
			while(i<j)
			{
				char t=*(buf+i);
				*(buf+i)=*(buf+j);
				*(buf+j)=t;
				i++;j--;
			}
			*(int*)addr=0;
		}
	}
	
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
	key_t key=ftok("./",6);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	printf("key=%#x\n",key);

	int shmid=shmget(key,64,IPC_CREAT|0664);
	if(shmid<0)
	{
		perror("shmget");
		return -1;
	}
	printf("shmid=%d\n",shmid);

	void* addr=shmat(shmid,NULL,0);
	if((void*)-1==addr)
	{
		perror("shmat");
		return -1;
	}
	printf("addr=%p\n",addr);

	*(int*)addr=0;
	char*buf=(char*)addr+4;
	strcpy(buf,"1234567");
	while(1)
	{
		if(0==*(int*)addr)
		{
			printf("%s\n",(char*)addr+4);
			*(int*)addr=1;
			sleep(1);
		}
	}
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值