0% found this document useful (0 votes)
2 views21 pages

Unix Notes

Uploaded by

octophoniex
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)
2 views21 pages

Unix Notes

Uploaded by

octophoniex
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

This is a comprehensive, exam-oriented master blueprint containing detailed, structured, and

easy-to-memorize answers for every module in your syllabus.

Module 1: Introduction to Unix & Linux


Long Questions
1. Explain the architecture of the Unix operating system with a neat diagram.

The Unix architecture is designed in a layered approach, structured like concentric circles where
each layer has a distinct responsibility. It isolates the hardware from the user application to
ensure stability and security.
●​ Layer 1: Hardware: The innermost core consisting of physical components like the CPU,
RAM, and hard disks.
●​ Layer 2: Kernel: The heart of the OS. It interacts directly with the hardware. It manages
memory, processes, file systems, and device drivers.
●​ Layer 3: The Shell: The command interpreter. It acts as an interface between the user
and the Kernel. When you type a command, the shell reads it, interprets it, and requests
the Kernel to execute it. Examples: Bash, Bourne Shell (sh), C Shell (csh).
●​ Layer 4: Application Programs / Utilities: The outermost layer where users execute
commands, scripts, compilers, and applications (e.g., vi, grep, gcc).

2. Compare Unix and Linux in detail.

Feature Unix Linux


Origin & Core Developed by AT&T Bell Labs Developed by Linus Torvalds
(1969). It is a complete (1991). It is strictly just a
operating system. Kernel, not a complete OS.
Source Code Source code is proprietary / Open-source. Anyone can view,
closed-source (mostly). modify, and distribute the code.
Distributions macOS, Solaris, AIX, HP-UX. Ubuntu, Fedora, Debian,
CentOS, Red Hat (RHEL).
File System Support Supports limited file systems Supports a vast array of file
(like UFS). systems (ext2, ext3, ext4, XFS,
Btrfs).
Cost Usually commercial and highly Completely free to use and
expensive. distribute.
3. Discuss POSIX and its importance in Unix/Linux systems.

●​ What it stands for: Portable Operating System Interface.


●​ Definition: POSIX is a family of standards specified by the IEEE Computer Society to
maintain compatibility between different operating systems.
●​ Importance:
○​ Portability: If code is written following POSIX standards, a C program compiled on
Unix (e.g., Solaris) can run smoothly on Linux or macOS without rewriting the core
logic.
○​ Standardized API: It establishes uniform rules for system calls, process
management, file permissions, and shell syntax.

4. Explain the Unix kernel and system call interface.

●​ The Kernel: It is the core program that loads into memory upon booting and remains
active until shutdown. Its core duties include:
○​ Process Scheduling: Deciding which process gets CPU time.
○​ Memory Management: Allocating and freeing RAM allocations.
○​ Device Management: Communicating with hardware via device drivers.
●​ System Call Interface: A user program cannot access the hardware directly due to
safety protocols. When a program needs an operation done (like reading a file from a
disk), it makes a System Call. The System Call interface acts as a secure gateway that
transitions the CPU from User Mode to Kernel Mode to perform safe hardware execution.
Common examples include fork(), open(), read(), and write().

5. Describe the Unix directory structure with examples.

The Unix file system follows a hierarchical, inverted tree structure starting from the Root
directory, represented by a single forward slash (/).
/ (Root)​
______|___________________________________________​
| | | | | | | |​
/bin /boot /dev /etc /home /lib /root /usr​

●​ /bin: Essential binary executables needed in single-user mode (e.g., ls, cp, mv).
●​ /boot: Contains files required to boot the system (e.g., Linux Kernel, GRUB bootloader).
●​ /dev: Essential device files (e.g., /dev/sda for hard disks, /dev/null).
●​ /etc: Holds system configuration files (e.g., /etc/passwd).
●​ /home: The default directory containing personal folders for regular users.
●​ /lib: Core shared library files needed by binaries in /bin and /sbin.
●​ /root: The dedicated home directory for the Superuser (administrator).
●​ /usr: Sub-hierarchy containing user binaries, documentation, and source code.

Short Questions
1. What is Unix?

Unix is a powerful, multi-user, multitasking operating system originally developed in 1969 at


AT&T Bell Labs by Ken Thompson and Dennis Ritchie.

2. What is Linux?

Linux is an open-source, monolithic, Unix-like operating system kernel created by Linus


Torvalds in 1991. When paired with GNU utilities, it forms a complete operating system.

3. What are Linux distributions? Give examples.

Since Linux is just a kernel, companies bundle it with graphical interfaces, packages, and
installation tools to create a usable operating system called a distribution (distro). Examples:
Ubuntu, Fedora, Red Hat Enterprise Linux (RHEL), Debian.

4. Define POSIX.

POSIX is a set of IEEE standards that defines the application programming interface (API) for
software compatible with variants of the Unix operating system.

5. What are system calls?

System calls are programmatic requests made by a running application to the operating system
kernel to perform privileged operations like hardware input/output or process creation.

Module 2: Unix File Commands


Long Questions
1. Explain file and directory management commands in Unix with examples.

●​ mkdir: Creates a new directory.​


mkdir my_folder​

●​ cd: Changes the current working directory.​


cd my_folder​

●​ cp: Copies files or directories. (Use -r for recursive directory copying).​


cp [Link] [Link]​
cp -r folder1 folder2​

●​ mv: Moves or renames files/directories.​


mv old_name.txt new_name.txt​

●​ rm: Removes files. (Use -r to delete a directory and its contents).​


rm [Link]​
rm -rf old_folder​

●​ ls: Lists directory contents. (Options: -l for long detailed listing, -a to show hidden files).​
ls -la​

2. Discuss different types of files in Unix/Linux.

Unix treats everything as a file. There are three primary classifications:


1.​ Ordinary/Regular Files (-): Contains text, data, source code, or executable binaries
(e.g., [Link], [Link]).
2.​ Directory Files (d): Special files containing a list of other file names and pointers to their
inodes.
3.​ Special/Device Files: Found in /dev, these represent hardware components.
○​ Character Special Files (c): Handle I/O data stream character-by-character (e.g.,
keyboards, terminals).
○​ Block Special Files (b): Handle data transfer in fixed-size blocks (e.g., hard drives).
4.​ Links: Pointers to other files on the system (Symbolic links show up as l).

3. Explain file permissions and security using chmod.

Every file has an ownership structure: User (u), Group (g), and Others (o). Permissions
consist of Read (r=4), Write (w=2), and Execute (x=1).
You change security settings via chmod using two methods:
●​ Symbolic Mode:​
chmod u+x [Link] # Adds execute permission to the owner​
chmod g-w [Link] # Removes write permission from the group​

●​ Absolute (Octal) Mode:​


chmod 755 [Link]​
# 7 (4+2+1) -> Owner gets rwx​
# 5 (4+0+1) -> Group gets r-x​
# 5 (4+0+1) -> Others get r-x​

4. Differentiate between hard links and soft links.

Feature Hard Link Soft Link (Symbolic / Symlink)


Inode Shares the exact same inode Has a completely unique inode
number as the original file. number.
Content Points directly to the file data Points to the file path of the
on disk. original file.
If original is deleted Data is still accessible via the The link breaks (becomes a
hard link. "dangling link").
Cross-File System Cannot cross different file Can span across different
systems. storage drives/file systems.
Command ln [Link] hard_link ln -s [Link] soft_link
5. Explain inode and its significance in Unix file systems.

An inode (Index Node) is a low-level data structure on a Unix file system that stores metadata
about a file. Crucially, the inode does not store the file name or actual file content.
An inode contains:
●​ File size
●​ Device ID
●​ User ID (UID) and Group ID (GID) of the owner
●​ File permissions (rwx)
●​ Timestamps (atime, mtime, ctime)
●​ Pointers to data blocks on the physical drive where content is stored.
Significance: Unix tracks files internally by their unique inode number, not their text name. This
allows files to be renamed instantly without moving actual underlying data blocks.

6. Explain the touch command and file timestamps (atime, mtime, ctime).

The touch command creates an empty file if it doesn't exist, or updates its timestamps if it does.
●​ atime (Access Time): Last time a file's content was read/opened (e.g., via cat or grep).
●​ mtime (Modification Time): Last time a file's content was edited or changed.
●​ ctime (Change Time): Last time a file's metadata changed (e.g., renaming it or altering
permissions via chmod).
Using touch options:
●​ -d: Update to an explicit date string.​
touch -d "2025-10-12 14:30" [Link]​

●​ -t: Change timestamp using a specific format [[CC]YY]MMDDhhmm[.ss].​


touch -t 202510121430.00 [Link]​

7. Explain the use of pipes (|), redirection (>, >>) and tee command.

●​ Redirection (>): Overwrites standard output to a file.​


ls > [Link] # Overwrites [Link] with directory list​

●​ Append Redirection (>>): Appends standard output to the end of a file without
overwriting.​
echo "New line" >> [Link]​

●​ Pipe (|): Passes the standard output of one command to the input of another.​
ls | grep ".txt" # Streams the file list directly into grep​

●​ tee Command: Splits output stream into two directions. It displays output on the terminal
screen and saves it into a file simultaneously.​
ls | tee output_log.txt​

Short Questions
1. What is an inode?

An inode is a metadata record on a disk containing critical structural details about a file
(permissions, owner, size, block locations), identified by an integer value.

2. Define hard link and soft link.

A hard link is a direct duplicate directory reference pointing to an existing file's underlying inode.
A soft link is an independent shortcut file storing a text string path to another file.

3. What is file redirection?


File redirection is the process of altering the standard input/output channels (keyboard/screen)
to read from or write data to physical files on disk instead.

4. What are access, modification, and change times?

●​ atime: Time file content was read.


●​ mtime: Time file content was modified.
●​ ctime: Time file properties/metadata were changed.

5. Explain the mkdir, rmdir, and cp commands.

●​ mkdir: Creates empty directories.


●​ rmdir: Deletes completely empty directories.
●​ cp: Replicates file data from a source file to a new target file path.

Module 3: File Processing Commands


Long Questions
1. Explain cut command for row-wise and column-wise selection.

The cut command slices vertical columns or byte strings out of file streams.
●​ Column Selection by Delimiter (-d and -f): Extracts precise column fields using a
character separator.​
# Extract 1st and 3rd field from a colon-separated file​
cut -d ":" -f 1,3 [Link]​

●​ Character Selection (-c): Extracts precise ranges of characters per row.​


cut -c 1-5 [Link] # Cuts out the first 5 characters of every
row​

2. Explain paste command with suitable examples.

While cut extracts columns, paste joins separate file contents horizontally, column-by-column,
separated by default with a tab character.
# Combine contents of [Link] and [Link] side-by-side​
paste [Link] [Link]​

# Custom delimiter option (-d)​
paste -d "," [Link] [Link]​

3. Discuss split command for dividing large files.

The split command breaks large monolithic files into manageable smaller pieces.
●​ Split by Line Count (-l):​
split -l 100 [Link] small_chunk_​
# Creates small_chunk_aa, small_chunk_ab, etc., each with 100
lines.​

●​ Split by Size (-b):​


split -b 50M [Link] chunk_​
# Splitting into 50 Megabyte components.​

4. Explain sort command and its options.

The sort command rearranges lines alphabetically by default.


●​ -n: Sorts lines numerically instead of textually (e.g., prevents "10" coming before "2").
●​ -r: Reverses the sorting outcome (Descending order).
●​ -k: Sorts text relative to a specific column index position.​
sort -n -k 2 data_scores.txt # Numerically sorts file using
column 2​

5. Compare diff and cmp commands.

Feature diff cmp


Output style Tells you what text changes are Tells you the exact byte position
required to make files match. and line number where the first
difference occurs.
Analysis Line-by-line detailed textual Byte-by-byte low-level
comparison. binary/text comparison.
Ideal for Text documents and source Binary files, compiled
code scripts. programs, or image assets.
Command diff [Link] [Link] cmp [Link] [Link]
6. Explain join and uniq commands with examples.

●​ join: Merges lines from two sorted files based on a shared common key column.​
# Merges rows matching on key column 1​
join [Link] [Link]​

●​ uniq: Drops contiguous duplicate lines from a sorted file.


○​ -c: Prefixes lines with an occurrence counter.
○​ -d: Outputs only the duplicate lines.
sort [Link] | uniq -c​

7. Discuss tr (transformation) command and its applications.

The tr command converts, maps, or purges target character characters coming via standard
input. It cannot read files directly; it requires standard input piping.
●​ Case Conversion:​
echo "hello" | tr 'a-z' 'A-Z' # Output: HELLO​

●​ Squeeze repetitions (-s):​


echo "aaabbbccc" | tr -s 'b' # Output: aaabccc​

●​ Deletion (-d):​
echo "Phone: 123-45" | tr -d '-' # Output: Phone: 12345​

Short Questions
1. What is wc command?

wc (Word Count) counts the lines (-l), words (-w), and characters (-c) present in a target file.

2. What is the use of sort -n?

It directs the sort utility to parse fields as numeric mathematical quantities rather than
alphabetical string sequences.

3. Differentiate diff and cmp.

diff identifies differences textually across lines, whereas cmp flags the first physical byte offset
mismatch location between two target files.

4. What is the purpose of join command?

It merges database-like fields from two separate relational data files horizontally into a
combined output using a matching identity column.

5. Explain uniq command.

uniq filters out repeating identical matching text values, provided they sit directly next to each
other sequentially.

Module 4: Utility Commands


Long Questions
1. Explain utility commands: cal, date, who, pr, bc, and echo.

●​ cal: Prints a formatted text calendar representation of the current month.


●​ date: Displays the current system timestamp and configuration day info.
●​ who: Lists all active user login terminal sessions connected to the machine.
●​ pr: Formats text data to make it print-ready (adds headers, page numbers, and margins).
●​ bc: An arbitrary-precision terminal calculator utility for evaluating mathematical operations.​
echo "scale=2; 22/7" | bc # Output: 3.14​
●​ echo: Prints input argument strings out to the system console monitor.

2. Explain file compression and archiving using zip, unzip, and gzip.

●​ gzip: Compresses a standalone file down using the Lempel-Ziv coding algorithm. It
replaces the original file with a .gz extension.​
gzip [Link] # Creates [Link]​

●​ gunzip: Reverts a compressed .gz asset back to its original raw file form.
●​ zip: Compresses multiple files and directories into a single .zip package.​
zip [Link] [Link] [Link]​

●​ unzip: Extracts packed components out of an existing .zip archive file structure.

3. Explain archiving files using tar command.

The tar (Tape Archive) utility bundles collections of directory structures into a single solid file
object (.tar), which can then be compressed.
●​ Create an Archive (-cvf): c (create), v (verbose output logging), f (file target definition).​
tar -cvf [Link] /home/user/docs​

●​ Extract an Archive (-xvf): x extracts items out.​


tar -xvf [Link]​

●​ Archive + Gzip Compression (-zcvf): Creates a .[Link] compressed file.​


tar -zcvf [Link] /home/user/docs​

Short Questions
1. What is gzip?

gzip is a file compression tool that reduces single file footprints. It outputs files with the .gz
extension.

2. What is tar?

tar is an archiving tool used to collect groups of separate physical files and folders into one
collective bundle.

3. What is bc command?

bc is an interactive CLI calculator application capable of managing floating-point mathematics.

4. What is who command?

who identifies user account logins, terminal lines used, and connection times on the active Unix
machine.

Module 5: VI Editor
Long Questions
1. Explain different modes of VI editor.

The vi text editor relies heavily on switching operational states to navigate and edit files without
mouse input. It operates in three main modes:
1.​ Command Mode: The default operational entry state. Key presses map to movement
and formatting commands (e.g., dd to delete a line) rather than printing letters to the
screen. Pressing Esc returns you here from other modes.
2.​ Insert Mode: The state used for entering text directly into the file buffer. You enter it from
Command Mode by pressing keys like i, I, a, or o.
3.​ Last-Line (Ex) Mode: Activated by typing a colon (:) from Command Mode. It allows you
to save files, quit the editor, run shell commands, or perform global search-and-replace
actions.

2. Discuss text editing operations in VI editor.

●​ Inserting text:
○​ i: Insert text before current cursor position.
○​ a: Append text after current cursor position.
○​ o: Open an empty new line below current cursor row.
●​ Deleting text:
○​ x: Delete a single character under the cursor.
○​ dw: Delete from the cursor position to the end of the current word.
○​ dd: Delete the current line.
●​ Copy & Paste:
○​ yy: "Yank" (copy) the current line.
○​ p: Paste copied buffer content below current cursor line.

3. Explain file operations in VI editor.

File management operations are executed from Last-Line Mode:


●​ :w — Writes (saves) changes made to the current file buffer.
●​ :w filename — Saves data out as a new file name copy.
●​ :q — Quits the editor (fails if there are unsaved changes).
●​ :q! — Forces exit immediately, discarding all unsaved changes.
●​ :wq or ZZ — Saves changes and exits the session.
●​ :e filename — Opens a new file for editing without leaving vi.

4. Explain searching and replacing text in VI editor.

●​ Searching Forward: Type / followed by search term in Command mode.​


/error​
(Press n to jump to the next matching instance).
●​ Searching Backward: Type ? followed by search term.​
?error​

●​ Replacing Text (Substitution):


○​ Current line replacement:​
:s/old/new/g​

○​ Global system-wide replacement (entire file):​


:%s/old/new/g​

○​ Global search with confirmation prompt:​


:%s/old/new/gc​

Short Questions
1. How do you enter insert mode?

Pressing lowercase i from Command Mode enters Insert Mode before the current cursor
location.

2. Explain yy, dd, and p commands.

●​ yy: Copies the current line into memory.


●​ dd: Deletes (cuts) the current line.
●​ p: Pastes the cut or copied text below the cursor line.

3. Difference between :q and :q!.

:q closes the editor only if all changes are saved. :q! forces an exit immediately, discarding all
unsaved modifications.

4. What is :wq?

It is a dual command that writes (saves) changes to disk and immediately quits the active vi
session.

5. Explain undo and redo operations.

In Command Mode, pressing u reverses the last modification. Pressing Ctrl + r redoes an action
that was undone.

Module 6: GREP and AWK


Long Questions
1. Explain grep command with examples.

grep (Global Regular Expression Print) searches line-by-line through input files for lines
containing matches to a specified pattern.
●​ Syntax: grep [options] pattern filename
●​ Common Options:
○​ -i: Ignores alphabetical character casing.
○​ -v: Inverts the match; displays lines that do not match the pattern.
○​ -c: Displays only the count of matching rows.
○​ -n: Prefixes each output line with its original line number.
●​ Examples:​
grep -i "admin" [Link] # Case-insensitive search​
grep -v "success" [Link] # Prints only non-success lines​

2. Discuss advanced searching using grep.

Advanced usage involves leveraging Regular Expressions (Regex) anchors and wildcards:
●​ ^ (Caret): Anchors a pattern to match only at the absolute beginning of a line.​
grep "^Error" [Link] # Lines starting with "Error"​

●​ $ (Dollar): Anchors a pattern to match only at the absolute end of a line.​


grep "failed$" [Link] # Lines ending with "failed"​

●​ . (Dot): Matches any single character.


●​ *: Matches zero or more repetitions of the previous character.

3. Explain AWK command with print and printf statements.

awk is a complete pattern-scanning and text-processing programming language. It treats a file


as a collection of records (lines) and fields (columns).
●​ By default, $1 represents the first column, $2 the second column, and $0 the entire line.
●​ Using print (automatically adds a newline):​
awk '{print $1, $3}' [Link]​

●​ Using printf (formatted printing, requires manual \n newline):​


awk '{printf "Name: %-10s ID: %d\n", $1, $2}' [Link]​

4. Explain BEGIN and END sections in AWK.

●​ BEGIN block: Evaluated exactly once before any text rows are read from the input file. It
is useful for setting up global variables or printing report headers.
●​ END block: Evaluated exactly once after all input file records have been completely
processed. It is useful for printing final summaries or total tallies.
awk 'BEGIN {print "--- START ---"} {print $1} END {print "--- FINISHED
---"}' [Link]​
5. Discuss AWK with comparison and arithmetic operators.

awk can evaluate data records conditionally.


●​ Arithmetic: +, -, *, /, %
●​ Comparison: >, <, >=, <=, ==, !=
●​ Examples:​
# Print lines where the third column is mathematically greater
than 500​
awk '$3 > 500 {print $1}' [Link]​

# Arithmetic calculation across records​
awk '{sum = $2 + $3; print $1, sum}' [Link]​

6. Explain built-in variables FS and OFS.

●​ FS (Field Separator): Defines the character that splits columns in the input file. Default is
whitespace.
●​ OFS (Output Field Separator): Defines the character used to separate columns when
printing. Default is a space.
# Process a comma-separated file (CSV) and output it as tab-separated​
awk 'BEGIN {FS=","; OFS="\t"} {print $1, $2}' [Link]​

7. Explain loops and conditional statements in AWK.

awk supports traditional standard programming language logic constructs.


●​ Conditional if-else statement:​
awk '{ if ($3 >= 40) print $1 " Passed"; else print $1 " Failed"
}' [Link]​

●​ Loops (while and for):​


# For loop evaluating across specific field values per row​
awk '{ for(i=1; i<=NF; i++) print $i }' [Link]​
(Note: NF is a built-in variable containing the Number of Fields in the current line).

8. Explain AWK string functions and arithmetic functions.

●​ Arithmetic Functions: int(x) (truncates to integer), sqrt(x) (square root), rand() (random
number).
●​ String Functions: * length(str): Returns character length.
○​ substr(str, start, len): Extracts a substring.
○​ tolower(str) and toupper(str): Case mapping.
awk '{print toupper($1), length($1)}' [Link]​
9. Explain search and substitute functions in AWK.

awk provides sub() and gsub() functions for regular expression modifications.
●​ sub(regex, replacement, target): Replaces only the first matching instance on the line.
●​ gsub(regex, replacement, target): Globally replaces all matching instances on the line.
# Globally replace "USA" with "United States" in column 3​
awk '{ gsub(/USA/, "United States", $3); print }' [Link]​

Short Questions
1. What is grep?

grep is a command-line tool used to scan a data stream or file text for any lines that match a
specified regular expression pattern.

2. What is AWK?

awk is a domain-specific, data-driven programming language designed for complex parsing, text
restructuring, and report generation from columnar text data.

3. Define FS and OFS.

FS is the character marker that dictates how input lines are split into variables, whereas OFS
defines how fields are separated in the output.

4. Difference between print and printf.

print appends a newline automatically and handles simple data dumps, while printf allows you to
define exact column widths and data type formatting.

Module 7: Process Management


Long Questions
1. Explain process management in Unix.

A process is an active, running instance of a program in memory. Every process is uniquely


identified by a PID (Process Identifier).
●​ Parent-Child Relationship: Processes are organized hierarchically. A process can
spawn new sub-processes using system calls. The creator is the Parent, and the
spawned process is the Child.
●​ Commands to manage processes:
○​ ps: Reports a snapshot of currently running processes. (ps -ef lists all system
processes).
○​ kill [PID]: Sends a termination signal to close a process.
2. Discuss Unix process states with a diagram.

During its lifecycle, a Unix process transitions through several operational states:
1.​ Created/New: The process is being initialized by the OS.
2.​ Ready/Runnable: The process is fully loaded in memory and waiting in a queue for the
CPU scheduler to assign it execution time.
3.​ Running: The CPU is actively executing the process instructions.
4.​ Blocked/Sleeping (Waiting): The process cannot continue until an external event or I/O
operation completes (e.g., waiting for user keyboard input).
5.​ Zombie: The process has finished execution but still occupies an entry in the system
process table so its parent can read its exit status code.

3. Explain fork(), getpid(), getppid(), and wait() system calls.

