Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

UNIT-III

Linux file system: Linux files, Inodes and structure and file system, file system components,
standard file system, file system types. Processes in Linux: Starting and Stopping Processes,
Initialization Processes, Mechanism of process creation, Job control in Linux using at, batch,
cron & time.
_________________________________________________________________________
Linux Files: -
A file is a container of text, images, codes, and so on. Everything is a file on a Unix/Linux
system. Not only the data, programs, and applications, but also the directories and
input/output (I/O) devices are considered special kinds of files. Generally, files are ordered
in a hierarchical tree-like structure with a root represented by the character, ‘/’. The
directories are the internal nodes of the tree structure, while the files are considered to be the
leaves.

Types of files: -
There are three types of files in Unix/Linux Operating System: -
1. Ordinary/Regular Files
2. Directory Files
3. Device Files

1. Ordinary/Regular Files: -
An ordinary file is the most common file type containing a stream of data. These files include
data, source programs, objects, executable codes, Unix commands, and any file created by
the user. Commands such as cat and ls are treated as ordinary files. An ordinary file is also
referred to as a regular file. This type of file can be further divided into two types:
• Text file
• Binary file
A text file contains only printable characters. All C and Java program sources, and shell and
Perl scripts, are text files. The characteristic feature of text files is that the data stored inside
them is divided into groups of lines, with each line terminated by the newline character. This
character is not visible, and it does not appear in the hard copy output. It is generated by the
system when we press the <Enter> key. A text file contains lines where each line is
terminated with the linefeed (LF) character, also known as newline. When you press [Enter]
while inserting text in a text editor like vi or pico, the LF character is appended to every line.
You won’t see this character normally, but the cat -e command makes it visible (by showing
a $ at the end of each line).

A binary file, on the other hand, contains both printable and nonprintable characters that
cover the entire ASCII range (0 to 255). Most UNIX commands are binary files, and the
object code and executables that you produce by compiling C programs are also binary files.
Picture, sound, and video files are binary files as well (with few exceptions). Displaying such
files with a simple cat command produces unreadable output and may even disturb your
terminal’s settings.

2. Directory File: -
A directory contains no external data, but it stores some details of the files and sub-directories
it contains. The Unix file system is organized into a number of such directories and sub-
directories, which can also be created as and when needed. Each entry has two components:
• The filename.
• A unique identification number for the file or directory (called the inode number).

We often need to group a set of files pertaining to a specific application. This enables two or
more files in separate directories to have the same filename. If a directory contains, for
example, 10 files, there will be 10 entries in the directory file displaying information such as
size of the file, date and time of creation, or last modification. You can’t, however, write a
directory file, but you can perform some action that makes the kernel write a directory. For
instance, when an ordinary file is created or removed, its entry in the corresponding directory
file is automatically updated by the kernel with the relevant information about the file.
Note: The directory file contains the names of all resident files in the directory. The name of
a file can only be found in its directory. The file itself doesn’t contain its own name or any
of its attributes, like its size or time of last modification.

3. Device File: -
In the Unix operating system, peripheral devices, terminals, printers, CD-ROMs, modems,
disks, and tapes are treated as special files that are termed device files. The representation of
devices in the form of device files simplifies the task of using them. For example, printing
content on a printer is as simple as copying that content to the printer device file.
Note: - A device file interacts with the device driver, making it possible for the user to directly
interact with the device driver using standard I/O system calls, hence controlling the device
more precisely.

There are two types of device files based on how data is read or written into them: -
• Character devices
• Block devices
Character Devices: - Character devices are those in which the read and write operations are
performed character by character, that is, one byte at a time. These devices are also known
as raw devices. The read and write operations in these device fi les are performed in the
actual transfer units of the device, that is, single characters at a time without collecting or
combining them into a block. It is quite obvious that character devices are comparatively
slow and have a large access time.
Examples include virtual terminals, terminals, and serial modems, like Mouse, Keyboard,
Monitor etc.
Block Devices: - Block devices are those in which the read and write operations are
performed one block at a time, where the size of one block can range from 512 bytes to 32
KB. When compared with character devices in which transactions are performed one
character at a time, block devices are quite fast. Moreover, block devices use caching to
reduce the access time.
By caching, we mean that when a block device is accessed, the kernel reads the whole block
into a buffer in the memory, so that future read and write operations are performed to the
cached version in the memory, hence reducing the access time to a great extent. Finally, the
modified buffer contents are written to block devices. The only drawback in using memory
buffers is that if the system crashes before modified buffers are written into the block device,
the data will be inconsistent. Hence, we need to periodically flush out the modified buffers
to the block device.
Examples include hard disk, DVD/CD ROM, and memory regions.
_________________________________________________________________________
The Parent-child relationship: -
All files in UNIX are organized in a hierarchical (an inverted tree) structure as shown in
given figure. This hierarchy has a top called root, which serves as the reference point for all
files. root is actually a directory that is represented by a / (forward-slash). Don’t mix up the
root directory with the user-id root, which is used by the system administrator to log in. The
root directory (/) has a number of subdirectories under it. These subdirectories have more
subdirectories and other files under them.
In the parent-child relationship, parent is always a directory and child can either be a
subdirectory or a file.

File system: -
The file system is organized as a tree with a single root node called root (written as ‘/’); every
non-leaf node of the file system structure is a directory of files, and file at the leaf nodes of
the tree is directories, regular files, or special device files.
The name of a file is indicated by a path name that describes how to locate the file in the file
system hierarchy. A path name is a sequence of component names that are separated by slash
characters.
A component is an arrangement of characters that designates a filename that is uniquely
contained in the previous (directory) component. A full path name starts with a slash
character and specifies a file that can be found by starting at the file system root and
traversing the file tree, following the branches, which lead to successive component names
of the path name.
Unix is an operating system that is divided into directories and sub-directories. The system
programs and libraries are categorized according to their functions and placed in their
respective directories. The forward slash (/) at the top of the tree is the root of the tree.
The following is the list of directories and their contents: -
1. /bin: - Executable files are kept in this directory and these files can be run by users.
After compilation, a program is converted into an executable binary and is usually
kept in the /bin directory. The PATH variable always shows these directories in its
list.
2. /dev: - All the special files in the file system, such as the keyboard or terminal device
drivers, are kept in this directory. These files don’t occupy space on disk. There could
be more subdirectories like pts, dsk in this directory.
3. /home: - All the users are housed here. All the users have their home directory in
/home. Let’s suppose, username is ABC, then home directory will be /home/ABC.
The system administrator often uses a different directory for users-like /user1, /user2
etc.
4. /sbin: - If there is a command that you can’t execute nut the system administrator can,
then it would be probably may not exist, and /etc could then contain all administrative
files. You won’t be able to execute some commands in these directories. Only the
system administrator’s PATH has the administrative commands.
5. /etc: - This directory contains the configuration files of the system. you can change a
very different aspect of the system functioning by editing a text file in this directory.
System not having /sbin and /usr/sbin have the administrative commands in this
directory. All administrative fi les of Unix are kept in this directory.
6. /tmp: - This directory where the users are allowed to create temporary files. These
files are wiped away regularly by the system. Programs usually need extra space to
store data on a disk. The /tmp directory is a directory used by programs that need extra
buffer area in order to be executed.
7. /usr: - This is the directory which is created for all the individual users.
8. /var: - The variable part of the file system. it contains all your print jobs, mail queues
and incoming mails.
9. /lib: - This is the central library storage for files that are commonly used by other
programs. A library is a collection of files (usually binary) that can be shared among
many processes. The advantage of having a library is that it is a single source of data
and each program can use it without needing a unique copy of these executable
functions for itself.
_________________________________________________________________________
Standard File System: -
In real life, most UNIX systems split a disk into as many as eight partitions. Moreover, if
you have multiple disks, every such disk must have at least one file system on it. In general,
most UNIX systems will always have these two file systems: -

1. root: - This file system must be present in every system. It contains the bare-bones
UNIX-root directory, /bin, /usr/bin, /etc, /dev, /tmp, /lib directories-all the root and
utilities that are just adequate to keep the system going. When the system is booted in
single-user mode, this is the only file system available to the system administrator.
2. swap: - Every system should have a swap file system which is used by the kernel to
control the movement of processes. When the system memory is heavily loaded, the
kernel has to move processes out of the memory to this file system. When these
swapped processes are ready to run, they are loaded back to memory. Users can’t
access data in this file system directly. It includes the concept of swap area.

Swapping is a useful technique that enables us to execute programs and manage files
that are larger than the computer’s primary memory. The program or file that we wish
to execute or manage is logically split into small blocks that are known as pages or
segments. One of the blocks is loaded into the primary memory where it can be worked
on, whereas the remaining blocks of the program or file are stored in the physical disk
drive. When a part of the program or file that is not in the primary memory is required,
swapping takes place.
Swapping is an operation by which the block of the program or file in the primary
memory is swapped out to the physical disk drive and the next block from the physical
disk drive is loaded into the primary memory. Swapping continues until the desired
block is loaded in the primary memory. The concept by which a system appears to
have more memory than what it actually has is known as virtual memory.
In the Unix operating system, a partitition of hard disk can be treated as virtual
memory. Thus, a separate partition known as swap partition is created on the disk that
is meant to hold the swapped pages of the program or file. Every system should have
a swap file system that is used by the kernel to control the movement of processes.
When the system memory is heavily loaded, the kernel has to move processes out of
the memory to this fi le system. When these swapped processes are ready to run, they
are loaded back to the memory. Users cannot access this file system directly.
System Files should be kept from data files created by the users
_________________________________________________________________________
File System Types: -
In simple terms, the filesystem manages how files are stored and accessed on devices. 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. It fetches files when needed and lets you
take actions on them. Therefore, you want a good filesystem to: -
• Be fast at accessing files on all kinds of devices.
• Be efficient in storing files on disk drives.
Command to check the supported filesystem types is as follows: -

Various types of file system are as: - ext2, ext3, swap, bfs, pcfs etc.
In Linux, the file system creates a tree structure. All the files are arranged as a tree and its
branches. The topmost directory called the root (/) directory. All other directories in Linux
can be accessed from the root directory.
Some key features of file system are as following:
o Specifying paths: Linux does not use the backslash (\) to separate the components; it
uses forward slash (/) as an alternative. For example, as in Windows, the data may be
stored in C:\ My Documents\ Work, whereas, in Linux, it would be stored in /home/
My Document/ Work.
o Case Sensitivity: Linux file system is case sensitive. It distinguishes between
lowercase and uppercase file names. Such as, there is a difference between test.txt and
Test.txt in Linux. This rule is also applied for directories and Linux commands.
o File Extensions: In Linux, a file may have the extension '.txt,' but it is not necessary
that a file should have a file extension. While working with Shell, it creates some
problems for the beginners to differentiate between files and directories. If we use the
graphical file manager, it symbolizes the files and folders.
o Hidden files: Linux distinguishes between standard files and hidden files, mostly the
configuration files are hidden in Linux OS. Usually, we don't need to access or read
the hidden files. The hidden files in Linux are represented by a dot (.) before the file
name (e.g., .ignore). To access the files, we need to change the view in the file manager
or need to use a specific command in the shell.
Various file system types are as: -
1. Ext, Ext2, Ext3 and Ext4 file system: - The file system Ext stands for Extended File
System. It was primarily developed for MINIX OS. The Ext file system is an older
version, and is no longer used due to some limitations.
Ext2 is the first Linux file system that allows managing two terabytes of data. Ext3 is
developed through Ext2; it is an upgraded version of Ext2 and contains backward
compatibility. The major drawback of Ext3 is that it does not support servers because
this file system does not support file recovery and disk snapshot.
Ext4 file system is the faster file system among all the Ext file systems. It is a very
compatible option for the SSD (solid-state drive) disks, and it is the default file system
in Linux distribution.
2. JFS File System: - JFS stands for Journaled File System, and it is developed
by IBM. It is an alternative to the Ext file system. It can also be used in place of Ext4,
where stability is needed with few resources. It is a handy file system when CPU
power is limited.
3. ReiserFS File System: - ReiserFS is an alternative to the Ext3 file system. It has
improved performance and advanced features. In the earlier time, the ReiserFS was
used as the default file system in SUSE Linux, but later it has changed some policies,
so SUSE returned to Ext3. This file system dynamically supports the file extension,
but it has some drawbacks in performance.

4. XFS File System: - XFS file system was considered as high-speed JFS, which is
developed for parallel I/O processing. NASA still using this file system with its high
storage server (300+ Terabyte server).
5. Btrfs File System: - Btrfs stands for the B tree file system. It is used for fault
tolerance, repair system, fun administration, extensive storage configuration, and
more.
6. Swap File System: - The swap file system is used for memory paging in Linux
operating system during the system hibernation. A system that never goes in hibernate
state is required to have swap space equal to its RAM size.
7. msdos file system: - Most Unix systems also support this file system. You can use
this file system pn floppy diskette and transfer files to it for use on a Windows system.
Linux and Solaris can also access the DOS filesystem in the hard disk.

_________________________________________________________________________
Structure/Components of File System: -

A file system is a group of files and directories that exists in the form of a tree-like structure
with its root in the form of a root directory. A hard disk can be partitioned into several parts
with each part having its own file system; there can be several file systems in a hard disk. A
file system cannot be split on two different disks; it has to be entirely on a single disk. A file
system has the following four sections that are known as blocks: boot, super, inode, and data.
The first block of a file system is called the boot block, and it is followed by super, inode,
and data blocks, as shown below: -

Boot Block: -
The boot block is the first block of the file system that contains a small bootstrap program.
The bootstrap program is a short program that is loaded by the basic input/output system
(BIOS) on starting any computer. It checks and initializes the I/O devices and also loads the
operating system. During bootstrapping, which is also known as booting, the master boot
record (MBR) is loaded into the memory, which in turn, loads the kernel into the memory.

