473,466 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

try this again

Modify the file system to allow the maximum size of a file to be as
large as the disk (128Kbytes). In the basic file system, each file is
limited to a file size of just under 4Kbytes. Each file has a header
(class FileHeader) that is a table of direct pointers to the disk blocks
for that file. Since the header is stored in one disk sector, the
maximum size of a file is limited by the number of pointers that will
fit in one disk sector. Increasing the limit to 128KBytes will probably
but not necessarily require you to implement double indirect, or
linked-list blocks.

i get a segment fault when the file size is larger thank 8K. I'm pretty
sure its in the Allocation method.

attachment is the code

// modified
// filehdr.cc
// Routines for managing the disk file header (in UNIX, this
// would be called the i-node).
//
// The file header is used to locate where on disk the
// file's data is stored. We implement this as a fixed size
// table of pointers -- each entry in the table points to the
// disk sector containing that portion of the file data
// (in other words, there are no indirect or doubly indirect
// blocks). The table size is chosen so that the file header
// will be just big enough to fit in one disk sector,
//
// Unlike in a real system, we do not keep track of file permissions,
// ownership, last modification date, etc., in the file header.
//
// A file header can be initialized in two ways:
// for a new file, by modifying the in-memory data structure
// to point to the newly allocated data blocks
// for a file already on disk, by reading the file header from disk
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.

#include "copyright.h"

#include "system.h"
#include "filehdr.h"

//----------------------------------------------------------------------
// FileHeader::Allocate
// Initialize a fresh file header for a newly created file.
// Allocate data blocks for the file out of the map of free disk blocks.
// Return FALSE if there are not enough free blocks to accomodate
// the new file.
//
// "freeMap" is the bit map of free disk sectors
// "fileSize" is the bit map of free disk sectors
//----------------------------------------------------------------------

bool
FileHeader::Allocate(BitMap *freeMap, int fileSize)
{
int i;

numBytes = fileSize;
numSectors = divRoundUp(fileSize, SectorSize);
if (freeMap->NumClear() < (numSectors+1))
return FALSE; // not enough space

int directSectors = numSectors % NumDirect;

for (i = 0; i < directSectors; i++)
dataSectors[i] = freeMap->Find();

if( (numSectors / NumDirect) > 0 ) {
indirectHdrSector = freeMap->Find();
FileHeader *hdr = new FileHeader;
for( i = 0; i < (numSectors - directSectors); i++ )
hdr->dataSectors[i] = freeMap->Find();
hdr->WriteBack(indirectHdrSector);
delete hdr;
}
return TRUE;
}

//----------------------------------------------------------------------
// FileHeader::Deallocate
// De-allocate all the space allocated for data blocks for this file.
//
// "freeMap" is the bit map of free disk sectors
//----------------------------------------------------------------------

void
FileHeader::Deallocate(BitMap *freeMap)
{
int i;

int directSectors = numSectors % NumDirect;

for (i = 0; i < directSectors; i++) {
ASSERT(freeMap->Test((int) dataSectors[i])); // ought to be marked!
freeMap->Clear((int) dataSectors[i]);
}

if( indirectHdrSector != 0 ) {
FileHeader *hdr = new FileHeader;
hdr->FetchFrom(indirectHdrSector);

for( i = 0; i < (numSectors - directSectors); i++) {
ASSERT(freeMap->Test((int) hdr->dataSectors[i]));
freeMap->Clear((int) hdr->dataSectors[i]);
}
delete hdr;
}
}

//----------------------------------------------------------------------
// FileHeader::FetchFrom
// Fetch contents of file header from disk.
//
// "sector" is the disk sector containing the file header
//----------------------------------------------------------------------

void
FileHeader::FetchFrom(int sector)
{
DEBUG('z', "Reading the sector from FileHeader:%d:\n", sector);
synchDisk->ReadSector(sector, (char *)this);
}

//----------------------------------------------------------------------
// FileHeader::WriteBack
// Write the modified contents of the file header back to disk.
//
// "sector" is the disk sector to contain the file header
//----------------------------------------------------------------------

void
FileHeader::WriteBack(int sector)
{
synchDisk->WriteSector(sector, (char *)this);
}

//----------------------------------------------------------------------
// FileHeader::ByteToSector
// Return which disk sector is storing a particular byte within the file.
// This is essentially a translation from a virtual address (the
// offset in the file) to a physical address (the sector where the
// data at the offset is stored).
//
// "offset" is the location within the file of the byte in question
//----------------------------------------------------------------------

int
FileHeader::ByteToSector(int offset)
{
int SectorNum = offset / SectorSize;

if( SectorNum < NumDirect )
return dataSectors[SectorNum];

else {
FileHeader *hdr = new FileHeader;
hdr->FetchFrom(indirectHdrSector);
int returnNum = hdr->dataSectors[ SectorNum - NumDirect ];
delete hdr;
return returnNum;

}
//return(dataSectors[offset / SectorSize]);
}

//----------------------------------------------------------------------
// FileHeader::FileLength
// Return the number of bytes in the file.
//----------------------------------------------------------------------

int
FileHeader::FileLength()
{
return numBytes;
}

//----------------------------------------------------------------------
// FileHeader::Print
// Print the contents of the file header, and the contents of all
// the data blocks pointed to by the file header.
//----------------------------------------------------------------------

void
FileHeader::Print()
{
int i, j, k;
char *data = new char[SectorSize];

printf("FileHeader contents. File size: %d. File blocks:\n", numBytes);
for (i = 0; i < numSectors; i++)
printf("%d ", dataSectors[i]);
printf("\nFile contents:\n");
for (i = k = 0; i < numSectors; i++) {
synchDisk->ReadSector(dataSectors[i], data);
for (j = 0; (j < SectorSize) && (k < numBytes); j++, k++) {
if ('\040' <= data[j] && data[j] <= '\176') // isprint(data[j])
printf("%c", data[j]);
else
printf("\\%x", (unsigned char)data[j]);
}
printf("\n");
}
delete [] data;
}

