IO day 9

#include 
#include 
#include 
#include
#include
#include
struct msgbuf
{
	long mtype;
	char mtext[128];
};
struct msgbuf1
{
	long mtype1;
	char mtext1[128];
};
int main(int argc, const char *argv[])
{
	key_t key=ftok("/home/ubuntu/",1);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	printf("key=%#x\n",key);
	//创建消息队列
	int msqid=msgget(key,IPC_CREAT|0664);
	if(msqid<0)
	{
		perror("msgget");
		return -1;
	}
	struct msgbuf sndbuf;
	struct msgbuf1 sndbuf1;
	ssize_t size;
	while(1)
	{
		printf("输入消息类型\n");
		scanf("%ld",&sndbuf.mtype);
		getchar();
		if(0==sndbuf.mtype)
		{
			break;
		}
		printf("输入消息内容\n");
		fgets(sndbuf.mtext,sizeof(sndbuf.mtext),stdin);
		sndbuf.mtext[strlen(sndbuf.mtext)-1]=0;
		if(msgsnd(msqid,&sndbuf,sizeof(sndbuf.mtext),0)<0) 
		{
			perror("msgsnd");
			return -1;
		}
		if(strcmp(sndbuf.mtext,"quit")==0)
		{
			break;
		}

		printf("发送成功\n");
		//接收函数
		size=msgrcv(msqid,&sndbuf1,sizeof(sndbuf1.mtext1),0,0);
		if(size<0)
		{
			perror("msgrcv");
			return -1;
		}
		if(strcmp(sndbuf1.mtext1,"quit")==0)
		{
			break;
		}
		printf("size=%ld mtype1=%ld mtext1=%s\n",size,sndbuf1.mtype1,sndbuf1.mtext1);
		system("ipcs -q");//让c代码执行shell指令
	}
	printf("msqid=%d\n",msqid);
	return 0;
}
#include 
#include 
#include 
#include
#include
#include
struct msgbuf
{
	long mtype;
	char mtext[128];
};
struct msgbuf1
{
	long mtype1;
	char mtext1[128];
};
int main(int argc, const char *argv[])
{
	key_t key=ftok("/home/ubuntu/",1);
	if(key<0)
	{ 
		perror("ftok");
		return -1;
	}
	printf("key=%#x\n",key);
	//创建消息队列
	int msqid=msgget(key,IPC_CREAT|0664);
	if(msqid<0)
	{
		perror("msgget");
		return -1;
	}
	struct msgbuf sndbuf;
	struct msgbuf1 sndbuf1;
	ssize_t size;
	while(1)
	{		//从指定的消息列表中读取数据
		//读取消息列队中的第一条消息 先进先出
		printf("%d\n",1);
		size=msgrcv(msqid,&sndbuf,sizeof(sndbuf.mtext),0,0);
		if(strcmp(sndbuf.mtext,"quit")==0)
		{
			break;
		}
		printf("%d\n",1);
		if(size<0)
		{
			perror("msgrcv");
			return -1;

		}
		printf("size=%ld mtype=%ld mtext=%s\n",size,sndbuf.mtype,sndbuf.mtext);
		//输入消息

		printf("输入消息类型\n");
		scanf("%ld",&sndbuf1.mtype1);
		getchar();
		if(0==sndbuf1.mtype1)
		{
			break;
		}
		printf("输入消息内容\n");
		fgets(sndbuf1.mtext1,sizeof(sndbuf1.mtext1),stdin);
		sndbuf1.mtext1[strlen(sndbuf1.mtext1)-1]=0;
		if(msgsnd(msqid,&sndbuf1,sizeof(sndbuf1.mtext1),0)<0)
		{
			perror("msgsnd");
			return -1;
		}
		if(strcmp(sndbuf1.mtext1,"quit")==0)
		{
			break;
		}
		printf("发送成功\n");
		system("ipcs -q");//让c代码执行shell指令

	}
	printf("msqid=%d\n",msqid);
	return 0;
}

要求用消息队列实现AB进程对话

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

B进程再回复一句话给A进程,A进程接收后打印

重复1.2步骤,当收到quit后,要结束AB进程

#include 
#include 
#include 
#include
#include
#include
#include
char *p=NULL;
int main(int argc, const char *argv[])
{
	key_t key=ftok("./",10);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	printf("key=%#x\n",key);
	//创建共享内存 获得shmin号
	int shmid=shmget(key,32,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;
	}
	int i,j;
	char t;
	int flag=0;

	pid_t pid;
	pid=fork();
	if(pid<0)
	{
		perror("fork");
		return -1;
	}
	printf("创建成功\n");
	char arr[]="1234567";
	strcpy((char*)addr+4,arr);
	p=(char*)addr+4;

	*(int *)addr=0;
	while(1)
	{
		if(pid>0)
		{
			if(*(int *)addr==0)
			{
				printf("%s\n",p);
				*(int *)addr=1;
			}
		}
		else if(pid==0)
		{	
			if(*(int *)addr==1) 
			{	
				for(i=0;*(p+i)!='\0';i++); 
				i=i-1;
				j=0;
				while(i>j)
				{
					t=*(p+i);
					*(p+i)=*(p+j);
					*(p+j)=t;
					j++;i--;
				}
				*(int *)addr=0;
			}
		}
	}
	system("ipcs -m");
	return 0;
}

IO day 9_第1张图片

 

 

你可能感兴趣的:(前端)