OS Pca1
OS Pca1
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:
Output:
Question: 3
Problem statement: Write a shell script to print Fibonacci series up to a specified range.
Code:
Output:
Question: 4
Problem statement: Write a shell script code to check whether the number is prime or
not.
Code:
Output:
Question: 5
Problem statement: Write a shell script code to check the number is palindrome or not.
Code:
Output:
Question: 6
Problem statement: Write a shell script to sort a given set of integers in ascending order
using bubble sort algorithm.
Code:
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
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.
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.
Output:
Question: 13
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: