Aim: Parallel Program to Sort the array of elements using Pivot element.
MSc IT Parallel Processing Practical No. 9
Index of all Practicals ~ Click Here
Code:
Output:
MSc IT Parallel Processing Practical No. 9
Index of all Practicals ~ Click Here
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];
}
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");
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;
}
#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];
}
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");
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:
0 comments:
Confused? Feel free to ask
Post a Comment