0% found this document useful (0 votes)
3 views3 pages

Shell Programs

The document provides a series of shell scripts related to various aspects of operating systems, including system overview, process management, synchronization, memory management, and I/O systems. Each section includes a demonstration script that showcases specific functionalities, such as acquiring locks, simulating memory allocation, and creating virtual disks. The scripts serve as practical examples for understanding core operating system concepts and their implementation in shell scripting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Shell Programs

The document provides a series of shell scripts related to various aspects of operating systems, including system overview, process management, synchronization, memory management, and I/O systems. Each section includes a demonstration script that showcases specific functionalities, such as acquiring locks, simulating memory allocation, and creating virtual disks. The scripts serve as practical examples for understanding core operating system concepts and their implementation in shell scripting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

WEEK-3.

Shell Script programming related to across all the units


1. OVERVIEW OF OPERATING SYSTEMS
• Demonstration: basic system information and a simple “OS overview” report.
Script: os_overview.sh------------→File Name
#!/usr/bin/env
bash
echo "=== Operating System Overview ==="
echo "Hostname: $(hostname)"
echo "Kernel: $(uname -s) $(uname -r)"
echo "Shell: $SHELL"
echo "Uptime: $(uptime -p)"
echo "Users logged in: $(who | wc -l)"
echo "Load averages (1m 5m 15m): $(uptime | awk -F 'load averages:' '{print $2}' | sed 's/^ *//')"
echo "Total memory: $(free -h | awk '/^Mem:/ {print $2}')"
echo "Total swap: $(free -h | awk '/^Swap:/ {print $2}')"
echo "Disk usage (root):"
df -h /
echo "Top 5 processes by CPU usage:"
ps -eo pid,ppid,user,%cpu,%mem,args --sort=-%cpu | head –n 6

Result: chmod +x os_overview.sh


./os_overview.sh

2. PROCESS MANAGEMENT AND SCHEDULING

• Demonstration: list processes by different criteria; simple round-robin-ish task launcher/scheduler using background jobs;
measure turnaround.

Script: proc_management.sh-----------→ File name

echo "Top 5 CPU-consuming processes:"


ps -eo pid,ppid,user,%cpu,%mem,cmd --sort=-%cpu | head -n 6
echo
echo "Top 5 Memory-consuming processes:"
ps -eo pid,ppid,user,%cpu,%mem,cmd --sort=-%mem | head -n 6
echo
echo "Zombie processes (if any):"
ps -eo pid,ppid,stat,cmd | awk '$3 ~ /Z/ {print $0}' | head

# Simple round-robin-like task launcher (demo)


run_task()
{
local name="$1"
sleep "$2"
echo "Task $name finished after ${2}s"
}
export -f run_task

echo
echo "Launching 3 tasks in parallel (round-robin style):"
bash -c 'for i in 1 2 3; do (run_task "T$i" $((RANDOM % 5 + 1)) &); done; wait'
echo "All tasks completed."

3. PROCESS SYNCHRONIZATION AND DEADLOCK


• Demonstration: file-based locking as a synchronization primitive; simple deadlock
avoidance example.
Script: synchronization_deadlock.sh-------------→File name
bash

LOCKDIR="/tmp/sync_demo_lock"
LOCKFILE="$LOCKDIR/lockfile"
mkdir -p "$LOCKDIR"
# Function to acquire a lock (non-blocking attempt for demonstration)
acquire_lock()
{
if mkdir "$LOCKDIR/.lock.$$" 2>/dev/null; then
echo "Lock acquired by $$"
echo "$$" > "$LOCKFILE"
return 0
else
echo "Lock busy, PID: $(cat "$LOCKFILE" 2>/dev/null || echo 'unknown')"
return 1
fi
}
# Release lock
release_lock()
{
if [ -f "$LOCKFILE" ]; then
echo "Lock released by $$"
rm -f "$LOCKFILE"
fi
# remove any stale directory if exists
while [ -d "$LOCKDIR/.lock.$$" ]; do
rm -rf "$LOCKDIR/.lock.$$"
done
}
# Example usage: two pseudo-processes trying to acquire lock
echo "Process 1 attempting to acquire lock..."
if acquire_lock; then
sleep 2
release_lock
fi
echo "Process 2 attempting to acquire lock..."
if acquire_lock; then
sleep 2
release_lock
fi
# Simple deadlock avoidance: avoid holding two locks in different orders.
# In real systems you'd use proper semaphores; this shell demo is for illustration.

