Sunday, February 6, 2011

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

No comments:

Post a Comment