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: Adelchi Fischer     Category: C Programming     Views: 1960

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<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include "headsem.h"
#include "headfork.h"

main()
{
    int max,np,a[10];
    int *lock1,*finalmax;
    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);
    finalmax=(int *)shmat(sid2,0,0);

    *finalmax=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);
    max=a[0];
    
    for(i=id;i<n;i+=np)
    {
        if(a[i]>max)
            max=a[i];
    }

    locksem(lock1);

    if(max > *finalmax)
        *finalmax=max;

    unlock(lock1);

    p_join(np,id);
    printf("Maximum of array is : %d",*finalmax);
}

OUTPUT

knoppix@ttyp0[pp]$ cc max_arr_spinl.c
knoppix@ttyp0[pp]$ ./a.out
Enter the size of array : 5
Enter the array elements :
6
4
2
1
3
Enter the no.of proc :3
Maximum of array is : 6
  
Share: 



Adelchi Fischer
Adelchi 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!