final pp

40
M.Sc. (Information Technology) Part-II Page No.___ Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/------- Roll No. : ----- PRACTICAL NO: 1 AIM: Create many processes -Accept the number of processes from the user, for each process assign a task. -Once the processing is over kill all the process. CODE: int main() { int id,n,i; printf("Enter the number of process "); scanf("%d",&n); id=create_process(n); for(i=0;i<n;i++) {if(id==i) { printf("\n Process Number : %d",id); }} join_process(n,id); if(id==0) { printf("\n Parent Process Retained"); Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Upload: pawan-kukreja

Post on 22-Oct-2014

79 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

PRACTICAL NO: 1

AIM: Create many processes

-Accept the number of processes from the user, for each process assign a task.

-Once the processing is over kill all the process.

CODE:

int main()

{

int id,n,i;

printf("Enter the number of process ");

scanf("%d",&n);

id=create_process(n);

for(i=0;i<n;i++)

{if(id==i)

{

printf("\n Process Number : %d",id);

}}

join_process(n,id);

if(id==0)

{

printf("\n Parent Process Retained");

}

return 0;

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 2: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

}

OUTPUT:

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 3: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

PRACTICAL NO: 2

AIM: Create 2 processes, process1 should do the SUM () and process2 should do Sum1.Using

Shared memory concepts add SUM0 () + SUM1 ().

CODE:

#include<stdio.h>

#include "shmlib.h"

int main()

{ int final_sum,*sum0,*sum1,id,id1,id2;

sum0=(int *)shared(sizeof(int),&id1);

sum1=(int *)shared(sizeof(int),&id2);

id=create_process(2);

if(id==0)

{ *sum0=4+5;

printf("\n sum0 : %d ",*sum0);

}

if(id==1)

{ *sum1=5+6; printf("\n sum1 : %d ",*sum1);

} join_process(2,id);

final_sum=*sum0+*sum1;

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 4: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

printf("\n Final result is : %d ",final_sum);

printf("\n");

free_shm(id1); free_shm(id2);

return 0; }

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 5: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

OUTPUT:

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 6: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

PRACTICAL NO: 3

AIM: Loop Splitting:

(A) Calculate the average of elements.

(B) Histogram Computation

(A)CODE:

#include<stdio.h>

#include"shmlib.h"

int main()

{

float *avg1,*avg2,avg;

int id,id1,id2,sum=0,sum1=0,i;

int num[10];

avg1=(float *)shared(sizeof(float),&id);

avg2=(float *)shared(sizeof(float),&id2);

printf("Enter 10 numbers: \n");

for(i=0;i<10;i++)

{scanf("%d", &num[i]); }

id1=create_process(2);

if(id1==0)

{for(i=0;i<5;i++) {

sum1=sum1+num[i];

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 7: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

} *avg1=sum1/5;

}

if(id1==1)

{

for(i=5;i<10;i++)

{sum=sum+num[i]; }

*avg2=sum/5;

}join_process(2,id1);

printf("\nOutput of process A is: \t %f",*avg1);

printf("\nOutput of process B is: \t %f",*avg2);

avg=(*avg1+*avg2)/2;

printf("\nFinal Answer:%f\t",avg);

return 0;

}

OUTPUT:

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 8: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

(B)CODE:

#include<stdio.h>

#include"shmlib.h"

int main()

{ int *arr;

int image[4][4];

int bit;

int i,j,c1; int id,id1;

int c,max; int x,y;

x=0; y=0;

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 9: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

arr=(int *)shared((sizeof(int)*8),&id);

printf("Enter the maximum value for image pixel");

scanf("%d",&max);

printf("Enter the image(4*4) values 0 to %d",max);

printf("\n");

for(x=0;x<4;x++)

{ for(y=0;y<4;y++)

{ scanf("%d",&image[x][y]); }

}

id1=create_process(2);

if(id1==1)

{

printf("Process A is executing\n");

for(i=0;i<2;i++)

{

for(j=0;j<4;j++)

{ c=image[i][j]; arr[c]=arr[c]+1;

printf("%d\t",c); }

printf("\n");

} }

else

{ printf("Process B is executing\n");

for(i=2;i<4;i++)

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 10: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

{

for(j=0;j<4;j++)

{ c=image[i][j];

arr[c]=arr[c]+1;

printf("%d\t",c); }

printf("\n");

} }

join_process(2,id1);

printf("Histogram Computation is \n");

for(i=0;i<(max+1);i++)

{ printf("%d \t %d",i,arr[i]);

printf("\n");

}

return 0;

}

OUTPUT:

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 11: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 12: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

PRACTICAL NO: 4

AIM: Write program to explain the Mutual Exclusion.

CODE:

#include<stdio.h>

#include"shmlib.h"

int main()

{ int id,id1,id2,id3;

int *inventory;

int *lock1,*lock2,i;

inventory=(int *)shared(sizeof(int),&id1);

lock1=(int *)shared(sizeof(int),&id2);

lock2=(int *)shared(sizeof(int),&id3);

lock_init(lock1);

for(i=0;i<4;i++)

{ lock(lock1);

*inventory=*inventory+2;

printf("The value in the inventory is: after producing is %d\n",*inventory);

unlock(lock1); lock(lock2);

*inventory=*inventory-1;

printf("The value in the inventory after consuming is: %d\n",*inventory);

unlock(lock2); }

return 0;

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 13: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

}

OUTPUT:

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 14: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

PRACTICAL NO: 5

AIM: Using the concept of Barrier write a program to compute the absolute deviation of an

array of numbers.

CODE:

#include<stdio.h>

#include"shmlib.h"

int a[100]; int nproc=3; int N;

int main()

{

int *total,k,sum,ctr,i,j; int sid1,sid2,sid3,*bar;

int *lock1,id; float dev,avg,*final_dev;

printf("Enter val of N:");

scanf("%d",&N);

final_dev=(float*) shared(sizeof(float),&sid1);

total=(int*) shared(sizeof(int),&sid2);

lock1=(int*) shared(sizeof(int),&sid3);

*total=0; *final_dev=0.0;

bar=barrier_init();

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 15: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

for(i=0;i<N;i++)

{ a[i]=i; }

lock_init(lock1);

id=create_process(nproc+1); sum=0;

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 16: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

for(i=id;i<=N;i+=nproc)

{ sum+=a[i];}

printf("ID: %d and Sum : %d",id,sum);

lock(lock1);

*total+=sum;

unlock(lock1); barrier(bar);

printf("\n%d passed barrier \n",id);

avg=((*total)*1.0/N);

printf("\n avg= %f",avg);

dev=0.0;

for(i=id;i<=N;i+=nproc)

{ dev=dev+(avg-a[i])*((avg>a[i])?1:-1);}

lock(lock1);

*final_dev+=dev;

unlock(lock1);

join_process(nproc+1,id);

printf("\n Avg deviation is %f\n",*final_dev/N);

free_shm(sid1); free_shm(sid2); free_shm(sid3);

clean_barrier(bar);

return 0;

}

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 17: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

OUTPUT:

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 18: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

PRACTICAL NO: 6

AIM: Matrix multiplication using self-scheduling.

-Accept the elements of the matrix and vector

-Declare a shared variable index whose initial value is (row*column) of matrix

-Every processor will picj the value of index under lock and do the multiplication.

CODE:

#include<stdio.h>

#include"shmlib.h"

int main()

{

int *A,*B,id,id1,*C,i,j,P1flag=0,P2flag=0,k;

int id2,id3;

A=(int *) shared(sizeof(int),&id1); B=(int *) shared(sizeof(int),&id2);

C=(int *) shared(sizeof(int),&id3); int x; x=2;

printf("Enter value of matrix A:");

printf("\n");

for(j=0;j<4;j++)

{scanf("%d",&A[j]);}

printf("Enter value of matrix B:\n");

for(i=0;i<4;i++)

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 19: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

{scanf("%d",&B[i]);}

printf("\nmatrix A\n");

for(j=0;j<2;j++)

{printf("\t%d",A[j]);}

printf("\n");

for(j=2;j<4;j++)

{printf("\t%d",A[j]);}

printf("\nmatrix B\n");

for(j=0;j<4;j++)

{printf("\t%d",B[j]);

if(j==1)

printf("\n");

}printf("\n");

id=create_process(2);

if(id==0)

{ C[0]=A[0]*B[0]+A[1]*B[2]; P1flag=1;}

if(id==1)

{ C[1]=A[0]*B[1]+A[1]*B[3]; P2flag=1;}

if(P1flag==1)

{ id=0; C[2]=A[2]*B[0]+A[3]*B[2]; P1flag=2;}

else

{ id=1; C[2]=A[2]*B[0]+A[3]*B[2]; P2flag=2;

}

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 20: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

if(P1flag==2)

{ id=0;

C[3]=A[2]*B[1]+A[3]*B[3]; }

else

{id=1; C[3]=A[2]*B[1]+A[3]*B[3]; }

join_process(2,id);

printf("\n\nFinal Matrix after Multiplication:\n");

for(k=0;k<2;k++)

{printf("\t%d",C[k]);}

printf("\n");

for(k=2;k<4;k++)

{printf("\t%d",C[k]); }printf("\n");return 0;

}

OUTPUT:

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 21: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

PRACTICAL NO: 7

AIM: Block Matrix Multiplication

-Accept the matrix (4*4).

-Divide both the matrix into 4 blocks.

-Create 4 processes.

-Multiply each block.

-Restore all the blocks in single matrix.

CODE:

#include<stdio.h>

#include"shmlib.h"

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 22: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

#include<stdlib.h>

int main()

{ int i,j,k,x,y,z,m=4,n=4;

static int mat1[4][4],mat2[4][4],mat3[4][4],mat4[4][4];

int temp,temp1; int tmp[4][4];

int *arr1,*arr2,*arr3,*arr4;

int id1,id2,id3,id4,idd;

arr1=(int *)shared(sizeof(int),&id1); arr2=(int *)shared(sizeof(int),&id2);

arr3=(int *)shared(sizeof(int),&id3); arr4=(int *)shared(sizeof(int),&id4);

for(i=0;i<m;i++)

{ for(j=0;j<n;j++)

{ mat1[i][j]=0;

mat1[i][j]=i+10; } }

for(i=0;i<n;i++)

{ for(j=0;j<n;j++)

{ mat2[i][j]=0;mat2[i][j]=i+10;

}

}printf("Matrix1 printed are:\n\n");

for(i=0;i<m;i++)

{ for(j=0;j<n;j++)

{ printf("\t%d",mat1[i][j]); }

printf("\n"); }

printf("Matrix2 printed are:\n\n");

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 23: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

for(i=0;i<n;i++)

{ for(j=0;j<n;j++)

{ printf("\t%d",mat2[i][j]); }

printf("\n"); }

idd=create_process(4);

if(idd==0)

{ for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ mat3[i][j]=0;

for(k=0;k<m/2;k++)

{ temp=mat1[i][k]*mat2[k][j]; mat3[i][j]=mat3[i][j]+temp;

}}}

for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ tmp[i][j]=0;

for(k=0;k<m/2;k++)

{ temp=mat1[i][k+2]*mat2[k+2][j];

tmp[i][j]=tmp[i][j]+temp;

} } } x=0;

for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ mat3[i][j]=mat3[i][j]+tmp[i][j];

arr1[x]=mat3[i][j];

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 24: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

x++;

} }

}

if(idd==1)

{ temp=0;

for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ mat3[i][j+2]=0;

for(k=0;k<m/2;k++)

{ temp=mat1[i][k]*mat2[k][j+2];

mat3[i][j+2]=mat3[i][j+2]+temp;

} }

} for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ tmp[i][j+2]=0;

for(k=0;k<m/2;k++)

{ temp=mat1[i][k+2]*mat2[k+2][j+2];

tmp[i][j+2]=tmp[i][j+2]+temp;

} } } x=0;

for(i=0;i<m/2;i++)

{for(j=0;j<n/2;j++)

{ mat3[i][j+2]=mat3[i][j+2]+tmp[i][j+2];

arr2[x]=mat3[i][j+2]; x++;

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 25: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

} }}

if(idd==2)

{ temp=0;

for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ mat3[i+2][j]=0;

for(k=0;k<m/2;k++)

{ temp=mat1[i+2][k]*mat2[k][j];

mat3[i+2][j]=mat3[i+2][j]+temp;

}} }

for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ tmp[i+2][j]=0;

for(k=0;k<m/2;k++)

{ temp=mat1[i+2][k+2]*mat2[k+2][j];

tmp[i+2][j]=tmp[i+2][j]+temp; }} }x=0;

for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ mat3[i+2][j]=mat3[i+2][j]+tmp[i+2][j];

arr3[x]=mat3[i+2][j];

x++;

} } }

if(idd==3)

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 26: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

{ temp=0;

for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ mat3[i+2][j+2]=0;

for(k=0;k<m/2;k++)

{

temp=mat1[i+2][k]*mat2[k][j+2];

mat3[i+2][j+2]=mat3[i+2][j+2]+temp;

} }

} for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{ tmp[i+2][j+2]=0;

for(k=0;k<m/2;k++)

{ temp=mat1[i+2][k+2]*mat2[k+2][j+2];

tmp[i+2][j+2]=tmp[i+2][j+2]+temp;

} }

}x=0;

for(i=0;i<m/2;i++)

{ for(j=0;j<n/2;j++)

{

mat3[i+2][j+2]=mat3[i+2][j+2]+tmp[i+2][j+2];

arr4[x]=mat3[i+2][j+2]; x++; } }

}join_process(4,idd);

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 27: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

for(i=0;i<2;i++)

{ mat4[0][i]=arr1[i]; }

for(i=0;i<2;i++)

{ mat4[1][i]=arr1[i+2]; }

for(i=0;i<2;i++)

{ mat4[0][i+2]=arr2[i]; }

for(i=0;i<2;i++)

