Linux进程间通信-fifo测试

1:fifo原因

有名管道

文件:安全性,不自动化,数据不完整没有保障;锁的机制

PHP开发网站的时候,登录,session


2:测试

mkfifo 

\\192.168.0.155\ncc\fifo_write.c
\\192.168.0.155\ncc\fifo_read.c


其中:

fifo_write.c

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

// 打印错误函数
void print_system_err(char *str,int err_no){
	perror(str);
	exit(err_no);
}

int main(int argc, char* argv[]){
	int fd;
	char buf[1024]="niexiaoqian ok";
	if (argc < 2)
	{
		printf("丢失fifo\n");
		exit(1);
	}

	fd=open(argv[1],O_WRONLY); //只写方式
	if (fd < 0)
	{
		print_system_err("open failed",1);
	}

	// 往管道里面写
	write(fd,buf,strlen(buf));
	close(fd);

	return 0;
}


fifo_read.c

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

// 打印错误函数
void print_system_err(char *str,int err_no){
	perror(str);
	exit(err_no);
}

int main(int argc, char* argv[]){
	int length;
	int fd;
	char buf[1024];
	if (argc < 2)
	{
		printf("丢失fifo\n");
		exit(1);
	}

	fd=open(argv[1],O_RDONLY); //只读方式
	if (fd < 0)
	{
		print_system_err("open failed",1);
	}

	// 从管道里面读
	length=read(fd,buf,sizeof(buf));
	// 打印到标准输出
	write(STDOUT_FILENO,buf,length);
	close(fd);

	return 0;
}


测试效果

1

另外一个客户端:

2


更详细的视频下载地址:

https://pan.baidu.com/s/1dilZny9gd7DPWGu1OS4Wpw#list/path=%2F

猜你喜欢

转载自blog.csdn.net/guyingping/article/details/80369291