C Programming Tutorial

 
 
 
 

Image

# 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;
}

 
 
Didn't find what you were looking for? Find more on Program to do sum of elements of an array by Loop Splitting