0% found this document useful (0 votes)
52 views14 pages

C and Bash Scripts for Process Management

The document contains a series of programming examples in C and Bash scripts that demonstrate various functionalities, including process management, mathematical operations, and data manipulation. Each set provides code snippets for tasks such as displaying process IDs, calculating factorials, checking prime numbers, and sorting arrays. The examples are structured to guide users through compiling and executing the code, along with explanations of their purpose.
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)
52 views14 pages

C and Bash Scripts for Process Management

The document contains a series of programming examples in C and Bash scripts that demonstrate various functionalities, including process management, mathematical operations, and data manipulation. Each set provides code snippets for tasks such as displaying process IDs, calculating factorials, checking prime numbers, and sorting arrays. The examples are structured to guide users through compiling and executing the code, along with explanations of their purpose.
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

Set-1: C Program to display PID and PPID

CopyEdit

#include <stdio.h>

#include <unistd.h>

int main() {

printf("Current Process ID (PID): %d\n", getpid());

printf("Parent Process ID (PPID): %d\n", getppid());

return 0;

Compile with gcc filename.c -o outputname and run with ./outputname

Set-2: Shell Script to Find Factorial

bash

CopyEdit

#!/bin/bash

read -p "Enter a number: " num

fact=1

for (( i=1; i<=num; i++ ))

do

fact=$((fact * i))

done

echo "Factorial of $num is $fact"

Set-3: Shell Script to Find Sum and Count of Digits

bash

CopyEdit

#!/bin/bash
read -p "Enter a number: " num

temp=$num

sum=0

count=0

while [ $temp -gt 0 ]

do

digit=$((temp % 10))

sum=$((sum + digit))

temp=$((temp / 10))

count=$((count + 1))

done

echo "Sum of digits of $num is $sum"

echo "Count of digits in $num is $count"

Set-4: Shell Script to Find Reverse of a Number

bash

CopyEdit

#!/bin/bash

read -p "Enter a number: " num

rev=0

temp=$num

while [ $temp -gt 0 ]

do

digit=$((temp % 10))

rev=$((rev * 10 + digit))

temp=$((temp / 10))
done

echo "Reverse of $num is $rev"

Set-5: Shell Script to Check Prime Number

bash

CopyEdit

#!/bin/bash

read -p "Enter a number: " num

if [ $num -le 1 ]; then

echo "$num is not a prime number"

exit

fi

is_prime=1

for (( i=2; i<=num/2; i++ ))

do

if [ $((num % i)) -eq 0 ]; then

is_prime=0

break

fi

done

if [ $is_prime -eq 1 ]; then

echo "$num is a prime number"

else

echo "$num is not a prime number"

fi
Set-6: Shell Script to Check Palindrome

bash

CopyEdit

#!/bin/bash

read -p "Enter a number: " num

original=$num

rev=0

while [ $num -gt 0 ]

do

digit=$((num % 10))

rev=$((rev * 10 + digit))

num=$((num / 10))

done

if [ $original -eq $rev ]; then

echo "$original is a palindrome number"

else

echo "$original is not a palindrome number"

fi

Set-7: Shell Script to Check Armstrong Number

(For 3-digit number)

bash

CopyEdit

#!/bin/bash

read -p "Enter a number: " num

original=$num
sum=0

while [ $num -gt 0 ]

do

digit=$((num % 10))

cube=$((digit * digit * digit))

sum=$((sum + cube))

num=$((num / 10))

done

if [ $original -eq $sum ]; then

echo "$original is an Armstrong number"

else

echo "$original is not an Armstrong number"

fi

Set-8: C Program to Create Zombie Process

CopyEdit

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main() {

pid_t pid = fork();

if (pid > 0) {

printf("Parent process. PID: %d\n", getpid());

sleep(10); // Sleep so child becomes zombie

} else if (pid == 0) {

printf("Child process. PID: %d\n", getpid());


exit(0); // Child exits immediately

} else {

perror("fork failed");

return 0;

Compile with gcc zombie.c -o zombie and run with ./zombie


Use ps -l or ps aux | grep zombie to see zombie in action (look for Z+)

Set-9: Shell Script for Basic Arithmetic Operations

bash

CopyEdit

#!/bin/bash

read -p "Enter first number: " a

read -p "Enter second number: " b

add=$((a + b))

sub=$((a - b))

mul=$((a * b))

# Check division by zero

if [ $b -ne 0 ]; then

div=$((a / b))

rem=$((a % b))

else

div="Undefined (division by zero)"

rem="Undefined"

fi
echo "Addition: $add"

echo "Subtraction: $sub"

echo "Multiplication: $mul"

echo "Division: $div"

echo "Remainder: $rem"

Set-10: Shell Script to Check Valid User

bash

CopyEdit

#!/bin/bash

read -p "Enter username to check: " uname

if id "$uname" &>/dev/null; then

echo "User '$uname' exists on the system."

else

echo "User '$uname' does NOT exist."

fi

Set-11: Shell Script to Generate Prime Numbers from 1 to 100

bash

CopyEdit

#!/bin/bash

echo "Prime numbers from 1 to 100 are:"

for (( num=2; num<=100; num++ ))

do

is_prime=1

for (( i=2; i<=num/2; i++ ))

do
if [ $((num % i)) -eq 0 ]; then

is_prime=0

break

fi

done

if [ $is_prime -eq 1 ]; then

echo -n "$num "

fi

done

echo

Set-12: Menu-Driven Calculator Script

bash

CopyEdit

#!/bin/bash

echo "Menu Driven Calculator"

echo "1. Addition"

echo "2. Subtraction"

echo "3. Multiplication"

echo "4. Division"

read -p "Enter first number: " a

read -p "Enter second number: " b

read -p "Choose operation [1-4]: " choice

case $choice in

1) echo "Result: $((a + b))";;

2) echo "Result: $((a - b))";;

3) echo "Result: $((a * b))";;


