Prog04.c

 /*	
Practical Assignment : 5
Name    : Divyen K Patel Roll No:24 MCA-4
Program : 5
Expression Splitting using multiple barriers
*/
# include <stdio.h>
# include "/usr/include/sys/types.h"
# include "/usr/include/sys/shm.h"
# include "/usr/include/sys/ipc.h"
# include "/usr/include/sys/sem.h"
# include "forkjoin.h"
# include "sharedlib.h"
# include "spinlock.h"
# include "barrier.h"

int main()
{
int iCount;
int nProc=10;
int const n=19;
float const r=1,t=1;
int id,k,**bar_ary,**shmid;
float *x,y[20],u[27],z[20],*sum;


shmid=(int **)malloc(sizeof(int)* ((nProc-1)/2 +3) );

for(iCount=0;iCount< ((nProc-1)/2+3); iCount++)
{
shmid[iCount]=(int *)malloc(sizeof(int));
}

sum=(float *)malloc(sizeof(float)*20);
x=(float *)malloc(sizeof(float)*20);

sum=(float *)sshared(sizeof(float)*20,shmid[0]);
x=(float *)sshared(sizeof(float)*20,shmid[1]);

bar_ary=(int**)malloc(sizeof(int)*((nProc-1)/2 +2));

for(iCount=0; iCount <= ((nProc-1)/2); iCount++)
{
bar_ary[iCount]=(int *)malloc(sizeof(int)*5);
bar_ary[iCount]=(int *)sshared(sizeof(int)*5,shmid[iCount+2]);
}

for(iCount=0;iCount<27;iCount++)
{
u[iCount]=iCount+3;
if(iCount<20)
{
y[iCount]=iCount+1;
z[iCount]=iCount+2;
x[iCount]=0;
sum[iCount]=0;
}
}

for(iCount=0;iCount <= ((nProc-1)/2);iCount++)
{
barrier_init(bar_ary[1,iCount],2);
}

id=process_fork(nProc);

for(k=1+(id/2); k<n; k+=nProc/2)
{
    if((id%2)==0)
    {
sum[id]=u[k] + r * ( z[k] + r * y[k] ) + t * ( u[k+3] + r * u[k+2] );
    }
    else
    {
        sum[id]=t * r * r * u[k+1] + t * t * ( u[k+6] + r * ( u[k+5] + r * u[k+4] ));
    }

    barrier(bar_ary[1,id/2]);

    if((id%2) == 0)
    {
x[k]=sum[id]+sum[id+1];
    }     
    barrier(bar_ary[1,id/2]);
}

process_join(nProc,id);

printf(" Results \n");
for(iCount=0; iCount<n ;iCount++)
{
printf(" x[%d]=%.2f \n",iCount,x[iCount]);
}

iCount=0;
while(iCount <  ((nProc-1)/2 + 3))
{
cleanup_memory(shmid[iCount]);
iCount++;
}
return 0;
}
/* Output

[divyen@localhost pp-tw5]$ cc -o Prog04 Prog04.c
[divyen@localhost pp-tw5]$ ./Prog04
Results
x[0]=0.00
x[1]=54.00
x[2]=63.00
x[3]=72.00
x[4]=81.00
x[5]=90.00
x[6]=99.00
x[7]=108.00
x[8]=117.00
x[9]=126.00
x[10]=135.00
x[11]=144.00
x[12]=153.00
x[13]=162.00
x[14]=171.00
x[15]=180.00
x[16]=189.00
x[17]=198.00
x[18]=207.00

*/

Project Homepage: