3CS LSP Unit 2
3CS LSP Unit 2
A file system is designed in a way so that it can manage and provide space for non-volatile
storage data. All file systems required a namespace that is a naming and organizational
methodology. The namespace defines the naming process, length of the file name, or a subset of
characters that can be used for the file name. It also defines the logical structure of files on a
memory segment, such as the use of directories for organizing the specific files. Once a
namespace is described, a Metadata description must be defined for that particular file.
The advanced data and the structures that it represents contain the information about the file
system stored on the drive; it is distinct and independent of the file system metadata. Linux file
system contains two-part file system software implementation architecture.
Note: Inode doesn't contain the file name. Reason for this is to maintain hard-links for the files.
When all the other information is separated from the file name then only we can have various file
names pointing to the same Inode.
Inode Contents: An Inode is a data structure containing metadata about the files. Following
contents are stored in the Inode from a file:
User ID of file
Group ID of file
Device ID
File size
Date of creation
Permission
Owner of the file
File protection flag
Link counter to determine number of hard links
FILE DESCRIPTORS
A file descriptor is a number that uniquely identifies an open file in a computer's operating
system. It describes a data resource, and how that resource may be accessed. When a program
asks to open a file or another data resource, like a network socket — the kernel:
Grants access.
Creates an entry in the global file table.
Provides the software with the location of that entry.
The descriptor is identified by a unique non-negative integer, such as 0, 12, or 567. At least one
file descriptor exists for every open file on the system. File descriptors were first used in UNIX,
and are used by modern operating systems including Linux, macOS, and BSD. In Microsoft
Windows, file descriptors are known as file handles.
Overview
When a process makes a successful request to open a file, the kernel returns a file descriptor
which points to an entry in the kernel's global file table. The file table entry contains information
such as the inode of the file, byte offset, and the access restrictions for that data stream (read-
only, write-only, etc.).
On a Unix-like operating system, the first three file descriptors, by default, are STDIN (standard
input), STDOUT (standard output), and STDERR (standard error).
SYSTEM CALLS FOR FILE MANAGEMENT
A system call is a procedure that provides the interface between a process and the operating
system. It is the way by which a computer program requests a service from the kernel of the
operating system. Different operating systems execute different system calls.
In Linux, making a system call involves transferring control from unprivileged user mode to
privileged kernel mode; the details of this transfer vary from architecture to architecture. The
libraries take care of collecting the system-call arguments and, if necessary, arranging those
arguments in the special form necessary to make the system call.
System calls are divided into 5 categories mainly:
Process Control
File Management
Device Management
Information Maintenance
Communication
(i) Process Control: This system calls perform the task of process creation, process
termination, etc. The Linux System calls under this are fork (), exit (), exec ().
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.
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.
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.
(ii) File Management: File management system calls handle file manipulation jobs like
creating a file, reading, and writing, etc. The Linux System calls under this are open (), read
(), write (), close ().
open():
It is the system call to open a file.
This system call just opens the file, to perform operations such as read and
write; we need to execute different system call to perform the operations.
read():
This system call opens the file in reading mode
We cannot edit the files with this system call.
Multiple processes can execute the read () system calls on the same file
simultaneously.
write():
This system call opens the file in writing mode
We can edit the files with this system call.
Multiple processes cannot execute the write () system calls on the same file
simultaneously.
close():
This system call closes the opened file.
(iii) Device Management: Device management does the job of device manipulation like
reading from device buffers, writing into device buffers, etc. The Linux System calls under
this is ioctl ().
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.
(iv) Information Maintenance: It handles information and its transfer between the OS and
the user program. In addition, OS keeps the information about all its processes and system
calls are used to access this information. The System calls under this are getpid (), alarm (),
sleep ().
getpid():
getpid stands for Get the Process ID.
The getpid() function shall return the process ID of the calling process.
The getpid() function shall always be successful and no return value is reserved
to indicate an error.
alarm():
This system call sets an alarm clock for the delivery of a signal that when it has
to be reached.
It arranges for a signal to be delivered to the calling process.
sleep():
This System call suspends the execution of the currently running process for
some interval of time
Meanwhile, during this interval, another process is given chance to execute
(v) Communication: These types of system calls are specially used for inter-process
communications. Two models are used for inter-process communication
a) Message Passing (processes exchange messages with one another)
b) Shared memory (processes share memory region to communicate)
The system calls under this are pipe (), shmget (), mmap ().
pipe():
The pipe () system call is used to communicate between different Linux
processes.
It is mainly used for inter-process communication.
The pipe () system function is used to open file descriptors.
shmget():
shmget stands for shared memory segment.
It is mainly used for Shared memory communication.
This system call is used to access the shared memory and access the messages
in order to communicate with the process.
mmap():
This function call is used to map or unmap files or devices into memory.
The mmap() system call is responsible for mapping the content of the file to the
virtual memory space of the process.
DIRECTORY API
(i) opendir - Open a Directory
CALLING SEQUENCE:
#include <sys/types.h>
#include <dirent.h>
int opendir
(
const char *dirname
);
DESCRIPTION:
This routine opens a directory stream corresponding to the directory specified by
the dirname argument. The directory stream is positioned at the first entry.
(ii) readdir - Reads a directory
CALLING SEQUENCE:
#include <sys/types.h>
#include <dirent.h>
int readdir
(
DIR *dirp
);
DESCRIPTION:
The readdir() function returns a pointer to a structure dirent representing the next directory entry
from the directory stream pointed to by dirp. On end-of-file, NULL is returned.
The readdir() function may (or may not) return entries for . Or.. Your program should tolerate
reading dot and dot-dot but not require them.
The data pointed to be readdir() may be overwritten by another call to readdir() for the same
directory stream. It will not be overwritten by a call for another directory.
DESCRIPTION:
The directory stream associated with dirp is closed. The value in dirp may not be usable after a
call to closedir ().
DESCRIPTION:
rmdir deletes a directory, which must be empty
DESCRIPTION:
The umask () function sets the process file creation mask to cmask. The file creation mask is
used during open (), creat (), mkdir (), mkfifo () calls to turn off permission bits in
the mode argument. Bit positions that are set in cmask are cleared in the mode of the created file.
On UNIX-based systems, a hard link is just another name for a file that already exists. It's
typically found in file systems that allow various hard links to the same file. Hard links have the
same Inode value, unlike soft links, but they point to the file location rather than the directory.
Accessing the value of Environment Variable: To access the value of a variable, execute the
echo command as follows:
Syntax: echo $variable name
The env command: The env command is used to display all the available variables in the
system.
Syntax: env
The above command will delete the specified variable and its components from the system.
To remove a variable new_variable from the system, execute the command as follows:
Syntax: unset new_variable
Modifying User: One often needs to modify some property of existing users based on
organization requirements, user requests, or system migrations. Most of these properties are easy
to modify though we need to ensure how it’ll affect the user environment and access to files
owned or accessed by the user.
Syntax: $ sudo usermod -s <shell> <username>
Removing User: A user can be removed from Linux using userdel command.
Syntax: $ sudo userdel <username>