20240108(作业)进程间有名管道通信

本文详细介绍了三个C语言程序,展示了如何使用FIFO(无名管道)进行进程间通信,包括创建FIFO文件、主进程向管道写入数据、子进程从管道读取数据并输出。
摘要由CSDN通过智能技术生成

代码一:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, const char *argv[])
{
	//create a FIFO special file
	if(argc<2){
		printf("please enter 2 arguments\n");
		printf("usage:./a.out  fileName\n");
		return -1;
	}

	int fifo;
	if((fifo=mkfifo(argv[1],0664))==-1){
		perror("mkfifo error");
		return -1;
	}

	return 0;
}

代码二:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>


int main(int argc, const char *argv[])
{
	if(argc<3){
		printf("please enter no less than 3 arguments\n");
		printf("usage:./a.out writeToFile  readFromFile\n");
		return -1;
	}



	pid_t pid;
	if((pid=fork())==-1){
		perror("process 1 fork error");
		return -1;
	}

	//process 01 main process write info to pipe 01
	if(pid>0){
		int fd;
		if((fd=open(argv[1],O_WRONLY))==-1){
			perror("open fifo file 01 error");
			exit(EXIT_FAILURE);
		}

		char buf[128]="";
		
		while(strcmp(buf,"quit")!=0){
			int res=read(0,buf,sizeof(buf));
			buf[strlen(buf)-1]='\0';
			write(fd,buf,res);
		}
	
		close(fd);

		wait(NULL);		
		exit(EXIT_SUCCESS);
	}

	//process 01 new process read info from pipe 02
	if(pid==0){
		int fd;
		if((fd=open(argv[2],O_RDONLY))==-1){
			perror("open fifo file 02 error");
			exit(EXIT_FAILURE);
		}

		char buf[128]="";
		
		while(strcmp(buf,"quit")!=0){
			int res=read(fd,buf,sizeof(buf));
			buf[strlen(buf)-1]='\0';
			printf(" process 01 new process read info from pipe 02---output\n");
			write(1,buf,res);
		}
	
		close(fd);
	
		exit(EXIT_SUCCESS);
	}
	return 0;
}

代码三:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>


int main(int argc, const char *argv[])
{
	if(argc<3){
		printf("please enter no less than 3 arguments\n");
		printf("usage:./a.out writeToFile  readFromFile\n");
		return -1;
	}



	pid_t pid;
	if((pid=fork())==-1){
		perror("process 1 fork error");
		return -1;
	}

	//process 01 main process write info to pipe 01
	if(pid>0){
		int fd;
		if((fd=open(argv[1],O_WRONLY))==-1){
			perror("open fifo file 01 error");
			exit(EXIT_FAILURE);
		}

		char buf[128]="";
		
		while(strcmp(buf,"quit")!=0){
			int res=read(0,buf,sizeof(buf));
			buf[strlen(buf)-1]='\0';
			write(fd,buf,res);
		}
	
		close(fd);

		wait(NULL);		
		exit(EXIT_SUCCESS);
	}

	//process 01 new process read info from pipe 02
	if(pid==0){
		int fd;
		if((fd=open(argv[2],O_RDONLY))==-1){
			perror("open fifo file 02 error");
			exit(EXIT_FAILURE);
		}

		char buf[128]="";
		
		while(strcmp(buf,"quit")!=0){
			int res=read(fd,buf,sizeof(buf));
			buf[strlen(buf)-1]='\0';
			printf(" process 01 new process read info from pipe 02---output\n");
			write(1,buf,res);
		}
	
		close(fd);
	
		exit(EXIT_SUCCESS);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值