Professional Documents
Culture Documents
Document 26
Document 26
import java.util.concurrent.Semaphore;
// Create 5 threads
// Acquire a permit
semaphore.acquire();
// Critical section
semaphore.release();
}).start();
import java.util.Scanner;
@Override
try
pc.produce();
catch(InterruptedException e)
e.printStackTrace();
});
// pc.consume()
@Override
try
pc.consume();
catch(InterruptedException e)
e.printStackTrace();
});
t1.start();
t2.start();
// t1 finishes before t2
t1.join();
t2.join();
// consume() methods.
// running at a time.
synchronized(this)
wait();
System.out.println("Resumed");
// Sleeps for some time and waits for a key press. After key
Thread.sleep(1000);
// running at a time.
synchronized(this)
s.nextLine();
notify();
// Sleep
Thread.sleep(2000);
import java.util.concurrent.locks.ReentrantLock;
// Acquire lock1
lock1.lock();
try {
lock2.lock();
} catch (Exception e) {
// Release lock1
lock1.unlock();
});
// Acquire lock2
lock2.lock();
try {
lock1.lock();
} catch (Exception e) {
// Release lock2
lock2.unlock();
});
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
1. a) Write a shell script sum.sh that takes an unspecified number of command line arguments (upto 9)
of integer and finds their average. Modify the code to add a number to the sum only if the number is
greater than 10.
#!/bin/bash
exit 1
fi
sum=0
count=0
exit 1
fi
sum=$((sum + arg))
count=$((count + 1))
fi
done
else
fi
b) Write a shell script that takes a filename as input and counts the occurrences of a specific word in
that file.
#!/bin/bash
exit 1
fi
filename=$1
word=$2
if [ ! -f "$filename" ]; then
exit 1
fi
echo "The word '$word' occurs $count times in the file '$filename'."
#!/bin/bash
exit 1
fi
directory=$1
if [ ! -d "$directory" ]; then
exit 1
fi
#!/bin/bash
exit 1
fi
path=$1
if [ -e "$path" ]; then
if [ -f "$path" ]; then
else
echo "'$path' exists but is neither a regular file nor a directory nor a symbolic link."
fi
else
fi
employees.txt
employee_operations.sh
#!/bin/bash
employee_file="employees.txt"
if [ ! -f "$employee_file" ]; then
exit 1
fi
echo "------------------------------"
head -n 5 "$employee_file"
echo
echo "-------------------------------------------------------"
echo
echo "------------------------------------"
exit 1
fi
# Input number
number=$1
exit 1
fi
else
fi
6. a) Write a shell script to say good morning/afternoon/evening/ night according to system time.
#!/bin/bash
current_hour=$(date +%H)
greeting="Good Morning"
greeting="Good Afternoon"
greeting="Good Evening"
else
greeting="Good Night"
fi
echo $greeting
b) Write a shell script to check a year is leap year or not. (User input)
#!/bin/bash
exit 1
fi
else
fi
7. a) Write a shell script to count how many prime numbers are given through command line.
#!/bin/bash
# Function to check if a number is prime
is_prime() {
num=$1
fi
return 0 # 2 is prime
fi
fi
limit=$(( num / 2 ))
fi
done
prime_count=0
(( prime_count++ ))
fi
done
#!/bin/bash
find_smallest() {
smallest=$1
smallest=$2
else
smallest=$3
fi
exit 1
fi
exit 1
fi
exit 1
fi
# Call the function to find the smallest number
9. a) Write a shell script that takes a name of a folder as a command line argument, and produce a file
that contains the names of all sub folders with size 0 (that is empty sub folders
#!/bin/bash
exit 1
fi
folder="$1"
if [ ! -d "$folder" ]; then
exit 1
fi
if [ -s "empty_subfolders.txt" ]; then
cat empty_subfolders.txt
else
fi
b) Write a shell script that will print only even numbers from the command line arguments.
#!/bin/bash
echo "$num"
fi
fi
done
10. a) Create a student file (Sid, Sname, DeptName, Marks) Enter at least 10 records.
students.txt
#!/bin/bash
student_file="students.txt"
if [ ! -f "$student_file" ]; then
exit 1
fi
echo "------------------------------"
tail -n 5 "$student_file"
echo
echo "---------------------------------------------"
b) Write a shell script to print Fibonacci series upto n terms. (user input)
#!/bin/bash
fibonacci_series() {
n=$1
a=0
b=1
count=1
next=$((a + b))
a=$b
b=$next
((count++))
done
read -p "Enter the number of terms for Fibonacci series: " num_terms
exit 1
fi
fibonacci_series "$num_terms"