Wednesday, April 13, 2011

C implementation of Midpoint Ellipse Drawing Algorithm

  No comments
April 13, 2011

Hi friends ,this time I am posting a program for Midpoint Ellipse Drawing Algorithm in C implemented during my Computer Graphics studies.Note that the output or screenshot shown is REAL and taken using a Virtual Machine.Program written is very simple and can be understood easily.Anyways do leave a comment to us if you got any queries .

PROGRAM CODE
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int gdriver=DETECT, gmode,i,x,y;
void main()
{
long d1,d2,rx,ry,rxsq,rysq,trxsq,trysq,dx,dy;
printf("\nPlease enter the x radius of the ellipse\n");
scanf("%ld",&rx);
printf("Please enter the y radius of the ellipse\n");
scanf("%ld",&ry);
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
int xmid,ymid;
xmid=getmaxx()/2;
ymid=getmaxy()/2;
line(xmid+rx+10,ymid-ry,xmid+rx+10,ymid);
line(xmid,ymid-ry-10,xmid+rx,ymid-ry-10);
char ch[20];
sprintf(ch,"%c",17);
outtextxy(xmid,ymid-ry-13,ch);
sprintf(ch,"%c",16);
outtextxy(xmid+rx-6,ymid-ry-13,ch);
sprintf(ch,"%c",30);
outtextxy(xmid+rx+7,ymid-ry,ch);
sprintf(ch,"%c",31);
outtextxy(xmid+rx+7,ymid-6,ch);
sprintf(ch,"%d",rx);
outtextxy(xmid+(rx/2)-5,ymid-ry-20,ch);
sprintf(ch,"%d",ry);
outtextxy(xmid+rx+20,ymid-(ry/2)-1,ch);
outtextxy(getmaxx()-450,getmaxy()-20,"MidPoint Ellipse Algorithm :Program By 1st Kinecteers");
rectangle(getmaxx()-455,getmaxy()-25,getmaxx()-30,getmaxy()-7);
rxsq=rx*rx;
rysq=ry*ry;
trxsq=2*rxsq;
trysq=2*rysq;
x=0;
y=ry;
d1=rysq-rxsq*ry+(0.25*rxsq);
dx=trysq*x;
dy=trxsq*y;
do
{
putpixel(xmid+x,ymid+y,15);
putpixel(xmid-x,ymid-y,15);
putpixel(xmid+x,ymid-y,15);
putpixel(xmid-x,ymid+y,15);
if(d1<0){
x=x+1;
y=y;
dx=dx+trysq;
d1=d1+dx+rysq;
}
else{
x=x+1;
y=y-1;
dx=dx+trysq;
dy=dy-trxsq;
d1=d1+dx-dy+rysq;
}}
while(dx<dy);
d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
do
{
putpixel(xmid+x,ymid+y,15);
putpixel(xmid-x,ymid-y,15);
putpixel(xmid+x,ymid-y,15);
putpixel(xmid-x,ymid+y,15);
if(d2>0){
x=x;
y=y-1;
dy=dy-trxsq;
d2=d2-dy+rxsq;
}
else{
x=x+1;
y=y-1;
dy=dy-trxsq;
dx=dx+trysq;
d2=d2+dx-dy+rxsq;
}}
while(y>0);
getch();
}
OUTPUT:


Read More

Two Pass Assembler

  No comments
April 13, 2011


This is a program for two pass assembler. This program uses static input source program. Shortage of time did not allow us to go for dynamic input. Dynamic program will be published after some time. The static input source program is in code[][] array. Field 1: Label, Field 2: Opcode, Field 3: Operand1, Field 4: Operand2. Please do comment for any queries.

Two-Pass Assembler

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char *ptr;


char *code[9][4]={
{"","START","0",""},
{"","USING","*","14"},
{"","L","1","FIVE"},
{"","A","1","FOUR"},
{"","ST","1","TEMP"},
{"FOUR","DC","F","\"4\""},
{"FIVE","DC","F","\"5\""},
{"TEMP","DS","1F",""},
{"","END","",""}
};
char av[2],avail[15]={'N','N','N','N','N','N','N','N','N','N','N','N','N','N','N'};
int i,j,k,count[3],lc[9]={0,0,0,0,0,0,0,0,0},loc=0,f=0;
clrscr();
printf("Input Program\n");
for(i=0;i<=8;i++)
{
for(j=0;j<=3;j++)
{
printf("%s\t",code[i][j]);
}
printf("\n");
}
printf("PASS 1 TABLE:\n---------------------------------------\n");
printf("LOC CNT\tLABEL\tOPCODE\tOP1\tOP2\n");
printf("---------------------------------------\n");
for(i=0;i<=8;i++)
{
if((strcmp(code[i][1],"START")!=0)&&(strcmp(code[i][1],"USING")!=0)&&(strcmp(code[i][1],"L")!=0))
lc[i]=lc[i-1]+4;
printf("%d\t",lc[i]);
if(strchr(code[i][3],'\"'))
printf("%s\t%s\t%s\t%s\n",code[i][0],code[i][1],code[i][2],code[i][3]);
else
printf("%s\t%s\t%s\n",code[i][0],code[i][1],code[i][2]);
}
printf("--------------------------------------");
getch();
printf("\n\nSYMBOL TABLE:\n----------------------------------------------------\n");
printf("SYMBOL\t\tVALUE\t\tLENGTH\t\tR/A");
printf("\n----------------------------------------------------\n");

for(i=0;i<9;i++)
{
/*if(strcmp(code[i][1],"START")==0)
{
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R');
}
else*/ if(strcmp(code[i][0],"")!=0)
{
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R');
loc=4+loc;
}
else if(strcmp(code[i][1],"USING")==0){}
else
{loc=4+loc;}
}
printf("----------------------------------------------------");
getch();
printf("\nLITERAL TABLE :\n----------------------------------------------------");
printf("\nLIT NAME\tVALUE\t\tLENGTH\t\tR/A\n");
printf("----------------------------------------------------\n");
for(i=0;i<=8;i++)
{
ptr=strchr(code[i][3],'\"');
if(ptr)
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][3],lc[i],4,'R');
}
printf("----------------------------------------------------");
getch();
clrscr();
printf("\n\nBASE TABLE:\n----------------------------------------\n");
printf("REG NO\t\tAVAILIBILITY\t");
printf("\n----------------------------------------\n");
for(j=0;j<=8;j++)
{
if(strcmp(code[j][1],"USING")!=0)
{}
else
{
strcpy(av,code[j][3]);
}
}
count[0]=(int)av[0]-48;
count[1]=(int)av[1]-48;
count[2]=count[0]*10+count[1];
avail[count[2]-1]='Y';

for(k=0;k<16;k++)
{
printf(" %d\t\t %c\n",k,avail[k-1]);
}
printf("----------------------------------------\n");
getch();
clrscr();
printf("PASS2 TABLE:\n-------------------------------------\n");
printf("LOC CNT\tLABEL\tOPCODE\tOP1\tOP2");
printf("\n-------------------------------------\n");
loc=0;
for(i=0;i<=8;i++)
{
f=0;
for(j=i;j<=7;j++)
{
if(strcmp(code[i][3],code[j][0])==0)
{
printf("%d\t%s\t%s\t%s\t%d\n",lc[i],code[i][0],code[i][1],code[i][2],lc[j]);
f=1;
break;
}
}
if(f==0)
printf("%d\t%s\t%s\t%s\t%s\n",lc[i],code[i][0],code[i][1],code[i][2],code[i][3]);
}
printf("-------------------------------------");
getch();
}


Output

Input Program
        START   0
        USING   *       14
        L       1       FIVE
        A       1       FOUR
        ST      1       TEMP
FOUR    DC      F       "4"
FIVE    DC      F       "5"
TEMP    DS      1F
        END
PASS 1 TABLE:
---------------------------------------
LOC CNT LABEL   OPCODE  OP1     OP2
---------------------------------------
0               START   0
0               USING   *
0               L       1
4               A       1
8               ST      1
12      FOUR    DC      F       "4"
16      FIVE    DC      F       "5"
20      TEMP    DS      1F
24              END
--------------------------------------

SYMBOL TABLE:
----------------------------------------------------
SYMBOL          VALUE           LENGTH          R/A
----------------------------------------------------
FOUR            16              4               R
FIVE            20              4               R
TEMP            24              4               R
----------------------------------------------------
LITERAL TABLE :
----------------------------------------------------
LIT NAME        VALUE           LENGTH          R/A
----------------------------------------------------
"4"             12              4               R
"5"             16              4               R
----------------------------------------------------


BASE TABLE:
----------------------------------------
REG NO          AVAILIBILITY
----------------------------------------
 0
 1               N
 2               N
 3               N
 4               N
 5               N
 6               N
 7               N
 8               N
 9               N
 10              N
 11              N
 12              N
 13              N
 14              Y
 15              N
----------------------------------------
PASS2 TABLE:
-------------------------------------
LOC CNT LABEL   OPCODE  OP1     OP2
-------------------------------------
0               START   0       0
0               USING   *       14
0               L       1       16
4               A       1       12
8               ST      1       20
12      FOUR    DC      F       "4"
16      FIVE    DC      F       "5"
20      TEMP    DS      1F
24              END
-------------------------------------

Read More

Monday, April 11, 2011

Java Implementation of Page Replacement Algorithm -FIFO,LRU,OPT

  2 comments
April 11, 2011

import java.io.*;
public class Paging {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int n, page[], f, frames[], faults, count;
double rate;

public Paging() throws IOException{
System.out.println("Enter number of pages");
n=Integer.parseInt(input.readLine());
page=new int[n];
System.out.println("Enter number of page frames");
f=Integer.parseInt(input.readLine());
frames=new int[f];
count=1;
}

void reset()
{
int j;
for(j=0;j<f;j++)
frames[j]=0;
faults=0;
count=1;
}



void read() throws IOException
{
int i;
System.out.println("Enter the pages");
for(i=0;i<n;i++)
{
System.out.println("Enter page number "+(i+1));
page[i]=Integer.parseInt(input.readLine());
}
for(i=0;i<f;i++)
frames[i]=-1;
}

void fifo()
{
int i,j,k=0;
reset();
boolean found=false;
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(page[i]==frames[j])
found=true;
}
if(found==false)
{
frames[k]=page[i];
if(k==f-1)
k=0;
else
k++;
faults++;

}
display();
found=false;
}
System.out.println("Number of page faults = "+faults);
System.out.println("Fault rate = "+(faults*1.0/n));
}

void lru()
{
int i,j,duration[],max;
reset();
duration=new int[f];
boolean found=false;
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
duration[j]++;
for(j=0;j<f;j++)
{
if(page[i]==frames[j])
{
found=true;
duration[j]=0;
}
}
if(found==false)
{
max=0;
for(j=0;j<f;j++)
{
if(duration[j]>duration[max])
max=j;
}
frames[max]=page[i];
duration[max]=0;
faults++;
}

display();
found=false;

}
System.out.println("Number of page faults = "+faults);
System.out.println("Fault rate = "+(faults*1.0/n));
}

void opt()
{
int i,j=0,k,duration[],max,flag[];
reset();
duration=new int[f];
flag=new int[f];
boolean found=false;

for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
flag[j]=0;
duration[j]=n;
}

for(k=i+1;k<n;k++)
{
for(j=0;j<f;j++)
if(page[k]==frames[j]&&flag[j]==0)
{
duration[j]=k;
flag[j]=1;
}
}

for(j=0;j<f;j++)
if(page[i]==frames[j])
found=true;
if(found==false)
{
max=0;
for(j=0;j<f;j++)
{
if(duration[j]>duration[max])
max=j;
if(frames[j]<0)
{
max=j;
break;
}
}
frames[max]=page[i];
faults++;
}

display();
found=false;

}
System.out.println("Number of page faults = "+faults);
System.out.println("Fault rate = "+(faults*1.0/n));
}

void display()
{
int i;
System.out.print("Page frame "+count+" :");
for(i=0;i<f;i++)
{
if(frames[i]==-1)
System.out.print(" -");
else
System.out.print(" "+frames[i]);
}
System.out.print("\n");
count++;
}
public static void main(String[] args) throws IOException{
int option;
String choice;
Paging p=new Paging();
p.read();
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("Menu");
System.out.println("1. FIFO");
System.out.println("2. LRU");
System.out.println("3. OPT");
System.out.println("Enter option");
option=Integer.parseInt(input.readLine());
switch(option)
{
case 1: p.fifo();
break;
case 2: p.lru();
break;
case 3: p.opt();
break;
default: System.out.println("Invalid input");
}
System.out.println("Press Y to continue");
choice=input.readLine();
}
while(choice.compareToIgnoreCase("y")==0);
// TODO Auto-generated method stub

}

}



You would be interested in:

1. Java Implementation of First-Fit, Best-Fit and Worst-Fit
2. Java Implementation of Bankers Algorithm
3. C implementation of CPU Scheduling Algorithm Shortest Remaining Time
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin

Read More

Java Implementation of First-Fit,Best-Fit and Worst-Fit

  No comments
April 11, 2011

