IO进程线程0806作业-洪庆乐

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

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

A进程 :

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

int main(int argc, const char *argv[])
{

	key_t key = ftok("./",1);
	if(key < 0)
	{
		perror("ftok");
		return -1;
	}
	printf("key获取成功为:%#x\n",key);

	int shmid = shmget(key,128,IPC_CREAT|0777);
	if(shmid < 0)
	{
		perror("shmget");
		return -1;
	}
	printf("共享内存创建成功,shmid为%d\n",shmid);

	void* pa = shmat(shmid,NULL,0);
	if(pa == NULL)
	{
		perror("shmat");
		return -1;
	}
	printf("共享内存连接成功 pa = %p\n",pa);

	int* ipa = (int*) pa;
	*ipa = 1314;
	ipa++;

	char* cpa = (char*)ipa;
	*cpa = 'a';

	system("ipcs -m");

	while(1)
		sleep(1);
	return 0;
}

B进程 :

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

int main(int argc, const char *argv[])
{

	key_t key = ftok("./",1);
	if(key < 0)
	{
		perror("ftok");
		return -1;
	}
	printf("key获取成功为:%#x\n",key);

	int shmid = shmget(key,128,IPC_CREAT|0777);
	if(shmid < 0)
	{
		perror("shmget");
		return -1;
	}
	printf("共享内存创建成功,shmid为%d\n",shmid);

	void* pb = shmat(shmid,NULL,0);
	if(pb == NULL)
	{
		perror("shmat");
		return -1;
	}
	printf("共享内存连接成功 pb = %p\n",pb);

	int* ipb = (int*) pb;
	printf("[%d]\n",*ipb);
	ipb++;

	char* cpb = (char*)ipb;
	printf("[%c]\n",*cpb);


	while(1)
		sleep(1);




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

使用父子进程完成

1)要求AB进程随时通信通信
1 . A进程发送一句话,B进程接收打印
2. 然后B进程发送给A进程一句话,A进程接收打印
3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;

 

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
 
 
int main(int argc, const char *argv[])
{
	umask(0);
	//创建有名管道
	if(mkfifo("./myfifo1",0777)<0)
	{
		if(errno!=EEXIST)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("myfifo1 success\n");
	//创建有名管道
	if(mkfifo("./myfifo2",0777)<0)
	{
		if(errno!=EEXIST)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("myfifo2 success\n");
 
 
 
	//已读方式打开管道文件
	int fd_r=open("./myfifo1",O_RDONLY);
	if(fd_r<0)
	{
		perror("open");
		return -1;
	}
	printf("open_rdonly success\n");
	
	//已写方式打开管道文件
	int fd_w=open("./myfifo2",O_WRONLY);
	if(fd_w<0)
	{
		perror("open");
		return -1;
	}
	printf("open_wronly success\n");
 
 
	ssize_t res;
	char arr[128];
	char arr1[128];
 
	//创建子进程
	pid_t pid=fork();
	if(pid>0)
	{
		while(1)
		{
			printf("请输入传输数据:\n");
			fgets(arr,sizeof(arr),stdin);
			arr[strlen(arr)-1]='\0';
			if(write(fd_w,arr,sizeof(arr))<0)
			{
				perror("write");
				return -1;
			}
			if(strcmp(arr,"quit")==0)
			{
				kill(pid,9);
				exit(0);
			}
		}
 
	}
	else if(0==pid)
	{
		
		while(1)
		{
			bzero(arr1,sizeof(arr1));
			res=read(fd_r,arr1,sizeof(arr1));
			if(res<0)
			{
				perror("read");
				return -1;
			}else if(0==res)
			{
				printf("对方进程退出\n");
				break;
			}
			if(strcmp(arr1,"quit")==0)
			{
				kill(getppid(),9);
				//printf("对方进程退出\n");
				exit(0);
			}
			printf("读取到对方传输的数据:%s\n",arr1);
		}
	}
	else
	{
		perror("fork");
		return -1;
	}
 
	
	//关闭文件
	close(fd_r);
	close(fd_w);
 
	return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
 
 
 
int main(int argc, const char *argv[])
{
	umask(0);
	//创建有名管道
	if(mkfifo("./myfifo1",0777)<0)
	{
		if(errno!=EEXIST)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("myfifo1 success\n");
	//创建有名管道
	if(mkfifo("./myfifo2",0777)<0)
	{
		if(errno!=EEXIST)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("myfifo2 success\n");
	
 
	//已写方式打开管道文件
	int fd_w=open("./myfifo1",O_WRONLY);
	if(fd_w<0)
	{
		perror("open");
		return -1;
	}
	printf("open_wronly success\n");
 
	//已读方式打开管道文件
	int fd_r=open("./myfifo2",O_RDONLY);
	if(fd_r<0)
	{
		perror("open");
		return -1;
	}
	printf("open_rdonly success\n");
 
	//创建线程获取读权限
 
	char arr[128]="";
	char arr1[128]="";
	ssize_t res;
 
	//创建子进程
	pid_t pid=fork();
	if(pid>0)
	{
		while(1)
		{
			printf("请输入要传输数据:\n");
			fgets(arr,sizeof(arr),stdin);
			arr[strlen(arr)-1]='\0';
			if(write(fd_w,arr,sizeof(arr))<0)
			{
				perror("write");
				return -1;
			}
			if(strcmp(arr,"quit")==0)
			{
				kill(pid,9);
				exit(0);
			}
			//printf("write success\n");
 
		}
 
	}
	if(0==pid)
	{
		while(1)
		{
			bzero(arr1,sizeof(arr1));
			res=read(fd_r,arr1,sizeof(arr1));
			if(res<0)
			{
				perror("read");
				return -1;
			}else if(0==res)
			{
				printf("对方进程退出\n");
				break;
			}
			if(strcmp(arr1,"quit")==0)
			{
				kill(getppid(),9);
				//printf("对方进程退出\n");
				exit(0);
			}
			printf("读取到对方传输的数据:%s\n",arr1);
		}
 
	}
	else
	{
		perror("fork");
		return -1;
	}
 
	
	
	//关闭文件
	close(fd_r);
	close(fd_w);
 
	return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值