Logo 
Search:

C Programming Articles

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

Program to copy array elements in another array using parallel processing

Posted By: Adalfreddo Fischer     Category: C Programming     Views: 3768

Write a Program to copy array elements in another array using parallel processing.

Code for Program to copy array elements in another array using parallel processing in C Programming

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

main()
{
    int id,i,n,npr,sid1,sid2;
    int *a1,*a2;

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

    sid1=shmget(IPC_PRIVATE,40,0666|IPC_CREAT);

    a1=(int *)shmat(sid1,0,0);

    sid2=shmget(IPC_PRIVATE,40,0666|IPC_CREAT);

    a2=(int *)shmat(sid2,0,0);
    
    printf("Enter the no. of proc :");
    scanf("%d",&npr);
    
    printf("Enter the limit of array :");
    scanf("%d",&n);

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

    for(i=0;i<n;i++)
        *(a1+i)=0;
    
    id=p_fork(npr);

    for(i=id;i<n;i+=2)
        *(a1+i)=*(a2+i);
    
    p_join(npr,id);

    printf("\nNew copied array is : ");

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

int p_fork(int x,int id)
{
    int t;
    for(t=1;t<x;t++)
    {
        if(fork()==0)
            return t;
    }
    return 0;
}

void p_join(int x,int id)
{
    int t;
    if(id==0)
    {
        for(t=1;t<x;t++)
            wait(0);
    }
    else
        exit(0);
}
  
Share: 



Adalfreddo Fischer
Adalfreddo Fischer author of Program to copy array elements in another array using parallel processing is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
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!