Os Lab Expt.s
Os Lab Expt.s
ex: $date
ex: $pwd
ex $man pwd
4: who: It print information about users who are currently logged in.
ex: $who
ex: $whoami
ex: $cal
ex3: $cal -3
10: cat: This command is used to create a new file and to append or
add the contents and display the contents.
11. cp: This command is used to copy content of the existing file to
new file or existing file and copy the file one location to another
location.(i.e. Folder to Folder)
12. ls: To display the all files and derectory present in the present
working dierctory.
ex:$ls
1. ls -l: to display long listing files and directory
ex: $ls -l
13. mv: This command is used for rename the file and directory.
ex: $cd ..
Experiment No - 2
AIM: To study about Advanced UNIX Commands.
1. touch: The touch command is used to create, change and modify
timestamps of a file.
ex2: $ls -l
ex3: $cat>>file2.txt
ex4: $ls -l
Syntax:$chmod[reference][operator][mode] file
u=rwx,g=rx,o=r myfile
There are three basic modes which correspond to the basic permissions:
r:Permission to read the file.
w:Permission to write the file.
x:Permission to execute the file
The permissions for the user, group, and others, in that order. Each
digit is a combination of the numbers 4, 2, 1, and 0 :
U G O
-rw-rw-r-- 1 master master 21 Aug 3 09:32 file2.txt
ex1: $chmod 777 file2.txt
ex: $ll (after execute this command see the file, check the status of
the file)
ex: $ll
ex: $ll
ex: $ll
ex: $ll
ex3: wc file100.txt
4. head: This command is used to display the first part (by default 10
lines) of the file.
8. sort: Sort command is used for printing lines of input text files
and concatenation of all files in sorted order. Sort command takes
blank space as field separator and entire Input file as sort key.
ex: cat>file200.txt
srfhrf
hf
bg
drf
f
b
cv
df
h
bcvf
6
8
5
9
1
3
2
4
7
pres ctrl + d
Experiment No - 3
AIM: To study about echo, read and expr UNIX commands and Compilation
of Shell Programs and practice sample shell programs.
2. read: The command read in the Linux is used to read the input from
the keyboard.
ex1: $read a
input some value 234
ex2: $read b
input some text or string GIET
ex3: $echo $a
ex4: $echo $b
read x
read y
sum=`expr $x + $y`
a=giet
b=`expr index $a e`
a=hello
b=`expr substr $a 2 3`
Questions:
1. Write a shell program to input two numbers and display the result
of all arithmetic operators.
2. Write a shell program to swap between two numbers using and without
using third variable.
x=`expr $x + $y`
y=`expr $x - $y`
x=`expr $x - $y`
3. Write a shell program to input radius and find the area of circle
and perimeter of a circle.
r=5
pi=3.14
AC=`echo $pi \* $r \* $r |bc`
3.14 * 5 * 5
5. Write a shell program to input basic salary. find the TA=10% of BS,
DA=15% of BS, HRA=20% of BS.Find total salary.
pi=3.14
area=`echo $pi \* $r \* $r |bc`
x=`expr $x + $y`
y=`expr $x - $y`
x=`expr $x - $y`
Experiment No - 4
AIM: Shell Scripting or Programs using if..else, nested if..else and
switch case.
Operators
-eq (==)
-lt (<)
-le (<=)
-gt (>)
-ge (>=)
-ne (!=)
-a (and)
-o (or)
Syntax:if..else
if [ condition ]
then
statement
else
statement
fi
if [ condition ]
then
statement
elif [ condition ]
then
statement
elif [ condition ]
then
statement
else
statement
fi
Q1. Write a shell program to check the given number is even or odd.
if [ `expr $n % 2` -eq 0 ]
Q2. Write a shell program to read a number and check whether the
number is +ve or -ve.
Q3. Write a shell program to find the greatest among three numbers.
Q4. Write a shell program to read a character and check the character
is vowel or consonant using CASE.
Experiment No - 5
AIM: Shell Scripting on while loop.
while [ condition ]
do
commands
statements
done
Q2. Write a shell program to find the sum of odd and even numbers upto
20.
Q4. Write a shell program to print the fibonacci series upto nth term.
echo "Enter the number"
read n
i=1
while [ $i -le $n ]
do
c=0
j=1
while [ $j -le $i ]
do
if [ `expr $i % $j` -eq 0 ] then
c=`expr $c + 1`
fi
j=`expr $j + 1`
done
if [ $c -eq 2 ] then
echo "$i"
fi
i=`expr $i + 1`
done
Q6. Write a shell program to find the sum of digits of a given number.
Experiment No - 6
AIM:- To Study about fork() system call.
Fork system call use for creates a new process, which is called child
process, which runs concurrently with process (which process called
system call fork) and this process is called parent process. After a
new child process created, both processes will execute the next
instruction following the fork() system call. A child process uses the
same pc(program counter), same CPU registers, same open files which
use in the parent process.
printf("Hello world!\n");
return 0;
}
Ex:2
----
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
Number of times hello printed is equal to number of process created.
Total Number of Processes = 2 power n, where n is number of fork
system calls. So here n = 3, 2 power 3 =8
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
if (fork() == 0)
printf("Hello from Child!\n");
else
printf("Hello from Parent!\n");
return 0;
}
Here, two outputs are possible because the parent process and child
process are running concurrently. So we don't know if OS first give
control to which process a parent process or a child process.
Important: Parent process and child process are running the same
program, but it does not mean they are identical. OS allocate
different data and state for these two processes and also control the
flow of these processes can be different.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int x = 1;
if (fork() == 0)
printf("Child has x = %d\n", ++x);
else
printf("Parent has x = %d\n", --x);
return 0;
}
===========================================
#include <stdio.h>
#include <unistd.h>
int main()
{
int n=0;
if(fork()==0)
{
printf("Child! Seq=%d\n", ++n);
}
else
{
printf("Parent! Seq=%d\n", ++n);
}
printf("Both! n=%d\n", ++n);
return 0;}
==============================================
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int pid;
pid = fork();
if (pid < 0)
{
printf("Fork failed\n");
exit(-1);
}
else if (pid == 0)
{
printf("Child Process,return from fork=%d \n",
pid);
}
else
{
printf("Parent Process return from fork=%d \n",
pid);
}
}
Experiment No - 7
AIM:Simulation of FCFS and SJF CPU Scheduling Algorithm.
FCFS C Program
No of Process : 3
Process BursTime WaitingTime TurnAtime
P[1] 24 0 24
P[2] 3 24 27
P[3] 3 27 30
#include<stdio.h>
void main()
{
int n,bt[5],wt[5],tat[5],tot_tat=0;
int avg_wt=0,avg_tat=0,i,j,tot_wt=0;
printf("Enter the no of process:\n");
scanf("%d",&n);
printf("Enter the Burst Time:\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]=wt[i]+bt[j];
}
}
printf("\nProcess\tBurstTime\tWaitingTime
\tTurnAtime\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
tot_wt=tot_wt+wt[i];
tot_tat=tot_tat+tat[i];
printf("\nP[%d]\t%d\t\t%d\t\t%d",i+1,bt[i],
wt[i],tat[i]);
}
avg_wt=tot_wt/i;
avg_tat=tot_tat/i;
SJF C Program
#include<stdio.h>
void main()
{
int n,bt[5],wt[5],tat[5],tot_tat=0;
int avg_wt=0,avg_tat=0,i,j,tot_wt=0;
int p[5],t,pos;
printf("Enter the no of process:\n");
scanf("%d",&n);
printf("Enter the Burst Time:\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++){
pos=i;
for(j=i+1;j<n;j++){
if(bt[j]<bt[pos]){
pos=j;}
}
t=bt[i];
bt[i]=bt[pos];
bt[pos]=t;
t=p[i];
p[i]=p[pos];
p[pos]=t;
}
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]=wt[i]+bt[j];
}
}
printf("\nProcess\tBurstTime\tWaitingTime
\tTurnAtime\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
tot_wt=tot_wt+wt[i];
tot_tat=tot_tat+tat[i];
printf("\nP[%d]\t%d\t\t%d\t\t%d",p[i],bt[i],
wt[i],tat[i]);
}
avg_wt=tot_wt/i;
avg_tat=tot_tat/i;
printf("\nAverage Waiting Time %d",avg_wt);
printf("\nAverage Turn_A_Time %d",avg_tat);
}
Experiment No - 8
AIM:Simulation of Round Robin and Priority CPU Scheduling Algorithm.
Priority.c
----------
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],p[20];
int i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
return 0;
}
Round Robin
------------
#include<stdio.h>
int main()
{
int c,j,n,time,r,flag=0,tq;
int wt=0,tat=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
r=n;
printf("Enter Arrival Time and Burst Time");
for(c=0;c<n;c++)
{
scanf("%d",&at[c]);
scanf("%d",&bt[c]);
rt[c]=bt[c];
}
printf("Enter Time Quantum:");
scanf("%d",&tq);
printf("\n\nProcess\t|Turnaround Time|
Waiting Time\n\n");
for(time=0,c=0;r!=0;)
{
if(rt[c]<=tq && rt[c]>0)
{
time+=rt[c];
rt[c]=0;
flag=1;
}
else if(rt[c]>0)
{
rt[c]-=tq;
time+=tq;
}
if(rt[c]==0 && flag==1)
{
r--;
printf("P[%d]\t|\t%d\t|\t%d\n",c+1,
time-at[c],time-at[c]-bt[c]);
wt+=time-at[c]-bt[c];
tat+=time-at[c];
flag=0;
}
if(c==n-1)
c=0;
else if(at[c+1]<=time)
c++;
else
c=0;
}
printf("Average Waiting Time=%f",
wait_time*1.0/n);
printf("Average Turnaround Time=%f",
turnaround_time*1.0/n);
return 0;
}
Experiment No - 8
AIM:Simulation of Banker's Algorithm.
#include <stdio.h>
void main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10]; int
completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");}
OUTPUT
Enter the no of processes : 5
Enter the no of resources : 3
Enter the Max Matrix for each process :
For process 1 : 7 5 3
For process 2 : 3 2 2
For process 3 : 7 0 2
For process 4 : 2 2 2
For process 5 : 4 3 3
Enter the allocation for each process :
For process 1 : 0 1 0
For process 2 : 2 0 0
For process 3 : 3 0 2
For process 4 : 2 1 1
For process 5 : 0 0 2
Enter the Available Resources : 3 3 2
Max matrix: Allocation matrix:
7 5 3 0 1 0
3 2 2 2 0 0
7 0 2 3 0 2
2 2 2 2 1 1
4 3 3 0 0 2
Experiment No - 10
AIM: Implimentation of FIFO and LRU Page replacement Algorithm.
FIFO
ALGORITHM
step 1. Start the process
step 4. Check the need of replacement from old page to new page in
memory
#include<stdio.h>
void main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
}
OUTPUT:
Page Fault Is 15
LRU
#include<stdio.h>
void main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
printf("\nThe no of page faults is %d",c);
}
OUTPUT:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2