Logo 
Search:

C Programming Articles

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

PROGRAM FOR ADD 2 ARRAY ELEMENTS AND STORE IN 3rd ARRAY USING LOOP SPLITING

Posted By: Reiner Fischer     Category: C Programming     Views: 7184

PROGRAM FOR ADD 2 ARRAY ELEMENTS AND STORE IN 3rd ARRAY USING LOOP SPLITING.

Code for PROGRAM FOR ADD 2 ARRAY ELEMENTS AND STORE IN 3rd ARRAY USING LOOP SPLITING in C Programming

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>

main()
{
        int id,i,n,npr,sid;
        int a1[10],a2[10],*a3;

        int p_fork(int);
        void p_join(int,int);

        sid=shmget(IPC_PRIVATE,40,0666|IPC_CREAT);
        a3=(int *)shmat(sid,0,0);

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

        printf("Enter the limit :");
        scanf("%d",&n);

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

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

        id=p_fork(npr);

        for(i=id;i<n;i+=2)
        {
                *(a3+i)=a1[i]+a2[i];
        }
        p_join(npr,id);
        
        printf("\nElements of Third Array :\n");
        for(i=0;i<n;i++)
                    printf("\n%d",*(a3+i));
}

int p_fork(int x)
{
    int t;
    for(t=0;t<x;t++)
    {
        if(fork()==0)
            return t;
    }
    return 0;
}
void p_join(int x,int id)
{
    int t;
    if(id==0)
    {
        for(t=0;t<x;t++)
            wait(0);
    }
    else
        exit(0);
}

OUTPUT
***********
[04mca58@LINTEL 04mca58]$  cc addindex.c
[04mca58@LINTEL 04mca58]$  ./a.out
Enter the no. of proc :2
Enter the limit :5
Enter the First array elements :
1
2
3
4
5
Enter the Second array elements :
1
2
3
4
5
Elements of Third Array :
2
4
6
8
10
  
Share: 



Reiner Fischer
Reiner Fischer author of PROGRAM FOR ADD 2 ARRAY ELEMENTS AND STORE IN 3rd ARRAY USING LOOP SPLITING 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!