4)

if [ $b -ne 0 ]; then

echo "Result: $((a / b))"

else

echo "Error: Division by zero"

fi

;;

*) echo "Invalid choice";;

esac

Set-13: Shell Script to Find Maximum of Two Numbers

bash

CopyEdit

#!/bin/bash

read -p "Enter first number: " a

read -p "Enter second number: " b

if [ $a -gt $b ]; then

echo "Maximum is: $a"

else

echo "Maximum is: $b"

fi

Set-14: Shell Script to Find Maximum of Three Numbers

bash

CopyEdit

#!/bin/bash

read -p "Enter first number: " a

read -p "Enter second number: " b


read -p "Enter third number: " c

if [ $a -ge $b ] && [ $a -ge $c ]; then

echo "Maximum is: $a"

elif [ $b -ge $a ] && [ $b -ge $c ]; then

echo "Maximum is: $b"

else

echo "Maximum is: $c"

fi

Set-15: Shell Script to Input n Numbers in an Array and Display Them

bash

CopyEdit

#!/bin/bash

read -p "Enter number of elements: " n

declare -a arr

echo "Enter $n numbers:"

for (( i=0; i<n; i++ ))

do

read -p "Element $((i+1)): " arr[i]

done

echo "Array elements are:"

for (( i=0; i<n; i++ ))

do

echo "${arr[i]}"

done

Set-16: Shell Script to Find Maximum Value in Array


bash

CopyEdit

#!/bin/bash

read -p "Enter number of elements: " n

declare -a arr

echo "Enter $n numbers:"

for (( i=0; i<n; i++ ))

do

read -p "Element $((i+1)): " arr[i]

done

max=${arr[0]}

for (( i=1; i<n; i++ ))

do

if [ ${arr[i]} -gt $max ]; then

max=${arr[i]}

fi

done

echo "Maximum value is: $max"

Set-17: Shell Script to Find Sum and Average of 3 Numbers

bash

CopyEdit

#!/bin/bash

read -p "Enter first number: " a

read -p "Enter second number: " b


read -p "Enter third number: " c

sum=$((a + b + c))

avg=$(echo "scale=2; $sum / 3" | bc)

echo "Sum = $sum"

echo "Average = $avg"

Set-18: Shell Script for Grading (MAKAUT Rule Example)

bash

CopyEdit

#!/bin/bash

read -p "Enter marks for Subject 1: " m1

read -p "Enter marks for Subject 2: " m2

read -p "Enter marks for Subject 3: " m3

total=$((m1 + m2 + m3))

avg=$((total / 3))

echo "Average Marks: $avg"

if [ $avg -ge 90 ]; then

grade="O (Outstanding)"

elif [ $avg -ge 80 ]; then

grade="E (Excellent)"

elif [ $avg -ge 70 ]; then

grade="A (Very Good)"

elif [ $avg -ge 60 ]; then

grade="B (Good)"

elif [ $avg -ge 50 ]; then


grade="C (Fair)"

elif [ $avg -ge 40 ]; then

grade="D (Pass)"

else

grade="F (Fail)"

fi

echo "Grade: $grade"

Set-19: C Program to Implement Zombie Process

(This is a repeat from Set-8, but re-adding here for completeness.)

CopyEdit

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main() {

pid_t pid = fork();

if (pid > 0) {

printf("Parent Process. PID: %d\n", getpid());

sleep(10); // Parent sleeps, child becomes zombie

} else if (pid == 0) {

printf("Child Process. PID: %d\n", getpid());

exit(0); // Exiting immediately

} else {

perror("Fork failed");

return 0;

Compile using gcc zombie.c -o zombie


Run with ./zombie, then check with ps aux | grep zombie
Set-20: Shell Script to Sort n Elements (Ascending)

bash

CopyEdit

#!/bin/bash

read -p "Enter number of elements: " n

declare -a arr

echo "Enter $n numbers:"

for (( i=0; i<n; i++ ))

do

read -p "Element $((i+1)): " arr[i]

done

# Bubble sort

for (( i=0; i<n; i++ ))

do

for (( j=0; j<n-i-1; j++ ))

do

if [ ${arr[j]} -gt ${arr[j+1]} ]; then

temp=${arr[j]}

arr[j]=${arr[j+1]}

arr[j+1]=$temp

fi

done

done

echo "Sorted array (ascending):"

for val in "${arr[@]}"

do

echo "$val"

done

You might also like