Operating System Experiment 5

16281004- Hong Huaxing - Experiment 5 operating system file system

About experiments

This experiment sought to develop a simple file system on top of the analog I / O system. By user create, open, read commands to interact with the file system. The disk file system is considered a logical block sequence number order, the logical block number from 0 to L - 1. I / O system utilizes an array of analog memory disk.

I / O-based unified

The actual physical structure of the disk is multidimensional: the concept of cylinder, head, sector and so on. Task I / O system is to hide the details of the structure of the disk, the disk in order to face the logical block presented to the file system. Logical block sequence number, numbered in the range of 0 to L-1, where L represents the total number of memory disks. In the experiment, we can use an array ldisk [C] [H] [ B] Construction disk model, wherein each CHB indicates the cylinder number, head number and sector number. Each sector size is 512 bytes. I / O system receives commands from the file system, according to the logical block number specified by the command to read the contents of the disk block command specified memory area or the memory area designated by the command to write the contents of a disk block. Between the file system and I / O system interface is defined by the following two functions:
• the Read_blocks (int i, char * p);
This function resets the contents of the logical block i is read into memory location pointed to by pointer p, copy character length number memory block B.
• write block (int i, char * p);
the number of characters the contents of the function pointer p pointing to write logic block i, the copy length of the memory block B. In addition, in order to facilitate testing, we also need to implement two additional functions: one for the array ldisk to a file; the other is used to restore the contents of the file into an array.

File system

File system is located on the I / O system.

The interface between the user and the file system

The file system must provide the following functions; create, destroy, open, read , write.
• create (filename): Create a new file with the specified file name.
• destroy (filename): delete the specified file.
• open (filename): open the file. The function returns the index number can be used for the subsequent read, write, lseek, or close operation.
• close (index): Close to develop file.
• read (index, mem_area, count ): order from the specified file to read count bytes memarea specified memory location. Read operation starting from the position indicated by the file read and write pointers.
• write (index, mem_area, count ): writes the specified file count byte order starting memarea specified memory location. Write operation starts from the location indicated by file read and write pointers.
• lseek (index, pos): the file read and write pointers to move to the specified position pos. pos is an integer representing an offset from the beginning of the file. When a file is opened, read and write pointers are automatically set to 0. After each read and write operation, it points to the next byte position of last accessed. lseek can read and write pointers can be changed in position without performing read and write operations.
• directory: list shows all files and their length.

Organization of the file system

First k disks are reserved area, which includes the following information: a bitmap and a file descriptor. Bitmap used to describe allocation of disk blocks. Each bit in the bitmap corresponds to one logical block. When you create or delete files, as well as the length of the file changes, file systems need to be bitmap operations. Before the remaining portion of the file descriptor contains a set of k blocks. Each file descriptor contains the following information:
• file length in bytes
• file allocation block number to the disk array. The length of the array is a system parameter. In the experiment, we can set it as a relatively small number, such as 3.

table of Contents

Our only one file system directory that contains all the files in the file system. In addition to not need to explicitly create and delete directories and similar in many ways an ordinary file. 0 corresponding to the directory file descriptor. Initially, no files directory, all, descriptors corresponding to the directory record length should be 0, and no disk block allocation. Each creates a file, the file length of the directory will increase one point. Content catalog file consists of a series of directory entries, where each directory entry is organized as follows:
• File name
• file descriptor number

Create and delete files

Need the following steps when creating a file;
• Locate an empty file descriptor (scanning ldisk [0] ~ ldisk [k - 1])
• assign a directory entry for the newly created files in the file directory (the directory may be required for the file allocation the new disk blocks)
• record file names assigned to items in the catalog number and descriptor.
• return status information (if no error occurs, etc.)
need the following steps delete the file (assuming the file is not opened):
• search for the file descriptor number in the directory
• delete directory entry corresponding to the file and the update bitmap
• release file descriptors
• return status information

Opening and closing the file

File system maintains a table of open files. Open the fixed length file table, which entry contains the following information:
• read and write buffers
• write pointer
• file descriptors
file is opened, then open a file table entry assigned; when the file is closed, the corresponding entry is released. A write buffer size is equal to the disk block. The following operation is required to open a file:
• search directories to locate the file descriptor corresponding to the number
• open a file allocation table entry
• entry allocated to the read and write pointers are set to 0, and records the descriptor number
• reading a file of a write to the buffer
• return the entry assigned to the open file table index
operation to close the file needs to be as follows:
• the contents of the buffer are written to disk
• releasing the open the corresponding file in the file table entry
• return status information

Read and write

It can read and write after the file is opened. Read complete the following tasks:

  1. Calculating a position deviation corresponding to read and write pointers in the read buffer
  2. The contents of the buffer memory is copied to the specified location, until one of the following events occur:
    • end of file has been copied or a specified number of bytes. At this time, update the information and return the corresponding read and write pointers
    • reach the end of the buffer. At this time, the contents of the buffer is written to disk, then the contents of a file to read the disk. Finally, return to step 2.

Ask someone else to consider their own.

test

To be able to test our simulation system, write an operating system file or shell of a menu-driven system.

answer:

I / O System

In the experiment, in an analogous character array ldisk [L] [B], build the disk model, wherein B represents the length of each memory block, L represents the total number of disk storage blocks

Functional structure

read_block (int i, char * p );
the number of characters of the contents of the logical function to the block i is read into the memory location pointed to by pointer p, length copy of the memory block B.

bool read_block(int i, char* p)
{
	memcpy(p, ldisk[i], B);
	return true;
}

write block (int i, char * p);
the contents of the function pointer p pointing to write logic block i, the number of copies of the character length of the memory block B. In addition, in order to facilitate testing, we also need to implement two additional functions: one for the array ldisk to a file; the other is used to restore the contents of the file into an array.

bool write_block(int i, char* p)
{
	memcpy(ldisk[i], p, B);
	return true;
}

File systems and directories

File information and file directories represented by the following structure:

typedef struct
{
	char name[10];
	short int length;
	short int allo[10];
} filedes;

typedef struct
{
	char Buffer[B];
	char* p;
	int id;
	int index;
	filedes dsc;
} OpenFileTable;

Function Interface

create (filename): Create a new file with the specified file name.

bool create(char* filename) 
{
	char* p = filename;
	int length = strlen(filename);
	int index = getfileheadblock();
	setfileheadblock();
	p = filename;
	filedes fd;
	memcpy(&fd.name, filename, length + 1);
	fd.length = 0;
	for (int i = 0; i < 10; i++)
	{
		fd.allo[i] = -1;
	}
	fd.allo[0] = getfreedatablock();
	setfileheadblock();
	memset(temp_block, -1, B);
	write_block(fd.allo[0], temp_block);
	write_block(FILEBLOCK + DATABLOCK + index, (char*)& fd);
	addfile(index);
	return true;
}

destroy (filename): delete the specified file.

bool destroy(char* filename)
{
	char temp_block[B];
	filedes fd;
	read_block(DATABLOCK + FILEBLOCK, (char*)& fd);
	for (int i = 0; i < 10; i++)
	{
		if (fd.allo[i] == -1)continue;
		read_block(fd.allo[i], temp_block);
		for (int j = 0; j < B; j++)
		{
			if (temp_block[j] != -1)
			{
				filedes tfd;
				read_block(DATABLOCK + FILEBLOCK + temp_block[j], (char*)& tfd);
				if (memcmp(filename, tfd.name, strlen(filename)) == 0)
				{
					int fileIndex = temp_block[j];
					temp_block[j] = -1;
					write_block(fd.allo[i], temp_block);
					read_block(0, temp_block);
					int a = fileIndex / 8;
					int b = fileIndex % 8;
					temp_block[a] = temp_block[a] & (~map[b]);
					write_block(0, temp_block);
					cout << "删除成功" << endl;
					return true;
				}
			}
		}
	}
	cout << "没有该文件" << endl;
	return false;
}

open (filename): open the file. The function returns the index number can be used for the subsequent read, write, lseek, or close operation.

