消息队列与共享内存空间练习

要求用消息队列实现AB进程对话
(1)A进程先发送一句话给B进程,B进程接收后打印
(2)B进程再回复一句话给A进程,A进程接收后打印
(3)重复1.2步骤,当收到quit后,要结束AB进程
实现随时收发:用多进程 多线程。
A进程

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
struct msbuf
{
	long mtype;
	char mtext[128];
};
int main(int argc, const char *argv[])
{
	key_t  key = ftok("/home/ubuntu",1);
	if(key < 0)
	{
		perror("ftok");
		return -1;
	}

	int msqid = msgget(key,IPC_CREAT|0664);
	if(msqid < 0)
	{
		perror("msgget");
		return -1;
	}
	struct msbuf messbuf;
	pid_t cpid = fork();
	ssize_t res = 0;
	if(cpid > 0)
	{
		while(1)
		{
			//终端输入消息
			messbuf.mtype = 1;
			printf("请输入消息>>>");
			fgets(messbuf.mtext,sizeof(messbuf.mtext),stdin);
			messbuf.mtext[strlen(messbuf.mtext) - 1] = 0;
			//向消息队列发送数据
			if(msgsnd(msqid,&messbuf,sizeof(messbuf.mtext),0) < 0)
			{
				perror("msgnd");
				return -1;
			}
			if(strcmp(messbuf.mtext,"quit") == 0)
				break;
			//		bzero(messbuf.mtext,sizeof(messbuf.mtext));
		}
	}

	else if(0 == cpid)
	{
		while(1)
		{
			res = msgrcv(msqid,&messbuf,sizeof(messbuf.mtext),2,0);
			if(res < 0)
			{
				perror("msgrcv");
				return -1;
			}
			if(strcmp(messbuf.mtext,"quit") == 0)
				break;
			printf("mtext:%s\n",messbuf.mtext);
		}
		kill(getpid(),2);
	}
	return 0;
}

B进程

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
struct msbuf
{
	long mtype;
	char mtext[128];
};
int main(int argc, const char *argv[])
{
	key_t  key = ftok("/home/ubuntu",1);
	if(key < 0)
	{
		perror("ftok");
		return -1;
	}

	int msqid = msgget(key,IPC_CREAT|0664);
	if(msqid < 0)
	{
		perror("msgget");
		return -1;
	}
	struct msbuf messbuf;
	pid_t cpid = fork();
	ssize_t res = 0;
	if(cpid > 0)
	{
		while(1)
		{
			//终端输入消息
			messbuf.mtype = 2;
			printf("请输入消息>>>");
			fgets(messbuf.mtext,sizeof(messbuf.mtext),stdin);
			messbuf.mtext[strlen(messbuf.mtext) - 1] = 0;
			//向消息队列发送数据
			if(msgsnd(msqid,&messbuf,sizeof(messbuf.mtext),0) < 0)
			{
				perror("msgnd");
				return -1;
			}
			if(strcmp(messbuf.mtext,"quit") == 0)
				break;
	//		bzero(messbuf.mtext,sizeof(messbuf.mtext));
		}
	}

	else if(0 == cpid)
	{
		while(1)
		{
		res = msgrcv(msqid,&messbuf,sizeof(messbuf.mtext),1,0);
		if(res < 0)
		{
			perror("msgrcv");
			return -1;
		}
		if(strcmp(messbuf.mtext,"quit") == 0)
			break;
		printf("mtext:%s\n",messbuf.mtext);
		}
		kill(getpid(),2);
	}
	return 0;
}

要求在共享内存中存入字符串 “1234567”。A进程循环打印字符串,B进程循环倒置字符串,要求结果不允许出现乱序:
提示:共享内存中存储 flag + string.

#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, const char *argv[])
{
	key_t key = ftok("./",10);
	if(key < 0)
	{
		perror("ftok");
		return -1;
	}
	int shmid = shmget(key,32,IPC_CREAT|0664);
	if(shmid < 0)
	{
		perror("shmget");
		return -1;
	}
	void *addr = shmat(shmid,NULL,0);
	if((void *)-1 == addr)
	{
		perror("shmat");
		return -1;
	}
	int flag=0;
	*(int *)addr = flag;
	char *p=(char *)addr + 4;
	//printf("%d",*(int*)addr);
	//printf("%s\n",(char *)addr+4);
	strcpy(p,"1234567");
	pid_t cpid = fork();
	if(cpid > 0)
	{
		while(1)
		{
			if(*(int *)addr == 0)
			{
				sleep(1);
				printf("%s\n",p);
				*(int *)addr = 1;
			}	
		}
	}
	else if(cpid == 0)
	{
		while(1)
		{
			if(1 == *(int *)addr)
			{
				int i=0,j=strlen(p)-1;
				while(i<j)
				{
					char temp=*(p+i);
					*(p+i)=*(p+j);
					*(p+j)=temp;
					i++;j--;
				}
				*(int *)addr = 0;
			}
		}
	}

	return 0;
}

你可能感兴趣的:(学习,网络)