Skip to content

Parallel Program to Sort the array of elements using Pivot element

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:

#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 arrayn”);
    for(i=0;i<10;i++)
    {
        scanf(“%dn”,&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 aren”);
    for(i=0;i<count;i++)
    {
        if(lhs[i]!=0)
        {
             printf(“%dn”,lhs[i]);
        }
       
    }
    printf(“The new elements in RHS aren”);   
    for(i=0;i<10;i++)
    {
        if(rhs[i]!=0)
        {
             printf(“%dn”,rhs[i]);
        }
    }
    return 0;
}


Output: 

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!