Super Block: - The boot block is followed by the super block. This block contains global
file information about disk usage and availability of data blocks and inodes. It also contains
a pointer to the head of the free list of data blocks.
The super block consists of the following information: -
• Size of the file system
• Number of free data blocks available in the file system
• List of free data blocks available in the file system
• The index of the next free data block in the free data block list
• Size of the inode list
• Number of free inodes available in the file system
• List of free inodes in the file system
• The index of the next free inode in the free inode list
The following figure shows a super block containing an array of free data block numbers and
a pointer to the head of the free list of data blocks.

The values 109, 125, 104, 175, and 138 in this figure refer to the free data block numbers.
The entry 109 is a data block that contains a pointer to an array of free data blocks. We
assume that the index, which points to the next free data block in the free data block list, is
pointing to the last data block number, which is 138.

When a process requests for a data block, it searches the free data blocks in the super block
and returns the available data block pointed to by the index, that is, the data block number
138 will be returned and the index will shift to another data block in the list. Thus, after
having assigned data block 138 to the requesting process, the index will shift to the point at
the data block numbered 175, and the procedure will continue. If the super block contains
only one entry, which is a pointer to the array of free data blocks, all the entries from that
block will be copied to the super block free list as shown in Fig.
As usual, the requesting process will continue to get block numbers from the ones listed in
the super block.

Inode Block: -
Every file or directory has an inode number—a unique number that recognizes the file or
directory in the file system. All inodes are stored in inode blocks. We know that a hard disk
is organized into blocks (or sectors) and a file stored in a hard disk may be scattered in
different blocks. The addresses of these blocks (containing file parts) are stored in the form
of a linked list in an inode block, that is, a table of blocks is maintained for each file.

Each Unix file system has its own inode table in which inode blocks are stored. Each inode
is referenced by a device + inode number pair. There are three reserved inode numbers—0,
1, and 2—defined as follows: -
• 0 refers to the deleted files and directories.
• 1 refers to file system creation time, bad blocks count, and so on.
• 2 refers to the root directory of the fi le system.
The inode block contains information on each file in the data block. The information
comprises owner of the file, file type, permissions, address of the file, and so on. Each inode
block usually contains the following entries:
• Owner: Indicates the owner of the inode
• Group: Indicates the group to which the inode belongs
• File type: Indicates the type of file, that is, whether the inode represents a file, a
directory, a FIFO, a character device, or a block device (The type value is set to 0 to
indicate that the inode is free).
• Permissions: Indicates the read, write, and execute permissions of the owner (user),
group, and other members, for the file
• Access time: Indicates the time at which the file was last accessed
• Modification time: Indicates the time at which the file was last modified
• Inode modification time: Indicates the time at which the inode was last modified. (The
inode is modified when the contents of the file are changed, the permission of the file
is changed, link for the file is created, and so on.)
• Number of links to the file: Indicates the number of links of the file
• Physical addresses: Indicates the blocks containing the file parts
• Size: Indicates the actual size of the file
A file’s inode number can be found using the ls -i command.

The Unix system also maintains an inode table in the memory for a file that it uses. When
the file is opened, its inode is copied from the hard disk to the system’s inode table.

Note: The inode contains all the attributes of a file except the filename. The filename is
stored in the directory in which the file is kept. The i number is also not stored in the inode,
but is used to locate the position of the inode in the inode blocks.

Data Block: -
The actual data is stored in data blocks. Apart from these direct data blocks, there are also
indirect blocks that contain the addresses of the direct blocks. The inode maintains a list of
these indirect block addresses. When a file is created, the kernel looks up the list available
in the super block to look for a free inode. The efficiency of the system is increased to a great
extent, as the list is always updated, and hence, quite reliable. The kernel reads and writes
the copy of the super block in the memory when controlling the allocation of inodes and data
blocks. The information in the fi le system needs to be written to the disk before the power
of the
Unix system is turned off. The system checks for a possible mismatch during booting. Since
the kernel works with the memory copy of the superblock rather than with the disk copy, the
kernel updates the disk copy with the memory copy. This is done with the sync operation.

Note: The kernel always maintains a copy of the superblock in the memory. The in-memory
copy actually contains the latest and the correct fi le system status rather than its disk copy.

The information stored in the inode table changes whenever we use any fi le or change its
permissions; hence, a copy of the super block and inode table are kept in the memory (RAM)
at start-up time, and all changes are made in the RAM copies of the super block and inode
table every time some modification occurs. The original super block and inode table in the
disk are updated after a fixed interval of time, say every 30 seconds, by a command called
sync. This command synchronizes the inode table in the memory with the one on the disk by
simply overwriting the memory copy on to the disk.

_________________________________________________________________________
Accessing File System: -
The Linux File system hierarchy is arranged in a tree, with the file system starting from the
root directory (/). All other child file systems branch out from the root directory.