import java.io.*;
public class Fitting {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int process[], rprocess[], block[], rblock[], usage[], rusage[];
int p, b, free, used, rfree, rused, c, c1;

public Fitting() throws IOException{
System.out.println("Enter number of blocks");
b=Integer.parseInt(input.readLine());
System.out.println("Enter number of processes");
p=Integer.parseInt(input.readLine());
process=new int[p];
rprocess=new int[p];
block=new int[b];
rblock=new int[b];
usage=new int[b];
rusage=new int[b];
c=0;
// TODO Auto-generated constructor stub
}




void read() throws IOException
{
int i;
System.out.println("Enter block sizes");
for(i=0;i<b;i++)
{
System.out.print("Block "+(i+1)+" : ");
rblock[i]=Integer.parseInt(input.readLine());
}
System.out.println("Enter block usage scenario");
for(i=0;i<b;i++)
{
System.out.println("Is block "+(i+1)+" used (1) or free (0)?");
rusage[i]=Integer.parseInt(input.readLine());
if(rusage[i]==1)
{
rused=rused+rblock[i];
c1++;
}
else
rfree=rfree+rblock[i];
}
System.out.println("Enter process demand");
for(i=0;i<p;i++)
{
System.out.print("Process "+(i+1)+" : ");
rprocess[i]=Integer.parseInt(input.readLine());
}
}

void reset()
{
int i;
for(i=0;i<b;i++)
{
block[i]=rblock[i];
usage[i]=rusage[i];
}
for(i=0;i<p;i++)
process[i]=rprocess[i];
used=rused;
free=rfree;
c=c1;
}

void display()
{
int total;
total=rused+rfree;
System.out.println("Blocks used = "+c);
System.out.println("Total used space = "+used);
System.out.println("Blocks free = "+(b-c));
System.out.println("Total free space = "+(total-used));
}

void f_fit()
{
int i,j;
for(i=0;i<p;i++) //Processes.
for(j=0;j<b;j++) //Blocks.
{
if(process[i]<=block[j]&&usage[j]==0)
{
usage[j]=1;
used=used+block[j];
c++;
System.out.println("Process "+(i+1)+" is in block "+(j+1));
break;
}
}
}

void b_fit()
{
int i,j,size,best;
for(i=0;i<p;i++)
{
size=32967;
best=-1;
for(j=0;j<b;j++)
{
if(process[i]<=block[j]&&usage[j]==0&&(block[j]-process[i])<size)
{
size=block[j]-process[i];
best=j;
}
}
if(size<32967&&best!=-1) //Ensuring a best fit.
{
usage[best]=1;
used=used+block[best];
c++;
System.out.println("Process "+(i+1)+" is in block "+(best+1));
}
}
}

void w_fit()
{
int i,j,size,worst;
for(i=0;i<p;i++)
{
size=0;
worst=-1;
for(j=0;j<b;j++)
{
if(process[i]<=block[j]&&usage[j]==0&&(block[j]-process[i])>size)
{
size=block[j]-process[i];
worst=j;
}
}
if(worst!=-1) //Ensuring a worst fit.
{
usage[worst]=1;
used=used+block[worst];
c++;
System.out.println("Process "+(i+1)+" is in block "+(worst+1));
}
}
}



/**
* @param args
*/
public static void main(String[] args) throws IOException{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int option;
String choice;
Fitting f=new Fitting();
f.read();
do
{
f.reset();
System.out.println("Menu");
System.out.println("1. First fit");
System.out.println("2. Best fit");
System.out.println("3. Worst fit");
System.out.println("4. Block information");
System.out.println("Enter option");
option=Integer.parseInt(input.readLine());

switch(option)
{
case 1: f.f_fit();
break;
case 2: f.b_fit();
break;
case 3: f.w_fit();
break;
case 4: f.display();
break;
default: System.out.println("Invalid input");
}
f.display();
System.out.println("Press Y to continue");
choice=input.readLine();
}
while(choice.compareToIgnoreCase("y")==0);
// TODO Auto-generated method stub
}
}

You would be interested in:
1. Java Implementation of Page Replacement Algorithm -FIFO, LRU, OPT
2. Java Implementation of Bankers Algorithm
3. C implementation of CPU Scheduling Algorithm Shortest Remaining Time
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin

Read More

Java Implementation of Bankers Algorithm

  No comments
April 11, 2011

import java.io.*;
class Bankersalgorithm
{
void disp(int m,int n,int alloc[][],int max[][],int avble[],int need[][])throws Exception
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
// BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
int i,j;
System.out.println("\n\tALLOCATION\tMAX\tNEED\tAVAILABLE");
System.out.println("\n\t\t");
for(i=0;i<4;i++)
{
for(j=0;j<m;j++)
System.out.println(" "+res[j]);
System.out.println(" ");
}


for(i=0;i<n;i++)
{
System.out.println("\n\t\t"+i);
for(j=0;j<m;j++)
System.out.println(" "+alloc[i][j]);
System.out.println(" ");
for(j=0;j<m;j++)
System.out.println(" "+max[i][j]);
System.out.println("\t");
for(j=0;j<m;j++)
System.out.println(" "+need[i][j]);
System.out.println(" ");
if(i==0)
{
for(j=0;j<m;j++)
System.out.println(" "+avble[j]);
}
}
}
void safesq(int m,int n,int alloc[][],int avble[],int need[][])throws Exception
{
int i,j,k=0,l,flag=0,flag1=0;
int work[]=new int[5];
int work1[]=new int[5];
int fin[]=new int[5];
int safesq[]=new int[6];
for(i=0;i<m;i++)
work[i]=avble[i];
for(i=0;i<n;i++)
fin[i]=0;
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
flag1=0;
if(fin[i]==0)
{
for(j=0; j<m; j++)
{
if(need[i][j] > work[j])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(j=0;j<m;j++)
work[j]=work[j]+alloc[i][j];
fin[i]=1;
safesq[k]=i;
k++;
}
}
}
}
for(i=0;i<n;i++)
{
if(fin[i]==0)
{
System.out.println("\n\tFOR THE GIVEN REQUIREMENT THE SYSTEM IS");
System.out.println(" NOT IN A SAFE STATE.\n");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println("\n\tTHE SAFE SEQUENCE IS:\t");
for(i=0;i<n;i++)
System.out.println(" "+safesq[i]);
}
}
void resreq(int m,int n,int alloc[][],int avble[],int need[][])throws Exception
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
int i,j,num,flag=0,flag1=0;
int alloc1[][]=new int[5][5];
int avble1[]=new int[5];
int req[]=new int[5];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
alloc1[i][j] = alloc[i][j];
for(j=0;j<m;j++)
avble1[j]=avble[j];
System.out.println("\n\tENTER THE PROCESS NO. THAT REQUIRES EXTRA RESOURCES:\t");
num=Integer.parseInt(d.readLine());
System.out.println("\n\tENTER THE INSTANCE REQUIREMENT OF PROCESS \n"+num);
for(i=0;i<m;i++)
{
System.out.println("\n\tRESOURCE :\t"+res[i]);
req[i]=Integer.parseInt(d.readLine());
}
for(i=0;i<m;i++)
{
if(req[i]>need[num][i])
{
flag=1;
break;
}
}
if(flag==0)
{
for(i=0;i<m;i++)
{
if(req[i] > avble1[i])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
avble1[i]=avble1[i]-req[i];
alloc1[num][i]=alloc1[num][i]+req[i];
need[num][i]=need[num][i]-req[i];
}
safesq(m,n,alloc1,avble1,need);
}
else
{
System.out.println("\n\tTHE PROCESS P%d HAS TO WAIT AS RESOURCES ");
System.out.println("ARE NOT AVAILABLE.\n");
}
}
else
System.out.println("\n\t REQUIREMENT EXCEEDS MAXIMUM CLAIM.\n");
}
}
class Result
{
public static void main(String args[])throws Exception
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
int i,j,ch=0,n,m;
int work1[]=new int[5];
int avble[]=new int[5];
int max[][]=new int[5][];
int alloc[][]=new int[5][5];
int need[][]=new int[5][5];
Bankersalgorithm b=new Bankersalgorithm();
BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nENTER THE NUMBER OF PROCESSES : \t");
n=Integer.parseInt(d.readLine());
System.out.println("\nENTER THE NUMBER OF RESOURCES : \t");
m=Integer.parseInt(d.readLine());
System.out.println("\nENTER THE MAXIMUM INSTANCES OF EACH RESOURCE\n");
for(i=0;i<m;i++)
{
System.out.println("\n\tRESOURCE %c:\t"+res[i]);
avble[i]=Integer.parseInt(d.readLine());
}
System.out.println("\nENTER THE MAXIMUM DEMAND OF EACH PROCESS FOR A RESOURCE\n");
for(i=0;i<n;i++)
{
System.out.println("\n\tFOR PROCESS \n"+i);
for(j=0;j<m;j++)
{
System.out.println("\n\tRESOURCE : \t"+res[j]);
max[i][j]=Integer.parseInt(d.readLine());
}
}
System.out.println("\nENTER THE MAX NO. OF INSTANCES OF A RESOURCE ALLOCATED TO");
System.out.println(" A PROCESS.\n");
for(i=0;i<n;i++)
{
System.out.println("\n\tFOR PROCESS \n"+i);
for(j=0;j<m;j++)
{
System.out.println("\n\tRESOURCE : \t"+res[j]);
alloc[i][j]=Integer.parseInt(d.readLine());
}
}
for(i=0;i<m;i++)
{
work1[i]=0;
for(j=0;j<n;j++)
work1[i]+=alloc[j][i];
avble[i]=avble[i] - work1[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(ch<5)
{
System.out.println("\n\n\tMENU:\n\t1]DISPLAY DATA\n\t2]GENERATE SAFE SEQUENCE");
System.out.println("\n\t3]RESOURCE REQUEST\n\t4]EXIT\n\tENTER YOUR CHOICE:\t");
ch=Integer.parseInt(d.readLine());
switch(ch)
{
case 1:
b.disp(m,n,alloc,max,avble,need);
break;
case 2:
b.safesq(m,n,alloc,avble,need);
break;
case 3:
b.resreq(m,n,alloc,avble,need);
break;
case 4:
default:
System.out.println("\n\tINVALID CHOICE ENTERED.\n");
}
}
System.out.println("\n\t\tThank You");
//return true;
}
}

You would be interested in:
1. Java Implementation of Page Replacement Algorithm -FIFO, LRU, OPT
2. Java Implementation of First-Fit, Best-Fit and Worst-Fit
3. C implementation of CPU Scheduling Algorithm Shortest Remaining Time
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin

Read More

C implementation of CPU Scheduling Algorithm Shortest Remaining Time

