【Linux】文件和管道(1)

一、文件

1.文件权限:

下图文件权限755(rwx   r-x   r-x )

【Linux】文件和管道(1)_第1张图片

设置文件权限命令:chmod

字符方式:chmod g+w 文件名 (为同组添加写权限)

数组方式:chmod 644 文件名

2.linux文件类型:

  • 普通文件:-
  • 目录:d
  • 软连接:l  (等效window的快捷方式)
  • 块设备文件:b
  • 字符设备文件:c
  • 管道文件:p(fifo)

D进程发给A进程使用getpgid()获取当前组进程(主进程)

二、管道

1.什么是管道

是一种进程间通信的方式,把从一个进程连接到另一个进程的一个数据流

他是一种单向的通信机制,数据只能读或者只能写(一个管道只能负责一个读或者写)

2.管道类型:

  • 匿名管道(PIPE):通常是在亲缘进程通常中使用(父子、兄弟)
  • 命名管道(FIFO):可以在任意进程的通信中使用
匿名管道:

int pipe(int file_descriptor[2]);

参数:

        file_descriptor:文件描述符数组

        file_descriptor[0]表示读端

        file_descriptor[1]表示写端

#include 
#include 
#include 
using namespace std;

//主进程读,子进程写
int main()
{
	int fds[2] = { 0 };
	pid_t pid = 0;
	int res = pipe(fds);
	char w_data[100] = "hello";
	char r_data[100] = { 0 };
	if (res < 0)
	{
		perror("pipe error");
		return 0;
	}
	pid = fork();
	if (pid > 0)//
	{
		close(fds[0]);//关闭读端
		write(fds[1], w_data, sizeof(w_data));
		while (1)
		{
		}
	}
	else if(pid==0)
	{
		close(fds[1]);//关闭写端
		read(fds[0], r_data, sizeof(r_data));
		cout << r_data << endl;
	}
	return 0;
}
命名管道(FIFO文件):

与文件相同,openclose函数来打开或关闭

注意程序不能以O_RDWR模式打开FIFO文件进行读写(只能单向进行一个操作)

一个写进程,一个读进程,必须都打开,不然会造成阻塞,还要注意打开顺序

int mkfifo(const char *filename,mode_t mode);

参数:

filename:文件路径

mode:权限

管道写入的内容不会存在磁盘中,而是在一个虚拟的中转站,文件里面没有数据

例题:模拟两个客户端,一个写,一个读

write:

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
	umask(0);//但凡创建文件权限要加这个
	//判断文件是否存在
	int res = access("/root/A2B.fifo", F_OK);
	if (res < 0)//文件不存在
	{
		//创建命名管道(管道文件一定要这个创建)
		res = mkfifo("/root/A2B.fifo", 0777);
		if (res < 0)
		{
			perror("mkfifo error");
			return 0;
		}
	}

	res = access("/root/B2A.fifo", F_OK);
	if (res < 0)//文件不存在
	{
		//创建命名管道(管道文件一定要这个创建)
		res = mkfifo("/root/B2A.fifo", 0777);
		if (res < 0)
		{
			perror("mkfifo error");
			return 0;
		}
	}


	//连接管道(打开管道文件),管道文件不使用O_RDER方式来打开(因为管道是单向的)
	int rfd = open("/root/B2A.fifo", O_RDONLY);
	int wfd = open("/root/A2B.fifo", O_WRONLY);//以写方式打开(管道必须打通读写通道才会完成管道的建立)
	cout << "管道建立成功" << endl;
	int pid = fork();
	if (pid < 0)
	{
		perror("fork error");
	}
	//cout << "用户A:" << endl;
	while (1)
	{
		char send[100] = { 0 };
		char recv[100] = { 0 };
		if (pid > 0)
		{
		//	cout << "用户A:" << endl;
			cin >> send;
			if (sizeof(send)!=0)
			{
				write(wfd, send, sizeof(send));
				cout << "用户A:" <

read:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main()
{

	umask(0);//但凡创建文件权限要加这个
	//判断文件是否存在
	int res = access("/root/A2B.fifo", F_OK);
	if (res < 0)//文件不存在
	{
		//创建命名管道(管道文件一定要这个创建)
		res = mkfifo("/root/A2B.fifo", 0777);
		if (res < 0)
		{
			perror("mkfifo error");
			return 0;
		}
	}

	res = access("/root/B2A.fifo", F_OK);
	if (res < 0)//文件不存在
	{
		//创建命名管道(管道文件一定要这个创建)
		res = mkfifo("/root/B2A.fifo", 0777);
		if (res < 0)
		{
			perror("mkfifo error");
			return 0;
		}
	}


	//连接管道(打开管道文件),管道文件不使用O_RDER方式来打开(因为管道是单向的)

	int wfd = open("/root/B2A.fifo", O_WRONLY);//以写方式打开(管道必须打通读写通道才会完成管道的建立)
	int rfd = open("/root/A2B.fifo", O_RDONLY);
	cout << "管道建立成功" << endl;
	int pid = fork();
	if (pid < 0)
	{
		perror("fork error");
	}
	//cout << "用户B:" << endl;
	while (1)
	{
		//char send[100] = "B2A:got it";
		char send[100] = { 0 };
		char recv[100] = { 0 };
		
		if (pid > 0)
		{
			read(rfd, recv, sizeof(recv));
			cout << "用户A:" << recv << endl;
		}
		else if (pid == 0)
		{
			//cout << "用户B:" << endl;
			cin >> send;
			if (sizeof(send) != 0)
			{
				write(wfd, send, sizeof(send));
				cout << "用户B:" <

你可能感兴趣的:(Linux与虚拟机,linux,运维,服务器,C++)