进程通讯fifo

//转自陈颢文(老师)的blog

fifo1.c

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

//./a.out fifoname
int main(int argc, const char *argv[])
{
	int fd = 0;
	if(argc < 2)		
	{
		fprintf(stderr,"Usage : %s argv[1]",argv[0]);	
		exit(EXIT_FAILURE);
	}

	//创建有名管道文件
	if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
	{
		perror("Fail to create mkfifo");
		exit(EXIT_FAILURE);
	}

	//打开有名管道文件
	//fd = open(argv[1],O_RDONLY);
	fd = open(argv[1],O_WRONLY | O_NONBLOCK);
//	fd = open(argv[1],O_RDWR);
	if(fd < 0)
	{
		perror("Fail to open");	
		exit(EXIT_FAILURE);
	}

	printf("open success!\n");

	close(fd);
	return 0;
}

fifo.c

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

//./a.out fifoname
int main(int argc, const char *argv[])
{
	int fd = 0;
	if(argc < 2)		
	{
		fprintf(stderr,"Usage : %s argv[1]",argv[0]);	
		exit(EXIT_FAILURE);
	}

	//创建有名管道文件
	if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
	{
		perror("Fail to create mkfifo");
		exit(EXIT_FAILURE);
	}

	//打开管道文件
	//fd = open(argv[1],O_RDONLY);
	fd = open(argv[1],O_WRONLY);
	if(fd < 0)
	{
		perror("Fail to open");	
		exit(EXIT_FAILURE);
	}

	printf("open success!\n");

	close(fd);
	return 0;
}
fifo2.c

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

//./a.out fifoname
int main(int argc, const char *argv[])
{
	int fd = 0;
	int count = 0;
	int n = 0;
	char buf[1024] = {0};
	char *string = "hello"; 
	int i = 0;
	if(argc < 2)		
	{
		fprintf(stderr,"Usage : %s argv[1]",argv[0]);	
		exit(EXIT_FAILURE);
	}
	//创建有名管道文件
	if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
	{
		perror("Fail to create mkfifo");
		exit(EXIT_FAILURE);
	}

	//打开有名管道文件
	//fd = open(argv[1],O_RDONLY);
	//fd = open(argv[1],O_WRONLY);
	fd = open(argv[1],O_RDWR);
	if(fd < 0)
	{
		perror("Fail to open");	
		exit(EXIT_FAILURE);
	}

	printf("open success!\n");

	//把数据写到管道文件中
	write(fd,string,strlen(string));

	n = read(fd,buf,sizeof(buf));

	printf("Read %d bytes : %s\n",n,buf);
	close(fd);
	return 0;
}
==================================
read_fifo.c
#include <errno.h>
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

void do_read(int fd)
{
	char buf[1024] = {0};
	int n = 0;

	while(1)
	{
		memset(buf,0,sizeof(buf));
		n = read(fd,buf,sizeof(buf));
		if(n == 0)
			break;
		printf("read %d bytes : %s\n",n,buf);
	}
	return ;
}
//./a.out fifoname
int main(int argc, const char *argv[])
{
	int fd = 0;
	char buf[1024] = {0};
	int n = 0;
	if(argc < 2)		
	{
		fprintf(stderr,"Usage : %s argv[1]",argv[0]);	
		exit(EXIT_FAILURE);
	}
	//创建有名管道文件
	if((mkfifo(argv[1],0666)) < 0 && errno != EEXIST)
	{
		perror("Fail to create mkfifo");
		exit(EXIT_FAILURE);
	}

	//打开有名管道文件
	fd = open(argv[1],O_RDONLY);
	if(fd < 0)
	{
		perror("Fail to open");	
		exit(EXIT_FAILURE);
	}

	printf("open for read success!\n");

	do_read(fd);

	close(fd);
	return 0;
}
write_fifo.c
#include <errno.h>
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h> 

void do_write(int fd)
{
	char buf[1024] = {0};
	int n = 0;
	while(1)
	{
		printf(">");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf) - 1] = '\0';

		if(strncmp(buf,"quit",4) == 0)
			break;
		n = write(fd,buf,strlen(buf));
		printf("write %d bytes is success\n",n);
	}
	 
	return ;
}

//./a.out fifoname
int main(int argc, const char *argv[])
{
	int fd = 0;
	int count = 0;
	int n = 0;
	char buf[1024] = {0};
	if(argc < 2)		
	{
		fprintf(stderr,"Usage : %s argv[1]",argv[0]);	
		exit(EXIT_FAILURE);
	}


	//创建有名管道文件
	if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
	{
		perror("Fail to create mkfifo");
		exit(EXIT_FAILURE);
	}

	//打开有名管道文件
	fd = open(argv[1],O_WRONLY);
	if(fd < 0)
	{
		perror("Fail to open");	
		exit(EXIT_FAILURE);
	}

	printf("open for write success!\n");


	do_write(fd);
	close(fd);
	exit(EXIT_SUCCESS);
}

