Operating System Lab
Operating System Lab
EDUCATIONAL AND
RESEARCH INSTITUTE
(Dr. M.G.R. UNIVERSITY)
RECORD NOTE BOOK
NAME :
REG.NO. :
BRANCH :
SUBJECT:
Year :
Dr. M.G.R.
EDUCATIONAL AND RESEARCH INSTITUTE
UNIVERSITY
Maduravoyal ,Chennai -600 095.
Register No.
RECORD NOTE
BOOK
2009-2010
Name of Lab :
Department: :
Year 2009.
read bp
then
echo DA=$DA
echo HRA=$HRA
then
echo DA=$DA
echo HRA=$HRA
then
echo HRA=$HRA
fi
OUTPUT:
4000
DA=400
HRA=800
7000
DA=1400
HRA=2100
15000
DA=6000
HRA=6000
FIND WHETHER NUMBER IS EVEN OR ODD
read n
r=`expr $n % 2`
if test $r -eq 0
then
else
fi
OUTPUT:
56
67
echo menu
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Exit
ch=y
while ch=y
do
read choice
read a b
case $choice in
echo $res;;
res=`expr $a - $b`
echo $res;;
res=`expr $a \* $b`
echo $res;;
res=`expr $a / $b`
echo $res;;
5)
exit;;
esac
read ch
done
OUTPUT:
menu
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
enter 2 number
2 5
2
enter 2 number
7 3
enter 2 number
5 3
15
enter 2 number
10 2
y
enter the choice
read n
if test $n -gt 0
then
then
else
fi
OUTPUT:
56
number is positive
-3
number is negative
00
number is zero
SWAP TWO NUMBERS WITHOUT USING ADDITIONAL VARIABLE
read a b
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
47 98
before swapping
value of a=47
value of b=98
after swapping
value of a=98
value of b=47
C
PROGRAMS
PROCESS CREATION
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(){
int pid;
pid=fork();
if(pid<0)
printf(“fork failed”);
exit(-1);
if(pid==0)
execlp(“bin/ls”,”ls”,NULL);
}
wait(NULL);
exit(0);
OUTPUT:
child complete
FIRST COME FIRST SERVED CPU SCHEDULING ALGORITHM
#include<stdio.h>
main()
{
int i,n,w[10],e[10],b[10];
float wa=0,ea=0;
printf("\nEnter the no of jobs: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the burst time of job %d :",i+1);
scanf("%d",&b[i]);
if(i==0)
{
w[0]=0;
e[0]=b[0];
}
else
{
e[i]=e[i-1]+b[i];
w[i]=e[i-1];
}
}
printf("\n\n\tJobs\tWaiting time \tBursttime\tExecution time\n");
printf("\t-------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\t%d\t\t%d\t\t%d\t\t%d\n",i+1,w[i],b[i],e[i]);
wa+=w[i];
ea+=e[i];
}
wa=wa/n;
ea=ea/n;
printf("\n\nAverage waiting time is :%2.2f ms\n",wa);
printf("\nAverage execution time is:%2.2f ms\n\n",ea);
}
OUTPUT:
#include<stdio.h>
struct sjfs
{
char pname[10];
int btime;
}proc[10],a;
int main( )
{
struct sjfs proc[10];
int n,i,j;
int temp=0,temp1=0,temp2;
char name[20];
float tt,awt;
printf("Enter the number of processes:\n");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter the process name: \n");
scanf("%s",&proc[i].pname);
printf("Enter the Burst time:\n");
scanf("%d",&proc[i].btime);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(proc[i].btime<proc[j].btime)
{
a=proc[i];
proc[i]=proc[j];
proc[j]=a;
}
}
}
printf("-------------------- CPU SCHEDULING ALGORITHM - SJFS -------------------------- ");
printf("\n\tprocess name \tBurst time \twaiting time \tturnaround time\n");
temp=0;
for(i=0;i<n;i++)
{
temp=temp1+temp+proc[i].btime;
temp1=temp1+proc[i].btime;
temp2=temp1-proc[i].btime;
printf("\n\t %s \t %d ms \t %d ms \t %d
ms\n",proc[i].pname,proc[i].btime,temp2,temp1);
}
printf("-------------------------------------------------------------------------------");
awt=(temp-temp1)/n;
tt=temp/n;
printf("\nThe Average Waiting time is %4.2f milliseconds\n",awt);
printf("\nThe Average Turnaround time is %4.2f",tt);
}
OUTPUT:
p4 2 ms 0ms 2 ms
p1 5 ms 2 ms 7 ms
p2 5 ms 7 ms 12 ms
p3 6 ms 12 ms 18 ms
-----------------------------------------------------------------------------------------------------
The Average Waiting time is 5.00 milliseconds
#include <stdio.h>
struct roundrobin
{
int b_time;
int run_time;
int w_time;
int t_time;
}rr[10];
int main()
{
int i,p_no=0,time=0;
int et=0;
int tot_pro,tot_time=0;
int tq,flag=0;
float awt;
int wt=0;
printf("\t\tProgram for Round Robin Scheduling\n\n");
printf("Enter Timequantom of a system=");
scanf("%d",&tq);
printf("\n\nEnter the number of Process=");
scanf("%d",&tot_pro);
for(i=1;i<=tot_pro;i++)
{
printf("\n\tEnter the Burst time of Process%d=",i);
scanf("%d",&rr[i].b_time);
tot_time=tot_time+rr[i].b_time;
}
printf("\n\t\tRESULT\n");
printf("\n\n\t--------------------------\n");
printf("\t| Start | Process | End | \n");
printf("\t| Time | No | Time | \n");
printf("\t--------------------------\n");
while(time<tot_time)
{
p_no=p_no+1;
if (p_no>tot_pro)
p_no=1;
if (rr[p_no].b_time!=0)
{
if (rr[p_no].b_time>tq)
{
if (flag!=p_no)
rr[p_no].w_time=et-rr[p_no].run_time;
rr[p_no].run_time=rr[p_no].run_time+tq;
et=et+tq;
printf("\t| %d",time);
rr[p_no].b_time=rr[p_no].b_time-tq;
time=time+tq;
flag=p_no;
}
else
{
if (flag!=p_no)
rr[p_no].w_time=et-rr[p_no].run_time;
rr[p_no].run_time=rr[p_no].run_time+rr[p_no].b_time;
et=et+rr[p_no].b_time;
printf("\t| %d",time);
time=time+rr[p_no].b_time;
rr[p_no].b_time=0;
flag=p_no;
}
printf("\t| P%d | %d\t |\n",p_no,time);
}
}
printf("\t--------------------------\n");
for(i=1;i<=tot_pro;i++)
{
printf("Run time of P%d is %d\n",i,rr[i].run_time);
printf("Wt P%d is %d\n",i,rr[i].w_time);
wt=wt+rr[i].w_time;
}
printf("Waiting time of the process is=%d\n",wt);
awt=wt/tot_pro;
printf("Average Waiting time is=%5.3f",awt);
}
OUTPUT:
|
Start time Process No. End time
0 P1 1
1 P2 2
2 P3 3
3 P4 4
4 P1 5
5 P2 6
6 P3 7
7 P4 8
8 P1 9
9 P2 10
10 P3 11
11 P4 12
12 P1 13
13 P3 14
14 P4 15
15 P3 16
16 P4 17
17 P4 18
Run time of P1 is 4 ms
Wt P1 is 9 ms
Run time of P2 is 3
Wt P2 is 7 ms
Run time of P3 is 5
Wt P3 is 11 ms
Run time of P4 is 6
Wt P4 is 12 ms
Average Waiting time is=9.000 ms
PRIORITY CPU SCHEDULING ALGORITHM
#include<stdio.h>
#include<malloc.h>
void line(int i)
int j;
for(j=1;j<=i;j++)
printf("__");
printf("\n");
struct process
int p_id,priority;
int etime,wtime,tatime;
};
int i,j,k,l,n;
float awtime=0,atatime=0;
int main()
scanf("%d",&n);
p[0].wtime=0;
p[0].tatime=0;
for(i=1;i<=n;i++)
scanf("%d",&p[i].etime);
scanf("%d",&p[i].priority);
p[i].p_id=i-1;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(p[j].priority>p[j+1].priority)
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
for(i=1;i<=n;i++)
p[i].wtime=p[i-1].tatime;
p[i].tatime=p[i-1].tatime + p[i].etime;
awtime+=p[i].wtime;
atatime +=p[i].tatime;
awtime=awtime/n;
atatime=atatime/n;
printf("Scheduling \n");
line(60);
line(60);
for(i=1;i<=n;i++)
line(60);
line(60);
return(0);
}
OUTPUT:
Scheduling____________________________________________
______________________________________________________
process execution wait turnaround
Id no time time time
_____________________________________________________
1 1 0 1
0 2 1 3
2 4 3 7
_____________________________________________________
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,z;
int res[10];
int resource,process,boolean=0;
int allocation[10][10],sel[10],max[10][10],need[10][10],work[10],work1[10];
int check=0;
int adpro;
int ar[10];
printf("Welcome to Bankers Algorithms");
printf("\n Enter the no .of processes:");
scanf("%d",&process);
printf("\n Enter the number of Resources");
scanf("%d",&resource);
for(i=0;i<resource;i++)
{
printf("Enter the instances of Resource %d:",i+1);
scanf("%d",&res[i]);
}
for(i=0;i<process;i++)
{
printf("\n Enter allocated resources for process %d:",i+1);
for(j=0;j<resource;j++)
{
printf("\n Resource %d:",j+1);
scanf("%d",&allocation[i][j]);
}
}
for(i=0;i<10;i++)
sel[i]=-1;
for(i=0;i<process;i++)
{
printf("Enter maximum need of process: %d",i+1);
for(j=0;j<resource;j++)
{
printf("\n Resource %d:",j+1);
scanf("%d",&max[i][j]);
}
}
for(i=0;i<process;i++)
for(j=0;j<resource;j++)
need[i][j]=max[i][j]-allocation[i][j];
printf("\n The Need is \n");
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
printf("\t%d",need[i][j]);
printf("\n");
}
printf("\n The Avalilable is ");
for(i=0;i<resource;i++)
{
work[i]=0;
for(j=0;j<process;j++)
work[i]=work[i]+allocation[j][i];
work[i]=res[i]-work[i];
}
for(z=0;z<process;z++)
{
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(work[j]>=need[i][j]&&sel[i]==-1)
{
boolean=1;
}
else
{
boolean=0;
break;
}
}
if(boolean==1)
{
sel[i]=1;
for(j=0;j<resource;j++)
{
work[j]=work[j]+allocation[i][j];
}
}
}
}
if(check==1)
printf("\n System is in Unsafe mode");
else
printf("\n System is in safe mode");
}
OUTPUT:
The Need is
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
The Available is 3 3 2
System is in safe mode
IMPLEMENTATION OF FIRST FIT ALGORITHM
#include<stdio.h>
#include<string.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;
printf("\t\t MEMORY MANAGEMENT \n");
printf("\t\t =================\n");
printf("\tEnter the total no of blocks: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the size of blocks: ");
scanf("%d",&size[i]);
}
cho=0;
while(cho==0)
{
printf("\n Enter the size of the file: ");
scanf("%d",&m);
x=0;
for(i=1;i<=n;i++)
{
if(size[i]>=m)
{
printf("\n size can occupy %d",size[i]);
size[i]-=m;
x=i;
break;
}
}
if(x==0)
{
printf("\n\nBlock can't occupy\n\n");
}
printf("\n\nSNO\t\tAvailable block list\n") ;
for(i=1;i<=n;i++)
printf("\n\n%d\t\t\t%d",i,size[i]);
printf("\n\n Do u want to continue.....(0-->yes/1-->no): ");
scanf("%d",&cho);
}
}
OUTPUT:
#include<stdio.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;
if(x==0)
{
printf("block can't occupy");
}
printf("\n\nSNO\t\t Available Block size\n") ;
for(i=1;i<=n;i++)
printf("\n%d\t\t%d",i,size[i]);
printf("\n\n Do u want to continue.....(0-->yes\t/1-->no)");
scanf("%d",&cho);
}
}
OUTPUT:
FIFO - PAGING
page 1 page 2 page 3
2 -2 -2
2 0 -2
2 0 3
2 0 3
2 0 3
2 0 3
5 0 3
5 9 3
No of page faults: 5
IMPLEMENTATION OF LRU-PAGE REPLACEMENT ALGORITHM
#include<stdio.h>
#include<stdlib.h>
#define max 100
int frame[10],count[10],cstr[max],tot,nof,fault;
main()
{
getdata();
push();
}
getdata()
{
int pno,i=0;
printf("\n\t\tL R U - Page Replacement Algorithm\n");
printf("\nEnter No. of Pages in main memory:");
scanf("%d",&nof);
printf("\nEnter the no of page references:\n");
scanf("%d",&pno);
for(i=0;i<pno;i++)
{
printf("Enter page reference%d:",i);
scanf("%d",&cstr[i]);
}
tot=i;
for(i=0;i<nof;i++)
printf("\tpage%d\t",i);
}
push()
{
int x,i,j,k,flag=0,fault=0,nc=0,mark=0,maximum,maxpos=-1;
for(i=0;i<nof;i++)
{
frame[i]=-1;
count[i]=mark--;
}
for(i=0;i<tot;i++)
{
flag=0;
x=cstr[i];
nc++;
for(j=0; j<nof; j++)
{
for(k=0; k<nof;k++)
count[k]++;
if(frame[j]==x)
{
flag=1;
count[j]=1;
break;
}
}
if(flag==0)
{
maximum = 0;
for(k=0;k<nof;k++)
{
if(count[k]>maximum && nc>nof)
{
maximum=count[k];
maxpos = k;
}
}
if(nc>nof)
{
frame[maxpos]=x;
count[maxpos]=1;
}
else
frame[nc-1]=x;
fault++;
dis();
}
}
printf("\nTotal Page Faults :%d",fault);
}
dis()
{
int i=0;
printf("\n\n");
while(i<nof)
{
printf("\t%d\t",frame[i]);
i++;
}
}
OUTPUT:
paging->LRU
Enter the number of pages in main memory: 3
#include<stdio.h>
char state[10],self[10],spoon[10];
void test(int k)
{
if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k+1)%5]!='e'))
{
state[k]='e';
self[k]='s';
spoon[k]='n';
spoon[(k+4)%5]='n';
}
}
void pickup(int i)
{
state[i]='h';
test(i);
if(state[i]=='h')
{
self[i]='w';
}
}
void putdown(int i)
{
state[i]='t';
spoon[i]='s';
spoon[i-1]='s';
test((i+4)%5);
test((i+1)%5);
}
int main()
{
int ch,a,n,i;
printf("\t\t Dining Philosopher Problem\n");
for(i=0;i<5;i++)
{
state[i]='t';
self[i]='s';
spoon[i]='s';
}
printf("\t\t Initial State of Each Philosopher\n");
printf("\n\t Philosopher No.\t Think/Eat \tStatus \tspoon");
for(i=0;i<5;i++)
{
printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit \n 2.Hungry\n3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
while(ch!=1)
{
switch(ch)
{
case 2:
{
printf("\n\t Enter which philosopher is hungry\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
pickup(n);
break;
}
case 3:
{
printf("\n\t Enter which philosopher is thinking\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
putdown(n);
break;
}
}
printf("\n\t State of Each philosopher\n\n");
printf("\n\t Philosoper No.\t Thinking\t Hungry");
for(i=0;i<5;i++)
{
printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit\n 2.Hungry\n 3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
}
}
OUTPUT:
Initial State of Each Philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 t s s
4 t s s
5 t s s
1.Exit
2.Hungry
3.Thinking
Enter your choice: 2
Enter which philosopher is hungry : 4
State of Each philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 t s n
4 e s n
5 t s s
1.Exit
2.Hungry
3.Thinking
Enter your choice 2
Enter which philosopher is hungry : 3
State of Each philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 h w n
4 e s n
5 t s s
1.Exit
2.Hungry
3.Thinking