bool open(char* filename)
{
	filedes fd;
	read_block(DATABLOCK + FILEBLOCK, (char*)& fd);
	for (int i = 0; i < 10; i++)
	{
		if (fd.allo[i] == -1)
		{
			cout << "文件不存在!\n";
			return false;
		}
		read_block(fd.allo[i], temp_block);
		for (int j = 0; j < B; j++)
		{
			if (temp_block[j] != -1)
			{
				filedes temp_fd;
				read_block(DATABLOCK + FILEBLOCK + temp_block[j], (char*)& temp_fd);
				if (memcmp(filename, temp_fd.name, strlen(filename)) == 0)
				{
					openFileTable.id = temp_block[j];
					openFileTable.index = 0;
					if (temp_fd.allo[0] == -1)
					{
						openFileTable.p == NULL;
					}
					read_block(temp_fd.allo[0], openFileTable.Buffer);
					openFileTable.p = openFileTable.Buffer;
					openFileTable.dsc = temp_fd;
					return true;
				}

			}
		}
	}
	return false;
}

close (index): Close to develop file.

bool close(char* index)
{
	if (openFileTable.id == -1)
	{
		cout << "没有打开的文件\n";
		return false;
	}
	openFileTable.id = -1;
	return true;
}

read (index, mem_area, count): sequentially read from the specified file memarea count bytes specified memory location. Read operation starting from the position indicated by the file read and write pointers.

bool read(char* index, char* mem_area, int count)
{
	int left = B - (openFileTable.p - openFileTable.Buffer);
	while (count)
	{
		if (count - left > 0) //当前缓冲区不能满足请求,再取
		{
			count -= left;
			if (openFileTable.p == NULL)
			{
				cout << "越界访问\n";
				return false;
			}
			memcpy(mem_area, openFileTable.p, left);
			openFileTable.index++;
			if (openFileTable.dsc.allo[openFileTable.index] == -1)
			{
				openFileTable.p = NULL;
			}
			read_block(openFileTable.dsc.allo[openFileTable.index], openFileTable.p);
			left = B;
		}
		else
		{
			if (openFileTable.p == NULL)
			{
				cout << "越界访问\n";
				return false;
			}
			memcpy(mem_area, openFileTable.p, count);
			write_block(openFileTable.dsc.allo[openFileTable.index], openFileTable.Buffer);
			openFileTable.p += left;
			count = 0;
			openFileTable.p += count;
		}
	}
	return true;
}

write (index, mem_area, count): writes the specified file memarea specified memory location starting count bytes sequentially. Write operation starts from the location indicated by file read and write pointers.

bool write(char* index, char* mem_area, int count)
{
	int left = B - (openFileTable.p - openFileTable.Buffer);
	while (count)
	{
		if (count - left > 0) //当前缓冲区不能满足请求,再取
		{
			count -= left;
			if (openFileTable.p == NULL)
			{
				cout << "访问超过范围\n";
				return false;
			}
			memcpy(openFileTable.Buffer, mem_area, left);
			write_block(openFileTable.dsc.allo[openFileTable.index], openFileTable.Buffer);
			openFileTable.index++;
			if (openFileTable.dsc.allo[openFileTable.index] == -1)
			{
				openFileTable.p = NULL;
			}
			openFileTable.p = openFileTable.Buffer;

			left = B;
		}
		else
		{
			if (openFileTable.p == NULL)
			{
				cout << "访问超过范围\n";
				return false;
			}
			memcpy(openFileTable.p, mem_area, count);
			write_block(openFileTable.dsc.allo[openFileTable.index], openFileTable.Buffer);
			openFileTable.p += count;
			openFileTable.dsc.length += count;
			write_block(DATABLOCK + FILEBLOCK + openFileTable.id, (char*)& openFileTable.dsc);
			return true;
		}
	}
	return false;
}

directory: list shows all files and their length.

bool directory() {
	filedes fd;
	read_block(FILEBLOCK + DATABLOCK, (char*)& fd);
	cout << "name\tsize\n";
	for (int i = 0; i < 10; i++)
	{
		if (fd.allo[i] != -1)
		{
			read_block(fd.allo[i], temp_block);
			for (int j = 0; j < B; j++)
			{
				if (temp_block[j] != -1)
				{
					filedes temp[B];
					read_block(FILEBLOCK + DATABLOCK + temp_block[j], (char*)& temp);
					cout <<temp->name;
					cout <<temp->length;
				}
			}
		}

	}
	return true;
}

operation result

Create a file and read the file operation

deleting a file
Here Insert Picture Description
read and write file operations
Here Insert Picture Description
GitHub Source: https://github.com/hhxhongchen/lab/tree/master/lab5

Guess you like

Origin blog.csdn.net/weixin_42474536/article/details/92601921