{ mat4[1][i+2]=arr2[i+2]; }

for(i=0;i<2;i++)

{ mat4[2][i]=arr3[i]; }

for(i=0;i<2;i++)

{ mat4[3][i]=arr3[i+2]; }

for(i=0;i<2;i++)

{ mat4[2][i+2]=arr4[i]; }

for(i=0;i<2;i++)

{ mat4[3][i+2]=arr4[i+2]; }

printf("\n\nFinal matrix after multiplication is:\n\n");

for(i=0;i<m;i++)

{ for(j=0;j<n;j++)

{ printf("\t%d",mat4[i][j]); }

printf("\n"); }

return 0;

}

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 28: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

OUTPUT:

PRACTICAL NO: 8

AIM: Finding the Maximum and Minimum element

-Accept the elements.

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 29: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

-Make the processes.

-Compare the successive elements in each process.

-Again create the processes and compare the successive elements in each process.

-Continue the step(ii) and (iii) till there is two elements in every process.

CODE:

#include<stdio.h>

#include"shmlib.h"

int main()

{

int *result,*result1,num[6],x,id1,id2,id,val,i,final;

result=(int *) shared(sizeof(int),&id1);

result1=(int *) shared(sizeof(int),&id2);

printf("Enter values");

for(x=0;x<6;x++)

{scanf("%d",&num[x]);}

id=create_process(3);

if(id==0)

{if(num[0]>=num[1]){

result[0]=num[0];result1[0]=num[1];}

else

{result[0]=num[1];result1[0]=num[0];}}

if(id==1)

{if(num[2]>=num[3])Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 30: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

{result[1]=num[2];result1[1]=num[3];}

else

{result[1]=num[3];result1[1]=num[2];}}

if(id==2)

{if(num[4]>=num[5])

{result[2]=num[4];result1[2]=num[5];}

else

{result[2]=num[5];result1[2]=num[4];

}}

join_process(3,id); id=create_process(0);

if(id==0)

{

if(result[0]>=result[1])

{result[3]=result[0];}

else

{result[3]=result[1];}}

if(result[3]>result[2])

{ printf("Maximum no is %d",result[3]);}

else

{printf("Maximum no is %d",result[2]);}

join_process(1,id); id=create_process(0);

if(id==0)

{if(result1[0]<=result1[1])

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 31: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

{result1[3]=result1[0];}

else

{result1[3]=result1[1];}}

if(result1[3]<result1[2])

{ printf("\n Minimum no is %d",result1[3]);}

else

{printf("\n Minimum no is %d",result1[2]);}

join_process(1,id);return 0;}

OUTPUT:

PRACTICAL NO: 9Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 32: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

AIM: SORTING USING THE PIVOT ELEMENT -Accept the elements to be sorted. -Choose the pivot element. -Store the elements less than pivot in one-array and rest elements in other array. -Sort both the arrays using different processors. -Finally merge both the arrays to get the sorted elements.

CODE:

#include<stdio.h>

#include"shmlib.h"

int main()

{ int a[10],i,pivot,j,temp,k,m;

int *lhs,*rhs,*lhs1,*rhs1; int count=0,count1=0;

int id1,id,*lock1,sid;

lock1=(int *)shared(sizeof(int),&id1);

lock_init(lock1); lhs=(int *)shared(sizeof(int),&id1);

rhs=(int *)shared(sizeof(int),&id1);

lhs1=(int *)malloc(sizeof(int)); rhs1=(int *)malloc(sizeof(int));

printf("Enter the values of array\n");

for(i=0;i<10;i++)

{scanf("%d\n",&a[i]); }

pivot=a[5];

printf("pivot is %d \n",pivot);

for(j=0;j<10;j++)

{if(a[j]<pivot)

{lhs[j]=a[j]; }

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 33: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

else if(a[j]>=pivot)

{ rhs[j]=a[j]; } }

id=create_process(1);

if(id==0)

{lock(lock1);

for(i=0;i<10;i++)

{count++; }

for(i=0;i<10;i++)

{ for(j=0;j<10;j++)

{if(lhs[i]<lhs[j])

{ temp=lhs[i];lhs[i]=lhs[j]; lhs[j]=temp;} }

}unlock(lock1);}

if(id==1)

{lock(lock1);

for(i=0;i<10;i++)

{ count1++; }

for(i=0;i<10;i++)

{for(j=0;j<10;j++)

{ if(rhs[i]<rhs[j])

{ temp=rhs[i];rhs[i]=rhs[j];rhs[j]=temp; }}

}unlock(lock1);

}join_process(2,id); printf("The new elements in LHS are\n");

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 34: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

for(i=0;i<count;i++)

{if(lhs[i]!=0)

{ printf("%d\n",lhs[i]); } }

printf("The new elements in RHS are\n");

for(i=0;i<10;i++)

{if(rhs[i]!=0)

{printf("%d\n",rhs[i]); } }

return 0;

}

OUTPUT:

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 35: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

PRACTICAL NO: 10

AIM: Parallel reduction - n elements are stored. - (n-1) processers are created. The elements of input to reduction problem are placed at the leaf node. Associative operator can be applied in parallel at a level of the tree at a time. In jth iteration, ith process choses (i+j) th and (i-2j) th element for addition.

CODE:

#include<stdio.h>

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 36: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

#include"shmlib.h"

int main()

{

int *result,num[6],x,id1,id,val,i,final;

result=(int *) shared(sizeof(int),&id1);

printf("Enter values");

for(x=0;x<6;x++)

{scanf("%d",&num[x]);}

id=create_process(2);

if(id==0)

{result[0]=num[0]+num[1];}

if(id==1)

{result[1]=num[2]+num[3];}

if(id==2)

{result[2]=num[4]+num[5];}

join_process(3,id); id=create_process(0);

if(id==0)

{result[3]=result[0]+result[1];}

join_process(1,id);

result[4]=result[3]+result[2];

printf("the parallely reduced value is %d",result[4]);

return 0;

}

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare

Page 37: final PP

M.Sc. (Information Technology) Part-II Page No.___

Paper: III (Section-I) Subject: Parallel Processing Date: ----/----/-------

Roll No. : -----

OUTPUT:

Chikitsak Samuha’s Patkar –Varde College, Goregaon Abhishek Vichare