// 1.OPTIMAL PAGE REPLACEMENT ALGORITHM #include<stdio.h>
//#include<conio.h>
main()
{
int fr[5],i,j,k,t[5],p=1,flag=0,page[25],psz,nf,t1,u[5];
// clrscr();
printf("enter the number of frames:");
scanf("%d",&nf);
printf("\n enter the page size");
scanf("%d",&psz);
printf("\nenter the page sequence:");
for(i=1;i<=psz;i++)
scanf("%d",&page[i]);
for(i=1;i<=nf;i++)
fr[i]=-1;
for(i=1;i<=psz;i++)
{
if(full(fr,nf)==1)
break;
else
{
flag=0;
for(j=1;j<=nf;j++)
{
if(page[i]==fr[j])
{
flag=1;
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: %d",p+3);
// getch();
}
int full(int a[],int n)
{
int k;
for(k=1;k<=n;k++)
{
if(a[k]==-1)
return 0;
}
return 1;}
//2.PROGRAM ON FIRST IN FIRST OUT#include<stdio.h>
//#include<conio.h>
int full(int [],int);
main()
{
int fr[5],i,j,k,p=1,flag=0,psz,page[25],nf,m=0;
//clrscr();
printf("enter the number of frames:");
scanf("%d",&nf);
printf("enter the page size:");
scanf("%d",&psz);
printf("enter the page sequence:");
for(i=1;i<=psz;i++)
scanf("%d",&page[i]);
for(i=1;i<=nf;i++)
fr[i]=-1;
for(i=1;i<=psz;i++)
{
if(full(fr,nf)==1)
break;
else
{
flag=0;
for(j=1;j<=nf;j++)
{
if(page[i]==fr[j])
{
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;
}
//3.PROGRAM ON LEAST RECENTLY USED ALGORITHM#include<stdio.h>
//#include<conio.h>
main()
{
int fr[5],i,j,k,t[5],p=1,flag=0,page[25],nf,t1,u[5],psz;
// clrscr();
printf("enter the number of frames:");
scanf("%d",&nf);
printf("\n enter the page size");
scanf("%d",&psz);
printf("\nenter the page sequence:");
for(i=1;i<=psz;i++)
scanf("%d",&page[i]);
for(i=1;i<=nf;i++)
fr[i]=-1;
for(i=1;i<=psz;i++)
{
if(full(fr,nf)==1)
break;
else
{
flag=0;
for(j=1;j<=nf;j++)
{
if(page[i]==fr[j])
{
flag=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: %d",p+3);
// getch();
}
int full(int a[],int n)
{
int k;
for(k=1;k<=n;k++)
{
if(a[k]==-1)
return 0;
}
return 1;
}
//4.PROGRAM ON DEADLOCK AVOIDENCE USING BANKER ALGORITHM#include<stdio.h> main(){int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];int first,p,r,i,j,prc,count,t;//clrscr();count=0;for(i=1;i<=7;i++)comp[i]=0;printf("Enter the no of processes:\n");scanf("%d",&p);printf("Enter the no of resources:\n");scanf("%d",&r);printf("Enter the claim for each process:");for(i=1;i<=p;i++){printf("\nFor process %d",i);for(j=1;j<=r;j++){scanf("%d",&clm[i][j]);}}printf("Enter the allocation for each process:\n");for(i=1;i<=p;i++){printf("\nFor process ",i);for(j=1;j<=r;j++){scanf("%d",&alloc[i][j]);}}printf("Enter total no of each resource:");for(j=1;j<=r;j++)scanf("%d",&rsrc[j]);for(j=1;j<=r;j++){int total=0;avail[j]=0;for(i=1;i<=p;i++){total=total+alloc[i][j];}avail[j]=rsrc[j]-total;}do{for(i=1;i<=p;i++){for(j=1;j<=r;j++){req[i][j]=clm[i][j]-alloc[i][j];}
}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!!");elseprintf("\nThe system is in unsafe state!!");//getch();
}
Enter the no of processes:5Enter the no of resources:3Enter the claim for each process:For process 1 :753For process 2:322For process 3:902For process 4:222For process 5:433Enter the allocation for each process:
For process 1:101
For process 2:2 0 0
For process 3:3 0 2
For process 4:2 1 1
For process 5:0 0 2Enter total no of each resource:1057
Available resorces is:Claim matrix: Allocation matrix:753 101322 200902 302
222 211433 002
The system is in an safe state!!//5.PROGRAM ON ROUNDROBIN ALGORITHM
#include<stdio.h>#include<conio.h>main(){ int st[10],bt[10],wt[10],tot[10],n,tq; int i,count=0,swt=0,stat=0,temp,sq=0; float awt=0.0,atot=0.0; clrscr(); printf("enter no of processes"); scanf("%d",&n); printf("enter burst time \n"); for(i=0;i<n;i++) { scanf("%d",&bt[i]);st[i]=bt[i]; } printf("enter time quantum"); scanf("%d",&tq); while(i) { for(i=0,count=0;i<n;i++) { temp=tq; if(st[i]==0) { count++; continue; } if(st[i]>tq) st[i]=st[i]-tq; else if(st[i]>=0) { temp=st[i]; st[i]=0; } sq=sq+temp; tot[i]=sq; } if(n==count) break; } for(i=0;i<n;i++) { wt[i]=tot[i]-bt[i]; swt=swt+wt[i]; stat=stat+tot[i]; } awt=(float)swt/n; atot=(float)stat/n;
printf("\nprocessno \tburst time "); printf("\twait time \tturn around time"); for(i=0;i<n;i++) printf("\n%d\t %d \t %d \t %d",i+1,bt[i],wt[i],tot[i]); printf("\navg wait time is %f", awt); printf("\navg turn around time %f",atot); getch(); }
. /*shell script on fibonacci*/echo enter numberread ai=0j=1print " %d \+ " $iwhile [ $j -le $a ];doprintf " %d \+ " $jt=`expr $i + $j`i=$jj=$tdone
2. /* shell script on multiplication*/echo enter n valueread nfor i in 1 2 3 4 5 6 7 8 9 10;doecho "$n * $i= `expr $n \* $i`"done
3. /shell script on calculater*/echo enter a bread a becho "enter operator(+,-,*,/)"read chcase $ch in+)echo a+b=`expr $a + $b`;;-)echo a-b=`expr $a - $b`;;\*)echo a*b=`expr $a \* $b`;;/)echo a/b=`expr $a / $b`;;*)echo invalid operator;;Esac
4. /* shell script on lines,words*/echo enter file nameread fnecho "no of words=`wc -w $fn`"echo "no of lines=`wc -l $fn`"
5. /* shell script on factorial*/echo enter n valueread ni=1fact=1while [ $i -le $n ];dofact=`expr $fact \* $i`i=`expr $i + 1`doneecho $fact
6. //shell script on change directoryecho "enter dir name"read dnamecd $dnamefor i in *;doecho $idone
/* 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;elsett->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;}}