0% found this document useful (0 votes)
25 views66 pages

Operating System and Unix Sem2

The document provides an overview of operating systems, detailing their functions, services, and components such as process management, CPU scheduling, and memory management. It covers key concepts like multiprogramming, time-sharing systems, system calls, and various scheduling algorithms, alongside examples in C programming. Additionally, it addresses issues like deadlocks, inter-process communication, and memory allocation techniques including paging and swapping.
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)
25 views66 pages

Operating System and Unix Sem2

The document provides an overview of operating systems, detailing their functions, services, and components such as process management, CPU scheduling, and memory management. It covers key concepts like multiprogramming, time-sharing systems, system calls, and various scheduling algorithms, alongside examples in C programming. Additionally, it addresses issues like deadlocks, inter-process communication, and memory allocation techniques including paging and swapping.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

OPERATING SYSTEM AND UNIX

Introduction to Operating Systems


1. What is an Operating System?
An Operating System (OS) is software that acts as an
intermediary between computer hardware and
application programs. It manages hardware resources
and provides services for application software.
2. Operating System Services
Operating systems provide various services to users
and applications, including:
• User Interface: Command-line or graphical
interface for user interaction.
• Program Execution: Facilitation of program loading
and execution.
• I/O Operations: Management of input and output
operations with devices.
• File System Manipulation: Support for file
operations (creation, deletion, reading, writing).
• Error Detection and Response: Monitoring for
errors in hardware or software and providing
feedback.
• Resource Allocation: Allocation of resources (CPU,
memory) to different processes.
3. Multiprogramming
Multiprogramming is the ability of an OS to execute
multiple programs simultaneously by managing their
execution in a way that maximizes CPU utilization. It
allows multiple processes to be in memory at the same
time, reducing idle time.
4. Time-Sharing System
A time-sharing system allows multiple users to interact
with a computer simultaneously. The CPU time is
divided among users, giving the illusion of concurrent
execution. This is achieved through rapid context
switching.
Storage Structures
Storage structures refer to how data is organized and
managed on storage devices, including:
• Primary Storage: Fast memory like RAM where the
OS keeps currently running processes.
• Secondary Storage: Slower storage (like hard
drives) used for persistent data storage.
• Cache Memory: A smaller, faster type of volatile
memory that provides high-speed access to
frequently used data.
System Calls
System calls are the programming interface through
which a program requests services from the OS.
Examples include:
• Process Control: fork(), exec(), exit().
• File Management: open(), read(), write(), close().
• Device Management: ioctl(), read(), write().
• Information Maintenance: getpid(), getuid().
• Communication: pipe(), shmget(), msgget().
Multiprocessor Systems
Multiprocessor systems consist of two or more CPUs
working together. They can be categorized as:
• Tightly Coupled Systems: Processors share
memory and resources.
• Loosely Coupled Systems: Each processor has its
own memory.
CPU Scheduling
1. Basic Concepts of CPU Scheduling
CPU scheduling is the process of determining which of
the ready, in-memory processes is to be executed by
the CPU next. Effective CPU scheduling improves
system responsiveness and efficiency.
2. Scheduling Criteria
Key criteria for evaluating scheduling algorithms
include:
• CPU Utilization: Percentage of time the CPU is
busy.
• Throughput: Number of processes completed in a
unit of time.
• Turnaround Time: Total time taken to execute a
process.
• Waiting Time: Total time a process spends waiting
in the ready queue.
• Response Time: Time from submission to the first
response.
3. Scheduling Algorithms
Common CPU scheduling algorithms include:
• First-Come, First-Served (FCFS): Processes are
scheduled in the order they arrive.
• Shortest Job Next (SJN): The process with the
shortest burst time is scheduled next.
• Round Robin (RR): Each process is assigned a fixed
time slice in rotation.
• Priority Scheduling: Processes are scheduled
based on priority; can be preemptive or non-
preemptive.
Example of a Round Robin Scheduling Algorithm in C:
c
Copy code
#include <stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int


wt[], int quantum) {
int rem_bt[n];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];

int t = 0; // Current time


while (1) {
int done = 1; // To check if all processes are done
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = 0; // If any process is pending
if (rem_bt[i] > quantum) {
t += quantum;
rem_bt[i] -= quantum;
} else {
t = t + rem_bt[i];
wt[i] = t - bt[i]; // Waiting time
rem_bt[i] = 0;
}
}
}
if (done == 1)
break;
}
}

void findTurnAroundTime(int processes[], int n, int bt[],


int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i]; // Turnaround time
}

void findavgTime(int processes[], int n, int bt[], int


quantum) {
int wt[n], tat[n];
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);

printf("Processes Burst time Waiting",


" time Turnaround time\n");
for (int i = 0; i < n; i++)
printf(" %d\t\t%d\t\t%d\t\t%d\n",
i + 1, bt[i], wt[i], tat[i]);
}

int main() {
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};
int quantum = 2; // Time quantum for RR
findavgTime(processes, n, burst_time, quantum);
return 0;
}
4. Algorithm Evaluation
Evaluating scheduling algorithms is done through
simulations and measuring performance metrics like
turnaround time, waiting time, and CPU utilization.
5. Multiple Processor Scheduling
In multiprocessor systems, scheduling can be more
complex. Techniques include:
• Load Balancing: Distributing processes evenly
across processors.
• Affinity Scheduling: Keeping processes on the
same processor to improve cache performance.
6. Real-Time Scheduling
Real-time systems require timely processing to meet
deadlines. Scheduling algorithms used in real-time
systems include:
• Rate Monotonic Scheduling (RMS): Assigns
priorities based on the frequency of process
requests.
• Earliest Deadline First (EDF): Prioritizes processes
with the nearest deadlines.
I/O Devices Organization
1. I/O Devices Organization
I/O devices are organized into a hierarchy based on
their speed, size, and access method. Common types
include:
• Block Devices: Read/write operations in blocks
(e.g., hard drives).
• Character Devices: Read/write operations are
done one character at a time (e.g., keyboard).
2. I/O Buffering
I/O buffering involves using memory space to hold data
temporarily while it is being transferred between the
device and the main memory. This improves
performance by allowing the CPU to continue
processing while waiting for I/O operations to
complete.
Types of buffering include:
• Single Buffering: One buffer used for I/O.
• Double Buffering: Two buffers used to hold data,
allowing overlap of I/O and processing.
• Circular Buffering: A fixed-size buffer treated as a
circular queue.
Example of I/O Buffering in C:
c
Copy code
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *fp;
char buffer[100];

// Open a file for writing


fp = fopen("example.txt", "w");
if (fp == NULL) {
perror("Error opening file");
return -1;
}

// Write to the buffer


sprintf(buffer, "Hello, this is a buffered I/O
example.\n");

// Use fwrite for buffered I/O


fwrite(buffer, sizeof(char), sizeof(buffer), fp);

// Close the file


fclose(fp);
return 0;
}
Process Management
1. Process Concept
A process is a program in execution, consisting of the
program code, current activity (represented by the
program counter), and the process stack. Each process
has a unique Process Control Block (PCB) containing
information like:
• Process ID (PID)
• Process State: Running, waiting, ready, etc.
• Program Counter: Address of the next instruction
to execute.
• CPU Registers: Values stored during execution.
• Memory Management Information: Page tables,
segment tables, etc.
2. Process Scheduling
Process scheduling is the method by which the
operating system decides which process to execute at a
given time. Key components include:
• Ready Queue: A queue of processes that are ready
to execute.
• Scheduler: A component that selects which
process to run.
Types of Schedulers:
• Long-term Scheduler: Decides which processes are
admitted to the system for processing (controls
degree of multiprogramming).
• Short-term Scheduler: Selects among the
processes that are ready to execute and allocates
CPU to one of them.
• Medium-term Scheduler: Swaps processes in and
out of memory to manage the level of
multiprogramming.
3. Operations on Processes
Common operations that can be performed on
processes include:
• Create: Initiate a new process (e.g., fork() in UNIX).
• Terminate: End a process (e.g., exit() in UNIX).
• Wait: Block a process until a specific event occurs
(e.g., waiting for another process to finish).
• Signal: Notify a process that an event has
occurred.
4. Threads
Threads are lightweight processes that can run
concurrently within a process. They share the same
memory space but have their own program counter,
registers, and stack.
• Benefits of Threads:
o More efficient than processes due to lower
overhead.
o Easier to create and manage.
o Share data easily among threads in the same
process.
5. Inter-Process Communication (IPC)
IPC allows processes to communicate and synchronize
their actions. Common IPC mechanisms include:
• Pipes: Used for one-way communication between
processes.
• Message Queues: Processes can send and receive
messages.
• Shared Memory: Multiple processes can access a
common memory area.
• Sockets: Used for communication between
processes over a network.
6. Precedence Graphs
Precedence graphs (or task graphs) are used to
represent dependencies between processes or tasks.
Nodes represent processes, and directed edges
indicate that one process must complete before
another can begin.
7. Critical Section Problem
The critical section problem arises when multiple
processes need to access shared resources. A critical
section is a part of the program where shared
resources are accessed. To solve this problem, a system
must ensure:
• Mutual Exclusion: Only one process can be in the
critical section at a time.
• Progress: If no process is in the critical section, the
selection of the next process cannot be postponed
indefinitely.
• Bounded Waiting: There must be a limit on the
number of times other processes can enter their
critical sections before a waiting process can enter.
8. Semaphores
Semaphores are synchronization tools used to manage
access to shared resources. They can be either:
• Binary Semaphores: Take values 0 or 1 (used for
mutual exclusion).
• Counting Semaphores: Can take any non-negative
integer value (used for managing access to a pool
of resources).
Example of Semaphore Usage in C:
c
Copy code
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

sem_t semaphore;

void* process(void* arg) {


sem_wait(&semaphore); // Wait (decrement
semaphore)
// Critical section
printf("Process %d in critical section\n", *(int*)arg);
sem_post(&semaphore); // Signal (increment
semaphore)
return NULL;
}

