0% found this document useful (0 votes)
19 views

3CS LSP Unit 2

Uploaded by

marveledit145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

3CS LSP Unit 2

Uploaded by

marveledit145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT – 2 LINUX FILE STRUCTURE

WHAT IS THE LINUX FILE SYSTEM?


Linux file system is generally a built-in layer of a Linux operating system used to handle the data
management of the storage. It helps to arrange the file on the disk storage. It manages the file
name, file size, creation date, and much more information about a file.

LINUX FILE SYSTEM STRUCTURE


Linux file system has a hierarchal file structure as it contains a root directory and its
subdirectories. All other directories can be accessed from the root directory. A partition usually
has only one file system, but it may have more than one file system.

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.

INODE (INDEX NODE)


An Inode number is a uniquely existing number for all the files in Linux and all UNIX type
systems. When a file is created on a system, a file name and Inode number is assigned to it.
Generally, to access a file, a user uses the file name but internally file name is first mapped with
respective Inode number stored in a table.

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.

(iii) closedir - Ends directory read operation


CALLING SEQUENCE:
#include <sys/types.h>
#include <dirent.h>
int closedir
(
DIR *dirp
);

DESCRIPTION:
The directory stream associated with dirp is closed. The value in dirp may not be usable after a
call to closedir ().

(iv) mkdir - Makes a directory


CALLING SEQUENCE:
#include <sys/types.h>
#include <sys/stat.h>
int mkdir
(
const char *path,
mode_t mode
);
DESCRIPTION:
The mkdir () function creates a new diectory named path. The permission bits (modified by the
file creation mask) are set from mode. The owner and group IDs for the directory are set from
the effective user ID and group ID. The new directory may (or may not) contain entries
for . and .. but is otherwise empty.
(v) rmdir - Delete a directory
CALLING SEQUENCE:
#include <unistd.h>
int rmdir
(
const char *pathname
);

DESCRIPTION:
rmdir deletes a directory, which must be empty

(vi) umask - Sets a file creation mask.


CALLING SEQUENCE:
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask
(
mode_t cmask
);

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.

FILE LINKS: HARD AND SOFT LINKS


What is the hard link?
A Hard Link is a copy of the original file that serves as a pointer to the same file, allowing it to
be accessed even if the original file is deleted or relocated. Unlike soft links, modifications to
hard-linked files affect other files, and the hard link remains active even if the source file is
deleted from the system.

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.

What is the soft link?


A soft link is a short pointer file that links a filename to a pathname. It's nothing more than a
shortcut to the original file, much like the Windows OS's shortcut option. The soft link serves as
a pointer to another file without the file's actual contents. It allows the users to delete or the soft
links without affecting the original file's contents.
Comparison between the hard links and soft links

ENVIRONMENT AND PATH SETTING


The environment variables are dynamic values that are stored within a system and used by
applications launched in shells or sub-shells. These variables have a name and their respected
value. The environment variable customizes the system performance and the behavior of an
application.

Common Environment Variables: Some standard environment variables are as follows:


 PATH: This variable contains a list of directories in which our system looks for files. It
separates directories by a (:) colon.
 USER: This variable holds the username.
 HOME: This variable holds the default path to the user's home directory.
 EDITOR: This variable contains the path to the specified editor.
 UID: This variable contains the path to the user's unique id.
 TERM: This variable contains the path to the default terminal emulator.
 SHELL: This variable contains the path to the default shell that is being used by the user.
 ENV: This variable displays the entire environment variable.
How to set Environment Variable in Linux?
There are multiple commands in Linux that allow us to set and create the environment variable.
Use the export command to set a new environment variable. To create a new variable, use the
export command followed by a variable name and its value.
Syntax:
export NAME=VALUE
To create a new variable say new_variable, execute the command as follows:
export new_variable=10
The echo command is used to display the variable:
echo new_variable
To display the value of the variable, use the $ symbol before the variable name:
echo $new_variable

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

Removing an Environment Variable: By removing an environment variable we can remove all


existing component of particular variable. To remove an environment variable, execute
the unset command followed by variable name:
Syntax: unset variable name

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

ADD, MODIFY AND DELETE USERS


Add users: The next command, useradd will work across RHEL based OS distributions as well
as works equally well on Ubuntu/Debian hosts. The simplest syntax (without any extra options)
to create a new user is:
Syntax: $ sudo useradd <username>

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>

You might also like