  9 comments
April 11, 2011

Hello Friends, here's C implementation of CPU Scheduling Algorithms(Shortest Remaining Time).This is a .To run this program in windows simply copy and paste the program into notepad save it with any name you want with extension ".c".To run this program under linux just remove '#include,getch(); and clrscr();' from the program and save it with extension ".c".Don't just copy ,understand it.Program developed and tested by Niraj.
For any queries,mistakes mail to rustamengg@live.com


#include<stdio.h>

typedef struct process
{
int id;
int arrival;
int burst;
int wait;
};
struct process pool[10];
struct process ready[10];
struct process exe[20];
struct process temp,current,tmppro;

gantt(struct process pool[],int nop);
int scanpro(int time,int nop);
arrange();
execute();
incwait1();
incwait2();

int time,x,y,z;
float at[10],at1[10];

int main()
{

int tmp,nop,no;
float at_float,awt,atrt;
int j,i,maxarrtime;
int counter=0;
time=0;
x=1;
y=0;
z=0;
clrscr();
printf("Please enter the number of processes : ");
scanf("%d",&nop);
for(i=0;i<nop;i++)
at[i]=0;
printf("\nPlease enter the ID, Burst time & Arrival Time of each process : ");
printf("\n");
for(i=0;i<nop;i++)
{
printf("\nID : P%d",i+1);
pool[i].id=i+1;
printf("\nBurst Time : ");
scanf("%d",&pool[i].burst);
printf("Arrival Time : ");
scanf("%f",&at[i]);
}
for(i=0;i<nop;i++)
{
at1[i]=at[i];
at_float=at[i]+0.999;
pool[i].arrival=(int)at_float;
at[i]=(float)pool[i].arrival-at1[i];
}
printf("\n* * * * * * * PROCESS TABLE * * * * * * *\n");
printf("\nP ID Burst Time Arrival Time");
for(i=0;i<nop;i++)
{
if(pool[i].id==0)
break;
printf("\nP%d\t\t%d\t%f",pool[i].id,pool[i].burst,at1[i]);
}
maxarrtime=pool[nop-1].arrival;
while(time<=maxarrtime)
{
printf("\n\nTime : %d ",time);
printf("\tpool[%d].arr : %d ",z,pool[z].arrival);
if(time==pool[z].arrival)
{
no=scanpro(time,nop);
printf("\n\nNo of Process with same arrival time : %d",no);
printf("\nMoving Arrived process from Pool of Process to Ready Queue...");
for(counter=0;counter<no;counter++)
{
ready[x]=pool[z];
printf("\npool[%d].id = P%d ",z,pool[z].id);
printf("\tready[%d].id = P%d",x,ready[x].id);
x++;
z++;
}
execute();
}
else
{
if(current.burst>0)
{
current.burst--;
incwait1();
printf("\nCurrent : P%d and Burst : %d",current.id,current.burst);
time++;
}
else
{
if(current.burst==0)
{
printf("\nProcess P%d completes.",current.id);
execute();
}
}
}
}
counter=temp.burst-current.burst;
tmp=0;
while(current.burst!=0)
{
current.burst--;
counter++;
tmp++;
}
i=1;
printf("\nIncrementing Wait Time of processes in Ready Queue by %d...",tmp);
printf("\nAfter Incrementing ...");
while(ready[i].id>0 && ready[i].burst!=0)
{
ready[i].wait=ready[i].wait+tmp;
printf("\nP%d waiting time : %d",ready[i].id,ready[i].wait);
i++;
}
temp.burst=counter;
exe[y++]=temp;
for(i=0;i<10;i++)
ready[i]=ready[i+1];
arrange();
incwait2();
x=0;
while(ready[x].id>0)
exe[y++]=ready[x++];
for(i=0;i<20;i++)
exe[i]=exe[i+1];
for(i=0;i<nop;i++)
{
for(j=0;j<20;j++)
{
if(pool[i].id==exe[j].id)
{
pool[i].wait=exe[j].wait;
}
}
at[i]=at[i]+pool[i].wait;
printf("\nP%d Total Wait Time -->%f\tTotal Turn Around Time -->%f",i+1,at[i],(at[i]+pool[i].burst));
}
awt=0;
atrt=0;
for(i=0;i<nop;i++)
{
awt=awt+at[i];
atrt=atrt+(at[i]+pool[i].burst);
}
awt=awt/nop;
atrt=atrt/nop;
printf("\nAverage Waiting Time : %f",awt);
printf("\nAverage Turn Around Time : %f",atrt);
gantt(exe,nop);
return 0;
}

execute()
{
ready[0]=current;
printf("\nMoving current process to first location in Ready Queue.");
arrange();
temp.burst=temp.burst-current.burst;
exe[y++]=temp;
current=ready[0];
printf("\nCurrent :: ID = P%d Burst = %d",current.id,current.burst);
temp=ready[0];
current.burst--;
incwait1();
printf("\nDecrementing Current Burst by 1. Current burst : %d",current.burst);
time++;
}

incwait1()
{
int i=1;
printf("\n\nIncrementing wait time of processes in Ready Queue ...");
while(ready[i].id>0 && ready[i].burst!=0)
{
ready[i].wait++;
printf("\nP%d wait : %d",ready[i].id,ready[i].wait);
i++;
}
}

incwait2()
{
int i=1;
int wt=0;
printf("\n\nIncrementing wait time of processes in Ready Queue ...");
wt=ready[0].burst;
while(ready[i].id>0 && ready[i].burst!=0)
{
ready[i].wait=ready[i].wait+wt;
wt=wt+ready[i].burst;
printf("\nP%d wait : %d",ready[i].id,ready[i].wait);
i++;
}
}

int scanpro(int time,int nop)
{
int i;
int counter=0;
for(i=0;i<nop;i++)
if(pool[i].arrival==time)
counter++;
return counter;
}

arrange()
{
int a,i,j,counter=0;
for(i=1;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(ready[j].burst>ready[j+1].burst)
{
tmppro=ready[j];
ready[j]=ready[j+1];
ready[j+1]=tmppro;
}
}
}
printf("\nReady Queue after arranging : ");
for(i=0;i<10;i++)
{
if(ready[i].id!=0 && ready[i].burst>0)
{
printf("\nP ID : P%d",ready[i].id);
printf("\tP Burst : %d",ready[i].burst);
}
}
i=0;
while(ready[i].burst==0)
{
counter++;
i++;
}
a=0;
for(i=counter;i<10;i++)
{
ready[a++]=ready[i];
ready[i].id=0;
ready[i].burst=0;
}
}


