0% found this document useful (0 votes)
10 views39 pages

Include

The document contains multiple C programs related to file allocation, page replacement algorithms, memory management schemes, and process scheduling. Key algorithms include Optimal, LRU, FIFO page replacement, and memory allocation strategies like First Fit and Worst Fit. Each section provides user prompts for input and outputs results based on the algorithms implemented.

Uploaded by

Shivanshu Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views39 pages

Include

The document contains multiple C programs related to file allocation, page replacement algorithms, memory management schemes, and process scheduling. Key algorithms include Optimal, LRU, FIFO page replacement, and memory allocation strategies like First Fit and Worst Fit. Each section provides user prompts for input and outputs results based on the algorithms implemented.

Uploaded by

Shivanshu Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

#include<stdio.

h>

#include<conio.h>

void main()

int f[50],i,st,len,j,c,k,count=0;

for(i=0;i<50;i++)

f[i]=0;

printf("Files Allocated are : \n");

x: count = 0;

printf("Enter starting block and length of files: ");

scanf("%d%d",&st,&len);

for(k=st;k<(st+len);k++)

if(f[k]==0)

count++;

if(len==count)

for(j=st;j<(st+len);j++)

if(f[j]==0)

f[j]=1;

printf("%d\t%d\n",j,f[j]);

if(j!=(st+len-1))

printf("The file is allocated to disk\n");

else

printf("The file is not allocated \n");

printf("Do you want to enter more file(Yes - 1/No - 0)");


scanf("%d",&c);

if(c==1)

goto x;

// else

// exit();

}
#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()

printf("OPTIMAL PAGE REPLACEMENT ALGORITHM");

// 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]);

printf("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("Reference 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");

printf("Number of page faults: %d",pf);

}
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;

}
#include<unistd.h>

#include<fcntl.h>

#include<sys/stat.h>

#include<stdio.h>

int main()

int n,fd;

char buff[50];

printf("Enter text to write in the file:\n");

n=read(0,buff,50);

fd=open("file",O_CREAT|O_RDWR,0777);

write(fd,buff,n);

write(1,buff,n);

int close(int fd);

return 0;

}
#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("LRU PAGE REPLACEMENT ALGORITHM");

printf("\nEnter no.of Frames: ");

scanf("%d",&nof);

printf("Enter no.of reference string: ");

scanf("%d",&nor);

printf("Enter reference string: ");

for(i=0;i<nor;i++)

scanf("%d",&ref[i]);

printf("LRU PAGE REPLACEMENT ALGORITHM ");

printf("\nThe given reference string: ");

//printf("\n…........................................... ");

for(i=0;i<nor;i++)

printf("%4d",ref[i]);

for(i=1;i<=nof;i++)

frm[i]=-1;

lrucal[i]=0;

for(i=0;i<10;i++)

recent[i]=0;

printf("\n");
for(i=0;i<nor;i++)

flag=0;

printf("Reference 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("%4d",frm[j]);

printf("\n");

recent[ref[i]]=i;

printf("No.of page faults...%d",pf);

}
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;

}
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

int f[50],p,i,st,len,j,c,k,a;

for(i=0;i<50;i++)

f[i]=0;

printf("Enter how many blocks already allocated: ");

scanf("%d",&p);

printf("Enter blocks already allocated: ");

for(i=0;i<p;i++)

scanf("%d",&a);

f[a]=1;

x: printf("Enter index starting block and length: ");

scanf("%d%d",&st,&len);

k=len;

if(f[st]==0)

for(j=st;j<(st+k);j++)

if(f[j]==0)

f[j]=1;

printf("%d-------->%d\n",j,f[j]);
}

else

printf("%d Block is already allocated \n",j);

k++;

else

printf("%d starting block is already allocated \n",st);

printf("Do you want to enter more file(Yes - 1/No - 0)");

scanf("%d",&c);

if(c==1)

goto x;

// else

// exit();

}
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;

for(i=0;i<50;i++)

f[i]=0;

x:printf("Enter the index block: ");

scanf("%d",&ind);

if(f[ind]!=1)

printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);

scanf("%d",&n);

else

printf("%d index is already allocated \n",ind);

goto x;

y: count=0;

for(i=0;i<n;i++)

scanf("%d", &index[i]);

if(f[index[i]]==0)

count++;

if(count==n)

{
for(j=0;j<n;j++)

f[index[j]]=1;

printf("Allocated\n");

printf("File Indexed\n");

for(k=0;k<n;k++)

printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);

else

printf("File in the index is already allocated \n");

printf("Enter another file indexed");

goto y;

printf("Do you want to enter more file(Yes - 1/No - 0)");

scanf("%d", &c);

