Skip to content

program to find Sum of elements of an array using loop splitting

Aim: program to find Sum of elements of an array using loop splitting

Source Code:

#include <stdio.h>
#include “shmlib.h”
#include<stdlib.h>

int main()
{
    int finalsum=0, *c, a[10],id,n,i,id1;
    printf(“Enter the number of processes you want to create:=>>”);
    scanf(“%d”,&n);
    for(i=0;i<10;i++)
    {
      printf(“Enter value for %d position of the array: “, i);
      scanf(“%d”,&a[i]);
    }
    c  = (int *)shared(sizeof(int) * n, &id1);
    id = create_process(n-1);
    c[id] = 0;
    for(i=id;i<10;i=i+n)
    {
        c[id] = c[id] + a[i];
    }
    printf(“Process[%d]:=>>%dn”,id, c[id]);
    join_process(n,id);
    for(i=0;i<n;i++)
        finalsum = finalsum + c[i];
    printf(“Final Sum:=>> %dn”,finalsum);
    //free_shm(&id1);
    return 0;
}

Output:

Comment bellow for your Query and Feedback

Leave a Reply

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

error: Content is protected !!