Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will be just writing three functions in b_io.c b_io_fd b_open (char * filename); int b_read (b_io_fd fd, char * buffer, int count); void b_close

You will be just writing three functions in b_io.c

 b_io_fd b_open (char * filename); int b_read (b_io_fd fd, char * buffer, int count); void b_close (b_io_fd fd); 

Your functions here will only use Linux I/O APIs. i.e. read, open, close (do not use the c library file functions).

The b_open should return a integer file descriptor (a number that you can track the file). You may want to also allocate the 512 byte buffer you will need for read operations here. Make sure though you know how to track the buffer for each individual file. You can return the same error code you receive if the Linux open returns an error.

The b_read takes a file descriptor, a buffer and the number of bytes desired. The Operation of your b_read function must only read 512 bytes chunks at a time from the operating system into your own buffer, you will then copy the appropriate bytes from your buffer to the callers buffer. This means you may not even need to do a read of the actual file if your buffer already has the data needed. Or, it may mean that you have some bytes in the buffer, but not enough and have to transfer what you have, read the next 512 bytes, then copy the remaining needed bytes to the callers buffer. The return value is the number of bytes you have transferred to the callers buffer. When it is positive but less than the request, it means you have reached the end of file. Hint: You may also need to track between calls where in the buffer you left off.

You also need to be able to handle if the read request is greater than 512, meaning that you may have to directly fill the caller's buffer from a 512 byte read (no need to buffer) then buffer just any amount needed to complete the caller's read request.

The b_close should close the file on the Linux side, and free and resources you were using.

You can write additional helper routines as needed.

Limits: You can assume no more than 20 files open at a time. (i.e. you need to ensure that multiple files can be open at one time, so the buffer you have for a file can not be global, but must be associated with that open file.

Your main program (separate file, starting template for main is provided) should use the command line arguments for the file to b_open. So your main program should open the file using b_open, read exactly 80 characters at a time from the file using b_read, print those 80 characters to the screen (ending in a newline character), and loop until you have read the entire file, then b_close the file and exit.

b_io_h

no need to change this file

#ifndef _B_IO_H #define _B_IO_H

typedef int b_io_fd;

b_io_fd b_open (char * filename); int b_read (b_io_fd fd, char * buffer, int count); void b_close (b_io_fd fd);

#endif

/***b_io.c***/

#include "b_io.h" #define B_CHUNK_SIZE 512

// You will create your own file descriptor which is just an integer index into an array // that you maintain for each open file. You will need to use the Linux open, read, close // functions. So you will need to store the Linux file descriptor, but that value should // never be exposed to the caller. // Hint - create a structure (struct) that contains fields like the LinuxFileDescriptor // Pointer to the file's buffer, length of data in the buffer, and index into the buffer // Hint 2: No where in this code should you be assuming on the size of the read (i.e. there // should NOT be any reference to 80 or BUFFERCNT

b_io_fd b_open (char * filename) { //*** TODO ***: Write open function to return your file descriptor // The returned file descriptor CAN NOT be the Linux file descriptor // You must use the Linux System Calls (open) // You may want to allocate the buffer here as well // But make sure every file has its own buffer } int b_read (b_io_fd fd, char * buffer, int count) { //*** TODO ***: Write buffered read function to return the data and # bytes read // You must use the Linux System Calls (read) and you must buffer the data // in 512 byte chunks. i.e. your linux read must be in B_CHUNK_SIZE } void b_close (b_io_fd fd) { //*** TODO ***: Close the file and release the buffer // You must use Linux System Calls (close) }

/***main***/ only change the TODO

#include #include "b_io.h"

#define BUFFERCNT 80

/*** ToDo: Add comments to the code. ***/

int main (int argc, char * argv[]) { int readCount; char * buffer; b_io_fd fd; if (argc != 2) { printf ("Proper usage is: command filename "); return -1; } buffer = malloc (BUFFERCNT + 2); //allocate a little extra space, need at least one for the null. /*** ToDo: Check if malloc succeeds ***/ fd = b_open (argv[1]); /*** ToDo: Check if b_open succeeds ***/ do { readCount = b_read (fd, buffer, BUFFERCNT); buffer[readCount] = '\0'; printf ("%s ", buffer); } while (readCount > 0); /*** TODO add b_close and free anything that needs freeing ***/ return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

Explain the Pascals Law ?

Answered: 1 week ago

Question

What are the objectives of performance appraisal ?

Answered: 1 week ago

Question

State the uses of job description.

Answered: 1 week ago