Friday, 29 June 2012

Write the simulation program for demand paging and show the page

Loading

/*
Write the simulation program for demand paging and show the page
scheduling and total number of page faults according to LRU page
replacement algorithm. Assume memory of 'n' frames.
Request Page Numbers:
5,8,10,14,10,9,5,10,8,5,11,10,9,5,10
*/

#include<stdio.h>
#include<conio.h>

int RefString[20],PT[10],nof,nor;

void Accept()
{
int i;
printf("Enter Reference String: \n");
for(i=0;i<nor;i++)
{
printf("[%d]=",i);
scanf("%d",&RefString[i]);
}
}

int Search(int s)
{
int i;
for(i=0;i<nof; i++)
if(PT[i]==s)
return(i);
return(-1);
}

int GetLRU(int e)
{
int i,j,Pos=99,Posi,k;
for(i=0;i<nof;i++)
{
for(j=e-1;j>=0;j--)
{
if(PT[i]==RefString[j])
{
if(j<Pos)
{
Pos=j;
Posi=i;
}
break;
}
}
}
return(Posi);
}

void LRU()
{
int i,j,k,Faults=0;

for(k=0,i=0; k<nof && i<nor; i++)
{
gotoxy(3*i+1,2);
printf("%2d",RefString[i]);
if(Search(RefString[i])==-1)
{
PT[k]=RefString[i];
for(j=0;j<3;j++)
{
gotoxy(3*i+1,j+3);
if(PT[j])
printf("%2d",PT[j]);
}
Faults++;
k++;
}
}
while(i<nor)
{
gotoxy(3*i+1,2);
printf("%2d",RefString[i]);
if(Search(RefString[i])==-1)
{
k = GetLRU(i);
PT[k]=RefString[i];
for(j=0;j<nof;j++)
{
gotoxy(3*i+1,j+3);
printf("%2d",PT[j]);
}
Faults++;
}
i++;
}
gotoxy(10,24);
printf("Total Page Faults: %d",Faults);
getch();
}

void main()
{
clrscr();
printf("Enter Length of reference string: ");
scanf("%d",&nor);
printf("Enter No.of Frames: ");
scanf("%d",&nof);
clrscr();
Accept();
clrscr();
LRU();
}

Write a program to implement following Unix commands (any two options)

Loading

/*
Write a program to implement following Unix commands (any two options)
1) wc –c <filename> Displays no of characters for the given file.
2) wc –w <filename> Displays no of words for the given file.
3) wc –l <filename> Displays no of  line for the given file.

*/
#include<stdio.h>
#include<stdlib.h>
char *buff,*t1,*t2,*t3,ch;
FILE *fp;
void count(char *t2,char *t3)
{
  int charcount=0,wordcount=0,linecount=0;
  if((fp=fopen(t3,"r"))==NULL)
printf("File not found");
  else
  {
while(1)
{
     ch=fgetc(fp);
printf("\nch=%c",ch);
if((ch==' ')||(ch=='\n')||(ch=='\t')||(ch==EOF))
  wordcount++;

if((ch=='\n')||(ch==EOF))
  linecount++;
if(ch==EOF)
break;
charcount++;
}

  fclose(fp);
  if(strcmp(t2,"-c")==0)
printf("The total no. of characters :%d\n",charcount);
  else if(strcmp(t2,"-w")==0)
printf("The total no. of words :%d\n",wordcount);
  else if(strcmp(t2,"-l")==0)
printf("The total no. of lines :%d\n",linecount);
  else
printf("Command not found");
 }

}
main()
{
   while(1)
   {
printf("myshell$");
fflush(stdin);
t1=(char *)malloc(80);
t2=(char *)malloc(80);
t3=(char *)malloc(80);

buff=(char *)malloc(80);
fgets(buff,80,stdin);
sscanf(buff,"%s %s %s",t1,t2,t3);
if(strcmp(t1,"exit")==0)
exit(0);
else if(strcmp(t1,"wc")==0)
count(t2,t3);
   }
}

Write the simulation program for demand paging and show the page


/*
Write the simulation program for demand paging and show the page
scheduling and total number of page faults according to FIFO page
replacement algorithm. Assume memory of 'n' frames.
Request Page Numbers:
*/

#include<stdio.h>
#include<conio.h>

int RefString[20],PT[10],nof,nor;

void Accept()
{
int i;
printf("Enter Reference String: \n");
for(i=0;i<nor;i++)
{
printf("[%d]=",i);
scanf("%d",&RefString[i]);
}
}

