Logo 
Search:

C Programming Articles

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

PROGRAM FOR ADD A CONSTANT TO EACH ARRAY ELEMENETS USING SELF SCHEDULING

Posted By: Adelberto Fischer     Category: C Programming     Views: 1615

PROGRAM FOR ADD A CONSTANT TO EACH ARRAY ELEMENETS USING SELF SCHEDULING.

Code for PROGRAM FOR ADD A CONSTANT TO EACH ARRAY ELEMENETS USING SELF SCHEDULING in C Programming

#include <stdio.h>
#include "headfork.h"
#include "headsem.h"
#include "headshr.h"

main()
{
    int i,j,no,nprc,t,id,shmid1,shmid2,shmid3;
    int *arr,*index,*lock;

    lock=(int *)shared(sizeof(int)*2,&shmid1);
    arr=(int *)shared(sizeof(int)*10,&shmid2);
    index=(int *)shared(sizeof(int)*2,&shmid3);

    printf("Enter the limit of an array: ");
    scanf("%d",&no);

    printf("\nEnter the values :");
    for(i=0;i<no;i++)
    {
        printf("\narr[%d] : ",i);
        scanf("%d",(arr+i));
    }

    printf("\nEnter the constant value : ");
    scanf("%d",&t);

    lock_init(lock);

    *index=0;

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

    id=p_fork(nprc);
    j=0;
    for(i=0;i<no;i++)
    {
        locksem(lock);
        j=*index;
        *index=*index + 1;
        unlock(lock);

        if(j>no)
            break;
        *(arr+j)+=t;
    }

    p_join(nprc,id);

    printf("\nAfter adding %d const to array is :",t);

    for(i=0;i<no;i++)
        printf("\n%d",*(arr+i));
}

OUTPUT

knoppix@ttyp0[pp]$ cc add_const_ss.c
knoppix@ttyp0[pp]$ ./a.out

Enter the limit of an array: 5

Enter the values :
arr[0] : 2
arr[1] : 4
arr[2] : 1
arr[3] : 3
arr[4] : 6

Enter the constant value : 3

Enter the no.of proc : 3

After adding 3 const to array is :
5
7
4
6
9
  
Share: 



Adelberto Fischer
Adelberto Fischer author of PROGRAM FOR ADD A CONSTANT TO EACH ARRAY ELEMENETS USING SELF SCHEDULING is from Frankfurt, Germany.
 
View All Articles

 
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!