Os Lab Manual
Os Lab Manual
NAME :
REGISTER NUMBER :
SEMESTER :
BRANCH :
ACADEMIC YEAR :
BONAFIDE CERTIFICATE
REGISTER NO:
Mr./Ms.__________________________ of Semester
for_______________________________________________________
Rajamadam.
OUTPUT:
[root@localhost ~]# cc dr.c
[root@localhost ~]# ./a.out lab_print
NAME OF ITEM
pri_output.doc
sjf_output.doc
fcfs_output.doc
rr_output.doc
ipc_pipe_output.doc
pro_con_prob_output.doc
TOTAL NUMBER OF ITEM IN THAT DIRECTORY IS 8
PROGRAM:
#include<stdio.h>
#include<string.h>
main(int ag,char* arg[])
{
char buf[200],line[200];
int i,j,n,fd1,count=0,opt;
if(ag==4)
{
fd1=open(arg[3],0);
if(strcmp(arg[1],"-c")==0)
opt=2;
if(strcmp(arg[1],"-i")==0)
opt=3;
}
else if(ag==3)
{
fd1=open(arg[2],0);
opt=1;
}
if(fd1==-1)
printf("error in opening");
j=0;
switch(opt)
{
case 1:
while((n=read(fd1,buf,sizeof(line)))>0)
{
for(i=0;i<n;i++,j++)
{
if(buf[i]!='\n') line[j]=buf[i];
else
{
line[j]='\n';
if(strstr(line,arg[1])!=0)
write(1,line,j+1);
}
}
}
break;
case 2:
while((n=read(fd1,buf,sizeof(line)))>0)
{
for(i=0;i<n;i++,j++)
{
if(buf[i]!='\n') line[j]=buf[i];
else
{
line[j]='\n';
if(strstr(line,arg[2])!=0)
count=count+1;
j=-1;
}
}
}
printf("%d \n",count);
break;
case 3:
while((n=read(fd1,buf,sizeof(line)))>0)
{
for(i=0;i<n;i++,j++)
{
if(buf[i]!='\n') line[j]=buf[i];
else
{
line[j]='\n';
if(strcasestr(line,arg[2])!=0)
write(1,line,j+1);
j=-1;
}
}
}
break;
}
close(fd1);
}
OUTPUT:
[root@localhost ~]# cat tst sd dsa AS D
[root@localhost ~]# ./a.out -i a tst aA
[root@localhost ~]# ./a.out -c a tst 1[root@localhost ~]# ./a.out -c A tst1
[root@localhost ~]# ./a.out -c sd tst 1[root@localhost ~]# ./a.out -c s tst 2
Program:
echo “enter the first string”
read str1
echo “enter the second string”
read str2
echo “the concatenated string is” $str1 $str2
Sample I/P:
Enter first string: Hello
Enter first string: World
Sample O/P:s
The concatenated string is Hello World
Program:
echo “enter the first string”
read str1
echo “enter the second string”
read str2
if [ $str1 = $str2 ]
then
echo “strings are equal”
else
echo “strings are unequal”
fi
Sample I/P:1
Enter first string: hai
Enter second string: hai
Sample O/P:1
The two strings are equal
Sample I/P:2
Enter first string: hai
Enter second string: cse
Sample O/P:2
The two strings are not equal
Program:
echo "enter A"
read a
echo "enter B"
read b
echo "enter C"
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo "A is greater"
elif [ $b -gt $a -a $b -gt $c ]
then
echo "B is greater"
else
echo "C is greater"
fi
Sample I/P:
Enter A:23
Enter B:45
Enter C:67
Sample O/P:
C is greater
Program :
echo enter the number
read n
a=-1
b=1
i=0
while [ $i –le $n ]
do
t=`expr $a + $b`
echo $t
a=$b
b=$t
i=`expr $i + 1
done
Sample I/P :
Enter the no: 5
Sample O/P:
0
1
1
2
3
5
Program:
echo 1.Addition
echo 2.Subraction
echo 3.Multiplication
echo 4.Division
echo enter your choice
read a
echo enter the value of b
read b
echo enter the value of c
read c
echo b is $b c is $c
case $a in
1)d=`expr $b + $c`
echo the sum is $d
;;
2)d=`expr $b - $c`
echo the difference is $d
;;
3)d=`expr $b \* $c`
echo the product is $d
;;
4)d=`expr $b / $c`
echo the quotient is $d
;;
esac
Sample I/P :
1.Addition
2.Subraction
3.Multiplication
4.Division
Enter your choice:1
Enter the value of b:3
Enter the value of c:4
b is 3 c is 4
the sum is 7
Sample O/P:
b is 3 c is 4
the sum is 7
PROGRAM:
#include<stdio.h>
#include<string.h>
int n,Bu[20],Twt,Ttt,A[10],Wt[10],w;
float Awt,Att;
char pname[20][20],c[20][20];
void Getdata();
void Gantt_chart();
void Calculate();
void fcfs();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n---------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n---------------------------------------------------------\n");
printf("\n");
for(i=1;i<=n;i++)
printf("%d\t\t",Wt[i]);
printf("%d",Wt[n]+Bu[n]);
printf("\n---------------------------------------------------------\n");
printf("\n");
}
void Calculate()
{
int i;
Wt[1]=0;
for(i=2;i<=n;i++)
{
Wt[i]=Bu[i-1]+Wt[i-1];
}
for(i=1;i<=n;i++)
{
Twt=Twt+(Wt[i]-A[i]);
Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
}
Att=(float)Ttt/n;
Awt=(float)Twt/n;
printf("\n\n Average Turn around time=%3.2f ms ",Att);
printf("\n\n AverageWaiting Time=%3.2f ms",Awt);
}
void fcfs()
{
int i,j,temp, temp1;
Twt=0;
Ttt=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(A[i]>A[j])
{
temp=Bu[i];
temp1=A[i];
Bu[i]=Bu[j];
A[i]=A[j];
Bu[j]=temp;
A[j]=temp1;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
}
Calculate();
Gantt_chart();
}
void main()
{
int ch;
Getdata();
fcfs();
}
OUTPUT:
GANTT CHART
------------------------------------------
| p1 | p2 | p3 |
------------------------------------------
0 4 10 18
------------------------------------------
PROGRAM:
#include<stdio.h>
#include<string.h>
int Twt,Ttt,A[20],Wt[20],n,Bu[20],B[10];
float Att,Awt;
char pname[20][20];
void Getdata();
void Gantt_chart();
void Sjf();
void Getdata()
{
int i;
printf("\n Enter the number of processes: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fflush(stdin);
printf("\n\n Enter the process name: ");
scanf("%s",&pname[i]);
printf("\n Enter The BurstTime for Process %s = ",pname[i]);
scanf("%d",&Bu[i]);
printf("\n Enter the Arrival Time for Process %s = ",pname[i]);
scanf("%d",&A[i]);
}
}
void Gantt_chart()
{
int i;
printf("\n\nGANTT CHART");
printf("\n----------------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("|\t%s\t",pname[i]);
printf("|\t\n");
printf("\n-----------------------------------------------------------\n");
printf("\n");
for(i=1;i<=n;i++)
printf("%d\t\t",Wt[i]);
printf("%d",Wt[n]+B[n]);
printf("\n------------------------------------------------------------\n");
printf("\n");
}
void Sjf()
{
int w,t,i,Tt=0,temp,j;
char S[10],c[20][20];
int temp1;
printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM
\n\n");
Twt=Ttt=0;
w=0;
for(i=1;i<=n;i++)
{
B[i]=Bu[i];
S[i]='T';
Tt=Tt+B[i];
}
for(i=1;i<=n;i++)
{
for(j=3;j<=n;j++)
{
if(B[j-1]>B[j])
{
temp=B[j-1];
temp1=A[j-1];
B[j-1]=B[j];
A[j-1]=A[j];
B[j]=temp;
A[j]=temp1;
strcpy(c[j-1],pname[j-1]);
strcpy(pname[j-1],pname[j]);
strcpy(pname[j],c[j-1]);
}
}
}
Wt[1]=0;
w=w+B[1];
t=w;
S[1]='F';
while(w<Tt)
{
i=2;
while(i<=n)
{
if(S[i]=='T'&&A[i]<=t)
{
Wt[i]=w;
S[i]='F';
w=w+B[i];
t=w;
i=2;
}
else
i++;
}
}
for(i=1;i<=n;i++)
{
Twt=Twt+(Wt[i]-A[i]);
Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
}
Att=(float)Ttt/n;
Awt=(float)Twt/n;
printf("\n\n Average Turn around time=%3.2f ms ",Att);
printf("\n\n AverageWaiting Time=%3.2f ms",Awt);
Gantt_chart();
}
void main()
{
Getdata();
Sjf();
}
OUTPUT:
GANTT CHART
-------------------------------------------
| p1 | p3 | p2 |
--------------------------------------------
0 5 8 16
---------------------------------------------
PROGRAM:
#include<stdio.h>
main()
{
int prs[10],prty[10];
int n,i,j,twait=0,tta=0,tot=0,totl=0,temp,temp1;
int wait[10],bst[10],p[10];
float wavg,tavg;
printf("\n\t\tPRIORITY SCHEDULING");
printf("\n enter the no of process:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n enter the process name: P");
scanf("%d",&prs[i]);
PRIORITY SCHEDULING
enter the no of process:5
***GRANTT CHART***
___________________________________________
| p4 | p1 | p3 | p2 | p5|
___________________________________________
0 8 13 15 21 28
OUTPUT:
[cse2@localhost ~]$ cc -pthread prod_cons.c
[cse2@localhost ~]$ ./a.out 4
Producer - Consumer Problem using Semaphore
-------------------------------------------
Producer.....buff[0] = 0
Consumer.....buff[0] = 0
Producer.....buff[1] = 1
Consumer.....buff[1] = 1
Producer.....buff[2] = 2
Consumer.....buff[2] = 2
Producer.....buff[3] = 3
Consumer.....buff[3] = 3
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main( )
{
int gd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10] [20] ;
clrscr( ) ;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice( ) ; setbkcolor(BLACK) ;
cir_x=mid/3 ;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<graphics.h>
Struct tree_element
{
char name[20];
int
x,y,ftype,lx,rx,nc,level;
struct
tree_element*link[5]; };
typedef struct tree_element
node; void main( )
{
int
gd=DETECT,gm;node*ro
ot; root=NULL;
clrscr( );
create(&root,0,"root",0,639,320)
; clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI"
);
display(root);getch();closegraph(
);
}
create(node**root,int lev, char * dname, int lx , int rx, int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("Enter name of dir/file(under%s):",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir / 2 for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;(*root)->lx=lx;(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else(*root)->nc=0;
}
}
display(node*root)
{
int i;
settextstyle(2,0,4);settextjustify(1,1);setfillstyle(1,BLUE);setcolor(14);
if( root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1)bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
elsefillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}
OUTPUT:
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
typedef struct
{
char from[20];
char to[20];
}link;
link L[10]; int nofl;
node * root;
void main()
{
int gd=DETECT,gm;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
draw_link_lines();
display(root);
getch();
closegraph();
}
read_links()
{
int i;
printf("how many links");
scanf("%d",&nofl);
for(i=0;i<nofl;i++)
{
printf("File/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name:");
fflush(stdin);
gets(L[i].to);
}
}
draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nofl;i++)
{
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
}
}
search(node *root,char *s,int *x,int *y)
{
int i;
if(root!=NULL)
{
if(strcmpi(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return;
}
else
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}
}
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname); fflush(stdin);
gets((*root)->name);
printf("enter 1 for dir/ 2 for
file:");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("no of sub directories /files (for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create( & ( (*root)->link[i] ) , lev+1 ,
(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else (*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14); if(root
!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root-
>name); for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}}}
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int r[1][10],av[1][10];
intall[10][10],max[10][10],ne[10][10],w[10],safe[10];
int i=0,j=0,k=0,l=0,np=0,nr=0,count=0,cnt=0;
clrscr();
printf("enter the number of processes in a
system"); scanf("%d",&np);
printf("enter the number of resources in a system");
scanf("%d",&nr);
for(i=1;i<=nr;i++)
{
printf("\n enter the number of instances of resource R%d " ,i);
scanf("%d",&r[0][i]);
av[0][i]=r[0][i];
}
for(i=1;i<=np;i++)
for(j=1;j<=nr;j++)
all[i][j]=ne[i][j]=max[i][j]=w[i]=0;
printf(" \nEnter the allocation matrix");
for(i=1;i<=np;i++)
{
printf("\n"
);
for(j=1;j<
=nr;j++)
{
scanf("%d",&all[
i][j]);
av[0][j]=av[0][j]-
all[i][j];
}
}
printf("\nEnter the maximum
matrix"); for(i=1;i<=np;i++)
{ printf("\n");
for(j=1;j<=nr;j++)
{
scanf("%d",&max[i][j]);
}
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nr;j++)
{
ne[i][j]=max[i][j]-all[i][j];
}
}
for(i=1;i<=np;i++)
{
printf("pocess P%d",i);
for(j=1;j<=nr;j++)
{
printf("\n allocated %d\t",all[i][j]);
printf("maximum %d\t",max[i][j]);
printf("need %d\t",ne[i][j]);
}
printf("\n_________________________\n");
}
printf("\nAvailability");
for(i=1;i<=nr;i++)
printf("R%d %d\t",i,av[0][i]);
printf("\n____________");
printf("\n safe sequence");
for(count=1;count<=np;count++)
{
for(i=1;i<=np;i++)
{
cnt=0;
for(j=1;j<=nr;j++)
{
if(ne[i][j]<=av[0][j] && w[i]==0)
cnt++;
}
if(cnt==nr)
{
k++;
safe[k]=i;
for(l=1;l<=nr;l++)
av[0][l]=av[0][l]+all[i][l];
printf("\n P%d ",safe[k]);
printf("\t Availability");
for(l=1;l<=nr;l++)
printf("R%d %d\t",l,av[0][l]);
w[i]=1;
}
}
}
getch();
}
OUTPUT:
PROGRAM:
#include <stdio.h>
main()
{
int found,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
printf("Enter total no of processes");
scanf("%d",&tp);
printf("Enter total no of resources");
scanf("%d",&tr);
printf("Enter claim (Max. Need) matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&c[i][j]);
}
printf("Enter allocation matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&p[i][j]);
}
printf("Enter resource vector (Total resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&r[i]);
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
{sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
}
OUTPUT:
OUTPUT:
FIFO PAGE REPLACEMENT ALGORITHM
Enter no.of frames....4
Enter number of reference string..
6
Enter the reference string..
564123
The given reference string:
...................................... 5 6 4 1 2 3
Reference np5-> 5 -1 -1 -1
Reference np6-> 5 6 -1 -1
Reference np4-> 5 6 4 -1
Reference np1-> 5 6 4 1
Reference np2-> 2 6 4 1
Reference np3-> 2 3 4 1
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames....");
scanf("%d",&nof);
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}
OUTPUT:
LRU PAGE REPLACEMENT ALGORITHM
Enter no.of Frames....3
Enter no.of reference string..6
Enter reference string..6 5 4 2 3 1
LRU PAGE REPLACEMENT ALGORITHM
The given reference string:
…………………. 6 5 4 2 3 1
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
No.of page faults...6
PROGRAM:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\n.................................");
printf("\nEnter the no.of frames");
scanf("%d",&nof);
printf("Enter the no.of reference string");
scanf("%d",&nor);
printf("Enter the reference string");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\n................................");
printf("\nThe given string");
printf("\n....................\n");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
{
frm[i]=-1;
optcal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n Number of page faults: %d",pf);
getch();
}
int optvictim(int index)
{
int i,j,temp,notfound;
for(i=0;i<nof;i++)
{
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
{
notfound=0;
optcal[i]=j;
break;
}
if(notfound==1)
return i;
}
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0;
}
OUTPUT:
OPTIMAL PAGE REPLACEMENT ALGORITHM
Enter no.of Frames....3
Enter no.of reference string..6
Enter reference string..6 5 4 2 3 1
OPTIMAL PAGE REPLACEMENT ALGORITHM
The given reference string:
…………………. 6 5 4 2 3 1
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
No.of page faults...6
PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
{
int child,shmid,i;
char * shmptr;
child=fork();
if(!child)
{
shmid=shmget(2041,32,IPC_CREAT|0666);
shmptr=shmat(shmid,0,0);
printf(“\n Parent writing\n”);
for(i=0;i<10;i++)
{
shmptr[i]=’a’+i;
putchar(shmptr[i]);
}
printf(“\n\n %s”, shmptr);
wait(NULL);
}
else
{
shmid=shmget(2041,32,0666);
shmptr=shmat(shmid,0,0);
printf(“\n Child is reading\n”);
for(i=0;i<10;i++)
putchar(shmptr[i]);
shmdt(NULL);
shmctl(shmid,IPC_RMID,NULL);
}
return 0;
}
OUTPUT:
[cse2@localhost ~]$ cc share.c
[cse2@localhost ~]$ ./a.out
Parent writing
abcdefghij
Child is reading
abcdefghij
PROGRAM:
#include<unistd.h>
main()
{
int fd[3],pid;
char *msg=("OS LAB\n");
char buff[25];
pipe(fd);
pid=fork();
if(pid==0)
{
puts("child process\n");
close(fd[0]);
write(fd[1],msg,30);
}
else
{
sleep(1);
puts("parent process\n");
close(fd[1]);
read(fd[0],buff,30);
printf("%s",buff);
}
}
OUTPUT:
parent process
OS LAB
[cse2@localhost ~]$
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 25
void printlayout(int[],int );
int firstfit(int[],int, int);
int bestfit(int[],int, int);
void main()
{
int i,a[25],n,req,choice,pos,ch;
clrscr();
printf("How MAny Segments");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Segent Size");
scanf("%d",&a[i]);
}
loop:
printf("How Much Is Your Memory Requirment");
scanf("%d",&req);
printf("\n1.Bestfit\n2.Firstfit\n3.Exit\n");
printf("Enter Your Choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
pos=bestfit(a,n,req);
break;
case 2:
pos=firstfit(a,n,req);
break;
}
printf("\tBestfit and Firstfit Algorithm\n");
printf("\t______________________________\n\n");
printlayout(a,n);
printf("Your Memory Requirment is :%d\n\n",req);
printf("Alloted Memory Region is:%d\n\n",a[pos]);
a[pos]=0;
printf("Do You Want To Continue 1/0");
scanf("%d",&ch);
if(ch==1)
goto loop;
getch();
}
void printlayout(int a[],int n)
{
int i,j;
printf("\t\tMemory Free List");
printf("\n\t\t_______________\n\n");
printf("\t\t|~~~|\n");
for(i=0;i<n;i++)
{
if(a[i]!=0)
{
for(j=1;j<=(a[i]/100);j++)
printf("\t\t| |\n");
printf("\t\t| %d|\n",a[i]);
printf("\t\t|---|\n");
}
}
printf("\n\n");
}
int firstfit(int a[],int n,int r)
{
int i;
for(i=0;i<n;i++)
if(a[i]>=r)
break;
return i;
}
int bestfit(int a[],int n,int r)
{
int b[25],i,j,temp,val;
for(i=0;i<n;i++)
b[i]=a[i];
for(i=0;i<n-1;i++)
for(j=i;j<n-1;j++)
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
for(i=0;i<n;i++)
if(b[i]>=r)
break;
val=b[i];
for(i=0;i<n;i++)
if(a[i]==val)
break;
return i;
}
OUTPUT:
[cse2@localhost ~]$ cc memsch1.c
[cse2@localhost ~]$ ./a.out
How Many Segments4
Segent Size500
Segent Size200
Segent Size300
Segent Size400
|~~~|
| |
| |
|500|
| --- |
| |
|200|
| --- |
| |
| |
|300|
| --- |
| |
| |
|400|
| --- |
1.Bestfit
2.Firstfit
3.Exit
Enter Your Choice
1
Bestfit and Firstfit Algorithm
___________________________________
Memory Free List
___________________________________
|~~~|
| |
| |
| |
|500|
| --- |
| |
|300|
| --- |
| |
| |
|400|
| --- |
if(k!=0)
{
printf(“\n Mutex Destroyed \n”);
exit(1);
}
}
return 0;
}
void *func(int n)
{
printf(“\nPhilosopher %d is thinking “,n);
pthread_mutex_lock(&chopstick[n]);
pthread_mutex_lock(&chopstick[(n+1)%5]);
printf(“\nPhilosopher %d is eating “,n);
sleep(3);
pthread_mutex_unlock(&chopstick[n]);
pthread_mutex_unlock(&chopstick[(n+1)%5]);
printf(“\nPhilosopher %d Finished eating “,n);
}
OUTPUT:
[root@localhost ~]$ gcc -o c dining.c -pthread
[root@localhost ~]$ ./c
Philosopher 1 is thinking
Philosopher 1 is eating
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 3 is eating
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 1 Finished eating
Philosopher 3 Finished eating
Philosopher 4 is eating
Philosopher 5 is eating
Philosopher 2 is eating
Philosopher 4 Finished eating
Philosopher 5 Finished eating
Philosopher 2 Finished eating
PROGRAM:
#include <stdio.h>
#include <conio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];
int pmsize,lmsize,psize,frame,page,ftable[20],frameno;
void info()
{
printf("\n\nMEMORY MANAGEMENT USING PAGING\n\n");
printf("\n\nEnter the Size of Physical memory: "); scanf("%d",&pmsize);
printf("\n\nEnter the size of Logical memory: ");
scanf("%d",&lmsize);
printf("\n\nEnter the partition size: ");
scanf("%d",&psize);
frame = (int) pmsize/psize;
page = (int) lmsize/psize;
printf("\nThe physical memory is divided into %d no.of frames\n",frame);
printf("\nThe Logical memory is divided into %d no.of pages",page);
}
void assign()
{
int i;
for (i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit= -1;
}
for(i=0; i<frame;i++)
ftable[i] = 32555;
for (i=0;i<page;i++)
{
printf("\n\nEnter the Frame number where page %d must be placed: ",i);
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)
{
ptable[i].fno = frameno;
ptable[i].pbit = 1;
}
}
getch();
printf("\n\nPAGE TABLE\n\n"); printf("PageAddress FrameNo. PresenceBit\n\n");
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n"); printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
getch();
clrscr();
printf("\n\n\n\tProcess to create the Physical Address\n\n");
printf("\nEnter the Base Address: "); scanf("%d",&baddr);
printf("\nEnter theLogical Address: "); scanf("%d",&laddr);
paddr = laddr / psize; disp = laddr % psize; if(ptable[paddr].pbit==1)
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe Physical Address where the instruction present: %d",phyaddr);
}
void main()
{
clrscr();
info();
assign();
cphyaddr();
getch();
}
OUTPUT: