Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to do sum of elements of an array by Loop Splitting

Posted By: Elliot Evans     Category: C Programming     Views: 4502

Program to do sum of elements of an array by Loop Splitting.

Code for Program to do sum of elements of an array by Loop Splitting in C Programming

# include <stdio.h>
# include </usr/include/sys/types.h>
# include </usr/include/sys/shm.h>
# include </usr/include/sys/ipc.h>

# include "forkjoin.h"int main()
{
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
    int FinalSum=0;
    int id,NPROC=5;
    int iCount;
    int shmid;
    int *parsum;
        shmid=shmget(IPC_PRIVATE,sizeof(int) * NPROC,IPC_CREAT| SHM_R | SHM_W);
    parsum=(int *)shmat(shmid,0,0);
    
    
    id=process_fork(NPROC);
    parsum[id]=0;
    for(iCount=id;iCount<10;iCount=iCount+NPROC)
    {
        parsum[id]=parsum[id] + arr[iCount];
    }    
    process_join(NPROC,id);
    
    
    for(iCount=0;iCount<NPROC;iCount++)
    {
        printf("ParSum[%d] : %d \n",iCount,parsum[iCount]);
        FinalSum=FinalSum + parsum[iCount];
    }

     printf("Final Sum is : %d \n",FinalSum);

    return 0;
}

/* Output

[divyen@localhost PP-TW1]$ ./Prog07
ParSum[0] : 7
ParSum[1] : 9
ParSum[2] : 11
ParSum[3] : 13
ParSum[4] : 15
Final Sum is : 55

*/
  
Share: 



Elliot Evans
Elliot Evans author of Program to do sum of elements of an array by Loop Splitting is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!