●​ fork(): Creates a new child process by making an exact duplicate copy of the calling
parent process. After invocation, both processes run concurrently.
○​ Return Value: Returns 0 inside the child process, and returns the Child's PID inside
the parent process.
●​ getpid(): Returns the unique Process ID (PID) of the active calling process.
●​ getppid(): Returns the Process ID of the Parent (PPID) that spawned the calling process.
●​ wait(): Suspends the parent process execution until one of its child processes terminates.

4. Explain zombie process with examples.

A Zombie process is a process that has completed its task and terminated, but its entry
remains in the process table. This happens because its parent process has not yet read its exit
status using the wait() system call.
Example scenario: If a child finishes its task while its parent is stuck executing an infinite
while(1) loop without calling wait(), the child becomes a zombie. It consumes no memory or
CPU, but it ties up a process table slot. If the process table fills up with zombies, no new
programs can be started.

5. Discuss pipe() and message passing.

These are mechanisms for Inter-Process Communication (IPC):


●​ pipe(): A system call that creates a unidirectional data channel. It sets up two file
descriptors: one for writing data into the pipe, and one for reading data out from the other
end. This allows the output of one process to be routed directly as the input to another
process.
●​ Message Passing: A system IPC framework where processes exchange data by sending
and receiving structured messages through operating system message queues.

6. Explain the init process and Unix login process.

●​ init Process: The grandfather of all processes. It is the first user-space process started
by the kernel during booting, always carrying a PID of 1. It runs continuously until
shutdown and is responsible for bringing up system services and adopting orphaned child
processes.
●​ Login Process:
1.​ init reads configuration files and spawns a getty process on communication lines.
2.​ getty sets up terminal lines and waits for a user to type a username.
3.​ Once entered, getty execs the login program, which prompts for a password and
verifies it against /etc/passwd.
4.​ If authenticated, login invokes the user's default shell (e.g., bash), presenting the
command prompt.

7. Discuss vmstat and top commands.

●​ top: Provides a real-time, interactive, dynamic view of active processor activity and
memory usage. It continuously lists the top resource-consuming processes.
●​ vmstat (Virtual Memory Statistics): Displays summaries of system memory, swap
utilization, CPU activity, and I/O traps. It is useful for identifying resource performance
bottlenecks.

8. Explain the nice command and process priority.

Unix uses a priority scheduling scale. The scheduling priority of a process can be altered using
a metric called a nice value.
●​ Nice values range from -20 (highest priority) to 19 (lowest priority).
●​ A high nice value makes the process "nice" to other programs by yielding CPU time.
●​ Usage:​
nice -n 10 backup_script.sh # Runs the script with a lower
priority​

Short Questions
1. What is a process?

A process is a program in execution, complete with its own memory space, registers, stack
pointer, and tracking state.

2. What is a zombie process?

A terminated process whose exit status has not been retrieved by its parent, leaving its identifier
stuck in the system process table.

3. What is fork()?

A core system call that creates an identical concurrent duplicate child process from the parent
process.

4. What is the use of top command?

It displays a real-time, auto-refreshing monitor of system resource metrics and a list of running
processes sorted by resource consumption.

Module 8: Shell Programming


Long Questions
1. Explain shell and different types of shells.

The Shell is a command-line interpreter that reads user commands and passes them to the
operating system kernel for execution.
Common Types of Shells:
●​ Bourne Shell (sh): The original standard Unix shell developed by Stephen Bourne.
●​ Bash (Bourne Again Shell): The standard default shell for most modern Linux systems.
It builds on sh with enhancements like tab-completion and command history.
●​ C Shell (csh): Uses a syntax that resembles the C programming language.
●​ Korn Shell (ksh): Combines features of both the Bourne and C shells.

2. Discuss system variables and user-defined variables.

●​ System Variables: Predefined variables created and maintained by the shell environment
to manage system settings. They are usually written in uppercase letters.
○​ HOME: The path to the current user's home directory.
○​ PATH: A colon-separated list of directories the shell searches to locate executable
commands.
○​ USER: The name of the logged-in user.
●​ User-Defined Variables: Temporary variables created by the user within a shell script.
They are case-sensitive and assigned using the = operator (without spaces).​
my_name="Alice"​
echo $my_name​

3. Explain single quotes, double quotes, and backslash in shell scripting.