4. MEMORY MANAGEMENT
• Demonstration: simulate a simple free list and memory allocation in shell arrays (very abstract). Not a real allocator, but shows allocation/deallocation
notions.
Script: memory_management.sh------→ File Name
#!/usr/bin/env bash
# memory_management.sh - abstract memory management simulation

TOTAL=1024 # total units


declare -a memory
for ((i=0;i<TOTAL;i++)); do memory[i]=0; done
alloc()
{
local needed="$1"
local start=0
local i
# first-fit search for a block of zeros
for ((start=0; start<TOTAL; )); do
# check block
local ok=1
for ((i=start; i<start+needed && i<TOTAL; i++)); do
if (( memory[i] != 0 )); then ok=0; break; fi
done
if (( ok )); then
for ((i=start; i<start+needed && i<TOTAL; i++)); do memory[i]=1; done
echo "Allocated ${needed} units at ${start}"
return 0
fi
# move to next position
((start++))
done
echo "Allocation of ${needed} units failed"
return 1
}
free_block()
{
local addr="$1"
local size="$2"
for ((i=addr; i<addr+size && i<TOTAL; i++)); do memory[i]=0; done
echo "Freed ${size} units from ${addr}"
}
display()
{
local used=0
for val in "${memory[@]}"; do ((val==1)) && ((used++)); done
echo "Memory usage: ${used}/${TOTAL} units used"
}
echo "Initial memory state"
display
echo
# Demo allocations
alloc 100
alloc 200
display
echo
free_block 50 50
display

5. I/O SYSTEM, MASS STORAGE STRUCTURE AND VIRTUALIZATION


• Demonstration: list block devices, show disk usage, and simulate a virtual disk with loopback device.
Script: io_and_storage.sh---------→ FILE NAME

#!/usr/bin/env bash
# io_and_storage.sh - basic I/O and storage introspection plus a loopback-based virtual
disk demo
echo "Block devices (lsblk):"
lsblk -o NAME,TYPE,SIZE,MODEL -d | head -n 20
echo
echo "Disk usage (root and /home):"
df -h /
df -h /home 2>/dev/null || echo "/home not mounted"
echo "I/O statistics (iostat) - if sysstat installed"
if command -v iostat >/dev/null 2>&1; then
iostat -dx 1 2 | sed -n '1,200p'
else
echo "iostat not installed (install sysstat to enable this)."
fi
echo
# Virtual disk via loopback (requires sudo)
VDISK=/tmp/virtual_disk.img
LOOP=/dev/loop0
SIZE=100M
if [ ! -f "$VDISK" ]; then
dd if=/dev/zero of="$VDISK" bs=1M count=100 conv=fsync >/dev/null 2>&1
echo "Created virtual disk at $VDISK"
fi
echo "Setting up loopback device (may require sudo):"
if ! sudo losetup -a | grep -q "$VDISK"; then
sudo losetup -f
LOOP_DEV=$(sudo losetup -f --show "$VDISK")
echo "Attached $VDISK to $LOOP_DEV"
sudo mkfs.ext4 "$LOOP_DEV" >/dev/null 2>&1 && echo "Filesystem created on
$LOOP_DEV"
else
echo "Loopback already attached."
fi
# Cleanup suggestion
echo "To detach (after use): sudo losetup -d $LOOP_DEV"

You might also like