if(c==1)

goto x;

else

exit(0);

getch();

}
#include<stdio.h>

#include<unistd.h>

#include<string.h>

#include<fcntl.h>

main()

int fd[2];

char buf1[25]="just a test\n";

char buf2[50];

fd[0]=open("file1",O_RDWR);

fd[1]=open("file2",O_RDWR);

write(fd[0],buf1,strlen(buf1));

//printf("\n Enter the text now....");

printf("Enter the text: ");

scanf("\n %s",buf1);

printf("Cat file1 is %s",buf1);

//printf("\n Cat file1 is \n Enter the text hai");

write(fd[0],buf1,strlen(buf1));

lseek(fd[0],SEEK_SET,0);

read(fd[0],buf2,sizeof(buf1));

write(fd[1],buf2,sizeof(buf2));

close(fd[0]);

close(fd[1]);

printf("\n");

return 0;

}
#include<stdio.h>

#include<limits.h>

#define max 25

void main()

int flag[max],b[max],f[max],i,j,nb,nf,temp;

static int bf[max],ff[max];

printf("\tMemory Management Scheme - First Fit");

printf("\nEnter the number of blocks:");

scanf("%d",&nb);

printf("Enter the number of files:");

scanf("%d",&nf);

printf("Enter the size of the blocks:-\n");

for(i=1;i<=nb;i++)

printf("Block %d:",i);

scanf("%d",&b[i]);

printf("Enter the size of the files :-\n");

for(i=1;i<=nf;i++)

printf("File %d:",i);

scanf("%d",&f[i]);

for(i=1;i<=nf;i++)

{
for(j=1;j<=nb;j++)

if(bf[j]!=1)

temp=b[j]-f[i];

if(temp>=0)

ff[i]=j;

break;

flag[i]=temp;

bf[ff[i]]=1;

printf("File_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");

for(i=1;i<=nf;i++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],flag[i]);

}
#include<stdio.h>

int main()

int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;

float avg_wt,avg_tat;

printf("Enter number of process:");

scanf("%d",&n);

printf("Enter 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;

temp=bt[i];

bt[i]=bt[pos];

bt[pos]=temp;

temp=p[i];

p[i]=p[pos];
p[pos]=temp;

wt[0]=0;

for(i=1;i<n;i++)

wt[i]=0;

for(j=0;j<i;j++)

wt[i]+=bt[j];

total+=wt[i];

avg_wt=(float)total/n;

total=0;

printf("Process\tBurstTime\tWaitingTime\tTurnaround Time");

for(i=0;i<n;i++)

tat[i]=bt[i]+wt[i];

total+=tat[i];

printf("\np%d \t %d \t %d \t %d",p[i],bt[i],wt[i],tat[i]);

avg_tat=(float)total/n;

printf("\nAverage Waiting Time=%f",avg_wt);

printf("\nAverage Turnaround Time=%f\n",avg_tat);

}
#include<stdio.h>

//#include<process.h>

void main()

int a[20],p[20],i,j,n,m;

printf("Enter no of Blocks.\n");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter the %dst Block size:",i);

scanf("%d",&a[i]);

printf("Enter no of Process.\n");

scanf("%d",&m);

for(i=0;i<m;i++)

printf("Enter the size of %dstProcess:",i);

scanf("%d",&p[i]);

for(i=0;i<n;i++)

for(j=0;j<m;j++)

if(p[j]<=a[i])

printf("The Process %d allocated to %d\n",j,a[i]);


p[j]=10000;

break;

for(j=0;j<m;j++)

if(p[j]!=10000)

printf("The Process %d is not allocated\n",j);

}
#include<stdio.h>

#include<conio.h>

#define max 25

void main()

int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;

static int bf[max],ff[max];

printf("\tMemory Management Scheme - Worst Fit");

printf("\nEnter the number of blocks:");

scanf("%d",&nb);

printf("Enter the number of files:");

scanf("%d",&nf);

printf("Enter the size of the blocks:-\n");

for(i=1;i<=nb;i++)

printf("Block %d:",i);

scanf("%d",&b[i]);

printf("Enter the size of the files :-\n");

for(i=1;i<=nf;i++)

printf("File %d:",i);

scanf("%d",&f[i]);

for(i=1;i<=nf;i++)

for(j=1;j<=nb;j++)

{
if(bf[j]!=1) //if bf[j] is not allocated

temp=b[j]-f[i];

if(temp>=0)

if(highest<temp)

ff[i]=j;

highest=temp;

frag[i]=highest;

bf[ff[i]]=1;

highest=0;

printf("File_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");

for(i=1;i<=nf;i++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);

}
#include<stdio.h>

#include<conio.h>

int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;

void main()

printf("FIFO PAGE REPLACEMENT ALGORITHM");

printf("\nEnter no.of frames: ");

scanf("%d", &nof);

printf("Enter number of Pages: ");

scanf("%d",&nor);

printf("Enter the Page No: ");

for (i=0; i<nor;i++)

scanf("%d",&ref[i]);

printf("The given Pages are: ");

for (i=0; i<nor;i++)

printf("%4d", ref[i]);

for (i=1; i<=nof;i++)

frm[i]=-1;

printf("\n");

for (i=0; i<nor;i++)

flag=0;

printf("page no %d->\t",ref[i]);

for (j=0; j<nof; j++)

if(frm[j]==ref[i])

flag=1;
break;

}}

if(flag==0)

pf++;

victim++;

victim=victim%nof;

frm[victim]=ref[i];

for (j=0; j<nof; j++)

printf("%4d",frm[j]);

printf("\n");

}}

printf("No.of pages faults:%d",pf);

}
#include<stdio.h>

int main()

int i,j,n,t,turn[20],burst[20],p[20],wt[20],c[20];

float await,aturn,twait=0,tturn=0;

printf("Enter the value of n: ");

scanf("%d",&n);

printf("Enter the process no, burst and arrivaltime:");

for(i=0;i<n;i++)

scanf("%d",&c[i]);

scanf("%d",&burst[i]);

scanf("%d",&p[i]);

for(i=0;i<n;i++)

for(j=i+1;j<n;j++)

if(p[i]>p[j])

t=p[i];

p[i]=p[j];

p[j]=t;

t=burst[i];

burst[i]=burst[j];

burst[j]=t;

t=c[i];

c[i]=c[j];

c[j]=t;

}
}

for(i=0;i<n;i++)

if(i==0){

wt[i]=0;

turn[i]=burst[i];

else

turn[i]=turn[i-1]+burst[i];

wt[i]=turn[i]-burst[i];

twait=twait+wt[i];

tturn=tturn+turn[i]; }

}await=twait/n;

aturn=(tturn)/n;

printf("pno\tbtime\tatime\twtime\tttime");

for(i=0;i<n;i++)

printf("\n%d\t%d\t%d\t%d\t%d",c[i],burst[i],p[i],wt[i],turn[i]);

printf("\nThe average waiting time is:%f",await);

printf("\nThe average turn around time is:%f",aturn);

}
#include<stdio.h>

#include<conio.h>

int main()

int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;

printf("Enter total number of processes(maximum 20):");

scanf("%d",&n);

printf("Enter Process 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]+=bt[j];

printf("Process\t\tBurst Time\tWaiting Time\tTurnaround Time");

for(i=0;i<n;i++)

tat[i]=bt[i]+wt[i];

avwt+=wt[i];

avtat+=tat[i];

printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;

avtat/=i;

printf("\nAverage Waiting Time:%d",avwt);

printf("\nAverage Turnaround Time:%d",avtat);

return 0;

}
#include<stdio.h>

#include<conio.h>

#include<stdbool.h>

struct semaphore

int mutex;

int rcount;

int rwait;

bool wrt;

};

void addR(struct semaphore *s)

if (s->mutex == 0 && s->rcount == 0)

printf("Sorry, File open in Write mode.\nNew Reader added to queue.\n");

s->rwait++;

else {

printf("Reader Process added\n");

s->rcount++;

s->mutex--;

return ;

void addW(struct semaphore *s)

if(s->mutex==1)

s->mutex--;
s->wrt=1;

printf("Writer Process added\n");

else if(s->wrt) printf("Sorry, Writer already operational\n");

else printf("Sorry, File open in Read mode\n");

return ;

void remR(struct semaphore *s)

if(s->rcount == 0) printf("No readers to remove\n");

else

printf("Reader Removed\n");

s->rcount--;

s->mutex++;

return ;

void remW(struct semaphore *s)

if(s->wrt==0) printf("No Writer to Remove\n");

else

printf("Writer Removed\n");

s->mutex++;

s->wrt=0;

if(s->rwait!=0)

s->mutex-=s->rwait;

s->rcount=s->rwait;
s->rwait=0;

printf("%d waiting Readers Added.",s->rcount);

int main()

struct semaphore S1={1,0,0};

while(1)

// system("cls");

printf("Options :-\n1.Add Reader\n2.Add Writer\n3.Remove Reader\n4.Remove Writer\n5.Exit\


nChoice: ");

int ch;

scanf("%d", &ch);

switch(ch)

case 1: addR(&S1); break;

case 2: addW(&S1); break;

case 3: remR(&S1); break;

case 4: remW(&S1); break;

case 5: printf("\tGoodBye!");

return 0;

default: printf("Invalid Entry!\n");

continue;

printf("<<<<<< Current Status >>>>>>\n\tMutex: %d\n\tActive Readers: %d\n\tWaiting Readers: %d\n\


tWriter Active: %s\n", S1.mutex, S1.rcount,S1.rwait, (S1.mutex==0 && S1.rcount==0) ? "YES" : "NO");

// system("pause");
}

}
#include<stdio.h>

int main()

int p[20],bt[20], su[20], wt[20],tat[20],i, k, n, temp;

float wtavg, tatavg;

printf("Enter the number of processes: ");

scanf("%d",&n);

for(i=0;i<n;i++)

p[i] = i;

printf("Enter the Burst Time of Process %d: ",i);

scanf("%d",&bt[i]);

printf("System/User Process (0/1): ");

scanf("%d", &su[i]);

for(i=0;i<n;i++)

for(k=i+1;k<n;k++)

if(su[i] > su[k])

temp=p[i];

p[i]=p[k];

p[k]=temp;

temp=bt[i];

bt[i]=bt[k];

bt[k]=temp;

temp=su[i];

su[i]=su[k];

su[k]=temp;

}
wtavg = wt[0] = 0;

