CS333 OS Lab8 BSCS5C
CS333 OS Lab8 BSCS5C
Date: 13-12-2017
Time: 02:00 PM – 05:00 PM
Instructor: Dr. Muhammad Ali Tahir
A file is a named collection of related information that is recorded on secondary storage such
as magnetic disks, magnetic tapes and optical disks.In general, a file is a sequence of bits, bytes,
lines or records whose meaning is defined by the files creator and user.
Objectives
Understand the file structures.
Tools/Software Requirement
➢ gcc
Description
File structure is a structure, which is according to a required format that operating system can
understand. A file has a certain defined structure according to its type. A text file is a sequence
of characters organized into lines. A source file is a sequence of procedures and functions. An
object file is a sequence of bytes organized into blocks that are understandable by the machine.
When operating system defines different file structures, it also contains the code to support
these file structure. Unix, MS-DOS support minimum number of file structure.
Before proceeding to task you are supposed to go through the following system calls: -
i) open
ii) read
iii) write
iv) close
Let’s continue with our first system call open() whose purpose is to open file for reading or writing
or to create new file. In order to demonstrate other system calls here's the example code for
program that copies input file passed as first argument into output file passed as second argument:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
/* Copy process */
while((ret_in = read (input_fd, &buffer, BUF_SIZE)) > 0){
ret_out = write (output_fd, &buffer, (ssize_t) ret_in);
if(ret_out != ret_in){
/* Write error */
perror("write");
return 4;
}
}
return (EXIT_SUCCESS);
Tasks
Task 1:
Write a simple grep like simple utility in the C programming language. Approach the problem
by following these steps:
a) Your task is to write a simple grep like utility in the C programming language. You can
name it “mygrep”. grep is basically used for searching. For example, “mygrep foo
myfile” command returns all the lines that contain a string matching the expression
"foo" in the file. Your code will be checked by running such a simple command.
It is easy to get overwhelmed with the complexity of grep. Note that we are looking for
a simple version that can search for a specific string in a single file. You may consult
grep man page if you are looking to get more info about it
Submission Guideline:
Submit your source code in a document and instead of submitting separate README file
write down the required explanation in your document with the heading README.