Logo 
Search:

C Programming Articles

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

Program to add the corresponding index value of two arrays and result is stored in the 3rd arra

Posted By: Rainor Fischer     Category: C Programming     Views: 6909

Write a C program to add the corresponding index value of two arrays and result is stored in the 3rd array.

Code for Program to add the corresponding index value of two arrays and result is stored in the 3rd arra 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 sum=0,fsum;
        int *a3,shmid,id;
        int i,n,a1[15],a2[15];
 
        printf("enter how many nos\n");
        scanf("%d",&n);
 
        for(i=0;i<n;i++)
        {
                printf("enter number for 1st array:\n");
                scanf("%d",&a1[i]);
        }
 
        for(i=0;i<n;i++)
        {
                printf("enter number for 2nd array:\n");
                scanf("%d",&a2[i]);
        }
 
        shmid=shmget(IPC_PRIVATE,4,0666|IPC_CREAT);
         
        a3=(int *)shmat(shmid,0,0);
 
        if(shmid < 0)
        {
                printf("Error\n");
        }
 
        for(i=0;i<n;i++)
        {
                *(a3+i)=0;
        }
 
        id=pfork(2);
 
        if(id==0)
        {
 
                for(i=0;i<n;i+=2)
                {
                        *(a3+i)=a1[i]+a2[i];
                }
 
                printf("parent doing sum\n");
                for(i=0;i<n;i+=2)
                {
                        printf("sum by parent %d\n",*(a3+i));
                }
                printf("\n");
        }
 
        else
        {
                if(id==1)
                {
 
                        for(i=1;i<n;i+=2)
                        {
 
                                *(a3+i)=a1[i]+a2[i];
                        }
                        printf("child doing sum\n");
                        for(i=1;i<n;i+=2)
                        {
                                printf("Sum by child %d\n",*(a3+i));
                        }
 
                        printf("\n");
                }
        }
 
 
        //printf("Calling process join for id %d\n",id);
        process_join(2,id);
 
        printf("The final sum array is : \n");
        for(i=0;i<n;i++)
        {
                printf(" %d ",*(a3+i));
 
        }
 
        printf("\n");
        printf("End 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 add3arr.c
[04mca8@LINTEL 04mca8]$ ./a.out
enter how many nos
5
enter number for 1st array:
1
enter number for 1st array:
3
enter number for 1st array:
5
enter number for 1st array:
7
enter number for 1st array:
9
enter number for 2nd array:
2
enter number for 2nd array:
4
enter number for 2nd array:
6
enter number for 2nd array:
8
enter number for 2nd array:
10
child doing sum
Sum by child 7
Sum by child 15
 
child pid 18249
parent doing sum
sum by parent 3
sum by parent 11
sum by parent 19
 
parent process id 18248
 
Parent calling wait:blocked state
The child deleted is : 18249
after exit 18248
The final sum array is :
 3  7  11  15  19
End Parent
  
Share: 



Rainor Fischer
Rainor Fischer author of Program to add the corresponding index value of two arrays and result is stored in the 3rd arra 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!