Operating Systems — Some Important Lab Programs [ Part 1]

Praseeda Saripalle
4 min readNov 23, 2022

Practice some of the important Lab Concepts from Operating Systems

1. Deadlock Avoidance:

#include<stdio.h>
#include<conio.h>
#include<windows.h>
void main()
{
int
k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][
5],pno,P[10],j,rz, count=0;
system("CLS");
printf("\n Enter the number of resources : ");
scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for(i=0;i<rz;i++)
{
avail[i]=0;
printf("%c= ",(i+65));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n ");
for(i=0;i<rz;i++)
printf(" %c",(i+65));
printf("\n");
for(i=0;i<pno;i++)
{
P[i]=i;
printf("P[%d] ",P[i]);
for(j=0;j<rz;j++)
{
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}
printf("\nEnter the MAX matrix \n ");
for(i=0;i<rz;i++)
{

printf(" %c",(i+65));
avail[i]=ins[i]-avail[i];
}
printf("\n");
for(i=0;i <pno;i++)
{
printf("P[%d] ",i);
for(j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);

}
printf("\n");
printf("\nNEED matrix(MAX-ALLOCATION)\n ");
for(i=0;i<rz;i++)
{
printf(" %c",(i+65));
}
printf("\n");
for(i=0;i <pno;i++)
{
printf("P[%d] ",i);
t=P[i];
for(j=0;j<rz;j++)
{
need[t][j] = MAX[t][j]-allocated[t][j];
printf("%d ",need[t][j]);
}
printf("\n");
}
A: d=-1;
for(i=0;i <pno;i++)

{
count=0; t=P[i];
for(j=0;j<rz;j++)
{
need[t][j] = MAX[t][j]-allocated[t][j];
if(need[t][j]<=avail[j])
count++;
}
if(count==rz)
{
output[k++]=P[i];
for(j=0;j<rz;j++)

avail[j]+=allocated[t][j];

}

else
P[++d]=P[i];
}
if(d!=-1)
{
pno=d+1;
goto A;
}
if(count==rz)
{
printf("\n The system is in a safe state!!\n");
printf("Safe Sequence is:\n");
printf("\t <");
for(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
}
else
printf("\nThe system is in an unsafe state!!");
getch(); }

2. Deadlock Prevention

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void main()
{
int k=0,n,m,b,count=0,l,i,j,ch,flag[20],p[20],a[20];
char r[20][20],res[20];
printf("Enter no. of resources::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
flag[i]=0;
a[i]=0;
}
printf("\nEnter the resources in the increasing order of their priority");
for(i=0;i<n;i++)
{
printf("\nEnter resource%d name::",i+1);
scanf(" %s",&r[i]);
}
do
{
count=0;
printf("\nEnter the resource name to be allocated for process P1::");
scanf(" %s",res);
for(i=0;i<n;i++)
{
if(strcmp(r[i],res)==0)
{
count+=1;
if(flag[i]==1)
{
printf("Resource %s is already been allocated\n",res);
}

else{
for(m=0;m<n;m++)
a[m]=0;
k=0;
for(j=i+1;j<n;j++)
{
if(flag[j]==1)
{
a[k++]=j;
}
}
if(a[0]!=0)
{
printf("\nRequested resource priority is less

than already allocated resources priority\n");

printf("The process must release the control of

resources::");

for(l=0;a[l]!=0;l++)
{
printf("%s ",r[a[l]]);
}
printf("\n");
printf("\nDo you agree to release::(Y(1):n(0))");
scanf("%d",&b);
if(b==1)
{
for(l=0;a[l]!=0;l++)
{
flag[a[l]]=0;
printf("\nResource %s has been

released",r[a[l]]);

}
flag[i]=1;
printf("\nResource %s has been allocated",res);
}
}
else{
flag[i]=1;
printf("\nResource %s has been allocated",res);
}

}
}
}
if(count==0)
printf("\n Resource %s doesn't exist",res);
printf("\nAllocated resources are:: ");
for(m=0;m<n;m++)
{
if(flag[m]==1)
{
printf("%s ",r[m]);
}
}
printf("\nDo U want to continue ::(Y(1):n(0))");
scanf("%d",&ch);
if(ch!=1)
break;
}while(1);
}

3. FIFO: Page Replacement Algorithms

#include<stdio.h>
int 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("\t%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);
return 0; }

4. LRU: Page Replacement Algorithms

#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter Frame Size::");
scanf("%d",&nof);
printf(" Enter the size of reference string::");
scanf("%d",&nor);
printf("\n Enter reference string with length %d::",nor);
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
{
printf("%2d",ref[i]);
}
printf("\n");
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
{
recent[i]=0;
}
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference Pg 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=lruvictim();
}
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
{
printf("%3d",frm[j]);
}
}
recent[ref[i]]=i;
}
printf("\n\n\t Total 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;

5. LFU: Page Replacement Algorithms

#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25],frame[10],arr[25],time[25];
int m,n,page,flag,k,minimum_time,temp;
printf("Enter Total Number of Pages:\t");
scanf("%d",&total_pages);
printf("Enter Total Number of Frames:\t");
scanf("%d",&total_frames);
for(m=0;m<total_frames;m++)
{
frame[m]=-1;
}
for(m=0;m<25;m++)
{
arr[m]=0;
}
printf("Enter Page Number\n");
for(m=0;m<total_pages;m++)
{
scanf("%d",&pages[m]);
}
printf("\n");
for(m=0;m<total_pages;m++)
{
arr[pages[m]]++;
//time[pages[m]] = m;
flag=1;
k=frame[0];
for(n=0;n<total_frames;n++)
{
if(frame[n]==-1)
{
hit++;
flag=0;
frame[n]=pages[m];
time[pages[m]]=m;
break;
}

if(frame[n]==pages[m])
{
flag=0;
frame[n]=pages[m];
break;

}
if(arr[k]>arr[frame[n]])
{
k=frame[n];
}
}
if(flag)
{
minimum_time=25;
for(n=0;n<total_frames;n++)
{
if(arr[frame[n]]==arr[k] && time[frame[n]]<minimum_time)
{
temp=n;
minimum_time=time[frame[n]];
}
}
arr[frame[temp]]--;
frame[temp]=pages[m];
time[pages[m]]=m;
hit++;
}
printf("Page Number=%d\n",pages[m]);
printf("Page Frames:");
for(n=0;n<total_frames;n++)
{
printf("\t%d",frame[n]);
}
printf("\n\n");
}
printf("Total Page Faults:\t%d\n",hit);
return 0;
}

6. Shared Memory

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;unistd.h&gt;
#include&lt;sys/shm.h&gt;
#include&lt;string.h&gt;
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);
printf(“Key of shared memory is %d\n”,shmid);

shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment
printf(”Process attached at %p\n”,shared_memory); //this prints the address where the
printf(“Enter some data to write to shared memory\n”);
read(0,buff,100); //get some input from user
strcpy(shared_memory,buff); //data written to shared memory
printf(“You wrote : %s\n”,(char *)shared_memory);
}
#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;unistd.h&gt;
#include&lt;sys/shm.h&gt;
#include&lt;string.h&gt;
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666);
printf(“Key of shared memory is %d\n”,shmid);
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment
printf(”Process attached at %p\n”,shared_memory);
printf(“Data read from shared memory is : %s\n”,(char *)shared_memory);
}

7. Producer — Consumer

#include<stdio.h>;
#include<stdlib.h>;
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf(“\n1.Producer\n2.Consumer\n3.Exit”);
while(1)
{
Printf”(\nEnter your choice”);
Scanf(“%d”,&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf(“Buffer is full!!”);
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf(“Buffer is empty!!”);

break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf(”\nProducer produces the item %d”,x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf(“\nConsumer consumes item %d”,x);
x--;
mutex=signal(mutex);
}

--

--