Logo 
Search:

C Programming Articles

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

Program to copy elements of one array to another for n number of processes

Posted By: Varick Fischer     Category: C Programming     Views: 4970

Write a C program to copy elements of one array to another for n number of processes.

Code for Program to copy elements of one array to another for n number of processes in C Programming

int pfork(int);
void process_join(int,int);
 
 
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
 
int main()
{
 
 
        int shmid1,shmid2,id;
        int i,n,*a1,*a2;
 
        int nproc;
 
        printf("Enter how many process:\n");
        scanf("%d",&nproc);
 
 
        printf("enter how many nos\n");
        scanf("%d",&n);
 
 
        shmid1=shmget(IPC_PRIVATE,4*nproc,0666|IPC_CREAT);
        shmid2=shmget(IPC_PRIVATE,4*nproc,0666|IPC_CREAT);
 
 
        a1=(int *)shmat(shmid1,0,0);
        a2=(int *)shmat(shmid2,0,0);
 
        for(i=0;i<n;i++)
        {
                printf("Enter number: ");
                scanf("%d",a1+i);
        }
 
 
         
        if(shmid1 < 0)
        {
                printf("Error\n");
        }
        if(shmid2 < 0)
        {
                printf("Error\n");
        }
 
 
 
        id=pfork(nproc);
         
 
        for(i=id;i<n;i+=nproc)
        {
                printf("The process executing is %d \n",id);
                *(a2+i)=*(a1+i);
        }
 
        process_join(nproc,id);
 
        printf("\nThe final array is:\n");
 
        for(i=0;i<n;i++)
        {
                printf("%d",*(a2+i));
                printf(" ");
        }
 
        printf("\n");
        printf("\nEnd Parent\n");
        return 1;
}
 
 
int pfork(int x)
{
        int j;
 
        for(j=1;j<=(x-1);j++)
        {
                if((fork())==0)
                        return j;
        }
 
        return 0;
}
 
void process_join(int x,int id)
{
        int j,val;
        if(id==0)
        {
                for(j=1;j<=(x-1);j++)
                {
                        printf("parent process id %d\n\n",getpid());
                        printf("Parent calling wait:blocked state\n");
 
                        val=wait(0);
                        printf("The child deleted is : %d\n",val);
                }
        }
        else
        {
                printf("child pid %d\n",getpid());
                exit(0);
        }
        printf("after exit %d\n",getpid());
 
 
}

Output:

[04mca8@LINTEL 04mca8]$ cc arrcommon.c

[04mca8@LINTEL 04mca8]$ ./a.out
Enter how many process:
3
enter how many nos
10
Enter number: 1
Enter number: 2
Enter number: 3
Enter number: 4
Enter number: 5
Enter number: 6
Enter number: 7
Enter number: 8
Enter number: 9
Enter number: 10
The process executing is 1
The process executing is 1
The process executing is 1
child pid 3865
The process executing is 2
The process executing is 2
The process executing is 2
child pid 3866
The process executing is 0
The process executing is 0
The process executing is 0
The process executing is 0
parent process id 3864
 
Parent calling wait:blocked state
The child deleted is : 3865
parent process id 3864
 
Parent calling wait:blocked state
The child deleted is : 3866
after exit 3864
 
The final array is:
1 2 3 4 5 6 7 8 9 10
 
End Parent

  
Share: 



Varick Fischer
Varick Fischer author of Program to copy elements of one array to another for n number of processes 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!