Logo 
Search:

C Programming Articles

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

PROGRAM FOR COPY THE ARRAY ELEMENETS USING N NO.OF PROCESSES

Posted By: Hayden Evans     Category: C Programming     Views: 1793

PROGRAM FOR COPY THE ARRAY ELEMENETS USING N NO.OF PROCESSES.

Code for PROGRAM FOR COPY THE ARRAY ELEMENETS USING N NO.OF PROCESSES 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);
}

OUTPUT

knoppix@ttyp0[pp]$ cc cparr_nproc.c
knoppix@ttyp0[pp]$ ./a.out
Enter the no. of proc :2
Enter the limit of array :5
Enter the array elements :
2
4
5
1
3
New copied array is :
2
4
5
1
3
  
Share: 



Hayden Evans
Hayden Evans author of PROGRAM FOR COPY THE ARRAY ELEMENETS USING N NO.OF PROCESSES is from London, United Kingdom.
 
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!