IO 用消息队列实现AB进程通话 用共享内存实现倒置打印字符串

目录

1.要求用消息队列实现AB进程对话:1)A进程发送一句话,B进程接收后打印;

2) B进程接着再发送一句话,A进程接收打印;3)重复上述步骤,当A进程或者B进程接收到quit后退出AB进程。

p1.c

1.c

2. 创建两个进程A、B,以及一个共享内存,共享内存中存储char str="123456",在不考虑进程退出的情况下,要求如下:1.A进程循环打印str字符串。2.B进程循环倒置str字符串,不使用辅助数组。注意是循环倒置,要把字符串倒过来,倒回去。3.要求A进程打印出来的结果是有序的,例如: "123456"或者654321",不允许出现"623451".,等无序情况提示:将flag + str一起写到共享内存中,当flag=0,打印当flag=1 ,倒置

p1.c

1.c


1.要求用消息队列实现AB进程对话:
1)A进程发送一句话,B进程接收后打印;

2) B进程接着再发送一句话,A进程接收打印;
3)重复上述步骤,当A进程或者B进程接收到quit后退出AB进程。


p1.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#define MSG_EXCEPT 020000

struct msgbuf{
	long mtype;
	char mtext[64];
};

int main(int argc, const char *argv[])
{
	//创建key值
	key_t key = ftok("./",1);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	//创建消息队列,并获取消息队列id号
	int msgid = msgget(key,IPC_CREAT | 0666);
	if(msgid<0)
	{
		perror("msgget");
		return -1;
	}

	//往消息队列里发送信息包
	struct msgbuf mqbuf;
	ssize_t res = 0;
	pid_t pid = fork();
	if(pid>0){
		while(1)
		{
			memset(&mqbuf,0,sizeof(mqbuf));
			//printf("请输入消息类型:");
			scanf(" %ld",&mqbuf.mtype);
			while(getchar()!=10);
			if(mqbuf.mtype == 0)
			{
				break;
			}
			printf("请输入消息内容:");
			fgets(mqbuf.mtext,sizeof(mqbuf.mtext),stdin);
			mqbuf.mtext[strlen(mqbuf.mtext)-1] = '\0';
			if(msgsnd(msgid,&mqbuf,sizeof(mqbuf.mtext),0)<0)
			{
				perror("msgsend");
				return -1;
			}
			if(strcmp(mqbuf.mtext,"quit")==0)
			{
				kill(pid,9);
				exit(1);
			}
			printf("--------------------\n");
		}
	}else{
		while(1){
			//读取消息队列里的信息
			memset(&mqbuf,0,sizeof(mqbuf));
			msgrcv(msgid,&mqbuf,sizeof(mqbuf.mtext),2,0);
			if(strcmp(mqbuf.mtext,"quit")==0)
			{
				kill(getppid(),9);
				exit(1);
			}
			printf("mtype:%ld mtext:%s\n",mqbuf.mtype,mqbuf.mtext);
		}
	}
	return 0;
}

1.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#define MSG_EXCEPT 020000

struct msgbuf{
	long mtype;
	char mtext[64];
};

int main(int argc, const char *argv[])
{
	//创建key值
	key_t key = ftok("./",1);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	//创建消息队列,并获取消息队列id号
	int msgid = msgget(key,IPC_CREAT | 0666);
	if(msgid<0)
	{
		perror("msgget");
		return -1;
	}

	//往消息队列里发送信息包
	struct msgbuf mqbuf;
	ssize_t res = 0;
	pid_t pid = fork();
	if(pid==0){
		while(1)
		{
			memset(&mqbuf,0,sizeof(mqbuf));
			//printf("请输入消息类型:");
			scanf(" %ld",&mqbuf.mtype);
			while(getchar()!=10);
			if(mqbuf.mtype == 0)
			{
				break;
			}
			printf("请输入消息内容:");
			fgets(mqbuf.mtext,sizeof(mqbuf.mtext),stdin);
			mqbuf.mtext[strlen(mqbuf.mtext)-1] = '\0';
			if(msgsnd(msgid,&mqbuf,sizeof(mqbuf.mtext),0)<0)
			{
				perror("msgsend");
				return -1;
			}
			if(strcmp(mqbuf.mtext,"quit")==0)
			{
				kill(getppid(),9);
				exit(1);
			}
			printf("--------------------\n");
		}
	}else{
		while(1){
			//读取消息队列的信息
			memset(&mqbuf,0,sizeof(mqbuf));
			msgrcv(msgid,&mqbuf,sizeof(mqbuf.mtext),1,0);
			if(strcmp(mqbuf.mtext,"quit")==0)
			{
				kill(pid,9);
				exit(1);
			}
			printf("mtype:%ld mtext:%s\n",mqbuf.mtype,mqbuf.mtext);
		}
	}
	return 0;
}

2. 创建两个进程A、B,以及一个共享内存,共享内存中存储char str="123456",在不考虑进程退出的情况下,要求如下:
1.A进程循环打印str字符串。
2.B进程循环倒置str字符串,不使用辅助数组。注意是循环倒置,要把字符串倒过来,倒回去。
3.要求A进程打印出来的结果是有序的,例如: "123456"或者654321",不允许出现"623451".,等无序情况
提示:将flag + str一起写到共享内存中,当flag=0,打印当flag=1 ,倒置

p1.c

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.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;
	}

	//创新共享内存,并获取id
	int shmid = shmget(key,32,IPC_CREAT|0666);
	if(shmid<0)
	{
		perror("shmget");
		return -1;
	}

	//映射到当前进程上
	void* shmaddr = shmat(shmid,NULL,0);
	if(shmaddr==(void*)-1)
	{
		perror("shmat");
		return -1;
	}

	while(1)
	{
		if(*(int*)shmaddr==1)
		{
			char* star = (char*)shmaddr+4;
			char* end = (char*)shmaddr+9;
			while(end>star)
			{
				*star ^= *end;
				*end ^= *star;
				*star ^= *end;
				star++;
				end--;
			}		
			*(int*)shmaddr = 0;
		}
	}

	return 0;
}

1.c

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

int main(int argc, const char *argv[])
{
	//创建key值
	key_t key = ftok("./",'a');
	if(key<0)
	{
		perror("ftok");
		return -1;
	}

	//创新共享内存,并获取id
	int shmid = shmget(key,32,IPC_CREAT|0666);
	if(shmid<0)
	{
		perror("shmget");
		return -1;
	}

	//映射到当前进程上
	void* shmaddr = shmat(shmid,NULL,0);
	if(shmaddr==(void*)-1)
	{
		perror("shmat");
		return -1;
	}
	
	while(1)
	{
		if(*(int*)shmaddr==0)
		{
		printf("%s\n",(char*)shmaddr+4);
		*(int*)shmaddr = 1; 
		}
		sleep(1);
	}


	system("ipcs -m");	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值