==============================================

talk

A.c

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

void do_read(int fd)
{
	char buf[1024] = {0};
	int n = 0;

	while(1)
	{
		memset(buf,0,sizeof(buf));
		n = read(fd,buf,sizeof(buf));
		if(n == 0)
			break;
		printf("read %d bytes : %s\n",n,buf);
	}

	exit(EXIT_SUCCESS);
}

void do_write(int fd)
{
	char buf[1024] = {0};
	int n = 0;
	while(1)
	{
		printf(">");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf) - 1] = '\0';

		if(strncmp(buf,"quit",4) == 0)
			break;
		n = write(fd,buf,strlen(buf));
		printf("write %d bytes is success\n",n);
	}
	 
	exit(EXIT_SUCCESS);
}
//./a.out fifoname
int main(int argc, const char *argv[])
{
	int fd1 = 0,fd2 = 0;
	char buf[1024] = {0};
	int n = 0;
	pid_t pid = 0;
	if(argc < 3)		
	{
		fprintf(stderr,"Usage : %s argv[1]",argv[0]);	
		exit(EXIT_FAILURE);
	}
	//创建有名管道文件
	if((mkfifo(argv[1],0666)) < 0 && errno != EEXIST)
	{
		perror("Fail to create mkfifo");
		exit(EXIT_FAILURE);
	}

	if((mkfifo(argv[2],0666)) < 0 && errno != EEXIST)
	{
		perror("Fail to create mkfifo");
		exit(EXIT_FAILURE);
	}

	pid = fork();
	if(pid < 0)
	{
		perror("Fail to fork");	
		exit(EXIT_FAILURE);
	}else if(pid > 0){
	
		//打开有名管道文件
		fd1 = open(argv[1],O_WRONLY);
		if(fd1 < 0)
		{
			perror("Fail to open");	
			exit(EXIT_FAILURE);
		}
		do_write(fd1);
		wait(NULL);
	}else if(pid == 0){
	
		fd2 = open(argv[2],O_RDONLY);
		if(fd2 < 0)
		{
			perror("Fail to open");	
			exit(EXIT_FAILURE);
		}

		do_read(fd2);
	}
	close(fd1);
	close(fd2);
	return 0;
}

b.c

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

void do_read(int fd)
{
	char buf[1024] = {0};
	int n = 0;

	while(1)
	{
		memset(buf,0,sizeof(buf));
		n = read(fd,buf,sizeof(buf));
		if(n == 0)
			break;
		printf("read %d bytes : %s\n",n,buf);
	}
	exit(EXIT_SUCCESS);
}

void do_write(int fd)
{
	char buf[1024] = {0};
	int n = 0;
	while(1)
	{
		printf(">");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf) - 1] = '\0';

		if(strncmp(buf,"quit",4) == 0)
			break;
		n = write(fd,buf,strlen(buf));
		printf("write %d bytes is success\n",n);
	}
	 
	exit(EXIT_SUCCESS);
}
//./a.out fifoname
int main(int argc, const char *argv[])
{
	int fd1 = 0,fd2 = 0;
	char buf[1024] = {0};
	int n = 0;
	pid_t pid = 0;
	if(argc < 3)		
	{
		fprintf(stderr,"Usage : %s argv[1]",argv[0]);	
		exit(EXIT_FAILURE);
	}
	//创建有名管道文件
	if((mkfifo(argv[1],0666)) < 0 && errno != EEXIST)
	{
		perror("Fail to create mkfifo");
		exit(EXIT_FAILURE);
	}

	if((mkfifo(argv[2],0666)) < 0 && errno != EEXIST)
	{
		perror("Fail to create mkfifo");
		exit(EXIT_FAILURE);
	}

	pid = fork();
	if(pid < 0)
	{
		perror("Fail to fork");	
		exit(EXIT_FAILURE);
	}else if(pid > 0){
	
		//打开有名管道文件
		fd1 = open(argv[2],O_WRONLY);
		if(fd1 < 0)
		{
			perror("Fail to open");	
			exit(EXIT_FAILURE);
		}

		do_write(fd1);
		wait(NULL);
	}else if(pid == 0){
	
		fd2 = open(argv[1],O_RDONLY);
		if(fd2 < 0)
		{
			perror("Fail to open");	
			exit(EXIT_FAILURE);
		}

		do_read(fd2);
	}

	close(fd1);
	close(fd2);
	return 0;
}
test.c

#include <stdio.h>

int main(int argc, const char *argv[])
{
	int *p = NULL;

	*p = 10;
	printf("*p : %d\n",*p);
	return 0;
}

hello.c

#include <stdio.h>

int main(int argc, const char *argv[])
{
	printf("hello world\n");	
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值