int Search(int s)
{
int i;
for(i=0;i<nof; i++)
if(PT[i]==s)
return(i);
return(-1);
}

void FIFO()
{
int i,j,k=0,Faults=0;
for(i=0;i<nor;i++)
{
gotoxy(3*i+1,2);
printf("%2d",RefString[i]);
if(Search(RefString[i])==-1)
{
PT[k]=RefString[i];
for(j=0;j<nof;j++)
{
gotoxy(3*i+1,j+4);
if(PT[j])
{
printf("%2d",PT[j]);
}
}
Faults++;
k=(k+1)%nof;
}
}
gotoxy(10,18);
printf("Total Page Faults: %d",Faults);
getch();
}

void main()
{
clrscr();
printf("Enter Length of reference string: ");
scanf("%d",&nor);
printf("Enter No.of Frames: ");
scanf("%d",&nof);
clrscr();
Accept();
clrscr();
FIFO();
}

Write the simulation program for preemptive scheduling algorithm using Priority.


/* Write the simulation program for preemptive scheduling algorithm using Priority. The arrival time, first CPU bursts and process priority of different jobs should be input to the system. The output should give the Gantt chart and Turnaround Time for each process and average times.*/
#include<stdio.h>

struct Input
{
char pname[10];
int  bt,at,ct,tbt,p;
}tab[5];

struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];

int finish,time,n,k,prev,q;

void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
printf("Enter priority: ");
scanf("%d",&tab[i].p);

tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;

printf("\nProcess\tBT\tAT\tpriority");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at,tab[i].p);
getch();
}

void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
int getmin(int t)
{
int i,mini,min=99;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0 && tab[i].p<min)
{
min = tab[i].p;
mini = i;
}
return mini;
}



void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
 tab[i].at,
 tab[i].bt,
 tab[i].ct,
 tab[i].ct-tab[i].at,
 tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\nAverage TAT = %f",AvgTAT);
printf("\nAverage WT = %f",AvgWT);
getch();
}

int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}

void processinput()
{
int i=0,j;
finish = k = 0;
while(finish!=n)
{
if(arrived(time))
{
     i=getmin(time);
time++;
tab[i].tbt--;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
tab[i].ct=time;
if(tab[i].tbt==0)
{
finish++;

}


}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}

}
}

void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}

void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\nData after sorting according to arrival time-: ");
printinput();
processinput();

printoutput();

ganttchart();


}

Write a program to implement following Unix commands (any two options)


/* Write a program to implement following Unix commands (any two options)
1)mv <source file> <target file> It moves source file into target file
2) cp <source file> <target file>  IT copies source file into target file
3) rm <filename> It deletes the file */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
char *buff,*t1,*t2,*t3,ch;
FILE *fp1,*fp2;
main()
{
   int n;
   clrscr();
   while(1)
   {
printf("myshell$");
fflush(stdin);
t1=(char *)malloc(80);
t2=(char *)malloc(80);
t3=(char *)malloc(80);

buff=(char *)malloc(80);
fgets(buff,80,stdin);
n=sscanf(buff,"%s %s %s",t1,t2,t3);
if(strcmp(t1,"exit")==0)
exit(0);

if(strcmp(t1,"mv")==0)
{
  if(rename(t2,t3)==-1)
   perror(" ");
  else
  {
   rename(t2,t3);
   printf("\n%s file is moved successfully into %s file",t2,t3);
  }
}
else if(strcmp(t1,"rm")==0)
{
 if(remove(t2)==-1)
   perror(" ");
 else
  {
   remove(t2);
   printf("\n%s File is deleted",t2);
  }
}

else if(strcmp(t1,"cp")==0)
{
 fp1=fopen(t2,"r");
 fp2=fopen(t3,"w");

 if(fp1==NULL)
      printf("\nError in opening file %s",t2);
 while(1)
  {
    ch=fgetc(fp1);
    if(ch==EOF)
break;
    fputc(ch,fp2);
  }
  fcloseall();
}

   }
}

.Write the simulation program for demand paging and show the page

.Write the simulation program for demand paging and show the page scheduling and total number of page faults according to MFU page replacement algorithm. Assume the memory of ‘n’ frames.

