Tuesday, 10 May 2011

Producer-Consumer Algorithm...


/*********************************************************************************
By => Ajinkya Mahajan... Xtream Kings... K.K.Wagh COE,Nashik...
           Ajinkya's Blog :- http://aju-feelitsayit.blogspot.com/
*********************************************************************************/
import java.io.DataInputStream;

class p extends Thread
{
public static int pcnt=0;
public void run()
{
if(PC.n<PC.np)
{
System.out.println("Producer Producing...");
PC.n++;
System.out.println("In Stock : "+PC.n);
pcnt++;
}
else
{
System.out.println("Stock Full!!!");
}
}
}
class c extends Thread
{
public static int ccnt=0;
    public void run()
{

if(PC.n>0)
{
System.out.println("Consumer Consuming...");
PC.n--;
System.out.println("In Stock : "+PC.n);
ccnt++;
}
else
{
System.out.println("Stock Empty!!!");
}
}
}
public class PC
{
public static int n=0,np,i;
public static void main(String[] args)
{
DataInputStream in=new DataInputStream(System.in);

try
{
System.out.println("Enter The Maximum Number Of Production Allowed : ");
np=Integer.parseInt(in.readLine());
System.out.println("NP : "+np);

p p1=new p();
c c1=new c();
p1.start();
c1.start();

for(i=0;;i++)
{
int n=(int) (Math.random()*50);
if(n<25)
{
p1.run();
}
else
{
c1.run();
}
if(p.pcnt==np)
                          break;
}
System.out.println("Total Production : "+p.pcnt);
System.out.println("Total Consumption : "+c.ccnt);
System.out.println("Total Transactions taken place(including stok empty): "+i);
}
catch(Exception e){}
}
}

Reader Writer Algorithm...


/*******************************************************************************
By => Anand Mahuli...  Xtream Kings... K.K.Wagh COE,Nashik...
           Anand's Blog :- http://designyourdreamshere.blogspot.com/
*******************************************************************************/
class ww extends Thread{
    ww()
    {
    start();
    }
public void run()
    {
if(RW1.sem!=3)
        {
        System.out.println("Writer(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Waiting...");  
            try{sleep((int)(Math.random()*1000));}catch(Exception e){}
        }
        else
        {
        RW1.sem=2;
        System.out.println("Writer(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Writing...");
            try{sleep((int)(Math.random()*10000));}catch(Exception e){}
            RW1.sem=3;
            System.out.println("Writer(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Done Writing...");
        }
    }
}

class rr extends Thread{
    rr()
    {
    start();
    }
public void run()
    {
if(RW1.sem==2)
        {
System.out.println("Reader(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Waiting...");            
            try{sleep((int)(Math.random()*1000));}catch(Exception e){}
        }
        else
        {
        RW1.sem=1;
        System.out.println("Reader(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Reading...");
        try{sleep((int)(Math.random()*1000));}catch(Exception e){}
        RW1.sem=3;
        System.out.println("Reader(i="+RW1.i+") (Thread : "+Thread.currentThread().getId()+") Done Reading...");
        }
    }
}
public class RW1 extends Thread
{
    public static int sem=3,i;
   
    public static void main(String args[])
    {
    int j;
        rr threadr[] = new rr[5];
        ww threadw[] = new ww[5];
     
        for(i=0;i<3;i++)
        {
        System.out.println(i);
        try {sleep(1000);} catch(Exception e) {}
        j=(int)(Math.random()*3);
        //this is the key area in this program...i used  a random switch so as two create random no of reader write at a paritcular moment
        switch(j)
        {
        case 0:
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+") Thread Created : "+threadw[i].getId());
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+"(2)) Thread Created : "+threadw[i].getId());
       
        break;
        case 1:
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+") Thread Created : "+threadr[i].getId());
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+"(2)) Thread Created : "+threadr[i].getId());
       
        break;
        case 2:
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+") Thread Created : "+threadw[i].getId());
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+") Thread Created : "+threadr[i].getId());
        break;
        case 3:
        threadr[i]=new rr();
        System.out.println("Reader(i="+i+") Thread Created : "+threadr[i].getId());
        threadw[i]=new ww();
        System.out.println("Writer(i="+i+") Thread Created : "+threadw[i].getId());
        break;
  }
        }
        System.out.println("Exit From Main Thread ");
}
}

/*
 0
Writer(i=0) Thread Created : 8
Writer(i=0(2)) Thread Created : 9
1
Writer(i=0) (Thread : 8) Writing...
Writer(i=0) (Thread : 9) Waiting...
Reader(i=1) Thread Created : 10
Reader(i=1(2)) Thread Created : 11
2
Reader(i=0) (Thread : 10) Waiting...
Reader(i=0) (Thread : 11) Waiting...
Writer(i=0) (Thread : 8) Done Writing...
Writer(i=2) Thread Created : 12
Reader(i=2) Thread Created : 13
Exit From Main Thread
Writer(i=0) (Thread : 12) Writing...
Reader(i=0) (Thread : 13) Waiting...
Writer(i=0) (Thread : 12) Done Writing...

 */

Pass-1 Of Two Pass MACRO Processor...

/*********************************************************************************
By  => Ajit Medhekar... Xtream Kings...  K.K.Wagh COE,Nashik...
            Ajit's Blog :- http://ammedhekar.blogspot.com
*********************************************************************************
Design suitable data structure & Implement the pass-I of two pass MACRO processor...                                                                              
*********************************************************************************/
# include <stdio.h>
# include<conio.h>
#include<string.h>

struct table1
{
 char name[10];
 int index,add_mdt;
}MNT[5];
struct table2
{
 char des[30];
 int add;
}MDT[30];
struct table3
{
 char param[10],no[3];

}ALA[20];

/*
struct table1 MNT[5];
struct table2 MDT[30];
struct table3 ALA[20];
*/
FILE *fp1,*fp2;
char ch,word[20],word1[20],*mdtdes;
int newline=0;
void get_word();
char* num[10]={"#1","#2","#3","#4","#5","#6","#7","#8","#9"};

