A,B进程共享内存,A,B进程随时收发

 

 1.A进程写入一个整型,在该整型后,写入一个字符串

2.B进程将共享内存中的整型以及字符串读取出来

(注,调用的函数都未加判断是否执行成功)

A进程代码:

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

int main(int argc, const char *argv[])
{
	//创建键值
	key_t key = ftok("./", 1);
	//使用该键值创建共享内存空间
	int shmid = shmget(key, 100, IPC_CREAT|0664);
	//将共享内存空间映射到进程中,获取该空间首地址
	void* p = shmat(shmid, NULL , 0);
	//将指针强转成int*类型写入整型数据
	int* q = (int*)p;
	printf("请输入整型数据--->");
	scanf("%d",q);
	getchar();
	//将整形后的第一个地址强转成char*类型写入字符串
	char* s = (char*)(q+1);
	char str[50]=""; //存手动输入的字符串
	printf("请输入字符串--->");
	fgets(str, sizeof(str), stdin);//终端获取字符串
	str[strlen(str)-1] = '\0';
	strcpy(s, str);
	return 0;
}

B进程代码: 

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

int main(int argc, const char *argv[])
{
	//拿到同一片共享内存
	key_t key = ftok("./", 1);
	int shmid = shmget(key, 100, IPC_CREAT|0664);
	void* p = shmat(shmid, NULL , 0);
	//将万能指针p强转成int类型读出数据
	int* q = (int*)p;
	printf("整数是:%d\n", *q);
	//将整形后的第一个地址强转成char*类型读出字符串
	char* s = (char*)(q+1);
	printf("字符串是:%s\n",s);
	return 0;
}

测试结果

要求AB进程做通信

  1. A进程发送一句话,B进程接收打印

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

  3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;

  4. AB进程能够随时收发数据(附加题)

 A进程代码(发送没有文字提示,直接输入即是发送)

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void* send_msg(void* arg)
{
	//只写打开管道文件1
	int op1 = open("./myfifo1", O_WRONLY);
	if(op1 < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}
	while(1)
	{
		//发送信息
		char send[128];
		bzero(send, 128);
		fgets(send, 128, stdin);
		send[strlen(send)-1] = '\0';
		if(strcmp(send, "quit")==0)
		{
			write(op1, send, 128);//告诉B结束进程,然后自己结束进程
			exit(1);
		}
		write(op1, send, 128);
	}
	close(op1);
}
void* accept_msg(void* arg)
{
	//只读打开管道文件2
	int op2 = open("./myfifo2", O_RDONLY);
	if(op2 < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}
	while(1)
	{

		//接收信息
		char accept[128];
		int res = 0;
		bzero(accept, 128);
		res = read(op2, accept, 128);
		if(strcmp(accept, "quit")==0)
		{
			exit(1); //结束进程
		}
		printf("A接收到的信息--->%s\n",accept);
	}
	close(op2);
}

int main(int argc, const char *argv[])
{
	umask(0);//文件掩码
	//创建用于发送信息的管道文件
	if(mkfifo("./myfifo1", 0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//创建用于接收信息的管道文件
	if(mkfifo("./myfifo2", 0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}

	//创建一个线程发送数据
	pthread_t tid1;
	if(pthread_create(&tid1, NULL,send_msg, NULL)<0)
	{
		perror("pthread_create");
		return -1;
	}

	//创建一个线程接收数据
	pthread_t tid2;
	if(pthread_create(&tid2, NULL,accept_msg, NULL)<0)
	{
		perror("pthread_create");
		return -1;
	}
	printf("准备就绪\n");
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	return 0;
}

B进程代码(发送没有文字提示,直接输入即是发送)

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void* accept_msg(void* arg)
{
	//只读打开管道文件1
	int op1 = open("./myfifo1", O_RDONLY);
	if(op1 < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}
	while(1)
	{

		//接收信息
		char accept[128];
		int res = 0;
		bzero(accept, 128);
		res = read(op1, accept, 128);
		if(strcmp(accept, "quit")==0)
		{
			exit(1);
		}
		printf("B接收到的信息--->%s\n",accept);
	}
	close(op1);
}

void* send_msg(void* arg)
{
	//只写打开管道文件2
	int op2 = open("./myfifo2", O_WRONLY);
	if(op2 < 0)
	{
		perror("open");
		pthread_exit(NULL);
	}
	while(1)
	{
		//发送信息
		char send[128];
		bzero(send, 128);
		fgets(send, 128, stdin);
		send[strlen(send)-1] = '\0';
		if(strcmp(send, "quit")==0)
		{
			write(op2, send, 128);//告诉A结束进程,然后自己结束进程
			exit(1);
		}
		write(op2, send, 128);
	}
	close(op2);
}

int main(int argc, const char *argv[])
{
	umask(0);//文件掩码
	//创建用于发送信息的管道文件
	if(mkfifo("./myfifo1", 0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//创建用于接收信息的管道文件
	if(mkfifo("./myfifo2", 0777) < 0)
	{
		if(errno != 17)
		{
			perror("mkfifo");
			return -1;
		}
	}
	//创建一个线程接收数据
	pthread_t tid2;
	if(pthread_create(&tid2, NULL,accept_msg, NULL)<0)
	{
		perror("pthread_create");
		return -1;
	}

	//创建一个线程发送数据
	pthread_t tid1;
	if(pthread_create(&tid1, NULL,send_msg, NULL)<0)
	{
		perror("pthread_create");
		return -1;
	}
	printf("准备就绪\n");
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	return 0;
}

测试结果

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傾语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值