#include<stdio.h>
int RefString[20],PT[10],nof,nor;
void Accept()
{
int i;
printf("\n enter the reference sting:");
for(i=0;i<nor;i++)
{
printf("[%d]=",i);
scanf("%d",&RefString[i]);
}
}
int search(int s)
{
int i;
for(i=0;i<nof;i++)
if(PT[i]==s)
return(i);
return(-1);
}
int GETMFU(int e,int s)
{
int i,j,cnt1=0,cnt2,posi,pos,pos1;
i=s;
do
{
cnt2=0;
for(j=e-1;j>=0;j--)
{
if(PT[i]==RefString[j])
{
cnt2++;
                                  pos=j;
}
}

                     for(j=e-1;j>=0;j--)
                {
                        if(PT[i]==RefString[j])
                        {
                             
                                  pos=j;
                                   break;
                        }
                }

                       //  printf("\nr=%d",RefString[i]);
if(cnt2>cnt1)
{
cnt1=cnt2;
posi=i;
                        pos1=pos;
}
                if(cnt2==cnt1 && pos1>pos)
                 {
                      cnt1=cnt2;
                      posi=i;
                 
}

i=(i+1)%nof;
}
while(i!=s);
return(posi);
}
void MFU()
{
int i,j,k,faults=0;
for(k=0,i=0;k<nof && i<nor;i++)
{
printf("%2d",RefString[i]);
if(search(RefString[i])==-1)
{
PT[k]=RefString[i];
for(j=0;j<nof;j++)
{
if(PT[j])
printf("%2d\t",PT[j]);
}
faults++;
k++;
}
printf("\n");
}
k=0;
while(i<nor)
{
printf("%2d",RefString[i]);
if(search(RefString[i])==-1)
{
k=GETMFU(i,k);
PT[k]=RefString[i];
k=(k+1)%nof;
for(j=0;j<nof;j++)
{
if(PT[j])
printf("%2d\t",PT[j]);
}
faults++;

}                      
i++;
printf("\n");
}
printf("total page faults:%d",faults);
}
int main()
{
printf("\n enter the length of Reference string:");
scanf("%d",&nor);
printf("\n enter the number of frames:");
scanf("%d",&nof);
Accept();
MFU();
}


Consider a system with ‘n’ processes and ‘m’ resource types. Accept number of instances for every resource type

Consider a system with ‘n’ processes and ‘m’ resource types. Accept number of instances for every resource type. For each process accept the allocation and maximum requirement matrices. Write a program to check if the system is in deadlock or not.


#include<stdio.h>
int A[10][10],M[10][10],N[10][10],Av[10],Safe[10],Finish[10],R[10][10],nor,nop;
void AcceptData(int X[][10])
{
int i,j;
for(i=0;i<nop;i++)
{
printf("P%d\n",i);
for(j=0;j<nor;j++)
{
printf("%c: ",65+j);
scanf("%d",&X[i][j]);
}
}
}
void AcceptAvailability()
{
int i;
for(i=0;i<nor;i++)
{
printf("%c: ",65+i);
scanf("%d",&Av[i]);
}
}
void AcceptRequest(int R[][10])
{
int i;
for(i=0;i<nor;i++)
{
printf("%c: ",65+i);
scanf("%d",&R[i]);
}
}
void DisplayData()
{
int i,j;
printf("\n\tAllocation\t\tMax\t\tNeed\n");
printf("\t");
for(i=0;i<3;i++)
{
for(j=0;j<nor;j++)
printf("%4c",65+j);
printf("\t");
}
for(i=0;i<nop;i++)
{
printf("\nP%d\t",i);
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",M[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
}
printf("\nAvailable\n");
for(j=0;j<nor;j++)
printf("%4d",Av[j]);
}
void CalcNeed()
{
int i,j;
for(i=0;i<nop;i++)
for(j=0;j<nor;j++)
N[i][j] = M[i][j] - A[i][j];
}
int checkNeed(int pno)
{
int i;
for(i=0;i<nor;i++)
if(N[pno][i] > Av[i])
return(0);
return(1);
}
int checkRequest()
{
int i;
for(i=0;i<nor;i++)
if(R[0][i] > Av[i])
return(0);
return(1);
}
void Banker()
{
int i=0,j=0,k=0,flag=0;
while(flag<2)
{
if(!Finish[i])
{
printf("\nNeed%d(",i);
for(j=0;j<nor;j++)
printf("%d,",N[i][j]);
if(!checkNeed(i))
{
printf("\b)>Available(");
for(j=0;j<nor;j++)
printf("%d,",Av[j]);
printf("\b)");
printf("\nNeed Cannot be satisfied, consider next process");
}
else
{
printf("\b)<=Available(");
for(j=0;j<nor;j++)
printf("%d,",Av[j]);
printf("\b)");
printf("\nNeed can be satisfied, so allocate required resources");
printf("\nAllocation%d\tAvailable\tNeed%d\n",i,i);
for(j=0;j<nor;j++)
{
A[i][j] = M[i][j];
Av[j] -= N[i][j];
N[i][j] = 0;
}
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",Av[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
printf("\nAfter P%d terminates it will release all its resources",i);
printf("\nAllocation%d\tAvailable\tNeed%d\n",i,i);
for(j=0;j<nor;j++)
{
Av[j] += A[i][j];
A[i][j] = 0;
}
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",Av[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
Safe[k++] = i;
Finish[i]=1;
}
}
if((i+1)%nop == 0)
flag++;
i=(i+1)%nop;
}
if(k==nop)
{
printf("\nSystem is in safe state...");
printf("\nSafe Sequence: ");
for(i=0;i<k;i++)
printf("P%d->",Safe[i]);
printf("\b\b  ");
printf("\nSystem is not in deadlock");
}
else
{
printf("\nSystem is in not safe state...");
printf("\nSystem is in deadlock");
}
}

