Linux无名管道pipe()与有名管道mkfifo()笔记

​​​​​​​​​​​​ps: 还是学生,初次写博客,太菜了,不好意思

int pipe(int pipefd[2]) 
pipefd

一个至少具有2int型的数组,用来存读/写描述符

成功返回0

失败返回-1

 

功能创建有名管道:FIFO
头文件

#include <sys/types.h>

#include <sys/stat.h>

原型int mkfifo(const char *pathname,mode_t mode
参数

 

 

 

pathname :FIFO的文件名

mode : 权限

返回值成功 0

失败 -1

 

 

 

 

 

 

 

 

 

 

  • 父进程与子进程采用无名管道进行通信

#include <stdio.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/wait.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <linux/input.h>


int main(int argc, char const *argv[])
{
	int fb[2],fb1[2];

	int ret = pipe(fb);
	int ret1 = pipe(fb1);
	if (ret == -1)
	{
		perror("pipe()");
		return 0;
	}

	pid_t pid = fork();

	if (pid == -1)
	{
		perror("foek()");
		return 0;
	}
	if (pid == 0)
	{
		int i = 0;
	
		char pri[100];
		char pri1[100];
		int flg = 1;

		while(1)
		{
			bzero(pri1,100);
			bzero(pri,100);

			read(fb1[0],pri1,sizeof(pri1));
			printf("父亲来话:%s\n",pri1);
			
			printf("发送会发给父亲:");
			fgets(pri,100,stdin);
			write(fb[1],pri,strlen(pri));
		}
		
	}

	if(pid >0)
	{

		char pri[100];
		char pri1[100];
		int flg =  1;

		while(1)
		{
			bzero(pri1,100);
			bzero(pri,100);

			printf("发送会发给儿子:");
			fgets(pri1,100,stdin);
			write(fb1[1],pri1,strlen(pri1));

			read(fb[0],pri,sizeof(pri));
			printf("儿子来话: %s\n",pri);
		}
	}

	return 0;
}
  • 两个程序采用有名管道通信 分别运行两个程序

  • #include <stdio.h>
    #include <sys/mman.h>
    #include <errno.h>
    #include <unistd.h>
    #include <string.h>
    #include <strings.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <fcntl.h>
    #include <linux/input.h>
    #define FIFO "/tmp/fifo_test"
    #define FIFO1 "/tmp/fifo_test1"
    
    int main(int argc, char const *argv[])
    {
    	if (access(FIFO,F_OK))
    	{
    		int ret = mkfifo(FIFO,0666);
    		if (ret = -1)
    		{
    			perror("[1] : mkfifo fail");
    			return 0;
    		}
    	}
    	if (access(FIFO1,F_OK))
    	{
    		int ret1 = mkfifo(FIFO1,0666);
    		if (ret1 = -1)
    		{
    			perror("[2] :mkfifo fail");
    			return 0;
    		}
    	}
    
    	int fd1 = open(FIFO1,O_RDONLY);
    	int fd = open(FIFO,O_WRONLY);
    
    	char buf[100];
    	char buf1[100];
    
    
    	pid_t par_pid = getpid();
    	pid_t pid = fork();
    	if (pid == -1)
    	{
    		perror("fork() fail: ");
    		return 0;
    	}
    	if (pid == 0)
    	{
    		while(1)
    		{	
    			
    			bzero(buf,100);
    			bzero(buf1,100);
    			read(fd1,buf1,sizeof(buf1));
    			if (strncmp(buf1,"exit",4) == 0)
    			{
    				kill(par_pid,9);
    				exit(3);
    			}
    			printf("from to two: %s",buf1);
    		}
    	}
    	if (pid > 0)
    	{
    		pid_t stat;
    		
    		while(1)
    		{	
    			waitpid(-1,&stat,WNOHANG);
    			if (WIFEXITED(stat))
    			{
    				return 0;
    			}
    
    			bzero(buf,100);
    			bzero(buf1,100);
    
    			fgets(buf,100,stdin);
    			write(fd,buf,strlen(buf));
    
    			usleep(1000);
    			if (strncmp(buf,"exit",4) == 0)
    			{
    				exit(4);
    			}		
    		}
    	}
    	
    	return 0;
    }
    #include <stdio.h>
    #include <sys/mman.h>
    #include <errno.h>
    #include <unistd.h>
    #include <string.h>
    #include <strings.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <fcntl.h>
    #include <linux/input.h>
    #define FIFO "/tmp/fifo_test"
    #define FIFO1 "/tmp/fifo_test1"
    
    int main(int argc, char const *argv[])
    {
    	if (access(FIFO,F_OK))
    	{
    		int ret = mkfifo(FIFO,0666);
    		if (ret = -1)
    		{
    			perror("[1]: mkfifo fail");
    			return 0;
    		}
    	}
    	if (access(FIFO1,F_OK))
    	{
    		int ret1 = mkfifo(FIFO1,0666);
    		if (ret1 = -1)
    		{
    			perror("[2]: mkfifo fail");
    			return 0;
    		}
    	}
    
    	int fd1 = open(FIFO1,O_WRONLY);
    	int fd = open(FIFO,O_RDONLY);
    	char buf[100];
    	char buf1[100];
    
    	pid_t pid = fork();
    	if (pid == -1)
    	{
    		perror("fork() fail: ");
    		return 0;
    	}
    	if (pid > 0)
    	{
    		while(1)
    		{
    			bzero(buf,100);
    			char buf1[100];
    			read(fd,buf,sizeof(buf));
    			usleep(1000);
    			if (strncmp(buf,"exit",4) == 0)
    			{
    				exit(2);
    			}
    			printf("from to one: %s",buf);
    
    		}	
    	}
    	if (pid == 0)
    	{
    		while(1)
    		{	
    			
    			
    			bzero(buf,100);
    			char buf1[100];
    
    			fgets(buf1,100,stdin);
    			write(fd1,buf1,strlen(buf1));	
    			if (strncmp(buf1,"exit",4) == 0)
    			{
    				exit(1);
    			}
    		}
    	}
    
    	return 0;
    }

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值