fpos_t pos;
void main()
{
 int i,j,mnt=0,mdt=0,ala=0,k,t=0,com=1,flag,flag1=0;
 char line[30],line1[30],line2[30];
 clrscr();

 fp1=fopen("c:\\a.asm","r");
 fp2=fopen("c:\\b.txt","w");
 i=0;
 while(!feof(fp1))
 {
  fgetpos(fp1,&pos);
  get_word();
  if(strcmp(word,"MACRO")==0)
  {
   fgetpos(fp1,&pos);
   get_word();

   strcpy(MNT[mnt].name,word);
   MNT[mnt].index=mnt;
   MNT[mnt].add_mdt=mdt;
   mnt++;

   fsetpos(fp1,&pos);
   i=0;
   while(!feof(fp1))
   {
    ch=fgetc(fp1);
    if(ch=='\n')
     goto l9;
    line1[i]=ch;
    i++;
   }
       l9:line1[i]=NULL;
   strcpy(MDT[mdt].des,line1);
   MDT[mdt].add=mdt;
   mdt++;

   fsetpos(fp1,&pos); //find actual parameter
   get_word();
   do
   {
    get_word();
    t=0;
    if(strstr(word,"=")!=0)
    {
        while(word[t]!='=')
        {
     word1[t]=word[t];
     t++;
        }
    }
    else
     strcpy(word1,word);

    strcpy(ALA[ala].param,word1);
    strcpy(ALA[ala].no,num[ala]);
    ala++;

   }while(ch!='\n');
   }
   else if(strcmp(word,"MEND")==0)
   {
   fsetpos(fp1,&pos);
   get_word();
   strcpy(MDT[mdt].des,word);
   MDT[mdt].add=mdt;
   mdt++;
   }
   else if(strcmp(word,"START")==0)
   {
   fsetpos(fp1,&pos);
   while(!feof(fp1))
   {
    ch=fgetc(fp1);
    fprintf(fp2,"%c",ch);
   }
   break;
   }
   else
   {
//code in comment to display macro call as INCR &A,&B,&REG2
//otherwise macro call is like INCR #4,#5,#6
//
/*   fsetpos(fp1,&pos);
   get_word();

   for(i=0;i<mnt;i++) //check nested call of macro
   {
    if(strcmp(word,MNT[i].name)==0)
    {
     fsetpos(fp1,&pos);
     fgets(line2,30,fp1);
     flag1=1;
    }
   }

   if(flag1==1)
   {
    flag1=0;
    strcpy(MDT[mdt].des,line2);
    MDT[mdt].add=mdt;
    mdt++;
   }
   else
*/   {
    fsetpos(fp1,&pos);
    get_word();

    strcpy(line2,word);
    com=1;
    do
    {
     if(ch=='\n')
      goto l1;
     get_word();
     strcat(line2," ");
     flag=0;
     for(i=0;i<ala;i++)
     {
      if(strcmp(word,ALA[i].param)==0)
      {
       strcpy(word1,ALA[i].no);
       strcat(line2,word1);
       flag=1;
       break;
      }
     }
     if(flag==0)
      strcat(line2,word);
     if(com==1)
     {
      strcat(line2,",");
      com=0;
     }
    }while(ch!='\n');
        l1:strcpy(MDT[mdt].des,line2);
    MDT[mdt].add=mdt;
    mdt++;
    }
   }
 }
 fclose(fp1);
 fclose(fp2);

 printf("\nMacro Name Table (MNT) :");
 printf("\n\nIndex\tName\tAddress In MDT");
 printf("\n-------------------------------");
 for(j=0;j<mnt;j++)
  printf("\n%d\t%s\t%d",MNT[j].index,MNT[j].name,MNT[j].add_mdt);

 printf("\n\nArgument Array List (ALA) :");
 printf("\n\nNo\tParam");
 printf("\n-----------------");
 for(j=0;j<ala;j++)
  printf("\n%s\t%s",ALA[j].no,ALA[j].param);

 printf("\n\nMacro Definition Table (MDT) :");
 printf("\n\nAdd\tDef");
 printf("\n-----------------");
 for(j=0;j<mdt;j++)
  printf("\n%d\t%s",MDT[j].add,MDT[j].des);

   getch();
}


void get_word()
{
 int i=0;
 do
 {
  ch=getc(fp1);
  if(ch!=' ' && ch!='\n' && ch!=',')
  {
   word[i]=ch;
   i++;
  }
  if(ch==' ' || ch=='\n' || ch==',')
  {
   word[i]=NULL;
   i=0;
   break;
  }
  }while(!feof(fp1));
}

/*********  OUTPUT ***********

Macro Name Table (MNT) :

Index   Name    Address In MDT
-------------------------------
0       INCR    0
1       DECR    5

Argument Array List (ALA) :

No      Param
-----------------
#1      &X
#2      &Y
#3      &REG1
#4      &A
#5      &B
#6      &REG2
                                                                             
Macro Definition Table (MDT) :                                                
                                                                             
Add     Def                                                                  
-----------------                                                            
0       INCR &X,&Y,&REG1=AREG                                                
1       MOVER #3, #1                                                          
2       ADD #3, #2                                                            
3       MOVEM #3, #1                                                          
4       MEND                                                                  
5       DECR &A,&B,&REG2=BREG                                                
6       MOVER #6, #4                                                          
7       SUB #6, #5                                                            
8       MOVEM #6, #4                                                          
9       INCR #4,#5,#6
10      MEND

*/
/*********** a.asm ******************/
MACRO INCR &X,&Y,&REG1=AREG
MOVER &REG1,&X
ADD &REG1,&Y
MOVEM &REG1,&X
MEND
MACRO DECR &A,&B,&REG2=BREG
MOVER &REG2,&A
SUB &REG2,&B
MOVEM &REG2,&A
INCR &A,&B,&REG2
MEND
START 100
READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END
/*************** b.txt **************/
START 100READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END