IO进程——进程通信

 要求实现AB进程对话:

1)A进程发送一句话,B进程接收后打印;

2)B进程接着再发送一句话,A进程接收打印;

3)重复上述步骤,当A进程或者B进程接收到quit后退出AB进程。

 A进程代码

#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include <string.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
	umask(0);
	if(mkfifo("./fif",0777)<0)
	{
		printf("errno=%d\n",errno);
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("success\n");
	//
	int fd = open("./fif",O_WRONLY);
	if(fd<0)
	{
		perror("open");
		return -1;

	}
	printf("open success\n");
	if(mkfifo("./fi",0777)<0)
	{
		printf("errno=%d\n",errno);
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("success\n");
	//
	int fp = open("./fi",O_RDONLY);
	if(fp<0)
	{
		perror("open");
		return -1;

	}
	char buf1[128]="";
	char buf2[128]="";
	ssize_t res2 = 0;
	ssize_t res1 = 0;
	int flag=0;
	
	while(1)
	{
			printf("请输入A:");
		fgets(buf1,sizeof(buf1),stdin);
		buf1[strlen(buf1)-1]=0;

		if((res1 = write(fd,buf1,sizeof(buf1)))<0)
		{
			perror("write");
			return -1;
		}
		printf("%s\n",buf1);
		if(strcmp(buf1,"quit")==0)
		{
			break;
		}

		res2 = read(fp,buf2,sizeof(buf2));
		if(res2<0)
		{
			perror("read");
			return -1;
		}
		else if(res2==0)
		{
			printf("B进程退出\n");
			break;
		}
		printf("%s\n",buf2);

		if(strcmp(buf2,"quit")==0)
		{
			break;
		}

	}
	close(fd);
	close(fp);
	return 0;
}

B进程代码

#include <stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include <string.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
	umask(0);
	if(mkfifo("./fif",0777)<0)
	{
		printf("errno=%d\n",errno);
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("success\n");
	//
	int fd = open("./fif",O_RDONLY);
	if(fd<0)
	{
		perror("open");
		return -1;
	}
	printf("open success\n");
	if(mkfifo("./fi",0777)<0)
	{
		printf("errno=%d\n",errno);
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	printf("success\n");
	//
	int fp = open("./fi",O_WRONLY);
	if(fp<0)
	{
		perror("open");
		return -1;
	}
	printf("open success\n");
	char buf1[128]="";
	char buf2[128]="";

	ssize_t res1=0;
	ssize_t res2=0;
	int flag=0;
	while(1)
	{
		bzero(buf1,sizeof(buf1));
		res1 = read(fd,buf1,sizeof(buf1));
		if(res1<0)
		{
			perror("read");
			return -1;
		}
		else if(res1==0)
		{
			printf("A进程退出");
		}
		printf("%s\n",buf1);
		if(strcmp(buf1,"quit")==0)
		{
			break;
		}

		printf("请输入>>>");
		fgets(buf2,sizeof(buf2),stdin);
		buf2[strlen(buf2)-1]=0;
		if((res2=write(fp,buf2,sizeof(buf2)))<0)
		{
			perror("write");
			return -1;

		}

		if(strcmp(buf2,"quit")==0)
		{
			break;
		}
	}
	close(fd);
	close(fp);
	return 0;
}

 

 

功能测试


2. 创建AB进程,要求用B进程杀死A进程;  提示:有名管道+信号

 

 A进程代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<signal.h>
#include<unistd.h>
typedef void (*sighandler_t)(int);
int main(int argc, const char *argv[])
{

	umask(0);
	//创建有名管道
	if(mkfifo("./fifo1",0777)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;
		}
	}
	int fd = open("./fifo1",O_RDWR);
	if(fd<0)
	{
		perror("open");
		return -1;
	}
	printf("open success\n");
	pid_t pid1=getpid();
	ssize_t res = write(fd,&pid1,sizeof(pid_t));
	printf("%d",pid1);

	while(1)
	{
		printf("main\n");
		sleep(1);

	}
	return 0;
}

 B进程代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<signal.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
	umask(0);
	//创建有名管道
	if(mkfifo("./fifo1",0777)<0)
	{
		if(17!=errno)
		{
			perror("mkfifo");
			return -1;

		}
	}
	int fd = open("./fifo1",O_RDONLY);
	if(fd<0)
	{
		perror("open");
		return -1;
	}
	pid_t pid1=0;
	ssize_t res = 0;
	res = read(fd,&pid1,sizeof(pid_t));
	printf("%d",pid1);
	if(res<0)
	{
		perror("read");
		return -1;
	}
	kill(pid1,2);
	return 0;
}

 功能测试

 3. 用信号的方式回收僵尸进程

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<unistd.h>
#include<signal.h>
#include<sys/wait.h>
typedef void(*sighandler_t)(int);
//子进程退出后,父进程会运行该代码
void handler(int sig)
{
	pid_t cpid =0;
	while(1)
	{
		//如果成功收到僵尸进程,则再执行一次,直到回收不到或者函数运行失败
		cpid = waitpid(-1,NULL,WNOHANG);
		printf("%d\n",cpid);

		if(cpid<=0)
			break;
	}
	//while(waitpid(-1,NULL,WNOHANG)>0);
	return;
}
int main(int argc, const char *argv[])
{
	//捕获17号信号
	sighandler_t s = signal(SIGCHLD,handler);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}
	int i=0;
	pid_t pid = 0;
	//创建n个子进程
	while(i<100)
	{
		pid = fork();
		if(0==pid)
			exit(1);
		i++;

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

 功能测试

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值