0% found this document useful (0 votes)
10 views15 pages

OS Pca1

Uploaded by

Bikram Dhibar
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)
10 views15 pages

OS Pca1

Uploaded by

Bikram Dhibar
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/ 15

Question: 1

Problem statement: Write a shell script code that accepts the number from the user and
display the squares of all numbers from 1 up to that number.

Code:
echo "Enter a Number:"
read n
for ((i=1; i<=n; i++))
do
echo "Square of $i is 'expr $i \* $i`"
done

Output:
Question: 2

Problem statement: Write a shell script to determine a given number is even or odd.

Code:

echo "Enter a number:"


read n
rem=$(( $n %2 ))
if [ $rem -eq 0 ]
then
echo "Number is even"
else
echo "Number is odd”
fi

Output:
Question: 3

Problem statement: Write a shell script to print Fibonacci series up to a specified range.

Code:

echo "Enter the range"


read x
n1=1
n2=0
echo "The series is ..."
for ((i=1; i<=x; i=i+1))
do
n2=`expr $n1 + $n2`
n1=`expr $n2-$n1`
echo "$n2"
done
echo” “

Output:
Question: 4

Problem statement: Write a shell script code to check whether the number is prime or
not.

Code:

echo -e "Enter Number:\c"


read n
for ((i=2; i<=$n/2; i++))
do
ans=$((n % i))
if [ $ans -eq 0 ]
then
echo "$n is not a prime number."
exit 0
fi
done
echo "$n is a prime number."

Output:
Question: 5

Problem statement: Write a shell script code to check the number is palindrome or not.

Code:

echo "Enter the number to check if the number is palindrome or not"


read number
num=$number
reverse=0
while [ $number -gt 0 ]
do
rem=`expr $number % 10`;
reverse=`expr $reverse \* 10 + $rem`;
number=`expr $number / 10`;
done
if [ $num -eq $reverse ]
then
echo "$num is palindrome"
else
echo "$num is not a palindrome"
fi

Output:
Question: 6
Problem statement: Write a shell script to sort a given set of integers in ascending order
using bubble sort algorithm.
Code:

echo "Enter The Number of Items:"


read n
echo "Enter The Data: "
for((i=0; i<$n; i++))
do
read a[i]
done
for((i=0; i<$n; i++))
do
flag=0
for((j=0; j<$n-$i-1; j++))
do
k=`expr $j \+ 1`
if [ `expr ${a[$j]} + 0` -gt `expr ${a[$k]} + 0` ]
then
flag=1
temp=`expr ${a[$j]} + 0`
a[j]=`expr ${a[$k]} + 0`
a[k]=$temp
fi
done
if [ $flag == 0 ]
then
break
fi
done
echo "The Sorted Items are:"
for((i=0; i<$n; i++))
do
echo -n ${a[i]} “ “
done
echo
Output;
Question: 7
Problem statement: Write a shell script to sort a given set of integers in ascending order
using selection sort algorithm.

Code:
echo -n “Enter The Number of Items:"
read n
echo "Enter The Data:"
for((i=0; i<$n; i++))
do
read a[i]
done
for((i=0; i<$n-1; i++))
do
lowest=$i
for((j=$i+1; j<$n; j++))
do
if [ `expr ${a[$j]} + 0` -lt `expr ${a[$lowest]} + 0` ]
then
lowest -$j
fi
done
temp=`expr ${a[$i]} + 0`
a[i]=`expr ${a[$lowest]} + 0`
a[lowest]=$temp
done
echo "The Sorted List is:"
for((i=0; i<$n; i++))
do
echo -n “$(a[i])”
done
echo

Output:
Question: 8

Problem statement: Write a Shell script that computes the gross salary of an employee
according to the following rules is given below.
The basic salary is input interactively though the keyboard.
If basic salary is < 1500 then HRA =10% of the basic and
DA=90% of the basic.
If basic salary is >=1500 then HRA= Rs 500 and
DA =98% of the basic.

Code:
echo "enter the basic salary:"
read bsal
if [ $bsal -lt 1500 ]
then
gsal=$((bsal + ((bsal/100) * 10) + (bsal/100) * 90))
echo "The gross salary: $gsal"
fi
if [ $bsal -ge 1500 ]
then
gsal=$(((bsal+500) + (bsal/100) * 98))
echo "the gross salary: $gsal
fi

Output:
Question: 9

Problem statement: A Shell script accepts a filename, starting and ending line numbers as
arguments and displays all the lines between the given line numbers.

Code:
echo "Enter the file name:”
read f
echo "Enter the starting line:"
read s
echo "Enter the ending line:"
read e
sed -n $s,$e\p $f

Output:
Question: 10

Problem statement: How can two files be merged using the sort command.

Given File:

Code:

a.txt: b.txt:
aaaa dddd
bbbb cccc
cccc bbbb
dddd aaaa

Use Command: Cat a.txt b.txt | sort > newfile.

Output:
Question: 11

Problem Statement: Write a command line to count the number of times a specific
character, A appears in a given file.
Given File:

A.txt:
location is good for location which is enough for a location to share this with friends, location
where we used to go for fun.

Use Command: grep -o -i location A.txt | wc-l

Output:
Question: 12

Problem Statement: Write a command to add today's date and time to the end of a given
file.
Given File:

output.txt
My name is soumili.
This is file2.

Use Command: date >> filename.txt

Output:
Question: 13

Problem Statement: Create a C program to create an orphan process program.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\n child pid:%d",getpid());
printf("\n parent pid:%d",getppid());
sleep(2);
printf("\n parent pid:%d", getppid());
}
else
{
printf("\n parent pid:%d",getpid());
printf("\n Parent of parent pid:%d",getppid()); sleep(1)
}
}

Outptut:

childpid: 8529
parentpid: 8528
parent of parent pid: 7652
ubntu@ubuntu:~/user$
parent pid: 8528
parent pid: 1
Question: 14

Problem Statement: Write a C program to display the process ID of child and parent
process where a child is created using fork system call.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\n child pid:%d", getpid());
printf("\n parent pid:%d",getppid());
}
else
{
printf("\n parent pid:%d",getpid());
printf("\n Parent of parent pid:%d",getppid());
}
}

Output:
childpid: 7655
parentpid: 7654
parentpid: 7654
parent of parent pid: 7662
Question: 15

Problem Statement: Write a C program using a system call so that parent is going to wait
till child finishes.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\n child pid:%d",getpid());
printf("\n parent pid:%d", getppid());
sleep(2);
}
else
{
printf("\n parent pid:%d",getpid());
printf("\n Parent of parent pid:%d", getppid());
sleep(1)
}
Wait(&status);
printf("\n PID %d terminates.\n", getpid());
}

Output:

Child pid: 9252


Parent pid: 9250
Parent pid: 9250
PID 9251 terminates.
parent of parent pid: 7562
PID 9250 terminates.

You might also like