8.7作业

要求 用消息队列实现AB进程之间对话,实现随时收发

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<head.h>
struct msgbuf
{
	long mtype;
	char mtext[128];
};
void*callBack1(void*arg)
{
	int msqid=*(int*)arg;
	struct msgbuf sndbuf;
	while(1)
	{
		sndbuf.mtype=2;
		printf("请输入消息内容: ");
		fgets(sndbuf.mtext,sizeof(sndbuf.mtext),stdin);
		sndbuf.mtext[strlen(sndbuf.mtext)-1]=0;
		if(msgsnd(msqid,&sndbuf,sizeof(sndbuf.mtext),0)<0)
		{
			ERR_MSG("msgsnd");
			return NULL;
		}
		if(strcmp(sndbuf.mtext,"quit")==0)
			exit(0);
		printf("发送成功\n");
	}
	pthread_exit(NULL);
}
void*callBack(void*arg)
{
	int msqid=*(int*)arg;
	struct msgbuf rcv;
	ssize_t res=0;
	while(1)
	{
		res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,0);
		if(strcmp(rcv.mtext,"quit")==0)
			exit(0);
		if(res<0)
		{
			ERR_MSG("msgrcv");
			return NULL;
		}
		printf("%s\n",rcv.mtext);
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	key_t key=ftok("/home/ubuntu/",2);
	if(key<0)
	{
		ERR_MSG("ftok");
		return -1;
	}
	printf("key=%#x\n",key);
	int msqid=msgget(key,IPC_CREAT|0664);
	if(msqid<0)
	{
		ERR_MSG("msgget");
		return -1;
	}
	printf("msqid=%d\n",msqid);
	pthread_t tid;
	if(pthread_create(&tid,NULL,callBack,(void*)&msqid)!=0)
	{
		fprintf(stderr,"failed __%d__",__LINE__);
		return -1;
	}
	pthread_t tid1;
	if(pthread_create(&tid1,NULL,callBack1,(void*)&msqid)!=0)
	{
		fprintf(stderr,"failed __%d__",__LINE__);
		return -1;
	}

	pthread_join(tid,NULL);
	pthread_join(tid1,NULL);
	return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<head.h>
struct msgbuf
{
	long mtype;
	char mtext[128];
};
void*callBack(void*arg)
{
	int msqid=*(int*)arg;
	struct msgbuf sndbuf;
	while(1)
	{
		sndbuf.mtype=1;
		printf("请输入消息内容: ");
		fgets(sndbuf.mtext,sizeof(sndbuf.mtext),stdin);
		sndbuf.mtext[strlen(sndbuf.mtext)-1]=0;
		if(msgsnd(msqid,&sndbuf,sizeof(sndbuf.mtext),0)<0)
		{
			ERR_MSG("msgsnd");
			return NULL;
		}
		if(strcmp(sndbuf.mtext,"quit")==0)
			exit(0);
		printf("发送成功\n");
	}
}
void*callBack1(void*arg)
{
	int msqid=*(int*)arg;
	struct msgbuf rcv;
	ssize_t res=0;
	while(1)
	{
		res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),2,0);
		if(strcmp(rcv.mtext,"quit")==0)
			exit(0);
		if(res<0)
		{
			ERR_MSG("msgrcv");
			return NULL;
		}
		printf("%s\n",rcv.mtext);
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	key_t key=ftok("/home/ubuntu/",2);
	if(key<0)
	{
		ERR_MSG("ftok");
		return -1;
	}
	printf("key=%#x\n",key);
	int msqid=msgget(key,IPC_CREAT|0664);
	if(msqid<0)
	{
		ERR_MSG("msgget");
		return -1;
	}
	printf("msqid=%d\n",msqid);
	pthread_t tid;
	if(pthread_create(&tid,NULL,callBack,(void*)&msqid)!=0)
	{
		fprintf(stderr,"failed __%d__",__LINE__);
		return -1;
	}
	pthread_t tid1;
	if(pthread_create(&tid1,NULL,callBack1,(void*)&msqid)!=0)
	{
		fprintf(stderr,"failed __%d__",__LINE__);
		return -1;
	}
	pthread_join(tid,NULL);
	pthread_join(tid1,NULL);
	system("ipcs -q");
	return 0;
}

要求在共享内存中存入字符串“1234567”.A进程循环打印字符串,B进程循环倒置字符串。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<head.h>
int main(int argc, const char *argv[])
{
	key_t key=ftok("./",10);
	if(key<0)
	{
		ERR_MSG("ftok");
		return -1;
	}
	printf("key= %#x\n",key);
	int shmid=shmget(key,32,IPC_CREAT|0664);
	if(shmid<0)
	{
		ERR_MSG("shmget");
		return -1;
	}
	printf("shmid =%d\n",shmid);
	void*addr=shmat(shmid,NULL,0);
	if((void*)-1==addr)
	{
		ERR_MSG("shmat");
		return -1;
	}
	printf("addr=%p\n",addr);
	int *p1=(int*)addr;
	while(1){
		if(*p1==1)
		{
			printf("%s\n",(char*)(p1+4));
			*p1=0;
		}
	}
	system("ipcs -m");
	return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<head.h>
int main(int argc, const char *argv[])
{
	key_t key=ftok("./",10);
	if(key<0)
	{
		ERR_MSG("ftok");
		return -1;
	}
	printf("key= %#x\n",key);
	int shmid=shmget(key,32,IPC_CREAT|0664);
	if(shmid<0)
	{
		ERR_MSG("shmget");
		return -1;
	}
	printf("shmid =%d\n",shmid);
	void*addr=shmat(shmid,NULL,0);
	if((void*)-1==addr)
	{
		ERR_MSG("shmat");
		return -1;
	}
	printf("addr=%p\n",addr);
	int flag=0;
	int *p1=(int*)addr;
	*p1=flag;
	char *p2=(char*)(p1+4);
	strcpy(p2,"1234567");
	while(1)
	{
		int i=0;
		int k=strlen(p2)-1;
		if(*p1==0){
			while(i<k)
			{
				int t=*(p2+i);
				*(p2+i)=*(p2+k);
				*(p2+k)=t;
				i++;k--;
			}
			*p1=1;
		}
	}
	system("ipcs -m");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值