1.optimal Page Replacement Algorithm
1.optimal Page Replacement Algorithm
printf("
\t%d:\t",page[i]);
break;
}
}
if(flag==0)
{
fr[p]=page[i];
printf("
\t%d:\t",page[i]);
p++;
}
for(j=1;j<=nf;j++)
printf(" %d
",fr[j]);
printf("\n");
}
}
p=0;
for(;i<=psz;i++)
{
flag=0;
for(j=1;j<=nf;j++)
{
if(page[i]==fr[j])
{
flag=1;
break;
}
}
if(flag==0)
{
p++;
for(j=1;j<=nf;j++)
{
for(k=i+1;k<=psz;k++)
{
if(fr[j]==page[k])
{
u[j]=k;
break;
}
else
u[j]=21;
}
}
for(j=1;j<=nf;j++)
t[j]=u[j];
for(j=1;j<=nf;j++)
{
for(k=j+1;k<=nf;k++)
{
if(t[j]<t[k])
{
t1=t[j];
t[j]=t[k];
t[k]=t1;
}
}
}
for(j=1;j<=nf;j++)
{
if(t[1]==u[j])
{
fr[j]=page[i];
u[j]=i;
}
}
printf("page fault\t");
}
else
printf("
\t");
printf("%d:\t",page[i]);
for(j=1;j<=nf;j++)
printf(" %d
",fr[j]);
printf("\n");
}
printf("\ntotal page faults:
// getch();
}
int full(int a[],int n)
{
int k;
for(k=1;k<=n;k++)
{
if(a[k]==-1)
return 0;
}
%d",p+3);
return 1;}
flag=1;
printf("
\t%d:\t",page[i]);
break;
}
}
if(flag==0)
{
fr[p]=page[i];
p++;
printf("\t
\t%d:\t",page[i]);
}
for(j=1;j<=nf;j++)
printf("
%d
",fr[j]);
printf("\n");
}
}
p=0;
for(;i<=psz;i++)
{
flag=0;
for(j=1;j<=nf;j++)
{
if(page[i]==fr[j])
{
flag=1;
break;
}
}
if(flag==0)
{
p++;
m++;
fr[p]=page[i];
printf("page fault");
}
else
printf("\t");
printf("
\t%d:\t",page[i]);
for(j=1;j<=nf;j++)
printf("
%d
",fr[j]);
printf("\n");
if(p%3==0)
p=0;
}
printf("\n total page faults:%d",m+3);
//
getch();
}
int full(int a[],int n)
{
int k;
for(k=1;k<=n;k++)
{
if(a[k]==-1)
return 0;
}
return 1;
}
u[j]=i;
printf("
\t%d:\t",page[i]);
break;
}
}
if(flag==0)
{
fr[p]=page[i];
u[p]=i;
printf("
\t%d:\t",page[i]);
p++;
}
for(j=1;j<=nf;j++)
printf(" %d
",fr[j]);
printf("\n");
}
}
p=0;
for(;i<=psz;i++)
{
flag=0;
for(j=1;j<=nf;j++)
{
if(page[i]==fr[j])
{
u[j]=i;
flag=1;
break;
}
}
if(flag==0)
{
p++;
for(j=1;j<=nf;j++)
t[j]=u[j];
for(j=1;j<=nf;j++)
{
for(k=j+1;k<=nf;k++)
{
if(t[j]>t[k])
{
t1=t[j];
t[j]=t[k];
t[k]=t1;
}
}
}
for(j=1;j<=nf;j++)
{
if(t[1]==u[j])
{
fr[j]=page[i];
u[j]=i;
}
}
printf("page fault\t");
}
else
printf("
\t");
printf("%d:\t",page[i]);
for(j=1;j<=nf;j++)
printf(" %d
",fr[j]);
printf("\n");
}
printf("\ntotal page faults:
// getch();
}
int full(int a[],int n)
{
int k;
for(k=1;k<=n;k++)
{
if(a[k]==-1)
return 0;
}
return 1;
}
%d",p+3);
}
printf("\n\nAvailable resorces is:");
for(j=1;j<=r;j++)
{
printf(" ",avail[j]);
}
printf("\nClaim matrix:\t\tAllocation matrix:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
printf("%d",clm[i][j]);
}
printf("\t\t\t");
for(j=1;j<=r;j++)
{
printf("%d",alloc[i][j]);
}
printf("\n");
}
prc=0;
for(i=1;i<=p;i++)
{
if(comp[i]==0) //if not completed
{
prc=i;
for(j=1;j<=r;j++)
{
if(avail[j])
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\nProcess ",prc,"runs to completion!");
count++;
for(j=1;j<=r;j++)
{
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
}
}
while(count!=p&&prc!=0);
if(count==p)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in unsafe state!!");
//getch();
2:
For process 3:
3 0 2
For process 4:
2 1 1
For process 5:
0 0 2
Enter total no of each resource:
10
5
7
Available resorces is:
Claim matrix:
753
322
902
Allocation matrix:
101
200
302
222
433
211
002
/* fork pro */
include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
main()
{
pid_t pid;
/*fork a child process*/
pid = fork();
if(pid < 0)
{
fprintf(stderr, "fork failed");
exit(-1);
}
else if(pid == 0)
{
execlp("/bin/ls","ls",NULL);
}
else
{
wait(NULL);
printf("child completed");
exit(0);
}
}
/* SJF */
#include<stdio.h>
#include<string.h>
//#include<process.h>
main()
{
char p[10][5],temp[5];
int tot=0,wt[10],pt[10],i,j,n,temp1;
float avg=0;
//clrscr();
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d name:\n",i+1);
scanf("%s",&p[i]);
printf("enter process time");
scanf("%d",&pt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+pt[i-1];
tot=tot+wt[i];
}
avg=(float)tot/n;
printf("p_name\t P_time\t w_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",p[i],pt[i],wt[i]);
printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
//getch();
}
/ * PRIORITY SHEDULING
#include<stdio.h>
*/
#include<string.h>
main()
{
char p[10][5],temp[5];
int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n;
float avgwt;
// clrscr();
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d name:",i+1);
scanf("%s",&p[i]);
printf("enter process time:");
scanf("%d",&pt[i]);
printf("enter priority:");
scanf("%d",&pr[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pr[i]>pr[j])
{
for(i=0;i<n;i++)
{
temp1=pr[i];
pr[i]=pr[j];
pr[j]=temp1;
/* temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;*/
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+pt[i-1];
totwt=totwt+wt[i];
}
avgwt=(float)totwt/n;
printf("p_name\t p_time\t priority\t w_time\n");
for(i=0;i<n;i++)
{
printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]);
}
printf("total waiting time=%d\n avg waiting time=%f",totwt,avgwt);
//getch();
}
/ * FILE ALLOCATION */
include<stdio.h>
//#include<process.h>
#include<string.h>
struct node {
int file_name;
int data;
int is_free;
int size;
struct node* link;
};
struct node* defaultFile()
{
struct node* temp = (struct node*)malloc( sizeof(struct node));
temp->link = NULL;
temp->is_free=0;
temp->data='a';
temp->file_name=99;
return temp;
}
struct node* insert(struct node *rt, int size, int fname)
{
struct node* temp = rt,*tt= rt;
struct node* new_node = defaultFile();
int flag=0,act_size=0;
while( flag==0 && temp ) {
if( sizesize && temp->is_free==0)
{
flag=1;
act_size = temp->size;
temp->size=size;
temp->is_free=1;
temp->file_name = fname;
new_node->link = temp->link;
new_node->size = act_size-temp->size;
temp->link = new_node;
}
temp = temp->link;
}
if( flag==0 )
{
printf("There is not partition available to allocation\n. Kindly do fragment to allocate
memory\n");
}
return rt;
}
struct node* combine(struct node *rt,int fname)
{
struct node *temp=rt,*nt=NULL,*temp1=temp->link;
int fname1=0,fname2=0;
while( temp1 ) {
if( temp->is_free==0 && temp1->is_free==0 )
{
fname1 = temp->file_name;
fname2 = temp1->file_name;
temp->file_name= fname1!=fname ? fname1 : fname2;
temp->size = temp->size+temp1->size;
temp->link = temp1->link;
//delete temp1;
temp1 = temp->link;
}
else
{
temp = temp->link;
temp1 = temp1->link;
}
}
return rt;
}
int printFiles(struct node* rt)
{
struct node *temp = rt;
printf("Files name and size are\n");
while( temp )
{
printf("\t%d\t%d\t\n",temp->file_name, temp->size);
temp = temp->link;
}
}
struct node* deleteFiles(struct node* rt, int fname)
{
struct node *temp = rt,*nt=NULL;
int flag=0;
while( temp && flag==0 )
{
if(temp->file_name==fname)
{
temp->is_free=0;
flag=1;
}
temp = temp->link;
}
if( flag==0 )
{
printf("There doesnt exist any file with that name\n");
}
return combine(rt,fname);
}
int main() {
int flag,no,size,data;
struct node *root;
root = defaultFile();
root->size=1000;
data=100;
flag=no=size=0;
while( flag==0 )
{
printf("Enter no's \n1.insert\n 2.Delete\n 3.Print files \n 4.Exit\n");
scanf("%d",&no);
printf(" no is %d\n",no);
switch(no)
{
case 1:
printf("Enter file size\n");
scanf("%d",&size);
root = insert(root, size, data);
data = data+1;
break;
case 2:
printf("Enter file name to delete\n");
scanf("%d",&size);
root = deleteFiles(root, size);
break;
case 3:
printFiles(root);
break;
case 4:
flag=1;
printf("Quitting from loop\n");
break;
default:
printf("Enter a valud no \n");
break;
}
}
}
/ * FILE INDEX */
include<stdio.h>
//#include
//#include
struct node {
int file_name;
int data;
int is_free;
int size;
int directory;
int cnt;
struct node* link;
struct node* inner_link[30];
};
struct node* defaultFile() {
struct node* temp = (struct node*)malloc( sizeof(struct node) );
temp->link = NULL;
temp->is_free=0;
temp->data='a';
temp->file_name=99;
temp->directory = 1;
return temp;
}
struct node* insert(struct node *rt, int size, int fname) {
struct node* temp = rt;
struct node* first=NULL,*last=NULL,*tt=NULL;
int flag=0,act_size=0, inner_fname=100,cnt=0;
last=first;
act_size = size;
if ( size>temp->size ) {
printf("There is not enough space on the disk to write that file\n");
return rt;
}
while( temp->link ) {
temp = temp->link;
}
first = defaultFile();
while( act_size>0 ) {
tt = defaultFile();
if (act_size>50)
tt->size = 50;
else
tt->size = act_size;
tt->file_name = inner_fname;
first->inner_link[cnt] = tt;
tt->directory = 0;
tt->is_free = 0;
act_size -= 50;
inner_fname += 1;
cnt += 1;
}
temp->link = first;
first->is_free = 1;
first->cnt = cnt;
first->size = size;
first->file_name = fname;
act_size = rt->size;
rt->size = act_size-size;
return rt;
}
void printFiles(struct node* rt) {
struct node *temp = rt, *tt;
int first=0,cnt=0;
printf("format is (File name, size)\n");
printf("\t(%d,%d)\n",rt->file_name,rt->size);
while( temp ) {
if ( temp->is_free ) {
printf("\t(%d,%d)\n",temp->file_name,temp->size);
first = 0;
while( cntcnt ) {
tt = temp->inner_link[cnt];
printf("\t\t(%d,%d)\n",tt->file_name, tt->size);
cnt += 1;
}
printf("\n");
}
temp = temp->link;
}
}
struct node* combine(struct node *rt,int fname) {
struct node *temp=rt,*nt=NULL,*temp1=temp->link;
int size=0;
if ( rt->file_name==fname ){
printf("You cannot that file as thats just to show that that much amount of space is left in
the disk\n");
return rt;
}
while( temp1 ) {
if (temp1->is_free==0 && temp1->file_name==fname ) {
size = temp1->size;
temp->link = temp1->link;
temp1 = temp->link;
}
else {
temp = temp1;
temp1 = temp1->link;
}
}
rt->size += size;
return rt;
}
struct node* deleteFiles(struct node* rt, int fname) {
struct node *temp = rt,*nt=NULL;
int flag=0;
while( temp && flag==0 ) {
if (temp->file_name==fname) {
temp->is_free=0;
flag=1;
}
temp = temp->link;
}
if( flag==0 ){
printf("There doesnt exist any file with that name\n");
}
return combine(rt,fname);
}
int main() {
int flag,no,size,data;
struct node *root;
root = defaultFile();
root->size=1000;
data=100;
flag=no=size=0;
while( flag==0 ) {
printf("Enter no's \n1.insert\n 2.Delete\n 3.Print files \n 4.Exit\n");
scanf("%d",&no);
printf(" no is %d\n",no);
switch(no) {
case 1:
printf("Enter file size\n");
scanf("%d",&size);
root = insert(root, size, data);
data = data+1;
break;
case 2:
printf("Enter file name to delete\n");
scanf("%d",&size);
root = deleteFiles(root, size);
break;
case 3:
printFiles(root);
break;
case 4:
flag=1;
printf("Quitting from loop\n");
break;
default:
printf("Enter a valud no \n");
break;
}
}
}
/ * FACT.C */
#include<stdio.h>
int fact(int);
main()
{
int n,result;
printf("enter ano.\n");
scanf("%d",&n);
result=factorial(n);
printf("the fatorial of %d is %d\n",n,result);
}
int factorial(int n)
{
int fact;
if(n==1)
return 1;
else
{
fact=n*factorial(n-1);
return fact;
}
}