These characters control how the shell processes special characters (quoting mechanisms):
●​ Double Quotes ("): Weak quoting. It preserves the literal value of most characters, but
allows variable expansion ($) and command substitution to execute.​
echo "Home is $HOME" # Output: Home is /home/user​

●​ Single Quotes ('): Strong quoting. It preserves the literal value of every single character
inside the quotes. No substitutions take place.​
echo 'Home is $HOME' # Output: Home is $HOME​

●​ Backslash (\): Escapes the character immediately following it, stripping away any special
meaning it has to the shell.​
echo "The price is \$10" # Output: The price is $10​
4. Explain command substitution with examples.

Command substitution allows the output of a command to be captured and assigned directly to
a variable or used within another statement. This can be done using backticks (`...`) or the
modern $() syntax.
# Using modern syntax (Recommended)​
current_users=$(who | wc -l)​
echo "There are $current_users users logged in."​

# Using legacy backticks​
today=`date`​
echo "Today is $today"​

5. Explain if statement with shell script examples.

The if statement evaluates a condition. If the condition is true, the corresponding block of code
runs. The block is closed with fi.
Syntax Example:
#!/bin/bash​
echo "Enter a score out of 100:"​
read score​

if [ $score -ge 50 ]​
then​
echo "Result: Passed"​
else​
echo "Result: Failed"​
fi​

(Note: -ge stands for greater than or equal to).

6. Explain for loop, while loop, and until loop.

●​ for loop: Iterates through a fixed list of items.​


for item in 1 2 3 4 5​
do​
echo "Count: $item"​
done​

●​ while loop: Continues executing a block of code as long as the specified condition
remains true.​
count=1​
while [ $count -le 3 ]​
do​
echo "Value: $count"​
count=$((count + 1))​
done​

●​ until loop: Continues executing code as long as the specified condition evaluates to
false (it stops once the condition becomes true).​
num=1​
until [ $num -gt 3 ]​
do​
echo "Number: $num"​
num=$((num + 1))​
done​

Frequently Asked Exam Programs


Here are the complete, ready-to-run shell scripts for the most frequently asked exam programs.

1. Shell program to find the factorial of a number

#!/bin/bash​
# Program to calculate factorial​
echo "Enter a number:"​
read num​

fact=1​
temp=$num​

while [ $num -gt 1 ]​
do​
fact=$((fact * num))​
num=$((num - 1))​
done​

echo "The factorial of $temp is $fact"​

2. Shell program to check whether a number is prime

#!/bin/bash​
# Program to check if a number is prime​
echo "Enter an integer:"​
read n​

if [ $n -lt 2 ]​
then​
echo "$n is not a prime number."​
exit 0​
fi​

i=2​
is_prime=1​

while [ $((i * i)) -le $n ]​
do​
if [ $((n % i)) -eq 0 ]​
then​
is_prime=0​
break​
fi​
i=$((i + 1))​
done​

if [ $is_prime -eq 1 ]​
then​
echo "$n is a prime number."​
else​
echo "$n is not a prime number."​
fi​

3. Shell program to find the largest among three numbers

#!/bin/bash​
# Program to find the largest of three inputs​
echo "Enter three numbers separated by spaces:"​
read a b c​

if [ $a -gt $b ] && [ $a -gt $c ]​
then​
echo "$a is the largest number."​
elif [ $b -gt $a ] && [ $b -gt $c ]​
then​
echo "$b is the largest number."​
else​
echo "$c is the largest number."​
fi​

4. Shell program using command-line arguments

This script processes arguments passed to it when it is run (e.g., ./[Link] 10 20). $1 captures
the first argument, $2 captures the second, and $# contains the total number of arguments
passed.
#!/bin/bash​
# Program showing command-line arguments execution​

if [ $# -lt 2 ]​
then​
echo "Error: Please provide at least two arguments."​
echo "Usage: $0 num1 num2"​
exit 1​
fi​

sum=$(($1 + $2))​
echo "Script Name: $0"​
echo "Total Arguments Received: $#"​
echo "First Argument: $1"​
echo "Second Argument: $2"​
echo "Sum of the two arguments: $sum"​

You might also like