#ifdef CHANGED
void FileHeader::SetFileAttr(int TotBytes, int TotSectors) {
numBytes = TotBytes;
numSectors = TotSectors;
}
#endif

// modified
// filehdr.h
// Data structures for managing a disk file header.
//
// A file header describes where on disk to find the data in a file,
// along with other information about the file (for instance, its
// length, owner, etc.)
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.

#include "copyright.h"

#ifndef FILEHDR_H
#define FILEHDR_H

#include "disk.h"
#include "bitmap.h"

#define NumDirect ((SectorSize - 3 * sizeof(int)) / sizeof(int))
#define MaxFileSize (NumDirect * SectorSize)

// The following class defines the Nachos "file header" (in UNIX terms,
// the "i-node"), describing where on disk to find all of the data in the file.
// The file header is organized as a simple table of pointers to
// data blocks.
//
// The file header data structure can be stored in memory or on disk.
// When it is on disk, it is stored in a single sector -- this means
// that we assume the size of this data structure to be the same
// as one disk sector. Without indirect addressing, this
// limits the maximum file length to just under 4K bytes.
//
// There is no constructor; rather the file header can be initialized
// by allocating blocks for the file (if it is a new file), or by
// reading it from disk.

class FileHeader {
public:
bool Allocate(BitMap *bitMap, int fileSize);// Initialize a file header,
// including allocating space
// on disk for the file data
void Deallocate(BitMap *bitMap); // De-allocate this file's
// data blocks

void FetchFrom(int sectorNumber); // Initialize file header from disk
void WriteBack(int sectorNumber); // Write modifications to file header
// back to disk

int ByteToSector(int offset); // Convert a byte offset into the file
// to the disk sector containing
// the byte

int FileLength(); // Return the length of the file
// in bytes

void Print(); // Print the contents of the file.

#ifdef CHANGED
void SetFileAttr(int TotBytes, int TotSectors);
#endif
int dataSectors[NumDirect];
int indirectHdrSector;

private:
int numBytes; // Number of bytes in the file
int numSectors; // Number of data sectors in the file
// int dataSectors[NumDirect]; // Disk sector numbers for each data
// block in the file
};

#endif // FILEHDR_H

Jul 22 '05 #1
1 1804

"Joseph" <cr*******@gmail.com> wrote in message
news:co**********@usenet01.srv.cis.pitt.edu...
Modify the file system to allow the maximum size of a file to be as
large as the disk (128Kbytes). In the basic file system, each file is
limited to a file size of just under 4Kbytes. Each file has a header
(class FileHeader) that is a table of direct pointers to the disk blocks
for that file. Since the header is stored in one disk sector, the
maximum size of a file is limited by the number of pointers that will
fit in one disk sector. Increasing the limit to 128KBytes will probably
but not necessarily require you to implement double indirect, or
linked-list blocks.

i get a segment fault when the file size is larger thank 8K. I'm pretty
sure its in the Allocation method.

attachment is the code

Your best bet to locate an error like this is to run it in the debugger, and
step through the funtion that you believe is failing. Look at the values of
the variables, especially the index when using loops. Also check that any
pointers that you dereference actually point to something that you've
dynamically allocated (and have not yet deleted). Once you've found the
line at which it actually crashes, you should know what's gone wrong. Then,
if you still don't see how to fix it, tell us which line fails, what the
relevant values were, and what you think they should have been. Then we can
help more easily.

I don't see SectorSize defined anywhere. The other calculations are all
based on this value. What is it defined as (and where is it defined)?
Could it be 0?

One other note: as mentioned in the FAQ, please post your responses either
at the bottom of the message, or intersepersed in the message, as
appropriate. Writing your response at the top is called "top posting", and
is frowned upon in usenet postings.

-Howard


Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phil Powell | last post by:
I am having this problem: My PHP script will set a cookie, it's there in my /Cookies folder. I delete the cookie (I have to for testing purposes, the PHP script I run behaves according to this...
5
by: Bruce | last post by:
I have a number of forms that do significant work based on variables POSTed from the form. What is the common method of detecting and preventing this work from being done when the form is POSTed as...
1
by: Jiten | last post by:
Hi Cor I spoke with u previously about this problem i had and u gave me some code that helped me. That code worked fine but i have now encountered another issuee that im hoping u may know how to...
8
by: jon morgan | last post by:
OK, I'm going to be brave. There is a bug in VS.Net 1.1 that causes random compiler errors. I have raised this issue in posts at least three time in the past couple of months without attracting...
17
by: Gabriel Mejía | last post by:
Services or applications using ActiveX Data Objects (ADO) 2.0 or greater may intermittently return empty recordsets on queries that should be returning valid results. At the time the problem...
3
by: penny336 | last post by:
dear all, i am using vc++ 6.0 sp5 i have a class called Address,it allocated some memory space for streetname and city i first define it as Address add = new Address("Madrian","UK"); ......
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
6
by: teedilo | last post by:
We have an application with a SQL Server 2000 back end that is fairly database intensive -- lots of fairly frequent queries, inserts, updates -- the gamut. The application does not make use of...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
5
by: Mike TI | last post by:
March 24, 2006 Hi all I am new to VB.NET and am using VB.NET 2005. I have an MDI form with a Split Container Control. On demand I am adding and removing User Controls on Panel 2. I am using...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.