Linux - Q & A
Linux - Q & A
Linux - Q & A
Kernel − Kernel is the core part of Linux. It is responsible for all major
activities of this operating system. It consists of various modules and it
interacts directly with the underlying hardware. Kernel provides the required
abstraction to hide low level hardware details to system or application
programs.
Architecture
The following illustration shows the architecture of a Linux system −
Utilities − Utility programs that provide the user most of the functionalities
of an operating systems.
1. What is kernel in Linux ?
The kernel is the core program that runs programs and manages hardware
devices such as disks and printers. It executes the commands which are
provided by the environment. Kernel provides the interface between the shell
and hardware.
A kernel is really nothing more than a resource manager. Whether the resource
being managed is a process, memory, or hardware device, the kernel manages
and arbitrates access to the resource between multiple competing users (both in
the kernel and in user space).
In order to manage all those files in an orderly fashion, it is arranged in an ordered tree-
like structure on the hard disk, as we know from MS-DOS (Disk Operating System) for
instance. The large branches contain more branches, and the branches at the end contain
the tree's leaves or normal files.
The tree of the file system starts at the trunk or slash, indicated by a forward slash (/).
This directory, containing all underlying directories and files, is also called the root
directory or "the root" of the file system.
Directories that are only one level below the root directory are often preceded by a slash,
to indicate their position and prevent confusion with other directories that could have the
same name.
Special files: the mechanism used for input and output. Most special files are
in /dev
Named pipes: act more or less like sockets and form a way for processes to
communicate with each other, without using network socket semantics.
1. / – Root
Every single file and directory starts from the root directory.
Only root user has write privilege under this directory.
Please note that /root is root user’s home directory, which is not same as /.
2. /bin – User Binaries
Contains binary executables.
Common linux commands you need to use in single-user modes are located
under this directory.
Commands used by all the users of the system are located here.
For example: ps, ls, ping, grep, cp.
When Linux is running in the main memory it is divided into two parts – User space and
Kernel space.
At the top is the user, or application, space. This is where the user applications
are executed. Below the user space is the kernel space. Here, the Linux kernel
exists.
There is also the GNU C Library (glibc). This provides the system call interface
that connects to the kernel and provides the mechanism to transition between the
user-space application and the kernel. This is important because the kernel and
user application occupy different protected address spaces. And while each user-
space process occupies its own virtual address space, the kernel occupies a
single address space.
The Linux kernel can be further divided into three gross levels. At the top is the
system call interface, which implements the basic functions such as
read and write. Below the system call interface is the kernel code, which can be
more accurately defined as the architecture-independent kernel code. This code
is common to all of the processor architectures supported by Linux. Below this is
the architecture-dependent code, which forms what is more commonly called a
BSP (Board Support Package). This code serves as the processor and platform-
specific code for the given architecture.
At a high level, and at lower levels, the kernel is layered into a number of
distinct subsystems. Linux can also be considered monolithic because it
lumps all of the basic services into the kernel. This differs from a
microkernel architecture where the kernel provides basic services such as
communication, I/O, and memory and process management, and more
specific services are plugged in to the microkernel layer.
The SCI is a thin layer that provides the means to perform function calls from user
space into the kernel. As discussed previously, this interface can be architecture
dependent, even within the same processor family. The SCI is actually an interesting
function-call multiplexing and demultiplexing service. The SCI implementation can be
found in ./linux/kernel, as well as architecture-dependent portions in ./linux/arch.
Process management
The kernel is in charge of creating and destroying processes and handling their
connection to the outside world(input and output). Communication among different
processes (through signals, pipes or inter process communication primitives is basic
to the overall system functionality and is also handled by the kernel. In addition, the
scheduler, which controls how processes share the CPU, is part of process
management. You can find the process management sources in ./linux/kernel
and architecture-dependent sources in ./linux/arch).
Memory management
Another important resource that's managed by the kernel is memory. For efficiency,
the hardware manages virtual memory, memory is managed in what are
called pages (4KB in size for most architectures). Linux includes the means to
manage the available memory, as well as the hardware mechanisms for physical
and virtual mappings. You can find the memory management sources in ./linux/mm
The virtual file system (VFS) is an interesting aspect of the Linux kernel because it
provides a common interface abstraction for file systems. The VFS provides a
switching layer between the SCI and the file systems supported by the kernel
At the top of the VFS is a common API abstraction of functions such as open, close,
read, and write. At the bottom of the VFS are the file system abstractions that define
how the upper-layer functions are implemented. These are plug-ins for the given file
system (of which over 50 exist). You can find the file system sources in ./linux/fs.
Below the file system layer is the buffer cache, which provides a common set of
functions to the file system layer (independent of any particular file system). This
caching layer optimizes access to the physical devices by keeping data around for a
short time. Below the buffer cache are the device drivers, which implement the
interface for the particular physical device.
Network stack
The network stack, by design, follows a layered architecture modelled after the
protocols themselves. Above TCP is the sockets layer, which is invoked through the
SCI.
The sockets layer is the standard API to the networking subsystem and provides a
user interface to a variety of networking protocols. From raw frame access to IP
protocol data units (PDUs) and up to TCP and the User Datagram Protocol (UDP),
the sockets layer provides a standardized way to manage connections and move
data between endpoints. You can find the networking sources in the kernel at
./linux/net.
Device drivers
The vast majority of the source code in the Linux kernel exists in device drivers that
make a particular hardware device usable. The Linux source tree provides a drivers
subdirectory that is further divided by the various devices that are supported, such as
Bluetooth, I2C, serial, and so on. You can find the device driver sources in
./linux/drivers.
Architecture-dependent code
While much of Linux is independent of the architecture on which it runs, there are
elements that must consider the architecture for normal operation and for efficiency.
The ./linux/arch subdirectory defines the architecture-dependent portion of the kernel
source contained in a number of subdirectories that are specific to the architecture
(collectively forming the BSP. Each architecture subdirectory contains a number of
other subdirectories that focus on a particular aspect of the kernel, such as boot,
kernel, memory management, and others. You can find the architecture-dependent
code in ./linux/arch.
Variab Description
le
echo $VARIABLE
Variables are- Case Sensitive. Make sure that you type the variable
name in the right letter case otherwise you may not get the desired
results.
VARIABLE_NAME= variable_value
Command Description
bzip2 filename
bunzip2 filename.bz2
gzip filename
gunzip filename.gz
The above command compresses file1, file2, file3, and the contents of
the /usr/work/school/ directory (assuming this directory exists) and places them in
a file named filename.gz.
unzip filename.zip
You can use zip to compress multiple files and directories at the same time by
listing them with a space between each one:
The above command compresses file1, file2, file3, and the contents of
the /usr/work/school/ directory (assuming this directory exists) and places them in
a file named filename.zip.
Archiving Files at the Shell Prompt
A tar file is a collection of several files and/or directories in one file. This is a good
way to create backups and archives.
Some of tar's options include:
-c — create a new archive
-f — when used with the -c option, use the filename specified for the creation
of the tar file; when used with the -x option, unarchive the specified file
-t — show the list of files in the tar file
-v — show the progress of the files being archived
-x — extract files from an archive
-z — compress the tar file with gzip
-j — compress the tar file with bzip2
To create a tar file, enter:
The above command places all the files in the work and the school subdirectories
of /home/mine in a new file called filename.tar in the current directory.
To list the contents of a tar file, enter:
This command does not remove the tar file, but it places copies of its unarchived
contents in the current working directory, preserving any directory structure that the
archive file used. For example, if the tarfile contains a file called bar.txt within a
directory called foo/, then extracting the archive file results in the creation of the
directory foo/ in your current working directory with the file bar.txt inside of it.
Remember, the tar command does not compress the files by default. To create a
tarred and bzipped compressed file, use the -j option:
tar files compressed with bzip2 are conventionally given the extension .tbz;
however, sometimes users archive their files using the tar.bz2 extension.
The above command creates an archive file and then compresses it as the
file filename.tbz. If you uncompress the filename.tbz file with
the bunzip2 command, thefilename.tbz file is removed and replaced
with filename.tar.
You can also expand and unarchive a bzip tar file in one command:
tar files compressed with gzip are conventionally given the extension .tgz.
This command creates the archive file filename.tar and compresses it as the
file filename.tgz. (The file filename.tar is not saved.) If you uncompress
the filename.tgz file with the gunzip command, the filename.tgz file is removed
and replaced with filename.tar.
You can expand a gzip tar file in one command:
What are different Run levels in Linux and how to switch between them?
Run levels:-Run levels define what processes or services to run automatically while
the system boots up. This is defined in /etc/inittab file.
The init process is the last step in the boot procedure and has pid of ‘1’.’ “init” is
responsible for starting system processes as per defined in the /etc/inittab file.
“init” process checks which default run level is defined in /etc/inittab and starts the
system in that run level which means all the services defined for that run level gets
executed.
There are 7 different run levels present (run level 0-6) in Linux system for different
purpose. The descriptions are given below.
0: Halt System (To shutdown the system)
1: Single user mode
2: Basic multi user mode without NFS
3: Full multi user mode (text based)
4: unused
5: Multi user mode with Graphical User Interface
6: Reboot System
Most desktop Linux distributions boot into run level 5, which starts up the Graphical
Login Prompt. This allows the user to use the system with X-Windows server
enabled. Most servers boot into run level 3, which starts the text based login prompt
as it is advisable not to install graphical windows in a server as lots of space goes
waste and also it takes lot of resource to run.
Each run level is defined inside its own directory structure. These directories are
located in the /etc/rc.d/ directory, under which you have rc1.d, rc2.d… rc6.d
directories where the number from 0 to 6 corresponds to the specific run level. Inside
each directory symbolic links are defined to a to master initscripts found in /etc/init.d
or /etc/rc.d/init.d.
If your system uses GRUB, you can change the boot runlevel by pressing the `e’ key
to edit the boot configuration. Append the run level (in our case 5) to the end of the
boot command as shown:
kernel /vmlinuz-2.6.18-164.el5 ro root=LABEL=/ rhgb quiet 5