gantt(struct process pool[],int nop)
{
int t=0,i;
int procount=0;
printf("\n\n\n* * * * * * * GANTT CHART * * * * * * *\n\n");
i=0;
while(pool[i].id>0)
{
procount++;
i++;
}
printf("\n");
for(i=0;i<procount;i++)
printf("--------");
printf("\n|");
for(i=0;i<procount;i++)
printf(" P%d |",pool[i].id);
printf("\n");
for(i=0;i<procount;i++)
printf("--------");
printf("\n");
for(i=0;i<procount;i++)
{
printf("%d\t",t);
t=t+pool[i].burst;
}
printf("%d",t);
}

OUTPUT:

Please enter the number of processes : 6

Please enter the ID, Burst time & Arrival Time of each process :

ID : P1
Burst Time : 7
Arrival Time : 0.000

ID : P2
Burst Time : 3
Arrival Time : 0.001

ID : P3
Burst Time : 1
Arrival Time : 5.001

ID : P4
Burst Time : 5
Arrival Time : 6.002

ID : P5
Burst Time : 4
Arrival Time : 6.020

ID : P6
Burst Time : 2
Arrival Time : 7.001

* * * * * * * PROCESS TABLE * * * * * * *

P ID Burst Time Arrival Time
P1 7 0.000000
P2 3 0.001000
P3 1 5.001000
P4 5 6.002000
P5 4 6.020000
P6 2 7.001000

Time : 0 pool[0].arr : 0

No of Process with same arrival time : 1
Moving Arrived process from Pool of Process to Ready Queue...
pool[0].id = P1 ready[1].id = P1
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P1 P Burst : 7
Current :: ID = P1 Burst = 7

Incrementing wait time of processes in Ready Queue ...
Decrementing Current Burst by 1. Current burst : 6

Time : 1 pool[1].arr : 1

No of Process with same arrival time : 1
Moving Arrived process from Pool of Process to Ready Queue...
pool[1].id = P2 ready[2].id = P2
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P2 P Burst : 3
P ID : P1 P Burst : 6
Current :: ID = P2 Burst = 3