int main() {
pthread_t threads[5];
int ids[5] = {0, 1, 2, 3, 4};

sem_init(&semaphore, 0, 1); // Initialize semaphore

for (int i = 0; i < 5; i++)


pthread_create(&threads[i], NULL, process,
&ids[i]);

for (int i = 0; i < 5; i++)


pthread_join(threads[i], NULL);
sem_destroy(&semaphore); // Clean up
return 0;
}
9. Classical Problems of Synchronization
Several classical synchronization problems include:
• Producer-Consumer Problem: One or more
producers generate data, and one or more
consumers use it. It involves managing a shared
buffer.
• Dining Philosophers Problem: A synchronization
problem involving multiple processes that need
limited resources (like forks for eating).
• Readers-Writers Problem: A situation where
multiple threads can read shared data, but only
one can write at a time.
Deadlock Problem
1. Deadlock Definition
A deadlock occurs when two or more processes are
blocked forever, each waiting for the other to release a
resource.
2. Deadlock Prevention
Techniques to prevent deadlocks include:
• Mutual Exclusion: Ensure that at least one
resource is non-shareable.
• Hold and Wait: Require processes to request all
required resources at once, preventing holding
onto resources.
• No Preemption: If a process holds some resources
and requests others, the OS should preempt
resources from the process.
• Circular Wait: Impose a strict ordering on resource
allocation to prevent circular dependencies.
3. Deadlock Avoidance
Deadlock avoidance dynamically examines resource
allocation to ensure that a circular wait condition
cannot occur. One common algorithm is the Banker’s
Algorithm, which checks if resource allocation can lead
to a safe state.
4. Deadlock Recovery
If a deadlock occurs, strategies for recovery include:
• Process Termination: Kill one or more processes
involved in the deadlock.
• Resource Preemption: Temporarily take resources
away from processes until the deadlock is
resolved.
Memory Management
1. Concepts of Memory Management
Memory management is the process of coordinating
and handling computer memory, ensuring that each
process has sufficient memory for execution while
maximizing efficiency. Key functions include:
• Allocation: Assigning memory to processes.
• Deallocation: Releasing memory when it’s no
longer needed.
• Tracking: Monitoring memory usage to avoid
fragmentation.
2. Logical and Physical Address Space
• Logical Address Space: The set of addresses that a
process can use. It is generated by the CPU during
program execution.
• Physical Address Space: The actual physical
memory address where data is stored in RAM. The
operating system maps logical addresses to
physical addresses.
3. Swapping
Swapping is a memory management technique that
involves moving processes between main memory and
disk (swap space) to ensure efficient memory
utilization. It allows the system to run processes that
may not fit entirely in physical memory.
4. Contiguous and Non-Contiguous Allocation
• Contiguous Allocation: Each process is allocated a
single contiguous block of memory. This is simple
but can lead to fragmentation.
• Non-Contiguous Allocation: Processes can be
allocated memory in different blocks scattered
throughout physical memory, reducing
fragmentation issues.
5. Paging
Paging is a memory management scheme that
eliminates the need for contiguous allocation. It divides
the logical address space into fixed-size blocks called
pages and the physical memory into frames of the
same size.
• Advantages of Paging:
o Simplifies memory management and reduces
fragmentation.
o Allows efficient use of memory.
Example of Paging in C:
c
Copy code
#include <stdio.h>
#include <stdlib.h>

#define PAGE_SIZE 4096 // Example page size

typedef struct {
int page_number;
char data[PAGE_SIZE]; // Page data
} Page;

