In Unix-like operating systems, the terms "kernel" and "shell" refer to two distinct components that play
crucial roles in the system.
1. Kernel:
- The kernel is the core of the operating system. It is a piece of software responsible for managing the
system's hardware and providing essential services for other software. The kernel acts as an
intermediary between the hardware and the user-level applications.
- Key responsibilities of the kernel include process management, memory management, device drivers,
file system management, and handling system calls. It is the part of the operating system that directly
interacts with the hardware components.
- The kernel runs in a privileged mode, and it is responsible for ensuring that multiple processes can
run concurrently without interfering with each other.
In Unix-like operating systems, the two main types of kernels are monolithic kernels and microkernels.
Additionally, there is a hybrid or modular kernel approach that combines features of both. Let's explore
these types in the context of Unix:
1. Monolithic Kernel:
- Description: A monolithic kernel is a single, large piece of software that includes all the essential
components of the operating system, such as process management, memory management, file system,
and device drivers.
- Characteristics:
- All kernel services run in a single address space.
- Communication between different kernel components is direct.
- Examples: The Linux kernel used in many Unix-like systems, traditional Unix kernels.
2. Microkernel:
- Description: A microkernel is designed to be minimalistic, with only the most essential functions
implemented in the core kernel. Additional functionalities, such as device drivers and file systems, are
implemented as separate user-level processes or servers.
- Characteristics:
- Core kernel provides basic services like inter-process communication and memory management.
- Other services are moved to user space for better modularity.
- Examples: MINIX, QNX.
3. Hybrid/Modular Kernel:
- Description: A hybrid or modular kernel combines features of both monolithic and microkernel
designs. It has a core set of essential functions in the kernel, and additional features can be added or
removed dynamically as load demands.
- Characteristics:
- Offers a balance between the performance of monolithic kernels and the modularity of
microkernels.
- Allows for loading and unloading of kernel modules.
- Examples: Some versions of the Linux kernel.
The choice of kernel type depends on various factors, including performance requirements, system
design goals, and trade-offs between complexity and modularity. The Linux operating system, for
example, predominantly uses a monolithic kernel but incorporates some features of modular kernels
through loadable kernel modules.
Shell:
- The shell is a command-line interpreter that allows users to interact with the operating system. It
provides a command-line interface (CLI) through which users can enter commands and receive output.
- The shell interprets the commands entered by the user and communicates with the kernel to execute
those commands. It acts as a user interface to the operating system, allowing users to perform various
tasks, such as navigating the file system, running programs, and managing processes.
- There are different shells available in Unix-like systems, such as Bash (Bourne Again SHell), Zsh (Z
Shell), and Fish. Each shell has its own set of features and capabilities.
In Unix-like operating systems, there are several types of shells, which are command interpreters that
allow users to interact with the operating system. Here are some commonly used Unix shells:
Bourne Shell (sh):
- The original Unix shell written by Stephen Bourne.
- Found on many Unix systems.
- Basic features and syntax.
Bourne-Again Shell (bash):
- An extended version of the Bourne Shell.
- Default shell for many Linux distributions.
- Provides additional features, command-line editing, and improved scripting capabilities.
C Shell (csh):
- Developed at the University of California, Berkeley.
- Known for its C-like syntax.
- Popular among programmers for its interactive features.
- Has a variant called tcsh (C Shell with enhancements).
Korn Shell (ksh):
- Developed by David Korn at Bell Labs.
- Combines features of the Bourne Shell and C Shell.
- Offers advanced scripting capabilities.
- ksh88 and ksh93 are notable versions.
Z Shell (zsh):
- An extended Bourne Shell with many enhancements.
- Features improved command-line editing and programmable completion.
- Offers interactive and scripting capabilities.
Difference between Shell and Kernel:
Shell Kernel
Users can communicate with the kernel using The kernel manages all system operations.
the shell.
It is an interpreter for command lines (CLI). It’s a low-level program that interacts with the
hardware (CPU, RAM, and drives) that
programs run on top of.
It serves as the user and kernel interface. It serves as the operating system’s foundation.
The several types include Bourne Shell, C Micro Kernel, Monolithic Kernel, Hybrid
Shell, Korn Shell, etc. Kernel, etc. are some of its types.
You can ask the OS to carry out a specific task It manages the processes.
using shell commands like ls, mkdir, and
many others.
Commands are executed on a file collection The memory is managed by it.
by giving a pattern to match.
It is OS’s outermost layer. It is the OS’s inner layer.
It communicates with the user and translates The kernel communicates directly with the
language for machines. hardware by receiving shell commands in a
language the kernel can understand.
Process in Operating system
In Unix-like operating systems, a process is a running instance of a program or command. Each process is
an independent entity with its own memory space, resources, and state. Here are key aspects of
processes in Unix:
1. Process Creation:
- A process is created when a program or command is executed. The `fork()` system call is commonly
used to create a new process.
- The new process is called the child process, and the original process is the parent process.
2. Process ID (PID):
- Each process is assigned a unique identifier known as the Process ID (PID). PIDs are used to manage
and identify processes.
- The `ps` command can be used to view information about running processes, including their PIDs.
3. Process Hierarchy:
- Processes are organized into a hierarchical structure known as the process hierarchy or process tree.
- The first process, with PID 1, is the init process. Other processes are descendants of init.
- The `pstree` command visualizes the process tree.
4. Process States:
- A process can be in one of several states, including:
- Running: Actively executing instructions.
- Sleeping: Awaiting an event (e.g., I/O completion).
- Stopped: Suspended or halted.
- Zombie: Terminated, but its exit status has not yet been collected by the parent process.
5. Foreground and Background Processes:
- A process running in the foreground interacts directly with the user, and the shell waits for it to
complete.
- A process running in the background does not block the shell, allowing the user to continue entering
commands.
6. Process Termination:
- Processes can terminate voluntarily or due to external signals.
- The `kill` command sends signals to processes, allowing users to control their behavior.
- A process can terminate by calling `exit()` or by receiving a termination signal.
7. Job Control:
- Job control allows users to manage multiple processes, both in the foreground and background.
- Commands like `bg`, `fg`, and `jobs` are used to manipulate background and foreground processes.
8. Process Communication:
- Processes can communicate with each other through inter-process communication (IPC)
mechanisms.
- IPC mechanisms include pipes (`|`), signals, shared memory, and message queues.
Understanding processes is essential for managing and troubleshooting Unix systems. Commands like
`ps`, `top`, and `kill`, as well as concepts like signals and job control, are commonly used for working with
processes in Unix-like operating systems.
System calls are fundamental interfaces between the operating system kernel and user-space
applications in Unix-like operating systems. These calls provide a way for programs to request services
from the kernel, such as process management, file system operations, and communication. Here are
some common system calls in Unix:
1. `fork()`:
- Creates a new process by duplicating the calling process. The new process is the child process, and
the calling process is the parent process.
2. `exec()`:
- Replaces the current process image with a new one. There are several variations of the `exec()`
system call, such as `execl()`, `execv()`, and others, allowing different ways to pass arguments to the new
program.
3. `exit()`:
- Terminates the calling process and returns the exit status to the parent process.
4. `wait()`, `waitpid()`:
- Waits for a child process to terminate and retrieves its exit status. The `waitpid()` system call provides
more control over which child process to wait for.
5. `open()`, `close()`:
- `open()` is used to open a file, creating a new file descriptor. `close()` is used to close a file descriptor.
6. `read()`, `write()`:
- `read()` reads data from a file descriptor, and `write()` writes data to a file descriptor. These calls are
fundamental for I/O operations.
7. `pipe()`:
- Creates a unidirectional communication channel (pipe) that can be used for inter-process
communication between parent and child processes.
8. `chdir()`:
- Changes the current working directory of the process.
9. `kill()`:
- Sends a signal to a process. Commonly used for process management, allowing processes to be
terminated, paused, or otherwise signaled.
10. `signal()`, `sigaction()`:
- `signal()` is used to install a signal handler for a specific signal. `sigaction()` is an improved version of
`signal()` with more options.
11. `brk()`, `sbrk()`:
- `brk()` sets the end of the data space of the calling process to a specified value. `sbrk()` increments
(or decrements) the program's data space.
12. `mkdir()`, `rmdir()`:
- `mkdir()` creates a new directory, and `rmdir()` removes an empty directory.
These are just a few examples of the many system calls available in Unix-like operating systems. System
calls provide a standardized interface for applications to interact with the kernel, enabling the execution
of privileged operations and access to various kernel services.
Features of UNIX
Unix is an operating system, so it has all the features that the OS must-have. UNIX also looks at a few
things in a different way than other OS. Features of UNIX are listed below :
1. Multiuser System :
Unix provides multiple programs to run and compete for the attention of the CPU. This happens in 2
ways :
Multiple users running multiple jobs
Single user running multiple jobs
In UNIX, resources are actually shared between all the users, so-called a multi-user system. For doing so,
computer give a time slice (breaking unit of time into several segments ) to each user. So, at any instant
of time, only one user is served but the switching is so fast that it gives an illusion that all the users are
served simultaneously.
2. Multitask System :
A single user may run multiple tasks concurrently. Example : Editing a file, printing another on the
printer & sending email to a person, and browsing the net too at the same time. The Kernel is designed
to handle user’s multiple needs.
The important thing here is that only one job can be seen running in the foreground, the rest all seems
to run in the background. Users can switch between them, terminate/suspend any of the jobs.
3. The Building-Block Approach :
The Unix developers thought about keeping small commands for every kind of work. So Unix has so
many commands, each of which performs one simple job only. You can use 2 commands by using pipes
(‘|’). Example : $ ls | wc Here, | (pipe) connects 2 commands to create a pipeline. This command counts
the number of files in the directory. These types of connected commands that can filter/manipulate
data in other ways are called filters.
Nowadays, many UNIX tools are designed in a way that the output of 1 can be used as an input for the
others.. We can create a large number of combinations by connecting a number of tools.
4. The UNIX Toolkit :
Unix has a kernel but the kernel alone can’t do much that could help the user. So, we need to use the
host of applications that usually come along with the UNIX systems. The applications are quite
diversified. General-purpose tools, text manipulation utilities (called filters), compilers and interpreters,
networked programs, and system administration tools are all included. With every UNIX release, new
tools are being added and the older ones are modified/ removed.
5. Pattern Matching :
Unix provides very sophisticated pattern matching features. The meta-char ‘*’ is a special character
used by the system to match a number of file names. There are several other meta-char in UNIX. The
matching is not confined to only filename. Advanced tools use a regular expression that is framed with
the characters from this set.
6. Programming Facility :
Unix provides shell which is also a programming language designed for programmers, not for casual end-
users. It has all the control structures, loops, and variables required for programming purposes. These
features are used to design the shell scripts ( programs that can invoke the UNIX commands).
Many functions of the system can be controlled and managed by these shell scripts.
7. Documentation :
It has a ‘man’ command that stands for the manual, which is the most important reference for any
commands and their configuration files. Apart from the offline documentation, there is a vast amount of
resources available on the Internet. If you’re stuck with a problem, there are various UNIX newsgroups
where you can post your concerns. You can also go through the FAQ(Frequently Asked Questions) – a
document that addresses several problems is widely used & available on the Net.
8. Portable:
Unix is highly portable, meaning it can be easily adapted to run on different hardware architectures.
9. Command-line interface:
Unix has a powerful command-line interface (CLI) that allows users to interact with the system through a
shell program.
10. File system:
Unix has a hierarchical file system, with all files and directories organized under a single root directory.
11. Networking:
Unix was designed from the ground up to support networking, making it an ideal choice for servers and
other networked systems.
12. Security:
Unix has robust security features built-in, including user-level permissions and access control lists.
13. Open-source:
Many versions of Unix are open-source, meaning that the source code is freely available for users to
modify and distribute.
14. Scalability:
Unix is highly scalable, allowing it to be used on everything from small embedded systems to large-scale
data centers.
Posix Basic Regular Expressions
POSIX stands for Portable Operating System Interface. It defines a set of standard operating system interfaces
based on the UNIX OS. These standards are specified by the IEEE (Institute of Electrical and Electronics
Engineers) society which maintains the compatibility between different operating systems. POSIX systems are
theorized in such a way that data or code can be transferred seamlessly between any two systems that are
POSIX compliant
One of the key features of POSIX is its adherence to a single-user specification. This means that POSIX-compliant operating systems are designed with the
assumption that there is typically only one user interacting with the system at a time.
The single-user specification impacts various aspects of the operating system design and behavior:
1. User authentication and permissions: POSIX-compliant systems typically have mechanisms for user authentication (e.g.,
username/password) and permission management (e.g., file permissions). These mechanisms are designed assuming a
single user or a limited number of users.
2. Resource management: Resources such as CPU time, memory, and I/O are typically managed with the assumption of a
single user. Processes and threads are typically created and managed within this context.
3. User environment: POSIX-compliant operating systems provide a user environment that includes a shell for command-line
interaction, utilities for managing files and processes, and other tools. These are designed for a single user to interact with
the system.
4. File systems: POSIX specifies a standard file system interface, including file access, manipulation, and permissions. These
operations are designed with the assumption of a single user or limited user base.
What is External and Internal Commands?
In Unix-like operating systems, commands can be broadly categorized into internal and external
commands based on their execution and implementation:
1. Internal Commands:
- Built-in commands: These are commands that are implemented within the shell itself. They are part
of the shell's code and are executed directly by the shell without invoking an external program.
Examples include `cd`, `echo`, `pwd`, `exit`, `history`, etc.
2. External Commands:
- Standalone Executables: These are commands that exist as separate executable files in the system's
file system. When you execute an external command, the shell searches for the executable file in
directories listed in the `PATH` environment variable and then invokes it. Examples include `ls`, `grep`,
`cat`, `sed`, `awk`, etc.
- External Scripts: Shell scripts or other executable scripts are also considered external commands.
These are files containing a sequence of commands that are interpreted or executed by an appropriate
interpreter. For example, a shell script (`filename.sh`) or a Python script (`filename.py`) would be
considered an external command.
When we type a command in a Unix-like shell, the shell first checks whether it is an internal command or
a built-in command. If it's a built-in command, the shell executes it directly. If not, the shell searches for
an executable file with that name in the directories specified by the `PATH` environment variable and
executes it if found. If neither internal nor external command is found, the shell displays an error
message indicating that the command is not found.
Differences between internal and external commands
The following table lists the main differences between internal and external commands.
Internal commands are part of the shell. External commands are not part of the shell.
Internal commands execute fast as they directly run External commands are slow as they run
from the shell. from the disk.
Internal commands provide only basic functionalities. External commands provide all
functionalities.
Internal commands are simple and straightforward. External commands are complex.
Internal commands always run from RAM. External commands first time run from disk.
1. Calendar (`cal`): This utility displays a simple calendar of the specified month or the current month if
no month is specified.
2. Display system date (`date`): The `date` command prints the current system date and time. It can
also be used to set the system date and time.
3. Message display (`echo`): `echo` is a command-line utility for printing text strings to the terminal or
redirecting them to a file. It's often used in shell scripting to display messages or to print variable values.
4. Calculator (`bc`): `bc`, which stands for "basic calculator," is a command-line calculator that supports
arithmetic operations, mathematical functions, and even more complex computations.
5. Password changing (`passwd`): This utility allows users to change their passwords on Unix-like
systems. It prompts the user to enter their current password and then the new password.
6. Knowing who are logged in (`who`): The `who` command displays information about users who are
currently logged in to the system. It typically shows the username, terminal, and login time.
7. System information using `uname`: The `uname` command provides basic information about the
operating system and the system hardware. It can display the system's name, kernel version, release
number, and machine architecture.
8. File name of terminal connected to the standard input (`tty`): The `tty` command prints the file name
of the terminal connected to the standard input. It's commonly used in shell scripts to determine the
terminal associated with a specific process or user session.
These utilities are fundamental components of Unix-like operating systems and are frequently used for
various tasks, from basic system administration to shell scripting and automation.