Incrementing wait time of processes in Ready Queue ...
P1 wait : 1
Decrementing Current Burst by 1. Current burst : 2

Time : 2 pool[2].arr : 6

Incrementing wait time of processes in Ready Queue ...
P1 wait : 2
Current : P2 and Burst : 1

Time : 3 pool[2].arr : 6

Incrementing wait time of processes in Ready Queue ...
P1 wait : 3
Current : P2 and Burst : 0

Time : 4 pool[2].arr : 6
Process P2 completes.

Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P1 P Burst : 6
Current :: ID = P1 Burst = 6
Incrementing wait time of processes in Ready Queue ...
Decrementing Current Burst by 1. Current burst : 5

Time : 5 pool[2].arr : 6

Incrementing wait time of processes in Ready Queue ...
Current : P1 and Burst : 4

Time : 6 pool[2].arr : 6

No of Process with same arrival time : 1
Moving Arrived process from Pool of Process to Ready Queue...
pool[2].id = P3 ready[3].id = P3
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P3 P Burst : 1
P ID : P1 P Burst : 4
Current :: ID = P3 Burst = 1

Incrementing wait time of processes in Ready Queue ...
P1 wait : 4
Decrementing Current Burst by 1. Current burst : 0

Time : 7 pool[3].arr : 7

No of Process with same arrival time : 2
Moving Arrived process from Pool of Process to Ready Queue...
pool[3].id = P4 ready[4].id = P4
pool[4].id = P5 ready[5].id = P5
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P1 P Burst : 4
P ID : P5 P Burst : 4
P ID : P4 P Burst : 5
Current :: ID = P1 Burst = 4

Incrementing wait time of processes in Ready Queue ...
P5 wait : 1
P4 wait : 1
Decrementing Current Burst by 1. Current burst : 3

Time : 8 pool[5].arr : 8

No of Process with same arrival time : 1
Moving Arrived process from Pool of Process to Ready Queue...
pool[5].id = P6 ready[6].id = P6
Moving current process to first location in Ready Queue.
Ready Queue after arranging :
P ID : P6 P Burst : 2
P ID : P1 P Burst : 3
P ID : P5 P Burst : 4
P ID : P4 P Burst : 5
Current :: ID = P6 Burst = 2

Incrementing wait time of processes in Ready Queue ...
P1 wait : 5
P5 wait : 2
P4 wait : 2
Decrementing Current Burst by 1. Current burst : 1
Incrementing Wait Time of processes in Ready Queue by 1...
After Incrementing ...
P1 waiting time : 6
P5 waiting time : 3
P4 waiting time : 3

Ready Queue after arranging :
P ID : P1 P Burst : 3
P ID : P5 P Burst : 4
P ID : P4 P Burst : 5

Incrementing wait time of processes in Ready Queue ...
P5 wait : 6
P4 wait : 10
P1 Total Wait Time -->6.000000 Total Turn Around Time -->13.000000
P2 Total Wait Time -->0.999000 Total Turn Around Time -->3.999000
P3 Total Wait Time -->0.999000 Total Turn Around Time -->1.999000
P4 Total Wait Time -->10.998000 Total Turn Around Time -->15.998000
P5 Total Wait Time -->6.980000 Total Turn Around Time -->10.980000
P6 Total Wait Time -->0.999000 Total Turn Around Time -->2.999000

Average Waiting Time : 4.495833

Average Turn Around Time : 8.162500


* * * * * * * GANTT CHART * * * * * * *

┌────┬────┬────┬────┬────┬────┬────┬────┬────┬
│ P1 │ P2 │ P1 │ P3 │ P1 │ P6 │ P1 │ P5 │ P4 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┴
0 1 4 6 7 8 10 13

You would be interested in:
1. Java Implementation of Page Replacement Algorithm -FIFO, LRU, OPT
2. Java Implementation of First-Fit, Best-Fit and Worst-Fit
3. Java Implementation of Bankers Algorithm
4. C Implementation of CPU Scheduling Algorithm FCFS, SJF, Round Robin

Do leave a comment if u appreciate the output style
:-)

Read More

Mini Project in Java Using Java Applet and Listeners

  1 comment
April 11, 2011

Hello friends. I am bringing to you a mini project using Java Applet and Listeners. This project draws an applet to display the energy saved by six states by using different types of Charts like pie-chart, bar chart and Line Chart using Java Applet. This Mini Project shows how Java Applet can be used to draw and display complex graphics on screen with simple coding. Using the same coding, you can create applets for other data also, as per your requirements.

Screen shots:





...For complete program contact us
at bitstogather@gmail.com

Read More