Wednesday, 18 September 2013


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]:=>>%d\n",id, c[id]);
    join_process(n,id);
    for(i=0;i<n;i++)
        finalsum = finalsum + c[i];
    printf("Final Sum:=>> %d\n",finalsum);
    //free_shm(&id1);
    return 0;
}

Output:

Comment bellow for your Query and Feedback

Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment