C Programming Tutorial

 
 
 
 
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include \"headsem.h\"
#include \"headfork.h\"

main()
{
    int tempsum=0,np,a[10];
    int *lock1,*finalsum;
    int id,n,i,sid1,sid2;

    sid1=shmget(IPC_PRIVATE,10,0666|IPC_CREAT);
    lock1=(int *)shmat(sid1,0,0);

    sid2=shmget(IPC_PRIVATE,10,0666|IPC_CREAT);
    finalsum=(int *)shmat(sid2,0,0);

    *finalsum=0;

    printf(\"Enter the size of array :\");
    scanf(\"%d\",&n);

    printf(\"Enter the array elements :\");
    for(i=0;i<n;i++)
        scanf(\"%d\",&a[i]);

    printf(\"Enter the no.of proc :\");
    scanf(\"%d\",&np);

    lock_init(lock1);

    id=p_fork(np);

    for(i=id;i<n;i+=np)
        tempsum+=a[i];

    locksem(lock1);

    *finalsum+=tempsum;

    unlock(lock1);

    p_join(np,id);
    printf(\"Sum : %d\",*finalsum);
}

    Related Post:
  1. Program to do sum of numbers from 1 to 10, by dividing the job into two processes parent and one child Using Shared Memory

  2. Program to implement a circular queue as a linked list

  3. Program to find the maximum and minimum element in an array using threads

  4. Program to copy array elements in another array using parallel processing

  5. Program that reads the encoded alphabetic text from a file and produces decoded file by replacing each character with the character that occurs five

  6. PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. INPUT STEPS FROM KEYBORD SUM =1+4-9+16-25+36-.....

  7. MILNES METHOD

  8. PROGRAM TO FIND VALUE OF V USING V = P(1 + R)^N

  9. PROGRAM TO EVALUATE CORRELATION OF TWO SETS OF DATA USING BARRIER

  10. Program for matrix operations like dertminant, singular, etc

  11. Program to get answer of inventor of c. Allow user to give answer in 3 attempts

  12. PROGRAM THAT PROVIDES THE USE OF SHORTHAND OPERATOR *=

  13. Program to interchange two numbers

  14. Program to append one string s2 to another string s1 i.e. concatenation of two strings use strcat function from string.h

  15. Program to build a binary search tree from an array

  16. Program to concatenate one linked list at end of another and than to erase all nodes present in the linked list

  17. Program to implement an array

  18. Program to calculate roots of 3 numbers using root1 = (-b + sqrt(discriminant))/(2.0*a) and root2 = (-b - sqrt(discriminant))/(2.0*a) formula

  19. Program to display array values and address of an array using function and for loop

  20. Program to compress given string

 
 
Didn't find what you were looking for? Find more on PROGRAM FOR SUM OF ARRAY USING SPIN LOCK