tatavg = tat[0] = bt[0];

for(i=1;i<n;i++)

wt[i] = wt[i-1] + bt[i-1];

tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i];

tatavg = tatavg + tat[i];

printf("PROCESS\t\t SYSTEM/USER PROCESS \tBURST TIME\tWAITING\tTIME\tTURNAROUND TIME\n");

for(i=0;i<n;i++)

printf("%d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],su[i],bt[i],wt[i],tat[i]);

printf("Average Waiting Time is: %f\n",wtavg/n);

printf("Average Turnaround Time is: %f\n",tatavg/n);

return 0;

}
#include <stdio.h>

int main()

int count = 0, m, n, process, temp, resource;

int allocation_table[5] = {0, 0, 0, 0, 0};

int available[5], current[5][5], maximum_clAim[5][5];

int maximum_resources[5], running[5], safe_state = 0;

printf("Enter The Total Number Of Processes: ");

scanf("%d", &process);

for(m = 0; m < process; m++)

running[m] = 1;

count++;

printf("Enter The Total Number Of Resources To Allocate: ");

scanf("%d", &resource);

printf("Enter The Claim Vector: ");

for(m = 0; m < resource; m++)

scanf("%d", &maximum_resources[m]);

printf("Enter Allocated Resource Table: ");

for(m = 0; m < process; m++)

for(n = 0; n < resource; n++)

scanf("%d", &current[m][n]);

}
}

printf("Enter The Maximum Claim Table: ");

for(m = 0; m < process; m++)

for(n = 0; n < resource; n++)

scanf("%d", &maximum_clAim[m][n]);

printf("The Claim Vector: ");

for(m = 0; m < resource; m++)

printf("\t%d ", maximum_resources[m]);

printf("\nThe Allocated Resource Table\n");

for(m = 0; m < process; m++)

for(n = 0; n < resource; n++)

printf("\t%d", current[m][n]);

printf("\n");

printf("The Maximum Claim Table\n");

for(m = 0; m < process; m++)

for(n = 0; n < resource; n++)

{
printf("\t%d", maximum_clAim[m][n]);

printf("\n");

for(m = 0; m < process; m++)

for(n = 0; n < resource; n++)

allocation_table[n] = allocation_table[n] + current[m][n];

printf("Allocated Resources");

for(m = 0; m < resource; m++)

printf("\t%d", allocation_table[m]);

for(m = 0; m < resource; m++)

available[m] = maximum_resources[m] - allocation_table[m];

printf("\nAvailable Resources:");

for(m = 0; m < resource; m++)

printf("\t%d", available[m]);

printf("\n");

while(count != 0)

{
safe_state = 0;

for(m = 0; m < process; m++)

if(running[m])

temp = 1;

for(n = 0; n < resource; n++)

if(maximum_clAim[m][n] - current[m][n] > available[n])

temp = 0;

break;

if(temp)

printf("\nProcess %d Is In Execution \n", m + 1);

running[m] = 0;

count--;

safe_state = 1;

for(n = 0; n < resource; n++)

available[n] = available[n] + current[m][n];

break;

if(!safe_state)
{

printf("The Processes Are In An Unsafe State\n");

break;

else

printf("The Process Is In A Safe State \n");

printf("\nAvailable Vector\n");

for(m = 0; m < resource; m++)

printf("\t%d", available[m]);

printf("\n");

return 0;

You might also like