C Programming Blog

 
 
 
# include <stdio.h>
# include \"/usr/include/sys/types.h\"
# include \"/usr/include/sys/shm.h\"
# include \"/usr/include/sys/ipc.h\"
# include \"/usr/include/sys/sem.h\"
# include \"forkjoin.h\"
# include \"sharedlib.h\"
# include \"spinlock.h\"
# include \"barrier.h\"

int main()
{
    int arrSize=11;
    int *arr;
    int arrold[11];            // For Holding boundary Cells

    int minIndex[11];        // For Holding the Start of Each Block
    int maxIndex[11];        // For Holding the End of Each Block

    int id;                // Process Identification
    int nProc=3;            // Number of Processes
    int *bararr;            // Barrier Array
    int shmidbararr,shmidarr;    // shmid for Shared Variables

    int blocksize = arrSize / nProc;    // Calculating Block Size

    int index,iCount;    

    // Calculating excesspoints

    // When Number of Array Elements to Process not divisible by 
    // Number of Processes, excesspoints occurs

    int excesspoints=arrSize - (blocksize * nProc);

    arr=(int*)sshared(sizeof(int)*10,&shmidarr);

    printf(\"Number of Processes : %d\\n\",nProc);
    printf(\"Number of Excesspoints      : %d\\n\",excesspoints);

    printf(\"Original Array ...\\n\");    
    for(iCount=1;iCount<=arrSize+1;iCount++)
    {
        arr[iCount]=iCount;    // Assigning Array Values
        printf(\"arr[%d] : %d\\n\",iCount,arr[iCount]);
    }
    bararr=(int*)sshared(sizeof(int)* 4,&shmidbararr);    

    // Initialize Barrier Array
    barrier_init(bararr,nProc);

    id=process_fork(nProc);

    if(excesspoints ==0)
    {
        // if no excesspoints
        minIndex[id]=(id * blocksize) + 1;
        maxIndex[id]=(id * blocksize) + blocksize;
    }
    else
    {        
        if(id < excesspoints)        
        {
        // Process From ID 0 to (excesspoints-1) Will handle 1 more element,
        // So that there is most equitable distribution of  work
            minIndex[id]=(id * (blocksize + 1)) + 1;
            maxIndex[id]=minIndex[id] + blocksize;
        }
        else
        {
            minIndex[id]=(id * blocksize) + 1 + excesspoints;
            maxIndex[id]=minIndex[id] + blocksize - 1;
        }

    }


    // Copying Boundary Values
    arrold[id] = arr[maxIndex[id]+1];

    // Calling Barrier        
    barrier(bararr);


    for(iCount=minIndex[id];iCount<maxIndex[id];iCount++)
    {
        arr[iCount]=arr[iCount+1];
    }

    // Assigning Boundary Values
    arr[maxIndex[id]] = arrold[id];

    process_join(nProc,id);

    printf(\"After Copy ...\\n\");
    for(iCount=1;iCount<=arrSize;iCount++)
    {
        printf(\"arr[%d] = %d\\n\",iCount,arr[iCount]);
    }
    cleanup_memory(&shmidbararr);
    cleanup_memory(&shmidarr);
    return 0;
}
/* Output
[divyen@localhost pp-tw3]$ cc -o Prog05 Prog05.c
[divyen@localhost pp-tw3]$ ./Prog05
Number of Processes : 3
Number of Excesspoints      : 2
Original Array ...
arr[1] : 1
arr[2] : 2
arr[3] : 3
arr[4] : 4
arr[5] : 5
arr[6] : 6
arr[7] : 7
arr[8] : 8
arr[9] : 9
arr[10] : 10
arr[11] : 11
arr[12] : 12
After Copy ...
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
arr[5] = 6
arr[6] = 7
arr[7] = 8
arr[8] = 9
arr[9] = 10
arr[10] = 11
arr[11] = 12

*/