Unit5os
Unit5os
System files and data are kept in the computer system's memory, and when
these files are needed by an application, the operating system must have some way to
read the memory and access the appropriate files.
FILE
A file can be defined as a data structure which stores the sequence
of records. Files are stored in a file system, which may exist on a disk or in
the main memory. Files can be simple (plain text) or complex (specially-
formatted).
The collection of files is known as Directory. The collection of
directories at the different levels, is known as File System.
8. Close
When the processing of the file is complete, it should be closed so that all the
changes made permanent and all the resources occupied should be released. On
closing it deallocates all the internal descriptors that were created when the file was
opened.
9. Append
This operation adds data to the end of the file.
10. Rename
This operation is used to rename the existing file.
File Type
Usual
File type extension Function
C, java, pas,
Source Code Source code in various languages
asm, a
Word
wp, tex, rrf, doc Various word processor formats
Processor
Archive arc, zip, tar Related files grouped into one compressed file
1. Sequential Access
The operating system reads the file word by word in sequential access method
of file accessing. A pointer is made, which first links to the file's base address. If the
user wishes to read the first word of the file, the pointer gives it to them and raises its
value to the next word. This procedure continues till the file is finished. It is the
most basic way of file access.
The data in the file is evaluated in
the order that it appears in the file and that
is why it is easy and simple to access a
file's data using sequential access
mechanism. For example, editors and
compilers frequently use this method to
check the validity of the code.
Advantages
The sequential access mechanism is very easy to implement.
It uses lexicographic order to enable quick access to the next entry.
Disadvantages
Sequential access will become slow if the next file record to be retrieved is
not present next to the currently pointed record.
Adding a new record may need relocating a significant number of records
of the file.
Assume that each block of storage holds four records and that the record we
want to access is stored in the tenth block. In such a situation, sequential access will
not be used since it will have to traverse all of the blocks to get to the required record,
while direct access will allow us to access the required record instantly.
The direct access mechanism requires the OS to perform some additional tasks
but eventually leads to much faster retrieval of records as compared to the sequential
access.
Advantages
The files can be retrieved right away with direct access mechanism,
reducing the average access time of a file.
There is no need to traverse all of the blocks that come before the required
block to access the record.
Disadvantages
The direct access mechanism is typically difficult to implement due to its
complexity.
Organizations can face security issues as a result of direct access as the
users may access/modify the sensitive information. As a result, additional
security processes must be put in place.
DIRECTORY STRUCTURE
Directory can be defined as the listing of the related files on the disk. The
directory may store some or the entire file attributes.
To get the benefit of different file systems on the different operating systems, A
hard disk can be divided into the number of partitions of different sizes. The partitions
are also called volumes or mini disks.
Each partition must have at least one directory in which, all the files of the
partition can be listed. A directory entry is maintained for each file in the directory
which stores all the information related to that file.
A directory can be viewed as a file which contains the Meta data of the bunch
of files.
Every Directory supports a number of common operations on the file:
File Creation
Search for the file
File deletion
Renaming the file
Traversing Files
Listing of files
The simplest method is to have one big list of all the files on the disk. The
entire system will contain only one directory which is supposed to mention all the files
present in the file system. The directory contains one entry per each file present on the
file system.
Advantages
1. Implementation is very simple.
2. If the sizes of the files are very small then the searching becomes faster.
3. File creation, searching, deletion is very simple since we have only one
directory.
Disadvantages
Naming problem: Users cannot have the same name for two files.
Grouping problem: Users cannot group files according to their needs.
TWO-LEVEL DIRECTORY
In two level directory systems, we can create a separate directory for each user.
There is one master directory which contains separate directories dedicated to each
user. For each user, there is a different directory present at the second level,
containing group of user's file. The system doesn't let a user to enter in the other user's
directory without permission.
Path name: Due to two levels there is a path name for every file to locate that
file.
Advantage
we can have the same file name for different users.
Searching is efficient in this method.
GENERAL-GRAPH DIRECTORY
This is an extension to the acyclic-graph directory. In the general-graph
directory, there can be a cycle inside a directory.
In the above image, we can see that a cycle is formed in the user 2 directory.
Although it provides greater flexibility, it is complex to implement this structure.
Advantages
Compared to the others, the General-Graph directory structure is more
flexible.
Cycles are allowed in the directory for general-graphs.
Disadvantages
It costs more than alternative solutions.
Garbage collection is an essential step here.
Operations like renaming, editing the existing file, copying; these can also be
controlled. There are many protection mechanism. each of them mechanism have
different advantages and disadvantages and must be appropriate for the intended
application.
Access Control
There are different methods used by different users to access any file. The
general way of protection is to associate identity-dependent access with all the files
and directories a list called access-control list (ACL) which specify the names of the
users and the types of access associate with each of the user.
The main problem with the access list is their length. If we want to allow
everyone to read a file, we must list all the users with the read access. This technique
has two undesirable consequences:
Constructing such a list may be tedious and unrewarding task, especially if we
do not know in advance the list of the users in the system.
Previously, the entry of the any directory is of the fixed size but now it changes
to the variable size which results in the complicates space management. These
C. Dinesh, AP/CSE-AIML, MRCE Page 9
UNIT – V CS403PC: OPERATING SYSTEMS
When an application program asks for a file, the first request is directed to
the logical file system. The logical file system contains the Meta data of the
file and directory structure. If the application program doesn't have the
required permissions of the file then this layer will throw an error. Logical
file systems also verify the path to the file.
Generally, files are divided into various logical blocks. Files are to be stored
in the hard disk and to be retrieved from the hard disk. Hard disk is divided
into various tracks and sectors. Therefore, in order to store and retrieve the
files, the logical blocks need to be mapped to physical blocks. This mapping
is done by File organization module. It is also responsible for free space
management.
Once File organization module decided which physical block the
application program needs, it passes this information to basic file system.
The basic file system is responsible for issuing the commands to I/O control
in order to fetch those blocks.
I/O controls contain the codes by using which it can access hard disk. These
codes are known as device drivers. I/O controls are also responsible for
handling interrupts.
ALLOCATION METHODS
There are various methods which can be used to allocate disk space to the files.
Selection of an appropriate allocation method will significantly affect the performance
and efficiency of the system. Allocation method provides a way in which the disk will
be utilized and the files will be accessed.
There are following methods which can be used for allocation.
1. Contiguous Allocation.
2. Linked Allocation
3. Indexed Allocation
4. Linked Indexed Allocation
5. Multilevel Indexed Allocation
Contiguous Allocation
A single continuous set of blocks
is allocated to a file at the time of file
creation. Thus, this is a pre-allocation
strategy, using variable size portions.
The file allocation table needs just a
single entry for each file, showing the
starting block and the length of the file.
This method is best from the point of
view of the individual sequential file.
Multiple blocks can be read in at
a time to improve I/O performance for
sequential processing. It is also easy to
retrieve a single block. For example, if
a file starts at block b, and the ith block
of the file is wanted, its location
on secondary storage is simply b+i-1.
Disadvantage
External fragmentation will occur, making it difficult to find contiguous
blocks of space of sufficient length. A compaction algorithm will be
necessary to free up additional space on the disk.
Also, with pre-allocation, it is necessary to declare the size of the file at the
time of creation.
Disadvantage
Internal fragmentation exists in the last disk block of the file.
There is an overhead of maintaining the pointer in every disk block.
If the pointer of any disk block is lost, the file will be truncated.
It supports only the sequential access of files.
Indexed Allocation
It addresses many of the problems of
contiguous and chained allocation. In this
case, the file allocation table contains a
separate one-level index for each file: The
index has one entry for each block allocated
to the file.
The allocation may be on the basis of
fixed-size blocks or variable-sized blocks.
Allocation by blocks eliminates external
fragmentation, whereas allocation by
variable-size blocks improves locality.
This allocation technique supports
both sequential and direct access to the file
and thus is the most popular form of file
allocation.
2. Linked List :
A linked list is another approach for free space management in an operating
system. In it, all the free blocks inside a disk are linked together in a linked list. These
free blocks on the disk are linked together by a pointer. These pointers of the free
block contain the address of the next free block and the last pointer of the list points to
null which indicates the end of the linked list.
This technique is not enough to traverse the list because we have to read each
disk block one by one which requires I/O time.
The operating system can use this linked list to allocate memory blocks to
processes as needed.
3. Grouping
The grouping technique is also called the "modification of a linked list
technique". In this method, first, the free block of memory contains the addresses of
the n-free blocks. And the last free block of these n free blocks contains the addresses
of the next n free block of memory and this keeps going on. This technique separates
the empty and occupied blocks of space of memory.
4. Counting
In memory space, several files are created and deleted at the same time. For
which memory blocks are allocated and de-allocated for the files. Creation of files
occupy free blocks and deletion of file frees blocks.
When there is an entry in the free space, it consists of two parameters-
"address of first free disk block (a pointer)" and "a number 'n'".
SYSTEM CALLS
1. create
The create() function is used to create a new empty file in C. We can specify
the permission and the name of the file which we want to create using the create()
function. It is defined inside <unistd.h> header file and the flags that are passed as
arguments are defined inside <fcntl.h> header file.
Syntax of create() in C
int create(char *filename, mode_t mode);
Parameter
filename: name of the file which you want to create
mode: indicates permissions of the new file.
Return Value
return first unused file descriptor (generally 3 when first creating use in the
process because 0, 1, 2 fd are reserved)
return -1 when an error
2. open
The open() function in C is used to open the file for reading, writing, or both.
It is also capable of creating the file if it does not exist. It is defined
inside <unistd.h> header file and the flags that are passed as arguments are defined
inside <fcntl.h> header file.
Syntax of open() in C
int open (const char* Path, int flags);
Parameters
Path: Path to the file which we want to open.
o Use the absolute path beginning with “/” when you are not working
in the same directory as the C source file.
o Use relative path which is only the file name with extension, when
you are working in the same directory as the C source file.
flags: It is used to specify how you want to open the file. We can use the
following flags.
Flags Description
Flags Description
O_ APPEND Opens the file and places the cursor at the end of the contents.
3. close
The close() function in C tells the operating system that you are done with a
file descriptor and closes the file pointed by the file descriptor. It is defined
inside <unistd.h> header file.
Syntax of close() in C
int close(int fd);
Parameter
fd: File descriptor of the file that you want to close.
Return Value
0 on success.
-1 on error.
4. read
From the file indicated by the file descriptor fd, the read() function reads the
specified amount of bytes cnt of input into the memory area indicated by buf. The
read() function is also defined inside the <unistd.h> header file.
Syntax of read() in C
size_t read (int fd, void* buf, size_t cnt);
Parameters
fd: file descriptor of the file from which data is to be read.
buf: buffer to read data from
cnt: length of the buffer
Return Value
return Number of bytes read on success
return 0 on reaching the end of file
return -1 on error
return -1 on signal interrupt
5. write
Writes cnt bytes from buf to the file or socket associated with fd. cnt should
not be greater than INT_MAX (defined in the limits.h header file). If cnt is zero,
write() simply returns 0 without attempting any other action.
The write() is also defined inside <unistd.h> header file.
Syntax of write() in C
size_t write (int fd, void* buf, size_t cnt);
Parameters
fd: file descriptor
buf: buffer to write data to.
cnt: length of the buffer.
Return Value
returns the number of bytes written on success.
return 0 on reaching the End of File.
return -1 on error.
return -1 on signal interrupts.
6. ioctl
ioctl() is referred to as Input and Output Control.
ioctl is a system call for device-specific input/output operations and other
operations which cannot be expressed by regular system calls.
7. fork
A new process is created by the fork() system call.
A new process may be created with fork() without a new program being run-
the new sub-process simply continues to execute exactly the same program
that the first (parent) process was running.
It is one of the most widely used system calls under process management.
8. exit
The exit() system call is used by a program to terminate its execution.
The operating system reclaims resources that were used by the process after
the exit() system call.
9. exec
A new program will start executing after a call to exec()
Running a new program does not require that a new process be created first:
any process may call exec() at any time. The currently running program is
immediately terminated, and the new program starts executing in the context
of the existing process.
10. wait
The wait() system call suspends execution of the current process until one of
its children terminates. The call wait(&status) is equivalent to:
waitpid(-1, &status, 0);
11. waitpid
The waitpid() system call suspends execution of the current process until a
child specified by pid argument has changed state. By default, waitpid() waits only
for terminated children, but this behaviour is modifiable via the options argument, as
described below.
The value of pid can be:
Tag Description
meaning wait for any child process whose process group ID is
< -1
equal to the absolute value of pid.
-1 meaning wait for any child process.
meaning wait for any child process whose process group ID is
0
equal to that of the calling process.
meaning wait for the child whose process ID is equal to the
>0
value of pid.
Disadvantages
It may cause starvation for some requests.
Switching direction on the frequent basis slows the working of algorithm.
It is not the most optimal algorithm.
Example
Consider the following disk request sequence for a disk with 100 tracks
45, 21, 67, 90, 4, 89, 52, 61, 87, 25. Head pointer starting at 50. Find the number of
head movements in cylinders using SSTF scheduling.
SCAN Algorithm
It is also called as Elevator Algorithm. In this algorithm, the disk arm moves
into a particular direction till the end, satisfying all the requests coming in its path and
then it turns back and moves in the reverse direction satisfying requests coming in its
path.
It works in the way an elevator works, elevator moves in a direction completely
till the last floor of that direction and then turns back.
Example
Consider the following disk request sequence for a disk with 100 tracks
98, 137, 122, 183, 14, 133, 65, 78. Head pointer starting at 54 and moving in left
direction. Find the number of head movements in cylinders using SCAN scheduling.
C-SCAN algorithm
In C-SCAN algorithm, the arm of the disk moves in a particular direction
servicing requests until it reaches the last cylinder, then it jumps to the last cylinder of
the opposite direction without servicing any request then it turns back and start
moving in that direction servicing the remaining requests.
Example
Consider the following disk request sequence for a disk with 100 tracks
98, 137, 122, 183, 14, 133, 65, 78. Head pointer starting at 54 and moving in left
direction.
LOOK Scheduling
It is like SCAN scheduling Algorithm to some extant except the difference that,
in this scheduling algorithm, the arm of the disk stops moving inwards (or outwards)
when no more request in that direction exists. This algorithm tries to overcome the
overhead of SCAN algorithm which forces disk arm to move in one direction till the
end regardless of knowing if any request exists in the direction or not.
Example
Consider the following disk request sequence for a disk with 100 tracks
98, 137, 122, 183, 14, 133, 65, 78. Head pointer starting at 54 and moving in left
direction.
C LOOK Scheduling
C Look Algorithm is similar to C-SCAN algorithm to some extent. In this
algorithm, the arm of the disk moves outwards servicing requests until it reaches the
highest request cylinder, then it jumps to the lowest request cylinder without servicing
any request then it again start moving outwards servicing the remaining requests.
It is different from C SCAN algorithm in the sense that, C SCAN force the disk
arm to move till the last cylinder regardless of knowing whether any request is to be
serviced on that cylinder or not.
Example
Consider the following disk request sequence for a disk with 100 tracks
98, 137, 122, 183, 14, 133, 65, 78. Head pointer starting at 54 and moving in left
direction. Find the number of head movements in cylinders using C LOOK
scheduling.