achille67
asked on
using lseek( )
how can i use a for loop to traverse bit_map[i] array and finds the next available free bitmap position using lseek( )?
Note: i use man lseek page but the concept is still not very clear to me can an expert help out?
Note: i use man lseek page but the concept is still not very clear to me can an expert help out?
lseek() is only used to position the read/write pointer to a file at a speciffied offset. It doesn't operate on arrays.
ASKER
Oh ok, so how do i use position a read/write pointer?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks khkremer,
The following code is simply importing linux_file from my linux file system to
to u_file residing in my own file system i just create by running the command:
./userfs -reformat 800000 file.system
At some point of the implementation of of u_import, i want to find
next free available free bitmap in bit_map[i] array and position lseek to that place with lseek(virtual_disk,BLOCK_S IZE_BYTES, SEEK_SET);
NOTE: u_import is a function of an entire file system homework(you may take a look at it at: http://www.clarkson.edu/~jnm/os/homework/userfs/fs.htm)
int u_import(char* linux_file, char* u_file)
{
int free_space;
int num_block;
struct stat st;
int file_size=0;
int remainder=0;
int i;
int j;
free_space = u_quota();
handle = open(linux_file,O_RDONLY);
if ( -1 == handle ) {
printf("error, reading file %s\n",linux_file);
return 0;
}
fstat(handle,&st);
file_size=(int)st.st_size;
num_block=(int)file_size/B LOCK_SIZE_ BYTES;
if((num_block*BLOCK_SIZE_B YTES)!=fil e_size)
{
num_block++;
remainder=file_size-(num_b lock*BLOCK _SIZE_BYTE S);
}
for(i=0;i<num_block;i++)
{
lseek(handle,BLOCK_SIZE_BY TES*i,SEEK _SET);
read(handle,&buffer,BLOCK_ SIZE_BYTES );
// crash_write(virtual_disk, bit_map, sizeof(BIT)*BIT_MAP_SIZE );
// bit_map[i] array and finds the next available free bitmap
//position lseek to that place with
for (j=0; j< BIT_MAP_SIZE; j++){
assert( free_space >= 1);
allocate_block(i);
lseek(virtual_disk,BLOCK_S IZE_BYTES, SEEK_SET);
}
.
.
.
return 1;
}
The following code is simply importing linux_file from my linux file system to
to u_file residing in my own file system i just create by running the command:
./userfs -reformat 800000 file.system
At some point of the implementation of of u_import, i want to find
next free available free bitmap in bit_map[i] array and position lseek to that place with lseek(virtual_disk,BLOCK_S
NOTE: u_import is a function of an entire file system homework(you may take a look at it at: http://www.clarkson.edu/~jnm/os/homework/userfs/fs.htm)
int u_import(char* linux_file, char* u_file)
{
int free_space;
int num_block;
struct stat st;
int file_size=0;
int remainder=0;
int i;
int j;
free_space = u_quota();
handle = open(linux_file,O_RDONLY);
if ( -1 == handle ) {
printf("error, reading file %s\n",linux_file);
return 0;
}
fstat(handle,&st);
file_size=(int)st.st_size;
num_block=(int)file_size/B
if((num_block*BLOCK_SIZE_B
{
num_block++;
remainder=file_size-(num_b
}
for(i=0;i<num_block;i++)
{
lseek(handle,BLOCK_SIZE_BY
read(handle,&buffer,BLOCK_
// crash_write(virtual_disk, bit_map, sizeof(BIT)*BIT_MAP_SIZE );
// bit_map[i] array and finds the next available free bitmap
//position lseek to that place with
for (j=0; j< BIT_MAP_SIZE; j++){
assert( free_space >= 1);
allocate_block(i);
lseek(virtual_disk,BLOCK_S
}
.
.
.
return 1;
}
Hi achille67,
I see that you use int for calculating offsets. That will very likely cause problems, as offsets should be 64 bits. Define an unsigned type which you use for FS block/seek oprations, like
typedef uint32_t fs_block_t; /* For storing block numbers */
typedef uint64_t fs_seek_t; /* For seeking in files */
The system offers the off_t type for seeks which you can also use.
Cheers,
Stefan
I see that you use int for calculating offsets. That will very likely cause problems, as offsets should be 64 bits. Define an unsigned type which you use for FS block/seek oprations, like
typedef uint32_t fs_block_t; /* For storing block numbers */
typedef uint64_t fs_seek_t; /* For seeking in files */
The system offers the off_t type for seeks which you can also use.
Cheers,
Stefan