int main() {
// Simulate paging by allocating pages
int num_pages = 5;
Page* pages = malloc(num_pages * sizeof(Page));

for (int i = 0; i < num_pages; i++) {


pages[i].page_number = i;
// Fill page with data
snprintf(pages[i].data, PAGE_SIZE, "Data for page
%d", i);
}

// Accessing pages
for (int i = 0; i < num_pages; i++) {
printf("Page %d: %s\n", pages[i].page_number,
pages[i].data);
}

free(pages); // Free allocated memory


return 0;
}
6. Segmentation
Segmentation is a memory management technique
that divides a process into different segments based on
its logical structure (like functions, arrays, etc.). Each
segment can be of different sizes and is managed
separately.
7. Paging Combined with Segmentation
This approach combines the benefits of paging and
segmentation. The logical address is divided into a
segment number and a page number, allowing variable
segment sizes while maintaining the fixed-size page
structure.
Virtual Memory
1. Virtual Memory
Virtual memory is a memory management technique
that allows processes to use more memory than is
physically available by temporarily transferring data to
disk storage. It gives the illusion of a large main
memory.
2. Demand Paging
Demand paging loads pages into memory only when
they are needed. When a page is not in memory and a
process requests it, a page fault occurs, triggering the
operating system to load the page from disk.
3. Page Replacement Algorithms
When physical memory is full, the OS must decide
which page to remove to make space for the new page.
Common page replacement algorithms include:
• Least Recently Used (LRU): Replaces the least
recently used page.
• First-In-First-Out (FIFO): Replaces the oldest page
in memory.
• Optimal Page Replacement: Replaces the page
that will not be used for the longest period in the
future.
4. Allocation of Frames
The operating system must manage how many frames
each process can use. This can be done using fixed
allocation (a fixed number of frames for each process)
or variable allocation (dynamic allocation based on
need).
5. Thrashing
Thrashing occurs when a process spends more time
paging than executing due to insufficient memory. It
can severely degrade performance and is usually
managed by reducing the degree of multiprogramming
or increasing physical memory.
6. Demand Segmentation
Demand segmentation loads segments into memory
only when they are accessed, similar to demand
paging. This approach optimizes memory usage by
loading only necessary segments.
Security, Threads, Protection, and Intruders
1. Security
Memory management is critical for maintaining system
security. It prevents unauthorized access to memory
spaces of other processes, protecting sensitive data.
2. Threads
Threads are lightweight processes that share the same
memory space. Proper memory management ensures
that threads can safely access shared data without
conflicts.
3. Protection Mechanisms
Operating systems implement various protection
mechanisms to prevent unauthorized access to
memory:
• Access Control Lists (ACLs): Define which users or
processes have permissions for specific resources.
• User and Kernel Modes: Distinguish between user
processes and system-level processes to protect
critical system resources.
4. Intruders and Viruses
• Intruders: Malicious users who attempt to gain
unauthorized access to system resources. Security
measures like firewalls, intrusion detection
systems, and strong authentication protocols are
essential to protect against intruders.
• Viruses: Malicious software that can spread and
infect other programs. Antivirus software and
regular updates help in identifying and removing
such threats.
5. Trusted Systems
A trusted system is an operating system designed to
provide a high level of security, with features that
prevent unauthorized access and ensure data integrity.
Characteristics of trusted systems include:
• Mandatory Access Control (MAC): Enforces
security policies that restrict access based on
security levels.
• Auditing and Logging: Keeps track of access and
modifications to resources to identify potential
security breaches.

Introduction to Open Source Technology


Open Source technology refers to software whose
source code is available for anyone to view, modify, and
distribute. This fosters community collaboration and
transparency. Key benefits include:
• Cost-Effective: Generally free to use.
• Flexibility: Users can customize software according
to their needs.
• Community Support: Users can access a wide
range of community-driven resources and forums
for assistance.
File System Hierarchy
The Linux file system hierarchy follows a standard
layout. Key directories include:
• /: Root directory.
• /bin: Essential user binaries (commands).
• /etc: Configuration files.
• /home: User home directories.
• /lib: Essential shared libraries.
• /usr: User programs and utilities.
• /var: Variable files, such as logs.
Logging In
To log into a Linux system:
1. Start the terminal or console.
2. Enter your username.
3. Enter your password when prompted.
Simple Commands
Here’s a list of common commands with brief
explanations:
Command Description
ls List directory contents.
cp Copy files or directories.
mv Move or rename files or directories.
wc Word count (lines, words, characters).
sort Sort lines of text files.
tsort Topological sort.
cat Concatenate and display files.
cut Remove sections from each line of files.
grep Search for patterns in files.
dd Convert and copy files.
head Output the first part of files.
tail Output the last part of files.
uniq Report or omit repeated lines.
diff Compare files line by line.
echo Display a line of text.
Command Description
Create an empty file or update file
touch
timestamps.
which Locate a command.
Locate the binary, source, and manual
whereis
page files for a command.
whatis Describe a command.
type Display information about command type.
who Show who is logged on.
whoami Display current logged-in user.
finger Display user information.
Show who is logged in and what they are
w
doing.
Print file name of terminal connected to
tty
standard input.
uname Display system information.
printf Format and print data.
ps Display current processes.
pwd Print working directory.
Command Description
history Show command history.
exec Replace shell with another command.
kill Terminate a process by PID.
pkill Terminate processes by name.
clear Clear the terminal screen.
lpstate Show the status of print jobs.
cancel Cancel a print job.
compress Compress files.
uncompress Decompress files.
exit Exit the shell.
Directory Commands
Commands to manage directories include:
Command Description
mkdir Create a new directory.
dir List directory contents (similar to ls).
cd Change the current directory.
df Display disk space usage.
Command Description
dfspace Show available disk space.
du Estimate file and directory space usage.
List detailed directory contents (long listing
ll
format).
dirname Strip the last component from a file name.
rmdir Remove an empty directory.
Directory Access Permissions
• Changing Permissions: Use chmod to change file
or directory permissions.
• Changing Group Ownership: Use chgrp to change
the group ownership.
• Changing Ownership: Use chown to change the
file owner.
Hard & Soft Links
• Hard Link: A reference to the same inode (physical
data on the disk). Changes to the file are reflected
in all hard links.
o Command Example: ln file1.txt link1.txt
• Soft Link (Symbolic Link): A pointer to another file
name in the file system.
o Command Example: ln -s file1.txt link1.txt
Environments and Path Setting
Environment variables store information about the
system and user session. The PATH variable is crucial as
it defines the directories the shell searches for
commands.
• Viewing Environment Variables: printenv or env
• Setting Environment Variables: export
VAR_NAME=value
I/O Redirection & Piping Commands
• I/O Redirection:
o >: Redirect output to a file (overwrite).
o >>: Redirect output to a file (append).
o <: Redirect input from a file.
• Piping: Connect the output of one command to
the input of another using the | symbol.
o Example: ls -l | grep "txt" (lists all .txt files)

vi Editor Overview
vi (Visual Editor) is a powerful text editor commonly
used in Unix and Linux systems. It operates in different
modes and offers a variety of commands for text
manipulation.
1. General Startup of vi
To start the vi editor, use the following command in the
terminal:
bash
Copy code
vi filename
• If filename exists, it opens the file for editing. If it
doesn't exist, vi creates a new file with that name.
2. Modes of vi
The vi editor has three primary modes:
• Normal Mode: The default mode for navigation
and command execution.
• Insert Mode: Used for inserting text.
• Command-Line Mode: Used for entering
commands that affect the editor or file.
Switching Between Modes
• To enter Insert Mode, press i (insert before the
cursor), a (append after the cursor), or o (open a
new line).
• To return to Normal Mode, press Esc.
• To enter Command-Line Mode, press : in Normal
Mode.
3. Creating and Editing Files
• To create a new file, use vi newfile.txt.
• After opening the file, switch to Insert Mode to
start typing.
• Save changes and exit with the command :wq in
Command-Line Mode.
4. Features of vi
• Syntax Highlighting: Automatically highlights
syntax based on the file type (in vim).
• Undo/Redo: Press u to undo changes and Ctrl + r
to redo.
• Search and Replace: Easily find and replace text
within the document.
5. Screen Movement
• Move the cursor with arrow keys or:
o h (left)
o j (down)
o k (up)
o l (right)
6. Cursor Movement
• To move by words: w (to the start of the next
word), b (to the start of the previous word).
• To move to the beginning or end of a line: 0
(beginning), $ (end).
• To move to specific line numbers: :n, where n is
the line number.
7. Insertion
• Press i, a, or o to insert text.
• To exit Insert Mode and return to Normal Mode,
press Esc.
8. Deletion
• To delete characters, use x in Normal Mode.
• To delete a whole line, use dd.
• To delete multiple lines, use ndd (where n is the
number of lines).
9. Searching
• To search for a text string, use /string (for forward
search) or ?string (for backward search).
• Press n to repeat the search in the same direction
and N to search in the opposite direction.
10. Submitting Operations
• To save the file, type :w.
• To quit without saving changes, type :q!.
11. Yank, Put, and Delete Commands
• Yank (copy): yy to copy a line.
• Put (paste): p to paste after the cursor or P to
paste before the cursor.
• Delete: Use d followed by movement commands
(e.g., dw to delete a word, dd to delete a line).
12. Reading & Writing Files
• Reading a file: Use :r filename to insert the
contents of another file into the current file.
• Writing a file: Use :w filename to save the current
buffer to a new file.
13. .exrc File for Setting Parameters
The .exrc file allows users to set vi parameters. Create a
.exrc file in your home directory with settings such as:
plaintext
Copy code
set number " Show line numbers
set tabstop=4 " Set tab width to 4 spaces
14. Advanced Editing Techniques
• Macros: Record a sequence of commands using q
followed by a letter to start recording, perform the
commands, then press q again to stop. Execute
with @letter.
• Search and Replace: :%s/old/new/g replaces all
occurrences of "old" with "new".
15. Vim (Improved vi)
Vim (Vi IMproved) is an extended version of vi that
includes many enhancements:
• Syntax Highlighting: Automatic syntax highlighting
for various programming languages.
• Multi-level Undo: Supports multiple undo levels.
• Split Windows: Allows editing multiple files in one
window.
• Plugins: Support for plugins to enhance
functionality.
Shell: Meaning and Purpose
A shell is a command-line interface that allows users to
interact with the operating system. It acts as an
intermediary between the user and the kernel,
interpreting commands and executing them. The
primary purposes of the shell include:
• Command Execution: Users can run programs and
scripts.
• File Management: Users can manipulate files and
directories.
• Automation: Users can write scripts to automate
tasks.
Types of Shell
Several types of shells are available in Unix/Linux
systems, each with unique features:
1. Bourne Shell (sh): The original Unix shell, known
for its simplicity.
2. Bourne Again Shell (bash): An enhanced version of
sh with additional features like command history
and improved scripting capabilities.
3. C Shell (csh): Similar to the C programming
language, offering features like job control and
aliases.
4. Korn Shell (ksh): Combines features of sh and csh,
with enhancements for scripting.
5. Z Shell (zsh): An extended version of bash with
powerful features like spell checking and globbing.
The Command Line
The command line is where users enter commands to
interact with the shell. A typical command format is:
bash
Copy code
command [options] [arguments]
• Command: The name of the program to execute.
• Options: Flags to modify the command's behavior
(usually preceded by -).
• Arguments: Additional inputs for the command,
such as file names.
Standard Input and Standard Output
• Standard Input (stdin): The default source of input
for commands, usually the keyboard.
• Standard Output (stdout): The default destination
for output from commands, usually the terminal.
Redirection
Redirection allows users to change the standard input
and output. Common redirection operators include:
• >: Redirect stdout to a file (overwrite).
bash
Copy code
command > output.txt
• >>: Redirect stdout to a file (append).
bash
Copy code
command >> output.txt
• <: Redirect stdin from a file.
bash
Copy code
command < input.txt
Pipes
Pipes allow users to connect the output of one
command to the input of another using the | operator.
bash
Copy code
command1 | command2
This command executes command1, and its output is
used as input for command2.
Filters
Filters are commands that process input and produce
output. Common filter commands include:
• grep: Search for patterns in text.
• sort: Sort lines of text.
• uniq: Remove duplicate lines.
Special Characters for Searching Files and Pathnames
• Wildcard Characters:
o *: Matches zero or more characters (e.g., *.txt
matches all .txt files).
o ?: Matches a single character (e.g., file?.txt
matches file1.txt, fileA.txt, etc.).
o [...]: Matches any one character within the
brackets (e.g., file[1-3].txt matches file1.txt,
file2.txt, file3.txt).
Built-ins
Built-in commands are part of the shell and do not
require an external program to execute. Examples
include:
• cd: Change the current directory.
• echo: Display a line of text.
• exit: Exit the shell.
Functions
Functions are reusable blocks of code defined by the
user in a shell script, allowing for modular
programming.
bash
Copy code
function_name() {
# commands
}
History
The shell maintains a history of commands executed by
the user, allowing for easy recall and reuse.
• To view history: history
• To execute the last command: !!
• To execute a specific command from history: !n
(where n is the command number).
Aliases
Aliases are shortcuts for commands, allowing users to
create simpler or customized commands.
bash
Copy code
alias ll='ls -l'
Job Control
Job control allows users to manage multiple processes
in the shell. Key commands include:
• &: Run a command in the background.
• jobs: List background jobs.
• fg: Bring a background job to the foreground.
• bg: Resume a suspended job in the background.
File Substitution
File substitution refers to the use of special characters
to represent files or directories in commands, such as *
for wildcard matching.
Source Code Management - RCS and CVS
• RCS (Revision Control System): A version control
system that keeps track of changes to files,
allowing users to retrieve previous versions.
• CVS (Concurrent Versions System): An extension
of RCS that supports multiple developers working
on the same project simultaneously.
awk Utility
awk is a powerful text-processing tool used for pattern
scanning and processing. It is particularly useful for
extracting and manipulating data from files or input
streams. The basic syntax is:
bash
Copy code
awk 'pattern { action }' filename
Example: Print the first column of a file:
bash
Copy code
awk '{ print $1 }' file.txt

Features of Linux
1. Open Source: Linux is free to use, modify, and
distribute, which encourages collaboration and
innovation.
2. Multiuser Capability: Multiple users can access the
system simultaneously without interfering with
each other.
3. Multitasking: Linux can run multiple tasks at the
same time, enhancing efficiency.
4. Portability: Linux can run on various hardware
platforms, from servers to embedded systems.
5. Security: Linux is known for its strong security
features, including user permissions and robust
firewall configurations.
6. Stability and Reliability: Linux systems are less
prone to crashes and can run for long periods
without requiring a reboot.
7. Flexibility: Users can customize and configure the
system to meet their specific needs, including the
desktop environment and applications.
8. Support for Networking: Linux includes extensive
networking capabilities and protocols, making it
ideal for servers and networking applications.
9. Large Community Support: A vast community of
developers and users contributes to ongoing
improvements, support, and documentation.
Drawbacks of Linux
1. Learning Curve: New users may find Linux
challenging to learn, especially those used to
graphical interfaces.
2. Compatibility Issues: Some proprietary software
applications may not be available or fully
compatible with Linux.
3. Hardware Compatibility: While Linux supports
many devices, some hardware may not have
available drivers, leading to functionality issues.
4. Limited Gaming Support: While improving, the
availability of games on Linux is still less than on
other operating systems.
5. Fragmentation: The existence of numerous
distributions can lead to confusion and
inconsistencies in applications and system
management.
Components of Linux
1. Kernel: The core component that manages system
resources and hardware communication.
2. System Libraries: Standard libraries that provide
basic functions for applications, enabling them to
interact with the kernel.
3. System Utilities: Essential tools and applications
for system management and configuration.
4. Shell: A command-line interface that allows users
to interact with the kernel and execute commands.
5. User Interface: Graphical user interfaces (GUIs)
such as GNOME, KDE, or Xfce for user interaction.
Memory Management Subsystems
• Virtual Memory: Linux uses virtual memory to
manage memory efficiently, allowing applications
to use more memory than physically available.
• Paging: Memory is divided into pages, which can
be swapped between physical memory and disk
storage.
• Memory Allocation: The kernel manages memory
allocation and deallocation for processes through
system calls like malloc and free.
• Caching: Frequently accessed data is cached in
RAM to speed up access and improve
performance.
Linux Process and Thread Management
• Process Management: The kernel manages
processes using process control blocks (PCBs) that
store process information such as state, priority,
and memory usage.
• Thread Management: Linux supports
multithreading, allowing multiple threads within a
process to run concurrently.
• Scheduling: The Linux kernel uses scheduling
algorithms (like Completely Fair Scheduler - CFS) to
allocate CPU time to processes and threads.
• Inter-process Communication (IPC): Various IPC
mechanisms, such as pipes, message queues, and
shared memory, allow processes to communicate
with each other.
File Management System
• File System Structure: Linux uses a hierarchical file
system structure with directories and files
organized in a tree-like format.
• File Types: Supports various file types, including
regular files, directories, symbolic links, and special
files.
• File Permissions: Users and groups can have
different permissions (read, write, execute) for
files, ensuring security.
• Mounting File Systems: File systems can be
mounted dynamically, allowing users to access files
on different storage devices.
• File System Types: Supports multiple file systems,
including ext4, XFS, Btrfs, and FAT32.
Device Drivers
• Kernel Modules: Device drivers are often
implemented as kernel modules that can be
loaded or unloaded as needed.
• Hardware Abstraction: Device drivers provide a
layer of abstraction between the hardware and the
kernel, allowing the kernel to interact with
hardware devices without needing specific
knowledge of them.
• Support for Various Devices: Linux supports a
wide range of devices, including storage, network,
and peripheral devices, through drivers.
Entering the Machine
To access a Linux system, you typically use a terminal or
console. If you're using a remote server, you might use
an SSH (Secure Shell) client to log in:
bash
Copy code
ssh username@hostname
User Names and Groups
• Usernames: Each user has a unique username that
identifies them on the system.
• Groups: Users can belong to one or more groups,
which manage permissions for file access and
execution. You can check your groups with:
bash
Copy code
groups
Logging In
When logging in, you'll enter your username and
password. Upon successful login, you'll have access to
the system.
Correcting Typing Mistakes
While typing commands, you can:
• Use the Backspace key to delete characters.
• Use the Arrow keys to navigate through the
command history.
• Press Ctrl + U to delete the entire line.
• Press Ctrl + W to delete the last word.
Format of Linux Commands
A basic Linux command structure is:
bash
Copy code
command [options] [arguments]
• command: The command to execute.
• options: Flags that modify the command behavior
(e.g., -l, -a).
• arguments: The target of the command (e.g.,
filenames or directories).
Changing Your Password
To change your user password, use the passwd
command:
bash
Copy code
passwd
Follow the prompts to enter your current password and
set a new one.
Characters with Special Meanings
Certain characters have special meanings in Linux:
• ~: Represents the home directory of the current
user.
• /: Represents the root directory.
• .: Represents the current directory.
• ..: Represents the parent directory.
• *: Wildcard that matches any number of
characters.
• ?: Wildcard that matches a single character.
• &: Run a command in the background.
• |: Pipe output from one command to another.
Linux Documentation
You can access documentation using the man (manual)
command:
bash
Copy code
man command_name
Example: To read the manual for the ls command:
bash
Copy code
man ls
You can also use info for more detailed documentation.
The File System
Linux uses a hierarchical file system where everything is
organized in a tree structure, starting from the root
directory /.
Current Directory
To check the current working directory, use the pwd
(print working directory) command:
bash
Copy code
pwd
Looking at the Directory Contents
To list the contents of a directory, use the ls command:
bash
Copy code
ls # List files in the current directory
ls -l # Long format (detailed view)
ls -a # Show hidden files (those starting with .)
Absolute and Relative Pathnames
• Absolute Pathname: The full path to a file or
directory, starting from the root directory. For
example:
bash
Copy code
/home/username/Documents/file.txt
• Relative Pathname: A path relative to the current
directory. For example, if you are in
/home/username:
bash
Copy code
Documents/file.txt
Some Linux Directories and Files
Here are some important directories in a typical Linux
file system:
• /: Root directory.
• /home: Contains user home directories.
• /etc: Configuration files for the system.
• /var: Variable data files (e.g., logs).
• /usr: User applications and files.
• /bin: Essential command binaries.
• /sbin: System binaries for administrative tasks.

Some Useful Commands


Here are some commonly used Linux commands:
1. File and Directory Management
o mkdir dirname: Create a new directory.
o rmdir dirname: Remove an empty directory.
o rm filename: Remove a file.
o cp source dest: Copy a file or directory.
o mv source dest: Move or rename a file or
directory.
2. File Viewing and Searching
o cat filename: Display the content of a file.
o less filename: View a file one screen at a time.
o head filename: Show the first 10 lines of a file.
o tail filename: Show the last 10 lines of a file.
o grep "pattern" filename: Search for a pattern
in a file.
3. System Information
o top: Display active processes and system
resource usage.
o df: Show disk space usage.
o free: Show memory usage.
o uname -a: Display system information.
4. Networking
o ping hostname: Check connectivity to a host.
o ifconfig: Display network interface
configuration (use ip a in newer systems).
o curl url: Transfer data from or to a server.
Permission Modes and Standard Files
• File Permissions: Each file and directory has
associated permissions that determine who can
read, write, or execute the file.
o Permissions are represented as r (read), w
(write), and x (execute).
o You can check permissions using:
bash
Copy code
ls -l
• Changing Permissions: Use the chmod command
to change permissions.
bash
Copy code
chmod u+rwx filename # User: read, write, and
execute
chmod g-w filename # Group: remove write
permission
chmod o+r filename # Others: add read permission
• Standard Files: In Linux, there are three standard
files for input and output:
o Standard Input (stdin): Keyboard input,
usually file descriptor 0.
o Standard Output (stdout): Output to the
terminal, usually file descriptor 1.
o Standard Error (stderr): Error messages,
usually file descriptor 2.
Pipes, Filters, and Redirection
• Pipes: Connect the output of one command to the
input of another using |.
bash
Copy code
ls -l | grep "pattern" # List files and filter the output
• Filters: Utilities that process input data (e.g., grep,
sort, awk).
o Example: Sorting the output of a command:
bash
Copy code
ps aux | sort -nrk 3 # Sort processes by memory
usage
• Redirection: Change where input and output go.
o Redirect output to a file:
bash
Copy code
command > output.txt # Overwrites the file
command >> output.txt # Appends to the file
o Redirect input from a file:
bash
Copy code
command < input.txt
Shell Scripts
• Shell Scripts: A shell script is a text file containing a
series of commands that the shell can execute.
o To create a shell script, create a file with a .sh
extension and add the following line at the
top:
bash
Copy code
#!/bin/bash
o Example script (myscript.sh):
bash
Copy code
#!/bin/bash
echo "Hello, World!"
o Make the script executable and run it:
bash
Copy code
chmod +x myscript.sh
./myscript.sh
Graphical User Interface (GUI)
• Graphical User Interface: Many Linux distributions
offer GUIs for easier interaction. Popular desktop
environments include:
o GNOME: A user-friendly environment with
modern design.
o KDE Plasma: Highly customizable and feature-
rich.
o XFCE: Lightweight and fast, suitable for older
hardware.
• You can launch applications from the GUI or use a
terminal emulator within the GUI.
Editor
• Text Editors: Several text editors are available in
Linux for editing files:
o nano: A simple command-line text editor. To
open a file:
bash
Copy code
nano filename
o vim: A powerful command-line text editor
with more features. To open a file:
bash
Copy code
vim filename
▪ Basic vim commands:
▪ Press i to enter insert mode (for
editing).
▪ Press Esc to exit insert mode.
▪ Type :w to save changes, :q to quit, or
:wq to save and quit.
o gedit: A graphical text editor available in
GNOME environments.
bash
Copy code
gedit filename &

UNIX System Administration


1. System Administration
System administration involves managing the
operations and maintenance of a UNIX system. Key
responsibilities include:
• User management: Creating and maintaining user
accounts.
• File system management: Managing disk space
and file permissions.
• Software management: Installing and updating
software packages.
• System monitoring: Monitoring system
performance and health.
• Security management: Ensuring system security
through user permissions and firewalls.
2. Installing Linux
The installation process for Linux can vary based on the
distribution (e.g., Ubuntu, CentOS). Common steps
include:
• Download the ISO: Obtain the installation image
from the official website.
• Create a bootable medium: Use tools like Rufus or
dd to create a bootable USB drive.
3. Choosing an Installation Method
There are several installation methods available:
• Graphical Installation: A user-friendly interface
guides users through the installation process.
• Text-Based Installation: A terminal-based interface
suitable for remote installations or systems with
limited resources.
• Network Installation: Installing the operating
system over a network using PXE (Preboot
Execution Environment).
4. Choosing an Installation Class
Installation classes can determine how the system is set
up:
• Desktop Environment: For users who need a
graphical user interface (GUI).
• Server Environment: For systems that will operate
without a GUI, focusing on performance and
resource management.
• Custom Installation: Allows users to select specific
packages and configurations.
5. Pre-installation Checks
Before installation, perform these checks:
• Hardware Compatibility: Ensure the hardware
meets the minimum requirements for the chosen
Linux distribution.
• Disk Space: Verify sufficient disk space is available
for the installation.
• Backup Existing Data: Backup any important data
to prevent loss during installation.
• Check BIOS/UEFI Settings: Ensure booting from
USB or DVD is enabled, and the boot order is
correct.
6. Installation
The installation process generally includes:
• Booting from the Installation Medium: Insert the
bootable USB or DVD and boot the system.
• Partitioning the Disk: Select or create partitions
for the operating system, swap space, and data.
• Selecting Packages: Choose the desired software
packages to install.
• Setting Up User Accounts: Create the initial user
account and set passwords.
7. Booting the System
After installation, the system can be booted into the
new environment:
• GRUB (Grand Unified Bootloader): A common
bootloader for Linux that allows users to select the
operating system or kernel version at boot.
• Boot Process: The system goes through the boot
sequence, loading the kernel, initializing hardware,
and starting services.
8. Maintaining User Accounts
• Creating User Accounts: Use the useradd
command to create new users:
bash
Copy code
useradd username
• Deleting User Accounts: Remove users with the
userdel command:
bash
Copy code
userdel username
• Modifying User Accounts: Change user details
with the usermod command:
bash
Copy code
usermod -aG groupname username # Add user to a
group
• Managing Passwords: Use the passwd command
to set or change passwords:
bash
Copy code
passwd username
9. File Systems and Special Files
• File System Types: Common file systems include
ext4, XFS, and Btrfs.
• Mounting File Systems: Use the mount command
to attach file systems to the directory structure:
bash
Copy code
mount /dev/sda1 /mnt/mydisk
• Checking File System Status: Use df to check disk
space and du to check directory usage:
bash
Copy code
df -h # Show available disk space
du -sh * # Show directory sizes
10. Backups and Restoration
Regular backups are crucial for data integrity and
recovery:
• Backup Methods:
o Full Backup: Copying all data.
o Incremental Backup: Copying only data that
has changed since the last backup.
o Differential Backup: Copying data that has
changed since the last full backup.
• Backup Tools:
o tar: To create archives:
bash
Copy code
tar -cvf backup.tar /path/to/directory
o rsync: For efficient file synchronization and
backups:
bash
Copy code
rsync -av /source /destination
• Restoration: Use the same tools to restore data.
For tar, you can extract files using:
bash
Copy code
tar -xvf backup.tar

You might also like