Tuesday, March 8, 2011

CRC

#include<stdio.h>
#include<conio.h>
#include<math.h>
int div[7],d[4],rec[7],i,n=0,k=0,flag=0,rem;
int main()
{
printf("Enter the 4 bit div:\n");
for(i=0;i<4;i++) 

scanf("%d",&div[i]);
printf("\nEnter the 4 bit data:\n");
for(i=0;i<4;i++) 
scanf("%d",&d[i]);
printf("\nEnter the recieved bit:\n");
for(i=0;i<7;i++) 
scanf("%d",&rec[i]); for(i=6;i>=0;i--)
n=n+div[i]*pow(2,i%6);
for(i=3;i>=0;i--)
k=k+d[i]*pow(2,i%3);
rem=n%k;
i=4;
while(rem!=0)
{
div[i]=rem%2;
rem=rem/2;
i++;
}
printf("\n");
for(i=0;i<7;i++)
printf("%d\t",div[i]);
for(i=0;i<7;i++)
{
if(rec[i]!=div[i])
{
flag=1;
break;
}
}
if(flag==0)
printf("\nThe obtained bit is correct!");
else
printf("\nThe obtained bit is incorrect!");
getch();
}

Monday, February 7, 2011

Program to restrict keyboard using c++

#include
#include
#include
using namespace std;
int main()
{
char input[250] = {'\0'};
char *i = input;
char c;
cout<<"ENTER ALPHA CHARACTERS : ";
do{
c = getch();
if(isalpha(c)) cout< }while(c!=13);
cout< <<"PHRASE : "< cin.ignore();
cin.get();
return 0;

}

Sunday, February 6, 2011

How can I make my blog load faster? - Blogger Help

How can I make my blog load faster? - Blogger Help

Process Synchronization

Dinning Philosopher Problem


OBJECTIVE: Implementation of process synchronization(DINNING PHILOSOPHER PROBLEM)
THEORY: The goal of problem is to create a program that simulate the behavior of 5 philosopher gathered around a table  to think and eat.
Each philosopher has one fork, but needs two forks to eat.Thus the pholoshper  decide to share their forks. When a philosopher wants to eat, he takes the fork on his left,whwn it is available,then the fork on his right,eats,and then puts forks back.
After the philosopher have thought and eaten several times ,it may happen that all philosophers simultaneously decide to eat.they take their left fork,and find the right forkalready taken by their right neighbor.In this case,if nothing has been foreseen the philosopher will stay in that situation(deadlock)indefinitely.the program should avoid that situation.

PROGRAM:

#include<sim/sim.h>
const double duration=52*7*24.0;
const int number =5;
const int eatingtime=2;
const int thinkingtime=5;
simulation*sim;
generator*g;
resource*chopstic[number];
histogram*thinking;
class eat:public event
{
public:
eat(int i);
virtual int operator()();
private:
int id;
};
Class think:public event
{
Public:
think(int i);
virtual int operator()();
private:
int id;
};
Class await:public event
{
public:
await(int i)
virtual int operator()();
private:
int id;
};
eat::eat(int i):event()
{
Id=I;
}
int eat::operator()()
{
double t=g->exponential(eatingtime);
think*th=new think(id);
sim->schedule(th,t);
sim->terminate(this);
return ok;
}
think::think(int i):event()
{
id=I;
}
Int think::operator()()
{
chopstick[id]->release();
chopstick[id+1]%number->release();
double t=g->exponential(thinkingtime);
thinking->sample(id,t/duration*100);
await*aw=new await(id);
sim->schedule(aw,t);
sim->terminate(this);
return ok;
}
await::await(int i):event()
{
id=I;
}
Int await::operator()()
{
If((chopstick[id]->available()&&(chopstick[(id+1)%number]->available()))
{
chopstick[id]->acquire();
chopstick[id+1]%number->acquire();
eat*e=new eat(id);
sim->passivate(this);
sim->schedule(e,o);
sim->terminate(this);
}
else if(!conditional())
sim->hold(this);
return ok;
}
class application:public session
{
public:
application(int argc,char**argv);
int main();
};
application::application(int argc,char**argv):session(argc,argv,”philosophers”)
{}
int application::main()
{
sim=new simulation();
g=new generator(80,20,19);
thinking=new histogram(“.h”,”-columns 5-title thinkingtime”);
tk->pack(thinkingtime);
tk->update();
for(int i=0;i<number,i++)
{
chopstick[i]=new resource(1);
await*aw=new await(i);
sim->schedule(aw,0);
}
sim->run(duration);
cout<<(*thinking)<<endl;
delete thinking;
delete sim;
return 0;
}
main(int,char**argv)
{
session*s=new application(argc,argv);
return s->run();
}


Critical section Problem Using Dekker's Algorithm


OBJECTIVE: Write a program for synchronizing of critical section problem using Dekker’s Algorithm.
THEORY: In concurrent programming a critical section is a piece of code that asses a shared resource (data structure or device ) that must not be concurrently accessed by more than one thread of execution. A critical section will usually terminate in fixed time, and a thread, task or process will only have to wait a fixed time to enter it (i.e. bounded waiting .
PROGRAM:
#include<sydio.h>
#include<conio.h>
void main()
{
int choice;
int c1=1,c2=1,turn=1;
clrscr();
do
{
printf(“\n1.Process 1 Enter”);
printf(“\n2.Process 2 Enter”);
printf(“\n3.Both Process Enter”);
printf(“\n4.Exit”);
scanf(“%d”,&choice);
if(choice==1)
{
c1=0;
while(c2==0)
{
                                if(turn==2)
c1=1
while(turn==2);
c1=0;
}
printf(“\nProcess P1 Enters the Critical section”);
c1=1;
turn=2;
else
printf(“\nIt is the turn process P2”);
}
if(choice==2)
{
                c2=0;
                while(c1==0)
{
                if(turn==1)
                c2=1;
while(turn==1);
c2=0;
}
printf(“\nProcess P2 enters in critical section”);
c2=1;
turn=1;
else
printf(“\nIt is turn of Process P1”);
}
}
while(choice!=4);
}
OUTPUT:
Enter  the choice:             1
Process 1 Enters Critical section
Enter choice       3
Both Process i

Synchronizing POSTFIX thread using MUREX variable


OBJECTIVE: Write a Program for synchronizing POSTFIX thread using Murex variable.
THEORY:
Technically, a thread is defined as an independent stream of instructions that can be scheduled to run as such by operating system. But what does this mean?
To the software developer, the concept of “procedure” that runs independently from its main program may best describes a thread.
PROGRAM:
#include<pthread.h>
#include<stdio.h>
#include<malloc.h>
typedef  struct{
            double *a;
            double *b;
            double sum;
int veclen;
}DOTDATA;
#define NUMTHREAD 4
#define VECLEN 100
DOTDATA dotstr;
Pthread_t callThd[NUMTHRDS];
Pthread_mutexsum;
void *dotprod(void *arg)
{
            int i,start,end,offset,len;
            double mysum, *x,*y;
offset = (int)arg;
len=dotstr.veclen;
start=offset*len;
end=start+len;
x=dotstr.a;
y=dotstr.b;
mysum=0;
for(i=start;i<end;i++)
{
            Mysum+=(x[i]*y[i]);
}
pthread_mutex_lock(&mutexsum);
dotstr.sum+=mysum;
pthead_mutex_unlock(&mutexsum);
pthread_exit((void *)0) ;
}
int main(int argc,char *arvg[])
{
int i;
double *a,*b;
int status;
pthread_attr_t attr;
a=(double *)malloc(NUMTHRDS*VECLEN*sizeof(double));
b==(double *)malloc(NUMTHRDS*VECLEN*sizeof(double));
for(i=0;i<VECLEN*NUMTHRDS;i++)
{
            a[i]=1.0;
            b[i]=a[i];
}
dotstr.vectlen=VECTLEN;
dotstr.a=a;
dotstr.b=b;
dotstr.sum=0;
pthread_mutex_init(&mutex,NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOOINABLE);
for(i=0;i<NUMTHRDS;i++)
{
            pthread_create(&callTHd[i],&attr,dotprod,void(*)i);
}
pthread_attr_destroy(&attr);
for(i=0;i<NUMTHRDS;i++)
{
            pthread_join(callThd[i],(void **)&status);
}
printf(“Sum=%f\n”,dotstr.sum);
free(a);
free(b);
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}
OUTPUT:
a=3i+5j-2k
b=2i-2j-2k
a.b=(3i+5j-2k).( 2i-2j-2k).
a.b=(3X2)+(5X