Let us assume we have a device such as a floppy or CD containing a few files, and we want
to view and access those files. The files on these devices make up an individual file system
with its root as ‘/’. The file system on these devices will not be accessible by the Linux
system unless it is mounted.
Mounting is a procedure of making the main existing file system aware of the new file
system.
Mounting file system: -
By mounting a file system of any device, we make its file and directories accessible through
the existing Linux file system. the format of mounting a file system is as follows: -
Syntax: - mount filesystem/device-name directory
The device name or file system is mounted on the given directory. The directory, also known
as mount point, is the name of the directory that the newly mounted file system will be
assigned to.

Note: For the file system to be mounted on a particular directory, the directory should already
exist on the current file system.

In order to mount a file system that has the special device name /dev/fd00 (for floppy disk
0) onto the existing /mnt directory, the following command is used:

#mount /dev/fd00 /mnt


The new file system is simply an extension of the /mnt directory. We can view and access
the files and directories of the mounted file system by changing the directory to the /mnt
directory. We can also create directories and files in the /mnt directory sub-tree.

The mount point (/mnt) should usually be an empty directory, as we will not be able to access
its original files and subdirectories once a file system is mounted on it. The files of the /mnt
directory will be accessible only when the file system is unmounted.
The file system that is mounted to the main file system should be unmounted after its job is
done. Before shutting down the Unix system, all the mounted file systems need to be
unmounted; otherwise, this may result in corruption of the content.

Unmounting the file system: -


Unmounting a file system means detaching the mounted file system from the directory of the
Linux system on which it was mounted. Once a file system is unmounted, we’ll not be able
to access its files or directories.
A file system cannot be unmounted if any of its files or directories are still active, that is, if
they are currently in use. If we try unmounting a file system whose file or directory is
currently open, we get an error message, ‘device busy’.
To unmount a file system that is mounted on any directory, first of all, we need to close all
the open files and directories and then proceed to unmount it. We use the following command
without any arguments, in order to know the file systems mounted within a file system:
#mount
It’ll give a list of the mounted file system, we might obtain the following output: -

This output shows that the file system /dev/fd00 is mounted on the /mnt directory.

The command that is used for unmounting the mounted file system is ‘umount’. The
following format is adopted for using the umount command: -
#unmount filesystem-name/mount-point
Example: - the command to unmount the file system, /dev/fd00 that we mounted on
/mnt directory will be as follows: -
#unmount /mnt

_________________________________________________________________________
Processes in Linux: -
A process is simply an instance of a running program. It is said to be born when the program
starts execution and remains alive as long as the program is active. After execution is
complete, the process is said to die. A process also has a name, usually the name of the
program being executed. For example, when you execute the grep command, a process
named grep is created.
The first process in the Unix system, also known as process 0, is related to bootstrapping.
The process of starting a computer is known as bootstrapping or booting. During
bootstrapping, a computer runs a self-test, and loads a boot program into the memory from
the boot device. The boot program loads the kernel and passes the control to the kernel, which
in turn, configures the devices, performs hardware status verification, detects new hardware,
and initializes the existing devices and system processes.

The shell serves the user, but the kernel handles processes. It manages memory and schedules
processes so that each process has a fair share of the CPU and other resources. It provides a
mechanism by which a process is able to execute for a finite period of time and then
relinquish control to another process. The kernel has to save the state of the current process
(like the instruction it was currently executing) so that when its turn comes up again for
execution, the kernel knows where to resume. All of this happens more than once a second,
making the user oblivious to the switching process.

Most process attributes are stored in the process table, a separate structure maintained in
memory by the kernel. A process retains an entry in this table until it dies “properly.” Because
the table is of finite size, there is a limit to the maximum number of processes that can run
on a system.
A process operates in either user mode or kernel mode: -
• User mode: - User mode is the mode in which processes related to user activities get
executed. Commands, programs, utilities, etc., executed by the user are run in this
mode. These processes being trivial in nature, the code in the user mode runs in a non-
privileged protection mode. Switching from user to kernel mode takes place either
when a user’s process requests services from the operating system by making a system
call or when some interrupt occurs during the events such as timers, keyboard, and
hard disk input/output (I/O).
• Kernel mode: - In kernel mode, the system processes, that is, the processes related to
managing a computer system and its resources get executed. The processes used to
allocate memory to access hardware peripherals such as printer and disk drive run in
this mode. These processes are critical in nature, that is, they can make an operating
system inconsistent if they are not handled properly. Hence for security reasons, these
processes are run in a privileged protection mode.
The user and kernel modes can be better understood with the help of a block diagram of the
kernel architecture.