void main()
{
printf("\nEnter No.of Processes & No.of Resources: ");
scanf("%d %d",&nop,&nor);
printf("Enter Allocation\n");
AcceptData(A);
printf("Enter Max Requirement\n");
AcceptData(M);
printf("Enter Availability\n");
AcceptAvailability();
CalcNeed();
DisplayData();
Banker();
printf("Enter the new Request from process");
AcceptRequest(R);
if((checkRequest())==1)
printf("\nNew Request for the process can be granted");
else
printf("\nNew Request for the process canot be granted");
}

Non-preemptive scheduling algorithm using SJF.


/*
Non-preemptive scheduling algorithm using SJF.
*/

#include<stdio.h>

struct Input
{
char pname[10];
int  bt,at,ct,tbt;
}tab[5];

struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];

int finish,time,n,k,prev;

void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
tab[i].tbt = tab[i].bt;
}
}

void printinput()
{
int i;

printf("\n\n\nProcess\tBT\tAT");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at);
getch();
}

void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}


void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
clrscr();
printf("\n      *******Final  Table*********");
printf("\n\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
 tab[i].at,
 tab[i].bt,
 tab[i].ct,
 tab[i].ct-tab[i].at,
 tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\n\nAverage TAT = %f",AvgTAT);
printf("\n\nAverage WT = %f",AvgWT);
getch();
}

int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}

int getmin(int t)
{
int i,mini,min=99;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0 && tab[i].tbt<min)
{
min = tab[i].tbt;
mini = i;
}
return mini;
}

void processinput()
{
int i,j;
finish=k=0;
while(finish!=n)
{
if(arrived(time))
{
i = getmin(time);
for(j=0;j<tab[i].bt;j++)
{
 time++;
 tab[i].tbt--;
 tab[i].ct=time;
 printinput();
 seq[k].start=prev;
 seq[k].end = time;
 strcpy(seq[k++].pname,tab[i].pname);
 prev = time;
 if(tab[i].tbt==0)
 {
finish++;
break;
 }
}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
}
}


void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
printf("\n          ******Gantt Chart*******");
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}

void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\n\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();

}

Consider a system with 'n' processes and 'm' resource type.


/*
Consider a system with 'n' processes and 'm' resource type. Accept number of
instances for every resource type. For each process accept the allocation
and maximum requirement matrices. Write a program to check if the given
request of a process can be granted immediately or not.
*/

#include<stdio.h>
int A[10][10],M[10][10],N[10][10],Av[10],Safe[10],Finish[10],R[10][10],nor,nop;
void AcceptData(int X[][10])
{
int i,j;
for(i=0;i<nop;i++)
{
printf("P%d\n",i);
for(j=0;j<nor;j++)
{
printf("%c: ",65+j);
scanf("%d",&X[i][j]);
}
}
}
void AcceptAvailability()
{
int i;
for(i=0;i<nor;i++)
{
printf("%c: ",65+i);
scanf("%d",&Av[i]);
}
}
void AcceptRequest(int R[][10])
{
int i;
for(i=0;i<nor;i++)
{
printf("%c: ",65+i);
scanf("%d",&R[i]);
}
}
void DisplayData()
{
int i,j;
printf("\n\tAllocation\t\tMax\t\tNeed\n");
printf("\t");
for(i=0;i<3;i++)
{
for(j=0;j<nor;j++)
printf("%4c",65+j);
printf("\t");
}
for(i=0;i<nop;i++)
{
printf("\nP%d\t",i);
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",M[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
}
printf("\nAvailable\n");
for(j=0;j<nor;j++)
printf("%4d",Av[j]);
}
void CalcNeed()
{
int i,j;
for(i=0;i<nop;i++)
for(j=0;j<nor;j++)
N[i][j] = M[i][j] - A[i][j];
}
int checkNeed(int pno)
{
int i;
for(i=0;i<nor;i++)
if(N[pno][i] > Av[i])
return(0);
return(1);
}
int checkRequest()
{
int i;
for(i=0;i<nor;i++)
if(R[0][i] > Av[i])
return(0);
return(1);
}
void Banker()
{
int i=0,j=0,k=0,flag=0;
while(flag<2)
{
if(!Finish[i])
{
printf("\nNeed%d(",i);
for(j=0;j<nor;j++)
printf("%d,",N[i][j]);
if(!checkNeed(i))
{
printf("\b)>Available(");
for(j=0;j<nor;j++)
printf("%d,",Av[j]);
printf("\b)");
printf("\nNeed Cannot be satisfied, consider next process");
}
else
{
printf("\b)<=Available(");
for(j=0;j<nor;j++)
printf("%d,",Av[j]);
printf("\b)");
printf("\nNeed can be satisfied, so allocate required resources");
printf("\nAllocation%d\tAvailable\tNeed%d\n",i,i);
for(j=0;j<nor;j++)
{
A[i][j] = M[i][j];
Av[j] -= N[i][j];
N[i][j] = 0;
}
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",Av[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
printf("\nAfter P%d terminates it will release all its resources",i);
printf("\nAllocation%d\tAvailable\tNeed%d\n",i,i);
for(j=0;j<nor;j++)
{
Av[j] += A[i][j];
A[i][j] = 0;
}
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",Av[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
Safe[k++] = i;
Finish[i]=1;
}
}
if((i+1)%nop == 0)
flag++;
i=(i+1)%nop;
}
if(k==nop)
{
printf("\nSystem is in safe state...");
printf("\nSafe Sequence: ");
for(i=0;i<k;i++)
printf("P%d->",Safe[i]);
printf("\b\b  ");
}
else
{
printf("\nSystem is in not safe state...");
}
}

void main()
{
printf("\nEnter No.of Processes & No.of Resources: ");
scanf("%d %d",&nop,&nor);
printf("Enter Allocation\n");
AcceptData(A);
printf("Enter Max Requirement\n");
AcceptData(M);
printf("Enter Availability\n");
AcceptAvailability();
CalcNeed();
DisplayData();
Banker();
printf("Enter the new Request from process");
AcceptRequest(R);
if((checkRequest())==1)
printf("\nNew Request for the process can be granted");
else
printf("\nNew Request for the process canot be granted");
}

write the simulation program for preemptive scheduling algorithm using SJF.


/*
Write the simulation program for preemptive scheduling algorithm using
SJF.
*/

#include<stdio.h>

struct Input
{
char pname[10];
int  bt,at,ct,tbt;
}tab[5];

struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];

int finish,time,n,k,prev;

void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
tab[i].tbt = tab[i].bt;
}
}

void printinput()
{
int i;

printf("\n\n\nProcess\tBT\tAT");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at);
getch();
}

void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}


void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
clrscr();
printf("\n      *******Final  Table*********");
printf("\n\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
 tab[i].at,
 tab[i].bt,
 tab[i].ct,
 tab[i].ct-tab[i].at,
 tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\n\nAverage TAT = %f",AvgTAT);
printf("\n\nAverage WT = %f",AvgWT);
getch();
}

int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}

int getmin(int t)
{
int i,mini,min=99;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0 && tab[i].tbt<min)
{
min = tab[i].tbt;
mini = i;
}
return mini;
}

void processinput()
{
int i,j;
finish=k=0;
while(finish!=n)
{
if(arrived(time))
{
i = getmin(time);
 time++;
 tab[i].tbt--;
 tab[i].ct=time;
 printinput();
 seq[k].start=prev;
 seq[k].end = time;
 strcpy(seq[k++].pname,tab[i].pname);
 prev = time;
 if(tab[i].tbt==0)
 {
finish++;

 }

}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
}
}


void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
printf("\n          ******Gantt Chart*******");
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}

void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\n\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();

}