In the Fig., the users initially execute their processes in the user mode. When the user process
needs some kernel service (such as accessing memory, disk fi le, printer, or other hardware
peripherals), it interacts with the kernel through the system call. System calls are functions
that run in the kernel mode. Hence while executing system calls, the user process switches
from the user mode to the kernel mode.
_________________________________________________________________________
Starting and stopping the processes: -
A program/command when executed, a special instance is provided by the system to the
process. This instance consists of all the services/resources that may be utilized by the
process under execution.
• Whenever a command is issued in Unix/Linux, it creates/starts a new process. For
example, pwd when issued which is used to list the current directory location the user
is in, a process starts.
• Each process in the system has a unique PID.
• Used up pid’s can be used in again for a newer process since all the possible
combinations are used.
• At any point of time, no two processes with the same pid exist in the system because
it is the pid that Unix uses to track each process.

A process is created through the fork command, and depending on the availability of the
primary memory, it is either kept in the memory in ready to run state or is swapped out to
the secondary memory in ready to run swapped out state.
Note: When a process is required, the space for it is created in the primary memory and is
swapped into the primary memory by the swapper switching it to the ready to run state. The
process in the pre-empted state returns to the user mode, that is, the user running state when
it is required by the user. The process running in the user mode switches to the kernel mode
when an interrupt occurs, a system call is made to access operating system services, or when
some fault or exception occurs.
Note: The scheduler decides the process that has to be submitted next to the CPU for action.

Stopping the Process: -


Besides the built-in processes that are auto created on booting the system, we can also create
our own processes. In addition, the processes can be terminated after their tasks are
completed in order to release the resources acquired by them.
A process consumes system resources, such as memory, disk space, and CPU time. If there
is more than one process running at a time, the kernel allocates system resources to one
process, while keeping other processes waiting
Stopping a process means cancelling the ongoing execution of a command. Consider a
situation where a command is taking a long time to complete or where we do not want to
execute a background job any more, or where a program has gone into an infinite loop. In
such a situation, we are left with no other option but to kill (terminate) the job. For this, kill
command is used.

_________________________________________________________________________
Initialization (init) Process: -
Booting Process is termed as the first process with process id: process 0.
The kernel, once it is loaded, finds init in sbin and executes it. When init starts, it becomes
the parent or grandparent of all of the processes that start up automatically on your Linux
system. The PID of init process is 1.
Whenever you execute any command on shell, shell creates a child process to execute the
respective command.
Example: - You want to execute ‘cat’ command on shell, then shell will create a child process
to execute cat command. Now, parent of ‘cat’ command is shell and parent of shell is init.

The init reads its initialization file, /etc/inittab. This instructs init to read an initial
configuration script for the environment, which sets the path, starts swapping, checks the file
systems, setting the clock, initializing serial ports and so on.
Then init continues to read the /etc/inittab file, which describes how the system should be
set up in each run level and sets the default run level. A run level is a configuration of
processes. All UNIX-like systems can be run in different process configurations, such as the
single user mode.

Run level: - Run level defines the state of the machine after boot. It is a way to start and stop
groups of services used in init. Like, ‘systemd’ provides a compatibility layer that maps run
level to targets, and associated binaries. Nevertheless, only one runlevel can be “active” at a
given time, while system can activate multiple targets concurrently, so the mapping to
runlevels is so confusing. Systems administrators set the default run level of a system
according to their needs, or use the run level command to find out the machine's current run
level to assess a system.
For example, the run level can indicate whether or not the system's network is operational.
Use the run level command /sbin/runlevel to find the current and previous run level of an
operating system.
Runlevels zero through six are generally delegated to single-user mode, multi-user mode
with and without network services started, system shutdown and system reboot. The setup
of these configurations differs between Linux distros and Unix versions.
Each basic level has a different purpose. Run levels 0, 1, 6 are always the same. Run levels
2 to 5 are different depending upon the Linux distribution in use. Only one run level is
executed when the system is booted. They are not implemented sequentially. For example,
either run level 4 or 5 or 6 is executed, not 4 then 5 then 6.

Runlevel No. Target


Runlevel 0 shuts down the system [Poweroff.target]
Runlevel 1 single-user mode target [system administrator]
Runlevel 2, 3, 4 multi-user mode target
Runlevel 5 Graphical Target
Runlevel 6 reboots the system to restart it [reboot.target]
//Command to know the current run level: - Either use “runlevel” or “who -r”

Note: - Ubuntu is the latest Linux distribution to switch to systemd. It means that it uses systemd, instead of using
init process. All the attributes of process are stored in the process table. In order to fetch these
attributes, ps command is used.
When you log in, the process representing the shell starts running at your terminal. This
process may be sh, ksh, csh, or bash. The shell maintains a set of environment variables,
and you have already encountered some of them like PATH and HOME. The shell’s own
pathname is stored in SHELL, but its PID is stored in a special “variable”, $$. To know the
PID of your current shell, type: - echo $$

The PID of the current shell is: - 2646


Knowledge of the PID is often necessary to control the activities at your terminal, especially
when things go wrong. The PPID of every login shell is always 1. This is the init process:
the second process of the system. init is a very important process and, apart from being the
parent of users’ shells, it is also responsible for giving birth to every service that’s running
in the system—like printing, mail, Web, and so on.

_________________________________________________________________________
Mechanism of Process Creation: -
A process can only be created by another process, and the creation mechanism involves three
phases. We call them fork, exec, and wait, mostly named after system calls of the same
name. The three phases work as follows: -
• Fork: - Forking creates a process by creating a copy of the existing process. The new
process has a different PID, and the process that created it becomes its parent.
Otherwise, parent and child have the same process image. If the child doesn’t do an
exec, both parent and child continue to execute the same code from the point forking
was invoked.
• Exec: - Forking creates a process, but it is not enough to run a new program. To do
that, the forked child needs to overwrite its own image with the code and data of the
new program. This mechanism is called exec, and the child process is said to exec a
new program. No new process is created here; the PID and PPID of the exec’d process
remain unchanged.
• Wait: - While the child is executing a new program, the parent normally waits for the
child to die. It then picks up the exit status of the child (explained shortly) before it
does something else.
To use an example, when you run cat from the shell, the shell first forks another shell process.
The newly forked shell then overlays itself with the executable image of cat, which then
starts to run. The parent (the original shell) waits for cat to terminate and then picks up the
exit status of the child. This is a number returned by the child to the kernel, and has great
significance in both shell programming and systems programming.
_________________________________________________________________________
Job control in Linux: -
Note: - ps command is used to display a list of your processes that are currently running
and obtain additional information about those processes.

1. Job scheduling using: - at command


The at command is used for executing Unix commands at a specific date and time.
Tasks such as taking backup of the disk at regular intervals or sending mail messages
at odd hours can be easily accomplished using the at command. We can specify the
Unix commands (to be executed) at the command prompt or save them in a file and
use the file to execute the commands.
Syntax: - at -options time
Option Purpose
-f filename It reads commands from the file i.e. to be executed instead of
std. input
-l It lists the commands that are scheduled to run
-r To cancels the scheduled command
Time By default, specify the time for execution of commands in the
24-hour format
-m It sends mail to user when the commands are executed
In the given example, time 22:34 is provided as an argument to the at command. After
pressing enter key, tasks, which are to be performed are specified such that: -
• creation of empty ‘text1’ file using touch command and
• saving the output of echo in ‘text1file’
Now, Checking the files as: -

Now, fetching the commands from a file. So, -f with filename option is to be used as: -

In the given example, one.sh shell file is created. In order to provide the execution command
for one.sh file, command i.e. sh one.sh is provided in the file ‘one’ and command for the
creation of file too. After one minute, check the ‘twwo’ whether the content is updated or
not and also check the file 'twwo1’ whether the specified file is created or not.

Now, using ‘-l’ option to check the scheduled commands: -

Here, after executing this command, it is clear that job number 23 and 24 are there in the
queue for their successful execution that the specified time.
Now, using ‘-r’ option for removing the specified job number from the queue of execution.

In the given figure, 23 and 24 is specified for their removal from the execution list. These
jobs can be removed before their execution.
The command ‘atq’ lists the jobs that are scheduled to run, similar to the ‘at -l’
command.
The command ‘atrm’ is used to delete the specified job number, similar to the ‘at -r’
command.
_________________________________________________________________________

2. Job scheduling using: - batch command


The batch command also schedules the job for later execution. As the name implies, the
batch command is used for issuing a set of commands that we want to execute collectively
(in a batch). The commands given in the batch will be executed later when the system
load permits, that is, when the CPU is free, it will execute the commands specified by the
batch command.
This command does not take any arguments but uses internal algorithm to determine the
execution time. This prevents too many CPU-hungry jobs from running at the same time.
Syntax: - batch

The response of batch is similar to the ‘at’ command. We can fetch the batch of commands
from a file too and file can be specified using redirection operator.
Note: - Restricting Use of at and batch
Not all users may be able to use the at and batch commands. The access to these
commands is restricted and controlled by the files at.allow and at.deny. The locations are
system-dependent; look up the FILES section of the man page of at for the location of the
files. If they exist at all, they could be in /etc, /etc/cron.d or /usr/lib/cron. They can only
be edited by the superuser.
The file at.allow controls the primary level of security. If it is present, only the users listed
in the file are permitted to use at and batch. If it is not present, the system checks at.deny
for a list of users who are barred from using these commands. If neither file is present,
only the system administrator is permitted to invoke at and batch
_________________________________________________________________________
3. Job scheduling using: cron command: -
The command, cron is a daemon that keeps running and fires every minute, that is, it gets
activated every minute and opens its special file to check if there are any processes
waiting to be executed in that particular minute. If none of the processes is found waiting,
it goes to sleep again (to fire in the next minute). If there are any processes to be executed
in that minute, it executes them and again goes to sleep. This daemon continues to execute
until the Unix system shuts down.
The cron automatically starts when the Unix system boots. The special file that is opened
by the cron to view the list of processes that are required to be executed is stored in the
/usr/spool/cron/crontabs directory.

We can also create our own crontab file containing the list of processes along with their
schedule. Let us see how a crontab file is created. The location of file is system-dependent.

The main difference between at, batch and cron command is that the command at and
batch are means for one-time execution but cron executes programs at a regular interval.

Each crontab file contains a list of commands along with the schedule of execution. The
Components of a crontab entry is as: -

There are six fields in the line, and the first five completely determine how often the
command will be executed. The following list shows the significance of the fields with
their permissible values shown in parentheses:
1 The minute (00 to 59)
2 The hour (0 to 23)
3 The day (0 to maximum number of days in month)
4 The month (1 to 12)
5 The day of the week (0 to 6, 0 being a Sunday)
6 Command to run
This find command runs at 18:15 hours on June 30 every year. We didn’t make use of the
fifth field (which doesn’t have much relevance here) and preferred to place a * there. When
you specify */5 in minute field means every 5 minutes. When you specify 0-10/2 in minute
field mean every 2 minutes in the first 10 minute.
Creating crontab entry: -
1. Create a file containing all 6 fields.
2. Then use ‘crontab’ commands: -
Syntax: - crontab -option filename
Option Purpose
-l Displays the content of crontab file
-r Removes the crontab file
-e Edits the crontab file using editor
Filename Contains commands and their schedule.

Now, creating a file, named as cron1, containing the commands and schedules of the
commands using cat commands. Then, crontab commands is used as: -
Using the ‘l’ option to display the content of specified file or to display the commands
which are scheduled to be executed using crontab command: -

In order to edit the commands, specified earlier, ‘e’ option is used with the crontab
command. With this, vi editor can be opened as: -

To remove or delete the crontab file, use crontab command with ‘r’ option: -
Example: -

By providing ‘*’ in all the five fields, it’ll execute the specified command in every 15
seconds. To edit the crontab, “crontab -e”: -

After editing crontab, check the list using “crontab -l”: -

Checking whether behappyfile and happy1file file created or not: -

_________________________________________________________________________
4. Job scheduling using: time command: -
To find out how efficiently the programs use the system resources, the time command
accepts a command to be timed as its arguments and does this work. It executes the
program and also displays the time usage on the terminal. This enables the programmer
to tune their program to keep CPU usage at an optimal level.

Syntax: - time command


Here, it specifies the command that you want to execute and want to check its efficiency
level.
It provides time value using three instances: -
1. Real time shows the clock elapsed time from invocation of the command till its
termination. This time may differ on multiuser system where several programs may
be running concurrently.
2. User time shows the time spent by the programs in executing itself i.e., in user mode.
3. Sys time indicates the time spent by the system in doing work on behalf of the user
i.e., in kernel mode.
The sum of user and system time actually represents the CPU time. But it is not necessary
that this sum be equal to real time since some time is consumed in doing other work in the
system. The more heavily loaded a system is, the greater is the difference.
_________________________________________________________________________
Question Bank: -
1. Explain Linux files. What are the types of Linux files. Explain further
categorisation of regular files and device files
2. Explain the structure of file system.
3. How to access the file system?
OR
What do you mean by mounting and unmounting the file system?
4. What is meant by parent-child relationship in Linux? Explain the file system
and its types.
5. What do you mean by starting and stopping a process?
6. What is a process. Explain the process creation mechanism in Linux.
7. Explain initialization process and various run level in Linux with their
respective commands.
8. Explain standard file system in Linux.
9. Explain various job controlling commands in Linux.
10.Explain the following commands with example: -
i) at
ii) batch
iii) time
iv) crontab
11. Implement the following job scheduling as per given specification: -
i) To execute the commands in the shell file, shell1.sh on 25th Sept at 5:47 p.m.
ii) To schedule the execution of script shell2.sh on 27 Nov, 2022 at 5:36 p.m.
using ‘at’ command
iii) To execute the commands in shell file, shell3.sh in a batch
iv) To schedule the job of deleting all files with extension .txt on 1 January and
1 July every year
v) To see the list of commands supplied to the crontab file
vi) Frame a crontab entry to execute the connect.sh script every 30 minutes on
every Monday, Wednesday, and Friday between the times of 8 a.m. and 6
p.m.

You might also like